adStruct.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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="background: #f19a37; width: 18px; height: 10px; margin-top: 8px; margin-left: 20px; display: inline-block; border-radius: 3px;"></span>
  10. <TextSelector v-model="barModelValue2" :options="computedBarOptions2" @change="changeBarTwo" style="margin-top: 5px; margin-left: 8px;"/>
  11. </div>
  12. <div ref="bar" style="height: 400px;"></div>
  13. </el-col>
  14. </el-row>
  15. </div>
  16. </template>
  17. <script setup>
  18. import { onMounted, ref, inject, computed, watch } from "vue"
  19. import * as echarts from "echarts"
  20. import TextSelector from '/@/components/TextSelector/index.vue'
  21. import { getAdStructureData } from "/@/views/adManage/sp/placement/api"
  22. import { createDisabledOptions } from '../../../utils/dropdowndisable'
  23. import { barOptionsMap } from "/@/views/adManage/utils/enum"
  24. import { useShopInfo } from '/@/stores/shopInfo'
  25. import { usePublicData } from "/@/stores/publicData"
  26. import { storeToRefs } from 'pinia'
  27. const shopInfo = useShopInfo()
  28. const publicData = usePublicData()
  29. const { dateRange } = storeToRefs(publicData)
  30. const { profile } = storeToRefs(shopInfo)
  31. let barChart = ref()
  32. const pie = ref()
  33. const bar = ref()
  34. const loading = ref(true)
  35. // 下拉框相关
  36. const barOptions1 = [
  37. {
  38. value: 'ACOS',
  39. label: 'ACOS',
  40. },
  41. {
  42. value: 'ROAS',
  43. label: 'ROAS',
  44. },
  45. {
  46. value: 'Spend',
  47. label: '花费',
  48. units: '$',
  49. },
  50. {
  51. value: 'TotalSales',
  52. label: '销售额',
  53. },
  54. {
  55. value: 'TotalPurchases',
  56. label: '订单数',
  57. },
  58. {
  59. value: 'TotalUnitOrdered',
  60. label: '销量',
  61. },
  62. {
  63. value: 'CPC',
  64. label: '点击成本'
  65. },
  66. {
  67. value: 'CPA',
  68. label: '订单成本'
  69. },
  70. {
  71. value: 'Impression',
  72. label: '曝光量',
  73. },
  74. {
  75. value: 'Click',
  76. label: '点击量',
  77. },
  78. {
  79. value: 'qwe',
  80. label: '点击率'
  81. },
  82. {
  83. value: '转化率',
  84. label: '转化率'
  85. },
  86. {
  87. value: 'TotalSalesSameSKU',
  88. label: '推广商品销售额'
  89. },
  90. {
  91. value: 'TotalSalesOtherSKU',
  92. label: '其他商品销售额'
  93. },
  94. {
  95. value: 'TotalPurchasesSameSKU',
  96. label: '推广商品订单数'
  97. },
  98. {
  99. value: 'TotalPurchasesOtherSKU',
  100. label: '其他商品订单数'
  101. },
  102. {
  103. value: 'TotalUnitOrderedSameSKU',
  104. label: '推广商品销量'
  105. },
  106. {
  107. value: 'TotalUnitOrderedOtherSKU',
  108. label: '其他商品销量'
  109. },
  110. {
  111. value: 'TopOfSearchImpressionShare',
  112. label: '搜索结果顶部展示份额'
  113. },
  114. ]
  115. let barModelValue1 = ref(barOptions1[0].value)
  116. const barOptions2 = [
  117. {
  118. value: 'ACOS',
  119. label: 'ACOS',
  120. },
  121. {
  122. value: 'ROAS',
  123. label: 'ROAS',
  124. },
  125. {
  126. value: 'Spend',
  127. label: '花费',
  128. units: '$',
  129. },
  130. {
  131. value: 'TotalSales',
  132. label: '销售额',
  133. },
  134. {
  135. value: 'TotalPurchases',
  136. label: '订单数',
  137. },
  138. {
  139. value: 'TotalUnitOrdered',
  140. label: '销量',
  141. },
  142. {
  143. value: 'CPC',
  144. label: '点击成本'
  145. },
  146. {
  147. value: 'CPA',
  148. label: '订单成本'
  149. },
  150. {
  151. value: 'Impression',
  152. label: '曝光量',
  153. },
  154. {
  155. value: 'Click',
  156. label: '点击量',
  157. },
  158. {
  159. value: 'qwe',
  160. label: '点击率'
  161. },
  162. {
  163. value: '转化率',
  164. label: '转化率'
  165. },
  166. {
  167. value: 'TotalSalesSameSKU',
  168. label: '推广商品销售额'
  169. },
  170. {
  171. value: 'TotalSalesOtherSKU',
  172. label: '其他商品销售额'
  173. },
  174. {
  175. value: 'TotalPurchasesSameSKU',
  176. label: '推广商品订单数'
  177. },
  178. {
  179. value: 'TotalPurchasesOtherSKU',
  180. label: '其他商品订单数'
  181. },
  182. {
  183. value: 'TotalUnitOrderedSameSKU',
  184. label: '推广商品销量'
  185. },
  186. {
  187. value: 'TotalUnitOrderedOtherSKU',
  188. label: '其他商品销量'
  189. },
  190. {
  191. value: 'TopOfSearchImpressionShare',
  192. label: '搜索结果顶部展示份额'
  193. },
  194. ]
  195. let barModelValue2 = ref(barOptions2[2].value)
  196. onMounted(async () => {
  197. barChart = echarts.init(bar.value)
  198. window.addEventListener('resize', resizeChart) // 监听窗口大小变化,调整图表大小
  199. setTimeout(() => {
  200. resizeChart()
  201. }, 0)
  202. await initBarData()
  203. initChart()
  204. })
  205. // 获取总数据
  206. let allData = null
  207. async function setAdStructureData() {
  208. allData = await getAdStructureData({ startDate: dateRange.value[0], endDate: dateRange.value[1], profileId: shopInfo.profile.profile_id })
  209. return allData.data
  210. }
  211. // 柱状图总数据
  212. let barData = null
  213. let responseData = null
  214. // 柱状图初始数据
  215. let ACOSList
  216. let SpendList
  217. let xAxisList
  218. let xAxisMapList
  219. async function initBarData() {
  220. responseData = await setAdStructureData()
  221. barData = responseData
  222. // 柱状图初始化数据
  223. ACOSList = barData.map(item => item.ACOS)
  224. SpendList = barData.map(item => item.Spend)
  225. // 将x轴映射为中文
  226. xAxisList = barData.map(item => item.placement)
  227. const classificationMap = {
  228. 'BROAD': '关键词-广泛',
  229. 'category': '品类',
  230. 'EXACT': '关键词-精准',
  231. 'asin': '商品',
  232. 'PHRASE': '关键词-词组',
  233. 'close-match': '紧密匹配',
  234. 'loose-match': '广泛匹配',
  235. 'substitutes': '同类商品',
  236. 'complements': '关联商品',
  237. 'top': '搜索结果顶部(首页)',
  238. 'rest_of_search': '搜索结果的其余位置',
  239. 'product_page': '商品页面'
  240. }
  241. xAxisMapList = xAxisList.map(item => classificationMap[item])
  242. loading.value = false
  243. }
  244. // 重置图像
  245. let flag = ref()
  246. let option
  247. let option2
  248. // 点击下拉框后重新渲染柱状图
  249. function changeBarOne(newValue) {
  250. barModelValue1.value = newValue
  251. updateBarChart()
  252. }
  253. function changeBarTwo(newValue) {
  254. barModelValue2.value = newValue
  255. updateBarChart()
  256. }
  257. function updateBarChart() {
  258. const barValues1 = barData.map(item => item[barModelValue1.value])
  259. const barValues2 = barData.map(item => item[barModelValue2.value])
  260. option.series[0].data = barValues1
  261. option.series[1].data = barValues2
  262. barChart.setOption(option)
  263. }
  264. // 监听时间变化重新渲染表格
  265. watch(dateRange, async () => {
  266. if (dateRange.value) {
  267. loading.value = true
  268. const resp = await setAdStructureData()
  269. updateBarChartData(resp)
  270. loading.value = false
  271. }
  272. })
  273. // 根据新数据和当前下拉框选择更新 柱状图数据
  274. function updateBarChartData(resp) {
  275. const barValues1 = resp.map(item => item[barModelValue1.value])
  276. const barValues2 = resp.map(item => item[barModelValue2.value])
  277. option.series[0].data = barValues1
  278. option.series[1].data = barValues2
  279. barChart.setOption(option)
  280. }
  281. const computedBarOptions1 = computed(() => createDisabledOptions(barOptions1, barModelValue2.value, barModelValue1.value))
  282. const computedBarOptions2 = computed(() => createDisabledOptions(barOptions2, barModelValue1.value, barModelValue2.value))
  283. // 初始化图表
  284. function initChart() {
  285. // 柱状图配置
  286. option = {
  287. tooltip: {
  288. trigger: 'axis',
  289. axisPointer: {
  290. type: 'shadow',
  291. label: {
  292. backgroundColor: '#6a7985'
  293. }
  294. }
  295. },
  296. // legend: {data: ['数据1', '数据2'],},
  297. toolbox: {
  298. feature: {
  299. saveAsImage: { yAxisIndex: 'none' }
  300. }
  301. },
  302. grid: { top: 50, right: 60, bottom: 50, left: 60 },
  303. xAxis: [
  304. {
  305. type: 'category',
  306. boundaryGap: true,
  307. data: xAxisMapList,
  308. },
  309. ],
  310. yAxis: [
  311. {
  312. type: 'value',
  313. axisLine: {
  314. show: true,
  315. lineStyle: {
  316. color: '#3a83f7' // 第一个 Y 轴的颜色
  317. }
  318. }
  319. },
  320. {
  321. type: 'value',
  322. splitLine: {
  323. show: false
  324. },
  325. axisLine: {
  326. show: true,
  327. lineStyle: {
  328. color: '#f19a37' // 第一个 Y 轴的颜色
  329. }
  330. }
  331. }
  332. ],
  333. series: [
  334. {
  335. name: barOptionsMap[barModelValue1.value],
  336. type: 'bar',
  337. barWidth: '3%',
  338. data: ACOSList,
  339. yAxisIndex: 0,
  340. itemStyle: {
  341. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  342. { offset: 0, color: '#3a83f7' },
  343. { offset: 1, color: 'rgb(111, 209, 206)' },
  344. ]),
  345. // 柱状图圆角
  346. borderRadius: [6, 6, 6, 6],
  347. },
  348. },
  349. {
  350. name: barOptionsMap[barModelValue2.value],
  351. type: 'bar',
  352. barWidth: '3%',
  353. data: SpendList,
  354. yAxisIndex: 1,
  355. itemStyle: {
  356. color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
  357. { offset: 0, color: '#f19a37' },
  358. { offset: 1, color: 'rgb(234,207,135)' },
  359. ]),
  360. // 柱状图圆角
  361. borderRadius: [6, 6, 6, 6],
  362. },
  363. },
  364. ],
  365. }
  366. barChart.setOption(option)
  367. resizeChart()
  368. }
  369. function resizeChart() {
  370. barChart.resize()
  371. }
  372. defineExpose({ resizeChart })
  373. </script>
  374. <style scoped>
  375. </style>