adStruct.vue 6.5 KB

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