LineChart.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <div id="line-chart" ref="lineChart" class="chart" :style="{ height: props.height }"></div>
  3. </template>
  4. <script setup lang="ts">
  5. import * as echarts from 'echarts/core';
  6. import {
  7. GridComponent,
  8. GridComponentOption,
  9. DataZoomComponent,
  10. DataZoomComponentOption,
  11. TooltipComponent,
  12. } from 'echarts/components';
  13. import { LineChart, LineSeriesOption } from 'echarts/charts';
  14. import { UniversalTransition } from 'echarts/features';
  15. import { CanvasRenderer } from 'echarts/renderers';
  16. import { onMounted, watch } from 'vue';
  17. echarts.use([
  18. GridComponent,
  19. LineChart,
  20. CanvasRenderer,
  21. UniversalTransition,
  22. DataZoomComponent,
  23. TooltipComponent,
  24. ]);
  25. type EChartsOption = echarts.ComposeOption<
  26. GridComponentOption | LineSeriesOption | DataZoomComponentOption
  27. >;
  28. const props = withDefaults(
  29. defineProps<{
  30. chartData: any[];
  31. chartLable: string[];
  32. height?: string;
  33. }>(),
  34. {
  35. height: '372px',
  36. },
  37. );
  38. let lineOption: EChartsOption = {} as EChartsOption;
  39. onMounted(() => {
  40. const line: echarts.ECharts = echarts.init(document.getElementById('line-chart')!);
  41. initLineChart(line);
  42. });
  43. watch(
  44. () => props.chartData,
  45. () => {
  46. const line: echarts.ECharts = echarts.init(document.getElementById('line-chart')!);
  47. drawLineChart(line);
  48. },
  49. {
  50. deep: true,
  51. },
  52. );
  53. const initLineChart = (line: echarts.ECharts) => {
  54. lineOption = {
  55. tooltip: {
  56. trigger: 'axis',
  57. axisPointer: {
  58. type: 'shadow',
  59. },
  60. },
  61. grid: {
  62. left: '3%',
  63. right: '4%',
  64. bottom: '3%',
  65. containLabel: true,
  66. },
  67. dataZoom: [
  68. {
  69. startValue: 0,
  70. endValue: 13,
  71. type: 'inside',
  72. },
  73. ],
  74. xAxis: [
  75. {
  76. type: 'category',
  77. data: props.chartLable,
  78. axisLabel: {
  79. rotate: 45,
  80. },
  81. },
  82. ],
  83. yAxis: [
  84. {
  85. name: '访问次数',
  86. type: 'value',
  87. },
  88. ],
  89. series: props.chartData.map((item) => {
  90. return {
  91. name: item.workshopName,
  92. type: 'line',
  93. showSymbol: false,
  94. data: item.counts,
  95. };
  96. }),
  97. };
  98. console.log(lineOption);
  99. line.setOption(lineOption);
  100. };
  101. const drawLineChart = (line: echarts.ECharts) => {
  102. line.clear();
  103. lineOption.xAxis = [
  104. {
  105. type: 'category',
  106. data: props.chartLable,
  107. axisLabel: {
  108. rotate: 45,
  109. },
  110. },
  111. ];
  112. lineOption.series = props.chartData.map((item) => {
  113. return {
  114. name: item.workshopName,
  115. type: 'line',
  116. showSymbol: false,
  117. data: item.counts,
  118. };
  119. });
  120. line.setOption(lineOption);
  121. };
  122. </script>
  123. <style scoped lang="scss">
  124. #line-chart {
  125. width: 968px;
  126. height: 372px;
  127. }
  128. </style>