historyChart.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <view class="chart-panel bg-white radius" :class="{ 'chart-panel--fullscreen': fullscreen }">
  3. <l-echart ref="chartRef" class="chart-panel__echart" :class="{ 'chart-panel__echart--fullscreen': fullscreen }"></l-echart>
  4. </view>
  5. </template>
  6. <script setup>
  7. import * as echarts from "echarts";
  8. import { ref, watch, nextTick } from "vue";
  9. const CHART_COLORS = ["#4a90e2", "#40b883", "#f5a623", "#ef4444", "#9b59b6", "#1abc9c"];
  10. const props = defineProps({
  11. metricName: {
  12. type: String,
  13. default: "",
  14. },
  15. chartData: {
  16. type: Array,
  17. default: () => [],
  18. },
  19. seriesList: {
  20. type: Array,
  21. default: () => [],
  22. },
  23. fullscreen: {
  24. type: Boolean,
  25. default: false,
  26. },
  27. });
  28. const chartRef = ref(null);
  29. let chartInstance;
  30. function getSeriesList() {
  31. if (props.seriesList.length) return props.seriesList;
  32. if (props.chartData.length) {
  33. return [{ name: props.metricName || "暂无数据", data: props.chartData }];
  34. }
  35. return [];
  36. }
  37. function buildOption() {
  38. const seriesList = getSeriesList();
  39. if (!seriesList.length) {
  40. return {
  41. title: {
  42. text: "暂无数据",
  43. left: "center",
  44. top: "center",
  45. textStyle: { color: "#999", fontSize: 14, fontWeight: "normal" },
  46. },
  47. };
  48. }
  49. const labelOrder = [];
  50. const labelSeen = new Set();
  51. seriesList.forEach((series) => {
  52. (series.data || []).forEach((point) => {
  53. if (!labelSeen.has(point.label)) {
  54. labelSeen.add(point.label);
  55. labelOrder.push({
  56. label: point.label,
  57. timestamp: point.timestamp || 0,
  58. });
  59. }
  60. });
  61. });
  62. labelOrder.sort((a, b) => a.timestamp - b.timestamp);
  63. const xData = labelOrder.map((item) => item.label);
  64. const lineSeries = seriesList.map((series, index) => ({
  65. name: series.name,
  66. type: "line",
  67. smooth: true,
  68. symbol: "circle",
  69. symbolSize: 4,
  70. connectNulls: true,
  71. data: xData.map((label) => {
  72. const point = (series.data || []).find((item) => item.label === label);
  73. return point ? point.value : null;
  74. }),
  75. itemStyle: {
  76. color: CHART_COLORS[index % CHART_COLORS.length],
  77. },
  78. }));
  79. return {
  80. color: CHART_COLORS,
  81. tooltip: {
  82. trigger: "axis",
  83. backgroundColor: "rgba(255, 255, 255, 0.9)",
  84. borderColor: "rgba(0, 0, 0, 0.08)",
  85. },
  86. legend: {
  87. type: "scroll",
  88. top: 10,
  89. left: "center",
  90. data: seriesList.map((item) => item.name),
  91. icon: "circle",
  92. itemWidth: 8,
  93. itemHeight: 8,
  94. textStyle: {
  95. fontSize: 12,
  96. color: "#666",
  97. },
  98. },
  99. grid: {
  100. left: props.fullscreen ? "8%" : "12%",
  101. right: props.fullscreen ? "8%" : "12%",
  102. top: props.fullscreen ? 48 : 56,
  103. bottom: props.fullscreen ? 36 : 60,
  104. },
  105. dataZoom: [
  106. {
  107. type: "slider",
  108. show: xData.length > 4,
  109. xAxisIndex: 0,
  110. height: 18,
  111. bottom: 10,
  112. borderColor: "transparent",
  113. backgroundColor: "#f5f6f7",
  114. fillerColor: "rgba(74, 144, 226, 0.15)",
  115. handleSize: "80%",
  116. },
  117. ],
  118. xAxis: {
  119. type: "category",
  120. boundaryGap: false,
  121. data: xData,
  122. axisLine: {
  123. lineStyle: { color: "#e5e5e5" },
  124. },
  125. axisLabel: {
  126. color: "#999",
  127. fontSize: 11,
  128. },
  129. },
  130. yAxis: {
  131. type: "value",
  132. splitLine: {
  133. lineStyle: {
  134. type: "dashed",
  135. color: "#eee",
  136. },
  137. },
  138. axisLabel: {
  139. color: "#999",
  140. fontSize: 11,
  141. },
  142. },
  143. series: lineSeries,
  144. };
  145. }
  146. function resizeChart(instance) {
  147. if (!instance) return;
  148. setTimeout(() => instance.resize(), 80);
  149. setTimeout(() => instance.resize(), 300);
  150. }
  151. function renderChart() {
  152. nextTick(() => {
  153. if (!chartRef.value) return;
  154. chartRef.value.init(echarts, (instance) => {
  155. chartInstance = instance;
  156. instance.setOption(buildOption(), true);
  157. if (props.fullscreen) {
  158. resizeChart(instance);
  159. }
  160. });
  161. });
  162. }
  163. watch(
  164. () => [props.metricName, props.chartData, props.seriesList, props.fullscreen],
  165. () => {
  166. renderChart();
  167. },
  168. { deep: true, immediate: true }
  169. );
  170. </script>
  171. <style lang="scss" scoped>
  172. .chart-panel {
  173. padding: 10px 0 0;
  174. &--fullscreen {
  175. flex: 1;
  176. width: 100%;
  177. height: 100%;
  178. padding: 0;
  179. border-radius: 0;
  180. display: flex;
  181. flex-direction: column;
  182. box-sizing: border-box;
  183. :deep(.lime-echart),
  184. :deep(.lime-echart__canvas) {
  185. width: 100% !important;
  186. height: 100% !important;
  187. }
  188. }
  189. &__echart {
  190. width: 100%;
  191. height: 520rpx;
  192. &--fullscreen {
  193. flex: 1;
  194. width: 100%;
  195. height: 100% !important;
  196. min-height: 0;
  197. }
  198. }
  199. }
  200. </style>