trend.vue 4.82 KB
<!--
 * @Description: 转化趋势
 * @Author: huyue
 * @Date: 2022-06-23 15:05:21
 * @LastEditors: huyue
 * @LastEditTime: 2022-06-30 18:04:44
-->
<template>
  <div>
    <div class="title">转化趋势</div>
    <div v-show="hasData" style="width: 100%">
      <div id="lineTrend" :style="{ height: height - 4.1 + 'vh' }"></div>
    </div>
    <div v-show="!hasData" class="empty">
      <img src="../../assets/images/empty.png" alt="" />
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { changeMixin } from '@/mixins'
export default {
  name: 'Trend',
  props: ['height'],
  mixins: [changeMixin],
  data() {
    return {
      echartsTrend: {},
      option: {},
      dataList: [],
      paramsData: [],
      list: ['enter', 'stayfile', 'test', 'placeorder', 'success'],
      hasData: true
    }
  },
  mounted() {
    this.echartsTrend = echarts.init(document.getElementById('lineTrend'))
    window.addEventListener('resize', () => {
      this.echartsTrend.resize()
    })
  },
  methods: {
    init(val) {
      this.getChartData(val)
    },
    getChartData(val) {
      var params = {
        req: {
          // queryItem: 'enter',
          querystartTime: +val.reportType === 1 ? this.getNDate(-30) : this.getNMonth(-12),
          queryendTime: +val.reportType === 1 ? this.getNDate(-1) : this.getNMonth(-1),
          queryType: val.reportType || 0
        }
      }
      var brandName = val.brand ? val.brand.name : ''
      this.storeOrBrandTxt = brandName ? '门店' : '品牌'
      const api = brandName ? 'storeTransTrend' : 'saleTransTrend'
      const p = []
      this.list.forEach((i, idx) => {
        p[idx] = new Promise((resolve, reject) => {
          params.req.queryItem = i
          this.api.sales[api](params).then(res => {
            resolve(res.data.dataList)
          })
        })
      })
      Promise.all([p[0], p[1], p[2], p[3], p[4]]).then(results => {
        console.log(results)
        this.dataList = results
        const flag = results.map(item => {
          if (item.length) {
            return item.map(i => i.num).some(j => j != 0)
          } else {
            return false
          }
        })
        console.log(
          flag,
          flag.every(i => !i)
        )
        // this.hasData = !flag.every(i => !i)
        this.$set(this, 'hasData', !flag.every(i => !i))
        this.draw()
      })
    },
    draw() {
      this.option = {
        tooltip: {
          trigger: 'axis',
          show: true,
          borderWidth: 0,
          backgroundColor: 'rgba(68,68,68,0.80)',
          // position: [20, 20],
          textStyle: {
            color: '#FFFFFF',
            fontSize: 8
          },
          extraCssText: 'width: 80px;',
          formatter: e => {
            var txt = ''
            e.forEach(item => {
              if (txt) {
                txt = txt + '<br>'
              }
              txt = txt + item.seriesName + ':' + item.value
            })
            return txt
          }
        },
        color: ['#4FA0FF', '#7CFFB2', '#FDF006', '#EB6001', '#F5AA01', '#BF98FF', '#58D9F9'],
        grid: {
          left: '3%',
          right: '4%',
          top: '10%',
          bottom: '10%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          axisLabel: {
            color: '#606266',
            fontSize: '0.8em'
          },
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '客流',
            type: 'line',
            symbol: 'circle',
            smooth: true,
            showSymbol: false,
            stack: 'Total',
            data: []
          },
          {
            name: '留档',
            type: 'line',
            symbol: 'circle',
            smooth: true,
            showSymbol: false,
            stack: 'Total',
            data: []
          },
          {
            name: '试驾',
            type: 'line',
            symbol: 'circle',
            smooth: true,
            showSymbol: false,
            stack: 'Total',
            data: []
          },
          {
            name: '订单',
            type: 'line',
            symbol: 'circle',
            showSymbol: false,
            smooth: true,
            stack: 'Total',
            data: []
          },
          {
            name: '成交',
            type: 'line',
            symbol: 'circle',
            showSymbol: false,
            smooth: true,
            stack: 'Total',
            data: []
          }
        ]
      }
      this.option.xAxis.data = this.dataList[0].map(item => item.statDate)
      this.list.forEach((i, index) => {
        this.option.series[index].data = this.dataList[index].map(item => item.num)
      })
      this.echartsTrend.setOption(this.option)
    }
  }
}
</script>