adGroup.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <MetricsCards v-model="metrics" :metric-items="metricItems" @change="changeMetric"></MetricsCards>
  3. <div style="height: 500px;" ref="chartRef"></div>
  4. </template>
  5. <script lang="ts" setup>
  6. import { ref,onBeforeMount,onMounted,onBeforeUnmount,inject, Ref } from 'vue'
  7. import MetricsCards from '/@/components/MetricsCards/index.vue'
  8. import { useShopInfo } from '/@/stores/shopInfo'
  9. import * as echarts from 'echarts'
  10. import XEUtils from 'xe-utils'
  11. import { getLineData, getCardData } from '../api'
  12. import { spCampaignMetricsEnum } from '/@/views/adManage/utils/enum.js'
  13. defineOptions({name: 'AdGroupChart'})
  14. const shopInfo = useShopInfo()
  15. const metrics = ref([
  16. {metric: 'Impression', color: 'aqua', 'label': '曝光量'},
  17. {metric: 'Click', color: 'blue', 'label': '点击量'},
  18. {metric: 'Spend', color: 'orange', 'label': '花费'},
  19. ])
  20. const metricItems: Ref<MetricData[]> = ref([])
  21. let chartObj:any
  22. const chartRef = ref()
  23. const dateRange: string[] = inject('dateRange', [])
  24. onBeforeMount(async () => {
  25. await getMetricItems()
  26. })
  27. onMounted(() => {
  28. initLine()
  29. addResize()
  30. })
  31. onBeforeUnmount(() => {
  32. if(chartObj) {
  33. chartObj.dispose()
  34. chartObj = null
  35. }
  36. removeResize()
  37. })
  38. const getMetricItems = async () => {
  39. const resp = await getCardData({start: dateRange[0], end: dateRange[1], profile: shopInfo.profile.profile_id})
  40. const data = resp.data
  41. XEUtils.arrayEach(spCampaignMetricsEnum, info => {
  42. const tmp: MetricData = {
  43. label: info.label,
  44. value: info.value,
  45. metricVal: data[info.value],
  46. gapVal: data[`gap${info.value}`],
  47. preVal: data[`prev${info.value}`],
  48. disabled: info.initShow ? true : false
  49. }
  50. metricItems.value.push(tmp)
  51. })
  52. }
  53. const getDataset = async () => {
  54. const resp = await getLineData({profile: shopInfo.profile.profile_id, start: dateRange[0], end: dateRange[1]})
  55. return resp.data
  56. }
  57. const initLine = async () => {
  58. chartObj = echarts.init(chartRef.value)
  59. const option: any = {
  60. dataset: {
  61. source: []
  62. },
  63. tooltip: {
  64. trigger: 'axis',
  65. axisPointer: {
  66. // type: 'cross',
  67. label: {
  68. backgroundColor: '#6a7985'
  69. }
  70. }
  71. },
  72. legend: {
  73. selected: {}, // 控制显隐
  74. show: false
  75. },
  76. grid: {
  77. top: 50, right: 150, bottom: 30, left: 55,
  78. },
  79. xAxis: {
  80. type: 'time'
  81. },
  82. yAxis: [],
  83. series: []
  84. }
  85. const items = await getDataset()
  86. option.dataset.source = items
  87. // chartObj.setOption(option)
  88. }
  89. const changeMetric = () => {
  90. }
  91. const resizeChart = () => { chartObj.resize() }
  92. const addResize = () => { window.addEventListener('resize', resizeChart) }
  93. const removeResize = () => { window.removeEventListener('resize', resizeChart) }
  94. // defineExpose({resizeChart})
  95. </script>
  96. <style scoped>
  97. </style>