AverageMonthly.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <script lang="ts" setup>
  2. /**
  3. * @Name: AverageMonthly.vue
  4. * @Description: 月平均分图表
  5. * @Author: Cheney
  6. */
  7. import * as echarts from 'echarts';
  8. import * as api from '../api';
  9. import { useResponse } from '/@/utils/useResponse';
  10. const props = defineProps({
  11. asin: String
  12. });
  13. const { asin } = props;
  14. let chartRef: any = useTemplateRef('chartRef');
  15. let chart: echarts.ECharts | null = null;
  16. let resizeObserver: ResizeObserver | null = null;
  17. let chartData: any = ref([]);
  18. onBeforeMount(() => {
  19. fetchChartData();
  20. });
  21. onMounted(() => {
  22. initChart();
  23. });
  24. onUnmounted(() => {
  25. resizeObserver?.disconnect();
  26. chart?.dispose();
  27. });
  28. function updateChart() {
  29. if (!chart || chartData.value.length === 0) return;
  30. const chartOptions = {
  31. title: {
  32. text: '每月平均评分'
  33. },
  34. tooltip: {
  35. trigger: 'axis'
  36. },
  37. legend: {
  38. data: [ '总量', '平均分' ]
  39. },
  40. grid: {
  41. top: '20%',
  42. left: '10%',
  43. right: '10%',
  44. bottom: '10%'
  45. },
  46. dataset: {
  47. dimensions: [ 'month', 'total', 'avg_score' ],
  48. source: chartData.value
  49. },
  50. xAxis: {
  51. type: 'category'
  52. },
  53. yAxis: [
  54. {
  55. id: 0,
  56. type: 'value',
  57. name: '总量',
  58. nameTextStyle: {
  59. fontSize: 14,
  60. color: '#3480CE',
  61. },
  62. splitLine: {
  63. show: true
  64. },
  65. axisLine: {
  66. show: true,
  67. lineStyle: {
  68. color: '#3480CE',
  69. width: 1
  70. }
  71. }
  72. },
  73. {
  74. id: 1,
  75. type: 'value',
  76. name: '平均分',
  77. nameTextStyle: {
  78. fontSize: 14,
  79. color: '#F19A38',
  80. },
  81. splitLine: {
  82. show: false
  83. },
  84. axisLine: {
  85. show: true,
  86. lineStyle: {
  87. color: '#F19A38',
  88. width: 1
  89. }
  90. }
  91. }
  92. ],
  93. series: [
  94. {
  95. yAxisIndex: 0,
  96. color: '#3480CE',
  97. name: '总量',
  98. type: 'bar',
  99. barWidth: '18px',
  100. itemStyle: {
  101. borderRadius: [ 4, 4, 4, 4 ]
  102. }
  103. },
  104. {
  105. yAxisIndex: 1,
  106. color: '#F19A38',
  107. name: '平均分',
  108. type: 'line',
  109. areaStyle: {
  110. // 设置渐变颜色
  111. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  112. {
  113. offset: 0,
  114. color: 'rgba(241,154,56, 0.1)'
  115. },
  116. {
  117. offset: 1,
  118. color: 'rgba(241,154,56, 0)'
  119. }
  120. ])
  121. }
  122. }
  123. ]
  124. };
  125. chart.setOption(chartOptions);
  126. }
  127. function initChart() {
  128. if (!chartRef.value) return;
  129. chart = echarts.init(chartRef.value);
  130. updateChart();
  131. resizeObserver = new ResizeObserver(() => {
  132. chart?.resize();
  133. });
  134. if (chartRef.value) {
  135. resizeObserver.observe(chartRef.value);
  136. }
  137. }
  138. async function fetchChartData() {
  139. const res = await useResponse(api.getChartData, { asin });
  140. if (res.code === 2000 && res.data) {
  141. chartData.value = res.data;
  142. updateChart();
  143. }
  144. }
  145. </script>
  146. <template>
  147. <el-card class="border-none" shadow="hover">
  148. <div v-show="chartData.length > 0" ref="chartRef" class="chart"></div>
  149. <el-empty v-if="chartData.length == 0" description="暂无数据" class="chart" />
  150. </el-card>
  151. </template>
  152. <style scoped>
  153. .chart {
  154. width: 100%;
  155. height: 400px;
  156. }
  157. </style>