adStruct.vue 6.8 KB

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