adStruct.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <template>
  2. <div v-loading="loading">
  3. <el-row :gutter="5">
  4. <el-col :span="24">
  5. <div style="margin-left: 45%">
  6. <span style="background: #3a83f7; width: 18px; height: 10px; margin-top: 8px; display: inline-block; border-radius: 3px"></span>
  7. <TextSelector v-model="barModelValue1" :options="computedBarOptions1" @change="changeBarOne" style="margin-top: 5px; margin-left: 8px" />
  8. <span
  9. style="
  10. background: #f19a37;
  11. width: 18px;
  12. height: 10px;
  13. margin-top: 8px;
  14. margin-left: 20px;
  15. display: inline-block;
  16. border-radius: 3px;
  17. "></span>
  18. <TextSelector v-model="barModelValue2" :options="computedBarOptions2" @change="changeBarTwo" style="margin-top: 5px; margin-left: 8px" />
  19. </div>
  20. <div ref="bar" style="height: 400px"></div>
  21. </el-col>
  22. </el-row>
  23. </div>
  24. </template>
  25. <script setup>
  26. import { computed, onMounted, ref, watch } from 'vue'
  27. import * as echarts from 'echarts'
  28. import TextSelector from '/@/components/TextSelector/index.vue'
  29. import { getAdStructureData } from '/@/views/adManage/sb/keywords/api'
  30. import { createDisabledOptions } from '../../../utils/dropdowndisable'
  31. import { barOptions1, barOptions2, barOptionsMap } from '/@/views/adManage/utils/enum'
  32. import { useShopInfo } from '/@/stores/shopInfo'
  33. import { usePublicData } from '/@/stores/publicData'
  34. import { storeToRefs } from 'pinia'
  35. const shopInfo = useShopInfo()
  36. const publicData = usePublicData()
  37. const { dateRange } = storeToRefs(publicData)
  38. const { profile } = storeToRefs(shopInfo)
  39. let barChart = ref()
  40. const pie = ref()
  41. const bar = ref()
  42. const loading = ref(true)
  43. // const dateRange = inject('dateRange')
  44. // 下拉框相关
  45. let barModelValue1 = ref(barOptions1[0].value)
  46. let barModelValue2 = ref(barOptions2[2].value)
  47. onMounted(async () => {
  48. barChart = echarts.init(bar.value)
  49. window.addEventListener('resize', resizeChart) // 监听窗口大小变化,调整图表大小
  50. setTimeout(() => {
  51. resizeChart()
  52. }, 0)
  53. await initBarData()
  54. initChart()
  55. })
  56. // 获取总数据
  57. let allData = null
  58. async function setAdStructureData() {
  59. allData = await getAdStructureData({ startDate: dateRange.value[0], endDate: dateRange.value[1], profileId: shopInfo.profile.profile_id })
  60. return allData.data
  61. }
  62. // 饼图总数据和柱状图总数据
  63. let barData = null
  64. let responseData = null
  65. // 柱状图初始数据
  66. let ACOSList
  67. let SpendList
  68. let xAxisList
  69. let xAxisMapList
  70. async function initBarData() {
  71. responseData = await setAdStructureData()
  72. barData = responseData
  73. // 柱状图初始化数据
  74. ACOSList = barData.map((item) => item.ACOS)
  75. SpendList = barData.map((item) => item.Spend)
  76. // 将x轴映射为中文
  77. xAxisList = barData.map((item) => item.matchType)
  78. const classificationMap = {
  79. BROAD: '关键词-广泛',
  80. category: '品类',
  81. EXACT: '关键词-精准',
  82. asin: '商品',
  83. PHRASE: '关键词-词组',
  84. 'close-match': '紧密匹配',
  85. 'loose-match': '广泛匹配',
  86. substitutes: '同类商品',
  87. complements: '关联商品',
  88. }
  89. xAxisMapList = xAxisList.map((item) => classificationMap[item])
  90. loading.value = false
  91. }
  92. // 重置图像
  93. let flag = ref()
  94. let option
  95. let option2
  96. // 点击下拉框后重新渲染柱状图
  97. function changeBarOne(newValue) {
  98. barModelValue1.value = newValue
  99. updateBarChart()
  100. }
  101. function changeBarTwo(newValue) {
  102. barModelValue2.value = newValue
  103. updateBarChart()
  104. }
  105. function updateBarChart() {
  106. const barValues1 = barData.map((item) => item[barModelValue1.value])
  107. const barValues2 = barData.map((item) => item[barModelValue2.value])
  108. option.series[0].data = barValues1
  109. option.series[1].data = barValues2
  110. // 同时更新系列的name属性,以确保鼠标悬停时显示的文本正确
  111. option.series[0].name = barOptionsMap[barModelValue1.value] || barModelValue1.value
  112. option.series[1].name = barOptionsMap[barModelValue2.value] || barModelValue2.value
  113. barChart.setOption(option)
  114. }
  115. // 监听时间变化重新渲染表格
  116. watch(dateRange, async () => {
  117. if (dateRange.value) {
  118. loading.value = true
  119. const resp = await setAdStructureData()
  120. updateBarChartData(resp)
  121. loading.value = false
  122. }
  123. })
  124. // 根据新数据和当前下拉框选择更新 柱状图数据
  125. function updateBarChartData(resp) {
  126. const barValues1 = resp.map((item) => item[barModelValue1.value])
  127. const barValues2 = resp.map((item) => item[barModelValue2.value])
  128. option.series[0].data = barValues1
  129. option.series[1].data = barValues2
  130. barChart.setOption(option)
  131. }
  132. const computedBarOptions1 = computed(() => createDisabledOptions(barOptions1, barModelValue2.value, barModelValue1.value))
  133. const computedBarOptions2 = computed(() => createDisabledOptions(barOptions2, barModelValue1.value, barModelValue2.value))
  134. // 初始化图表
  135. function initChart() {
  136. // 柱状图配置
  137. option = {
  138. tooltip: {
  139. trigger: 'axis',
  140. axisPointer: {
  141. type: 'shadow',
  142. label: {
  143. backgroundColor: '#6a7985',
  144. },
  145. },
  146. },
  147. // legend: {data: ['数据1', '数据2'],},
  148. toolbox: {
  149. feature: {
  150. saveAsImage: { yAxisIndex: 'none' },
  151. },
  152. },
  153. grid: { top: 50, right: 60, bottom: 50, left: 60 },
  154. xAxis: [
  155. {
  156. type: 'category',
  157. boundaryGap: true,
  158. data: xAxisMapList,
  159. },
  160. ],
  161. yAxis: [
  162. {
  163. type: 'value',
  164. axisLine: {
  165. show: true,
  166. lineStyle: {
  167. color: '#3a83f7', // 第一个 Y 轴的颜色
  168. },
  169. },
  170. },
  171. {
  172. type: 'value',
  173. splitLine: {
  174. show: false,
  175. },
  176. axisLine: {
  177. show: true,
  178. lineStyle: {
  179. color: '#f19a37', // 第一个 Y 轴的颜色
  180. },
  181. },
  182. },
  183. ],
  184. series: [
  185. {
  186. name: barOptionsMap[barModelValue1.value],
  187. type: 'bar',
  188. barWidth: '3%',
  189. data: ACOSList,
  190. yAxisIndex: 0,
  191. itemStyle: {
  192. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  193. { offset: 0, color: '#3a83f7' }, // 起始的鲜亮蓝色
  194. { offset: 0.5, color: '#5a9ef4' }, // 中间色,中度蓝色
  195. { offset: 1, color: '#8ab6f1' }, // 结束的浅蓝色
  196. ]),
  197. // 柱状图圆角
  198. borderRadius: [4, 4, 4, 4],
  199. },
  200. },
  201. {
  202. name: barOptionsMap[barModelValue2.value],
  203. type: 'bar',
  204. barWidth: '3%',
  205. data: SpendList,
  206. yAxisIndex: 1,
  207. itemStyle: {
  208. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  209. { offset: 0, color: '#f19a37' },
  210. { offset: 0.5, color: '#f7b96c' }, // 中间色,浅橙色
  211. { offset: 1, color: 'rgb(234, 207, 135)' }, // 结束的浅黄色
  212. ]),
  213. // 柱状图圆角
  214. borderRadius: [4, 4, 4, 4],
  215. },
  216. },
  217. ],
  218. }
  219. barChart.setOption(option)
  220. resizeChart()
  221. }
  222. function resizeChart() {
  223. barChart.resize()
  224. }
  225. defineExpose({ resizeChart })
  226. </script>
  227. <style scoped></style>