Chart.vue 12.6 KB
<template>
  <div class="canvas-box">
    <!-- 客户的折线图 -->

    <canvas
      id="lineCustomerChart"
      class="canvas"
      v-show="
        (userInfo.accountLevel != 4 && reportType == 1 && tabType.index == 2 && tabIndex == 0 && trendChartList.length > 0) ||
        (userInfo.accountLevel == 4 && reportType == 1 && tabType.index == 0 && tabIndex == 0 && trendChartList.length > 0)
      "
    ></canvas>
    <div class="line-tooltip flex" ref="lineCustomerTooltip" id="2" v-if="tabIndex == 0"></div>
    <!-- 产值的折线图 -->

    <canvas
      id="lineChart"
      class="canvas"
      v-show="
        (tabIndex == 1 && userInfo.accountLevel != 4 && reportType == 1 && tabType.index == 2 && trendList.length > 0) ||
        (tabIndex == 1 && userInfo.accountLevel == 4 && reportType == 1 && tabType.index == 0 && trendList.length > 0)
      "
    ></canvas>
    <div class="line-tooltip flex" ref="lineTooltip" id="3" v-if="tabIndex == 1"></div>
    <canvas class="canvas" id="pieChart" v-show="tabIndex == 1 && tabType.index == 1 && productionValueReportList.length > 0"></canvas>

    <template
      v-if="
        (userInfo.accountLevel != 4 && reportType == 1 && tabType.index == 2 && tabIndex == 0 && trendChartList.length == 0) ||
        (userInfo.accountLevel == 4 && reportType == 1 && tabType.index == 0 && tabIndex == 0 && trendChartList.length == 0) ||
        (tabIndex == 1 && userInfo.accountLevel != 4 && reportType == 1 && tabType.index == 2 && trendList.length == 0) ||
        (tabIndex == 1 && userInfo.accountLevel == 4 && reportType == 1 && tabType.index == 0 && trendList.length == 0) ||
        (tabIndex == 1 && tabType.index == 1 && productionValueReportList.length == 0)
      "
    >
      <Empty></Empty>
    </template>
  </div>
</template>

<script type='text/ecmascript-6'>
import F2 from '@antv/f2/lib/index-all'
import { mapState, mapGetters, mapActions } from 'vuex'
export default {
  name: 'Chart',
  computed: {
    ...mapState({
      tabIndex: state => state.report.tabIndex,
      tabType: state => state.report.tabType,
      productionValueReportList: state => state.report.productionValueReportList, //饼图
      reportType: state => state.report.reportType,
      userInfo: state => state.report.userInfo,
      trendList: state => state.report.trendList, //产值折线
      trendChartList: state => state.report.trendChartList //客户趋势
    })
  },
  watch: {
    tabType: {
      handler(n) {
        let { userInfo } = this
        this.$nextTick(() => {
          this.initCanvas(userInfo.accountLevel)
        })
      },
      deep: true
    },
    trendChartList(n) {
      let { userInfo } = this
      this.$nextTick(() => {
        this.initCanvas(userInfo.accountLevel)
      })
    },
    trendList(n) {
      let { userInfo } = this
      this.$nextTick(() => {
        this.initCanvas(userInfo.accountLevel)
      })
    },
    productionValueReportList(n) {
      let { userInfo } = this
      this.$nextTick(() => {
        this.initCanvas(userInfo.accountLevel)
      })
    }
  },
  data() {
    return {
      chart: null,
      lineChart: null,
      lineChartCustomer: null,
      tickInterval: 5,
      orderSource: {
        1: '商城订单',
        2: '直播订单',
        3: '活动订单',
        4: '门店服务套餐',
        5: '精品升级',
        6: '权益提单',
        7: '商城订单'
      }
    }
  },
  mounted() {
    let { userInfo } = this
    if (userInfo) {
      this.$nextTick(() => {
        this.initCanvas(userInfo.accountLevel)
      })
    }
  },
  methods: {
    initCanvas(type) {
      //tabIndex 0 客户 1 产值
      this.$nextTick(() => {
        let { reportType, tabIndex, tabType } = this
        if ((tabIndex == 1 && type != 4 && reportType == 1 && tabType.index == 2) || (tabIndex == 1 && type == 4 && reportType == 1 && tabType.index == 0)) this.initLineChart() //产值
        if ((tabIndex == 0 && type != 4 && reportType == 1 && tabType.index == 2) || (tabIndex == 0 && type == 4 && reportType == 1 && tabType.index == 0)) this.lineCustomerChart() //客户
        if ((type == 4 && tabIndex == 1 && tabType.index == 1) || (type != 4 && tabIndex == 1 && tabType.index == 1) || reportType == 0) this.initPieChart()
      })
    },
    initLineChart(id = 'lineChart') {
      let { trendList } = this
      trendList.forEach(item => {
        item.type = '新增产值'
      })
      this.lineChart && this.lineChart.clear()
      this.lineChart = null
      this.lineChart = new F2.Chart({
        id,
        padding: [70, 50, 20, 50],
        pixelRatio: window.devicePixelRatio
      })

      this.lineChart.source(trendList, {
        dataDateStr: {
          type: 'timeCat',
          mask: 'MM/DD',
          tickCount: 5,
          range: [0, 1]
        },
        orderMoneyTotalSum: {}
      })
      this.lineChart.axis('dataDateStr', {
        label: function label(text, index, total) {
          var textCfg = {}
          if (index === 0) {
            textCfg.textAlign = 'left'
          } else if (index === total - 1) {
            textCfg.textAlign = 'right'
          }
          return textCfg
        }
      })
      // this.lineChart.tooltip({
      //   showTitle: true, // 展示  tooltip 的标题
      //   layout: 'vertical'
      // })
      const tooltipEl = this.$refs.lineTooltip

      this.lineChart.tooltip({
        custom: true,
        showCrosshairs: false,
        triggerOn: ['touchstart', 'touchmove'],
        triggerOff: 'touchend',
        onChange(ev) {
          const currentData = ev.items[0]
          let reg = /(\d{4})-(\d{1,2})-(\d{1,2})/
          const time = currentData.origin.dataDateStr.replace(reg, '$1年$2月$3日')
          tooltipEl.innerHTML = `<div class="time">${time}</div>
        <div class="item flex"><span class="arc"  style="color:#3975C6;">●</span>新增产值:${ev.items[0].value}</div>`
          tooltipEl.style.opacity = 1
          tooltipEl.style.left = currentData.x - 140 / 2 + 'px'
          tooltipEl.style.top = currentData.y - 45 - 15 + 'px'
        },
        onHide() {
          tooltipEl.style.opacity = 0
        }
      })
      this.lineChart.legend(false)
      this.lineChart.line().position('dataDateStr*orderMoneyTotalSum').shape('smooth').color('type', ['#3975C6'])
      const textObj = {
        top: true,
        position() {
          return [0, 0] // 位置信息
        },
        style: {
          fill: '#888', // 文本颜色
          fontSize: '10' // 文本大小
        }
      }

      this.lineChart.guide().text({
        ...textObj,
        content: '日期',
        offsetX: window.screen.width - 100,
        offsetY: 14
      })
      this.lineChart.guide().text({
        ...textObj,
        content: '产值',
        offsetX: -10,
        // offsetY: -`${offsetY}`
        offsetY: -156
      })
      this.lineChart.render()
    },
    initPieChart(id = 'pieChart') {
      let { productionValueReportList, orderSource } = this
      productionValueReportList.map(e => {
        e.count = 'count'
      })
      this.chart && this.chart.clear()
      this.chart = null
      this.chart = new F2.Chart({
        id,
        pixelRatio: window.devicePixelRatio,
        padding: [40, 40, 40, 40]
      })
      this.chart.source(productionValueReportList)
      this.chart.coord('polar', {
        transposed: true,
        radius: 0.9,
        innerRadius: 0.6
      })
      this.chart.axis(false)
      this.chart.legend(false)
      this.chart.tooltip(false)
      this.chart.guide().html({
        position: ['50%', '50%'],
        html: '<div style="text-align: center;width:150px;height: 50px;">\n      <p style="font-size: 12px;color: #999;margin: 0" id="title"></p>\n      <p style="font-size: 18px;color: #343434;margin: 0;font-weight: bold;" id="money"></p>\n      </div>'
      })
      this.chart.interval().position('count*newProductionValue').adjust('stack').color('orderSource', ['#3975C6', '#43B899', '#FFC667', '#FF9DAA'])
      this.chart.pieLabel({
        sidePadding: 10,
        activeShape: true,
        label1(data, color) {
          return {
            text: `${orderSource[data.orderSource]}-占${data.newProductionValueRate}`,
            fill: color
          }
        },
        label2(data) {
          return {
            text: `¥${data.newProductionValue}`,
            fill: '#333',
            fontSize: 12,
            fontWeight: 'bold'
          }
        },
        onClick(ev) {
          const data = ev.data
          if (data) {
            // console.log(data)
          }
        }
      })
      this.chart.render()
    },
    async lineCustomerChart(id = 'lineCustomerChart') {
      let offsetY
      offsetY = document.getElementById(id).height * 0.4
      await this.dynamicTick() //动态生成各个标度点的间距
      this.lineChartCustomer && this.lineChartCustomer.clear()
      this.lineChartCustomer = null
      this.lineChartCustomer = new F2.Chart({
        id,
        padding: [70, 50, 20, 50],
        pixelRatio: window.devicePixelRatio
      })
      let trendList = []
      let { trendChartList } = this
      trendChartList.forEach(item => {
        trendList.push(
          {
            dayTime: item.dayTime,
            type: '新增企微客户数',
            value: item.entMicroCount
          },
          {
            dayTime: item.dayTime,
            type: '新增客户',
            value: item.newlyCustomerCount
          }
        )
      })
      this.lineChartCustomer.source(trendList, {
        dayTime: {
          type: 'timeCat',
          mask: 'MM/DD',
          range: [0, 1],
          tickCount: 5
        },
        value: {
          type: 'linear',
          nice: true,
          tickInterval: this.tickInterval
        }
      })
      this.lineChartCustomer.axis('dayTime', {
        label: function label(text, index, total) {
          var textCfg = {}
          if (index === 0) {
            textCfg.textAlign = 'left'
          } else if (index === total - 1) {
            textCfg.textAlign = 'right'
          }
          return textCfg
        }
      })
      // Tooltip 配置
      // this.lineChartCustomer.tooltip({
      //   showTitle: true, // 展示  tooltip 的标题
      //   layout: 'vertical'
      // })
      const tooltipEl = this.$refs.lineCustomerTooltip
      this.lineChartCustomer.tooltip({
        custom: true,
        showCrosshairs: false,
        triggerOn: ['touchstart', 'touchmove'],
        triggerOff: 'touchend',
        onChange(ev) {
          const currentData = ev.items[0]
          let reg = /(\d{4})-(\d{1,2})-(\d{1,2})/
          const time = currentData.origin.dayTime.replace(reg, '$1年$2月$3日')
          tooltipEl.innerHTML = `<div class="time">${time}</div>
        <div class="item flex"><span class="arc"  style="color:#3975C6;">●</span>新增企微客户:${ev.items[0].value}</div>
        <div class="item flex"><span class="arc" style="color:#FA7C0B;">●</span>新增客户:${ev.items[1].value}</div>`
          tooltipEl.style.opacity = 1
          tooltipEl.style.left = currentData.x - 140 / 2 + 'px'
          tooltipEl.style.top = currentData.y - 45 - 15 + 'px'
        },
        onHide() {
          tooltipEl.style.opacity = 0
        }
      })
      this.lineChartCustomer.line().position('dayTime*value').shape('smooth').color('type', ['#3975C6', '#FA7C0B'])
      this.lineChartCustomer.legend(false)
      const textObj = {
        top: true,
        position() {
          return [0, 0] // 位置信息
        },
        style: {
          fill: '#888', // 文本颜色
          fontSize: '10' // 文本大小
        }
      }
      this.lineChartCustomer.guide().text({
        ...textObj,
        content: '日期',
        offsetX: window.screen.width - 100,
        offsetY: 0
      })
      this.lineChartCustomer.guide().text({
        ...textObj,
        content: '数量',
        offsetX: -10,
        offsetY: -156
      })
      this.lineChartCustomer.render()
    },
    dynamicTick() {
      let entMicroCountData = [] //新增企微客户数
      let newlyCustomerCountData = []
      let entMicroCount
      let newlyCustomerCount
      let tickData = [...this.trendChartList]
      tickData.forEach(i => {
        entMicroCountData.push(i.entMicroCount)
        newlyCustomerCountData.push(i.newlyCustomerCount)
      })
      entMicroCountData.sort((a, b) => {
        return b - a
      })
      newlyCustomerCountData.sort((a, b) => {
        return b - a
      })
      entMicroCount = entMicroCountData[0]
      newlyCustomerCount = newlyCustomerCountData[0]
      this.tickInterval = Math.ceil(entMicroCount > newlyCustomerCount ? entMicroCount / 5 : newlyCustomerCount / 5)
    }
  }
}
</script>

<style scoped lang='scss'>
@import '@/assets/css/statistics.scss';
.canvas {
  width: 345px;
  height: 195px;
}
</style>