trend.vue 6.35 KB
<!--
 * @Description: 转化趋势
 * @Author: huyue
 * @Date: 2022-06-23 15:05:21
 * @LastEditors: huyue
 * @LastEditTime: 2022-07-04 18:25:19
-->
<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(() => {
              reject()
            })
        })
      })
      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,
          // position: (point, params, dom, rect, size) => {
          //   var x = 0 // x坐标位置
          //   var y = 0 // y坐标位置

          //   // 当前鼠标位置
          //   var pointX = point[0]
          //   var pointY = point[1]

          //   // 外层div大小
          //   // var viewWidth = size.viewSize[0];
          //   // var viewHeight = size.viewSize[1];

          //   // 提示框大小
          //   var boxWidth = size.contentSize[0]
          //   var boxHeight = size.contentSize[1]

          //   // boxWidth > pointX 说明鼠标左边放不下提示框
          //   if (boxWidth > pointX) {
          //     x = 5
          //   } else {
          //     // 左边放的下
          //     x = pointX - boxWidth
          //   }

          //   // boxHeight > pointY 说明鼠标上边放不下提示框
          //   if (boxHeight > pointY) {
          //     y = 5
          //   } else {
          //     // 上边放得下
          //     y = pointY - boxHeight
          //   }
          //   return [x, y]
          // },
          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 => {
              // if (txt) {
              //   txt = txt + '<br />'
              // }
              txt = txt + item.seriesName + ':' + item.value + '<br />'
            })
            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: 10
          },
          data: []
        },
        yAxis: {
          type: 'value',
          splitLine: {
            lineStyle: {
              color: '#606266'
            }
          },
          splitNumber: 3
        },
        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>