usageChart.vue 4.6 KB

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