canvasMixin.js 6.22 KB
import F2 from '@antv/f2/lib/index-all'
const orderSource = {
  1: '商城订单',
  2: '直播订单',
  3: '活动订单',
  4: '门店套餐'
}
export default {
  data() {
    return {
      chart: null,
      lineChart: null,
      orderSource
    }
  },
  mounted() {
    let { userInfo } = this.data
    if (userInfo) {
      this.$nextTick(() => {
        this.initCanvas(userInfo.accountLevel)
      })
    }
  },
  watch: {
    data(n) {
      let { userInfo } = n
      if (userInfo) {
        this.$nextTick(() => {
          this.initCanvas(userInfo.accountLevel)
        })
      }
    }
  },
  methods: {
    initCanvas(type) {
      let { reportType } = this
      if (type != 4 && reportType == 1) this.initLineChart()
      this.initPieChart()
    },
    initLineChart(id = 'lineChart') {
      console.log(this.$parent)
      let { tabIndex } = this.$parent
      this.lineChart && this.lineChart.clear()
      this.lineChart = null
      this.lineChart = new F2.Chart({
        id,
        padding: [50, 50, 20, 50],
        pixelRatio: window.devicePixelRatio
      })
      if (tabIndex == 0) {
        this.lineChart.source([], {
          date: {
            range: [0, 1]
          },
          number: {
            type: 'linear',
            formatter(e) {
              return Math.ceil(e)
            }
          }
        })
        this.lineChart.axis('date', {
          label(text, index, total) {
            const textCfg = {}
            if (index === 0) {
              textCfg.textAlign = 'left'
            } else if (index === total - 1) {
              textCfg.textAlign = 'end'
            }
            return textCfg
          }
        })
        const tooltipEl = this.$refs.lineTooltip
        this.lineChart.tooltip({
          custom: true,
          showCrosshairs: false,
          onChange(ev) {
            const currentData = ev.items[0]
            const text = currentData.value
            tooltipEl.innerHTML = `<div class="time">2021年10月27日</div>
        <div class="item flex"><span class="arc"  style="color:#3975C6;">●</span>新增企微客户:23</div>
        <div class="item flex"><span class="arc" style="color:#FA7C0B;">●</span>流失客户:23</div>`
            tooltipEl.style.opacity = 1
            // tooltipEl.style.left = currentData.x - 140 / 2 + 'px'
            // tooltipEl.style.top = currentData.y - 70 - 15 + 'px'
          },
          onHide() {
            tooltipEl.style.opacity = 0
          }
        })
        this.lineChart.legend(false)
        this.lineChart
          .area()
          .position('date*number')
          .color('country', val => {
            if (val == '新增企微客户') return ['#DADAFF', '#F7F7F7']
            return ['#FFECDB', '#F7F7F7']
          })
          .adjust('stack')
        this.lineChart
          .line()
          .position('date*number')
          .color('country', val => {
            if (val == '新增企微客户') return '#3975C6'
            return '#FA7C0B'
          })
          .adjust('stack')
      } else {
        let { trendList } = this.data
        this.lineChart.source(trendList, {
          dataDateStr: {
            type: 'timeCat',
            mask: 'MM/DD',
            tickCount: 8,
            range: [0, 1]
          },
          orderMoneyTotalSum: {
            formatter(e) {
              return Math.ceil(e)
            }
          }
        })
        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(false)
        this.lineChart.line().position('dataDateStr*orderMoneyTotalSum').shape('smooth').color('#3975C6')
        this.lineChart.point().position('dataDateStr*orderMoneyTotalSum').shape('smooth').style({
          stroke: '#fff',
          lineWidth: 1
        })
      }
      const textObj = {
        top: true,
        position() {
          return [0, 0] // 位置信息
        },
        style: {
          fill: '#888', // 文本颜色
          fontSize: '10' // 文本大小
        }
      }

      this.lineChart.guide().text({
        ...textObj,
        content: '日期',
        offsetX: window.screen.width - 70,
        offsetY: 14
      })
      this.lineChart.guide().text({
        ...textObj,
        content: '数量',
        offsetX: -10,
        offsetY: -160
      })
      this.lineChart.render()
    },
    initPieChart(id = 'pieChart') {
      let { productionValueReportList } = this.data
      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.5
      })
      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: 30,
        activeShape: true,
        label1(data) {
          return {
            text: `${orderSource[data.orderSource]}-占${data.newProductionValueRate}`,
            fill: '#333'
          }
        },
        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()
    }
  }
}