PriceChart.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <script setup lang="ts">
  2. /**
  3. * @Name: TrendChart.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. country: String
  13. });
  14. const { asin, country } = props;
  15. let chartRef: any = useTemplateRef('chartRef');
  16. let chart: echarts.ECharts | null = null;
  17. let resizeObserver: ResizeObserver | null = null;
  18. let chartData: any = ref([]);
  19. const loading = ref(false)
  20. onBeforeMount(() => {
  21. fetchChartData();
  22. });
  23. onMounted(() => {
  24. initChart();
  25. });
  26. onUnmounted(() => {
  27. resizeObserver?.disconnect();
  28. chart?.dispose();
  29. });
  30. function updateChart() {
  31. if (!chart || chartData.value.length === 0) return;
  32. const chartOptions = {
  33. title: {
  34. text: '商品价格趋势'
  35. },
  36. tooltip: {
  37. trigger: 'axis'
  38. },
  39. legend: {
  40. data: [ '价格' ]
  41. },
  42. grid: {
  43. top: '20%',
  44. left: '10%',
  45. right: '10%',
  46. bottom: '10%'
  47. },
  48. dataset: {
  49. dimensions: [ 'create_datetime', 'new_val' ],
  50. source: chartData.value
  51. },
  52. xAxis: {
  53. type: 'category'
  54. },
  55. yAxis: [
  56. {
  57. type: 'value',
  58. name: '价格',
  59. nameTextStyle: {
  60. fontWeight: 'bold',
  61. fontSize: 14,
  62. color: '#333'
  63. },
  64. splitLine: {
  65. show: true
  66. },
  67. axisLine: {
  68. show: true,
  69. lineStyle: {
  70. color: '#333',
  71. width: 1
  72. }
  73. }
  74. }
  75. ],
  76. series: [
  77. {
  78. name: '价格',
  79. type: 'line',
  80. areaStyle: {
  81. // 设置渐变颜色
  82. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  83. {
  84. offset: 0,
  85. color: 'rgba(84,112,198, 0.4)'
  86. },
  87. {
  88. offset: 1,
  89. color: 'rgba(84,112,198, 0.1)'
  90. }
  91. ])
  92. }
  93. }
  94. ]
  95. };
  96. chart.setOption(chartOptions);
  97. }
  98. function initChart() {
  99. if (!chartRef.value) return;
  100. chart = echarts.init(chartRef.value);
  101. updateChart();
  102. resizeObserver = new ResizeObserver(() => {
  103. chart?.resize();
  104. });
  105. if (chartRef.value) {
  106. resizeObserver.observe(chartRef.value);
  107. }
  108. }
  109. async function fetchChartData() {
  110. const res = await useResponse(api.getChartData, { asin, country_code: country }, loading);
  111. if (res.code === 2000 && res.data) {
  112. chartData.value = res.data;
  113. console.log("(PriceChart.vue: 126)=> chartData", chartData);
  114. updateChart();
  115. }
  116. }
  117. </script>
  118. <template>
  119. <el-card class="border-none mt-2">
  120. <div v-show="chartData.length > 0" ref="chartRef" class="chart"></div>
  121. <el-empty v-if="chartData.length == 0" description="暂无数据" class="chart" />
  122. </el-card>
  123. </template>
  124. <style scoped>
  125. .chart {
  126. width: 100%;
  127. height: 400px;
  128. }
  129. </style>