line.vue 4.73 KB
<template>
  <div class="line-container">
    <canvas class="canvas" id="orderRef"></canvas>
    <div class="line-tooltip flex" ref="lineTooltip"></div>
  </div>
</template>
<script>
import F2 from '@antv/f2/lib/index-all'
export default {
  name: 'lineChart',
  data() {
    return {
      chart: null,
      // tickInterval: 5,
      chartColor: this.color,
      refId: this.id
    }
  },
  props: {
    color: {
      type: Array,
      default: () => []
    },
    list: {
      type: Array,
      default: () => []
    },
    id: {
      type: String,
      default: () => ''
    },
    lineData: {
      type: Array,
      default: () => []
    }
  },
  watch: {
    lineData: {
      handler() {
        this.initCanvas()
      },
      deep: true
    }
  },
  mounted() {
    this.initCanvas()
  },
  methods: {
    initCanvas(id = 'orderRef') {
      //   let offsetY
      //   offsetY = document.getElementById(id).height * 0.4
      //   await this.dynamicTick() //动态生成各个标度点的间距
      const data = this.lineData
      let tickIntervalNum = 0
      // 判断lineData是否有值,根据barList中最大值计算tickInterval
      if (data.length) {
        let s = data[0].value
        for (var i = 1; i < data.length; i++) {
          if (s < data[i].value) {
            s = data[i].value
          }
        }
        tickIntervalNum = s
      }
      this.chart && this.chart.clear()
      this.chart = null
      this.chart = new F2.Chart({
        id,
        padding: [30, 40, 30, 40],
        pixelRatio: window.devicePixelRatio
      })
      this.chart.source(data, {
        date: {
          type: 'timeCat',
          mask: 'MM/DD',
          range: [0, 1],
          tickCount: 5
        },
        value: {
          type: 'linear',
          nice: true,
          tickCount: 4,
          tickInterval: Math.ceil(tickIntervalNum / 3)
        }
      })
      this.chart.axis('date', {
        label: function label(text, index, total) {
          var textCfg = {}
          if (index === 0) {
            textCfg.textAlign = 'left'
          } else if (index === total - 1) {
            textCfg.textAlign = 'right'
          }
          return textCfg
        }
      })
      const tooltipEl = this.$refs.lineTooltip
      this.chart.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.date.replace(reg, '$1年$2月$3日')
          tooltipEl.innerHTML = `<div class="time">${currentData.origin.date}</div>
        <div class="item flex"><span class="arc"  style="color:#FA7C0B;">●</span>预约到店:${ev.items[0].value}</div>
        <div class="item flex"><span class="arc" style="color:#3975C6;">●</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.chart.line().position('date*value').shape('smooth').color('type', [ '#FA7C0B','#3975C6'])
      this.chart.legend(false)
      this.chart.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 lang="scss" scoped>
.line-container {
  position: relative;
  .canvas {
    width: 100%;
    height: 180px;
  }
  .line-tooltip {
    opacity: 0;
    position: absolute;
    width: 140px;
    height: 70px;
    border-radius: 6px;
    box-shadow: 0px 2px 5px 0px rgba(0, 0, 0, 0.05);
    background: #ffffff;
    z-index: 99;
    flex-direction: column;
    font-size: 12px;
    box-sizing: border-box;
    padding: 10px;
    left: 0;
    top: 0;
    /deep/.time {
      color: #a9a9a9;
    }
    /deep/.item,
    /deep/.time {
      width: 100%;
      text-align: left;
      font-size: 12px;
    }
    /deep/.arc {
      border-radius: 100%;
      font-size: 5px;
      margin-right: 4px;
    }
  }
}
</style>