adStruct.vue 6.7 KB

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