trend.vue 5.1 KB
<!--
 * @Description: 转化趋势
 * @Author: huyue
 * @Date: 2022-06-23 15:05:21
 * @LastEditors: huyue
 * @LastEditTime: 2022-07-06 15:07:56
-->
<template>
  <div>
    <div class="title">转化趋势</div>
    <div v-show="hasData" class="linetrend-box">
      <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)
            })
            .catch(err => {
              reject(err)
            })
        })
      })
      Promise.all([p[0], p[1], p[2], p[3], p[4]])
        .then(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
            }
          })
          this.$set(this, 'hasData', !flag.every(i => !i))
          this.draw(val.reportType)
        })
        .catch(() => {
          this.hasData = false
        })
    },
    draw(reportType) {
      this.option = {
        tooltip: {
          trigger: 'axis',
          show: true,
          borderWidth: 0,
          backgroundColor: 'rgba(68,68,68,0.80)',
          confine: true,
          textStyle: {
            color: '#FFFFFF',
            fontSize: 8
          },
          extraCssText: 'width: 80px;',
          formatter: e => {
            const index = e[0].dataIndex
            const date = this.dataList[0][index].statDate
            var txt = date + '<br />'
            e.forEach(item => {
              txt = txt + item.seriesName + ':' + item.value + '<br />'
            })
            return txt
          }
        },
        color: ['#4FA0FF', '#FF6E76', '#7CFFB2', '#FDF006', '#EB6001'],
        grid: {
          left: '3%',
          right: '4%',
          top: '10%',
          bottom: '10%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          axisLabel: {
            color: '#606266',
            fontSize: 10
          },
          data: []
        },
        yAxis: {
          type: 'value',
          splitLine: {
            lineStyle: {
              color: '#606266'
            }
          },
          splitNumber: 4
        },
        series: [
          {
            name: '客流',
            type: 'line',
            symbol: 'circle',
            smooth: true,
            showSymbol: false,
            data: []
          },
          {
            name: '留档',
            type: 'line',
            symbol: 'circle',
            smooth: true,
            showSymbol: false,
            data: []
          },
          {
            name: '试驾',
            type: 'line',
            symbol: 'circle',
            smooth: true,
            showSymbol: false,
            data: []
          },
          {
            name: '订单',
            type: 'line',
            symbol: 'circle',
            showSymbol: false,
            smooth: true,
            data: []
          },
          {
            name: '成交',
            type: 'line',
            symbol: 'circle',
            showSymbol: false,
            smooth: true,
            data: []
          }
        ]
      }
      const xData = []
      this.option.xAxis.data = xData
      this.dataList[0].map(e => {
        const month = new Date(e.statDate).getMonth()
        xData.push(reportType == 0 ? `${month == 0 ? '1月' : `${Number(month) + 1}月`}` : e.statDate)
      })
      this.list.forEach((i, index) => {
        this.option.series[index].data = this.dataList[index].map(item => item.num)
      })
      this.echartsTrend.setOption(this.option)
    }
  }
}
</script>
<style lang="scss" scoped>
.linetrend-box {
  width: 100%;
  overflow: hidden;
}
</style>