adStruct.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 * as echarts from 'echarts'
  27. import { storeToRefs } from 'pinia'
  28. import { computed, onMounted, ref, watch } from 'vue'
  29. import { createDisabledOptions } from '../../../utils/dropdowndisable'
  30. import TextSelector from '/@/components/TextSelector/index.vue'
  31. import { usePublicData } from '/@/stores/publicData'
  32. import { useShopInfo } from '/@/stores/shopInfo'
  33. import { getAdStructureData } from '/@/views/adManage/sb/keywords/api'
  34. import { barOptions1, barOptions2, barOptionsMap } from '/@/views/adManage/utils/enum'
  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. console.log('🚀 ~ xAxisList', xAxisList)
  79. const classificationMap = {
  80. BROAD: '关键词-广泛',
  81. THEME: '主题',
  82. EXACT: '关键词-精准',
  83. PHRASE: '关键词-词组',
  84. }
  85. xAxisMapList = xAxisList.map((item) => classificationMap[item])
  86. loading.value = false
  87. }
  88. // 重置图像
  89. let flag = ref()
  90. let option
  91. let option2
  92. // 点击下拉框后重新渲染柱状图
  93. function changeBarOne(newValue) {
  94. barModelValue1.value = newValue
  95. updateBarChart()
  96. }
  97. function changeBarTwo(newValue) {
  98. barModelValue2.value = newValue
  99. updateBarChart()
  100. }
  101. function updateBarChart() {
  102. const barValues1 = barData.map((item) => item[barModelValue1.value])
  103. const barValues2 = barData.map((item) => item[barModelValue2.value])
  104. option.series[0].data = barValues1
  105. option.series[1].data = barValues2
  106. // 同时更新系列的name属性,以确保鼠标悬停时显示的文本正确
  107. option.series[0].name = barOptionsMap[barModelValue1.value] || barModelValue1.value
  108. option.series[1].name = barOptionsMap[barModelValue2.value] || barModelValue2.value
  109. barChart.setOption(option)
  110. }
  111. // 监听时间变化重新渲染表格
  112. watch(dateRange, async () => {
  113. if (dateRange.value) {
  114. loading.value = true
  115. const resp = await setAdStructureData()
  116. updateBarChartData(resp)
  117. loading.value = false
  118. }
  119. })
  120. // 根据新数据和当前下拉框选择更新 柱状图数据
  121. function updateBarChartData(resp) {
  122. const barValues1 = resp.map((item) => item[barModelValue1.value])
  123. const barValues2 = resp.map((item) => item[barModelValue2.value])
  124. option.series[0].data = barValues1
  125. option.series[1].data = barValues2
  126. barChart.setOption(option)
  127. }
  128. const computedBarOptions1 = computed(() => createDisabledOptions(barOptions1, barModelValue2.value, barModelValue1.value))
  129. const computedBarOptions2 = computed(() => createDisabledOptions(barOptions2, barModelValue1.value, barModelValue2.value))
  130. // 初始化图表
  131. function initChart() {
  132. // 柱状图配置
  133. option = {
  134. tooltip: {
  135. trigger: 'axis',
  136. axisPointer: {
  137. type: 'shadow',
  138. label: {
  139. backgroundColor: '#6a7985',
  140. },
  141. },
  142. },
  143. // legend: {data: ['数据1', '数据2'],},
  144. toolbox: {
  145. feature: {
  146. saveAsImage: { yAxisIndex: 'none' },
  147. },
  148. },
  149. grid: { top: 50, right: 60, bottom: 50, left: 60 },
  150. xAxis: [
  151. {
  152. type: 'category',
  153. boundaryGap: true,
  154. data: xAxisMapList,
  155. },
  156. ],
  157. yAxis: [
  158. {
  159. type: 'value',
  160. axisLine: {
  161. show: true,
  162. lineStyle: {
  163. color: '#3a83f7', // 第一个 Y 轴的颜色
  164. },
  165. },
  166. },
  167. {
  168. type: 'value',
  169. splitLine: {
  170. show: false,
  171. },
  172. axisLine: {
  173. show: true,
  174. lineStyle: {
  175. color: '#f19a37', // 第一个 Y 轴的颜色
  176. },
  177. },
  178. },
  179. ],
  180. series: [
  181. {
  182. name: barOptionsMap[barModelValue1.value],
  183. type: 'bar',
  184. barWidth: '3%',
  185. data: ACOSList,
  186. yAxisIndex: 0,
  187. itemStyle: {
  188. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  189. { offset: 0, color: '#3a83f7' }, // 起始的鲜亮蓝色
  190. { offset: 0.5, color: '#5a9ef4' }, // 中间色,中度蓝色
  191. { offset: 1, color: '#8ab6f1' }, // 结束的浅蓝色
  192. ]),
  193. // 柱状图圆角
  194. borderRadius: [4, 4, 4, 4],
  195. },
  196. },
  197. {
  198. name: barOptionsMap[barModelValue2.value],
  199. type: 'bar',
  200. barWidth: '3%',
  201. data: SpendList,
  202. yAxisIndex: 1,
  203. itemStyle: {
  204. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  205. { offset: 0, color: '#f19a37' },
  206. { offset: 0.5, color: '#f7b96c' }, // 中间色,浅橙色
  207. { offset: 1, color: 'rgb(234, 207, 135)' }, // 结束的浅黄色
  208. ]),
  209. // 柱状图圆角
  210. borderRadius: [4, 4, 4, 4],
  211. },
  212. },
  213. ],
  214. }
  215. barChart.setOption(option)
  216. resizeChart()
  217. }
  218. function resizeChart() {
  219. barChart.resize()
  220. }
  221. defineExpose({ resizeChart })
  222. </script>
  223. <style scoped></style>