NegativeLabelChart.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <script setup lang="ts">
  2. /**
  3. * @Name: NegativeLabelChart.vue
  4. * @Description:
  5. * @Author: Cheney
  6. */
  7. import * as echarts from 'echarts';
  8. import * as api from '/@/views/product-manage/comment-detail/api';
  9. const props = defineProps({
  10. asin: String
  11. });
  12. const { asin } = props;
  13. const loading = ref(false);
  14. let responseData: any = null;
  15. const chartRef: any = useTemplateRef('chartRef');
  16. let chart: echarts.ECharts | null = null;
  17. let resizeObserver: ResizeObserver | null = null;
  18. const hasData = ref(true);
  19. onBeforeMount(() => {
  20. fetchWordCloudData();
  21. });
  22. onBeforeUnmount(() => {
  23. // 清理 ResizeObserver
  24. if (resizeObserver) {
  25. resizeObserver.disconnect();
  26. }
  27. if (chart) {
  28. chart.dispose();
  29. chart = null;
  30. }
  31. });
  32. async function fetchWordCloudData() {
  33. loading.value = true;
  34. const query = {
  35. asin,
  36. agg_field: 'raw_label'
  37. };
  38. try {
  39. responseData = await api.getWordCloudData(query);
  40. if (!responseData.data || responseData.data.length === 0) {
  41. // 处理空数据的情况
  42. hasData.value = false;
  43. if (chart) {
  44. chart.clear();
  45. }
  46. return;
  47. }
  48. hasData.value = true;
  49. setOption();
  50. } catch (error) {
  51. console.error('==Error==', error);
  52. } finally {
  53. loading.value = false;
  54. }
  55. }
  56. function setOption() {
  57. const option = {
  58. title: {
  59. text: '负面标签'
  60. },
  61. tooltip: {
  62. show: true
  63. },
  64. series: [
  65. {
  66. type: 'wordCloud',
  67. width: '90%',
  68. height: '85%',
  69. // 词云图的形状
  70. shape: 'star', // 可以是 'circle', 'cardioid', 'diamond', 'triangle-forward', 'triangle', 'pentagon', 'star'
  71. // 控制字体大小
  72. sizeRange: [ 12, 60 ],
  73. // 文本旋转角度
  74. rotationRange: [ 0, 0 ],
  75. rotationStep: 45,
  76. gridSize: 8,
  77. drawOutOfBound: false,
  78. shrinkToFit: false,
  79. layoutAnimation: true,
  80. // 全局的文字样式
  81. textStyle: {
  82. fontFamily: 'PingFang',
  83. fontWeight: 'bold',
  84. // Color can be a callback function or a color string
  85. color: function() {
  86. // Random color
  87. return 'rgb(' + [
  88. Math.round(Math.random() * 160),
  89. Math.round(Math.random() * 160),
  90. Math.round(Math.random() * 160)
  91. ].join(',') + ')';
  92. }
  93. },
  94. // 鼠标hover时的样式
  95. emphasis: {
  96. focus: 'self',
  97. textStyle: {
  98. textShadowBlur: 10,
  99. textShadowColor: '#333'
  100. }
  101. },
  102. // left: '10%',
  103. // top: '10%',
  104. // right: '10%',
  105. // bottom: '10%',
  106. data: responseData.data
  107. }
  108. ],
  109. backgroundColor: '#fff'
  110. };
  111. if (!chart) {
  112. chart = echarts.init(chartRef.value);
  113. }
  114. chart.setOption(option);
  115. // 添加 ResizeObserver 以处理图表大小变化
  116. if (!resizeObserver) {
  117. resizeObserver = new ResizeObserver(() => {
  118. chart?.resize();
  119. });
  120. resizeObserver.observe(chartRef.value);
  121. }
  122. }
  123. </script>
  124. <template>
  125. <el-card v-loading="loading" class="border-none" shadow="never">
  126. <!-- 空状态 和 词云图 -->
  127. <div class="w-full" style="min-height: 400px">
  128. <div v-show="!loading && !hasData" style="min-height: 400px">
  129. <el-empty :image-size="200" />
  130. </div>
  131. <div v-show="hasData" ref="chartRef" style="width: 100%; height: 400px"></div>
  132. </div>
  133. </el-card>
  134. </template>
  135. <style scoped>
  136. </style>