DrawLine.js 4.38 KB
const _ctx_ = Symbol('_ctx_')
const _pixelRatio_ = Symbol('_pixelRatio_')
const _canvas_ = Symbol('_canvas_')
const _params_ = Symbol('_params_')
class DrawLineChart {
  [_ctx_] = null;
  [_pixelRatio_] = null;
  [_canvas_] = null;
  [_params_] = null;
  maxYNum = null;
  currentSegment = 1;
  constructor(el, params, componentsThis) {
    const query = wx.createSelectorQuery().in(componentsThis)
    query.select(el).fields({
      node: true,
      size: true
    }).exec((res) => {
      console.log(res)
      const canvas = res[0].node,
        ctx = canvas.getContext('2d'),
        dpr = wx.getSystemInfoSync().pixelRatio,
        width = res[0].width * dpr,
        height = res[0].height * dpr
      canvas.width = width
      canvas.height = height

      this[_canvas_] = canvas
      this[_ctx_] = ctx
      this[_pixelRatio_] = dpr
      this[_params_] = params
      let max = Math.max(...params.data)
      if (max < params?.areaData?.max) max = params.areaData.max
      this.maxYNum = max + max / 10 || 10
      this.drawLine(params.data)
    })
  }
  drawXLine() {
    const {
      height,
      width
    } = this[_canvas_],
      lineNum = 5
    this[_ctx_].lineWidth = this.toRpx(1)
    for (let i = 0; i < lineNum; i++) {
      const y = height / lineNum * i
      this[_ctx_].beginPath()
      this[_ctx_].strokeStyle = '#E5E5EF'
      this[_ctx_].setLineDash([this.toRpx(6), this.toRpx(4)])
      this[_ctx_].moveTo(0, y)
      this[_ctx_].lineTo(width, y)
      this[_ctx_].stroke()
      this[_ctx_].save()
    }
    this[_ctx_].beginPath()
    this[_ctx_].strokeStyle = '#E5E5EF'
    this[_ctx_].setLineDash([], 0)
    this[_ctx_].moveTo(0, height)
    this[_ctx_].lineTo(width, height)
    this[_ctx_].stroke()
    this[_ctx_].save()
  }
  drawAreaView() {
    const {
      height,
      width
    } = this[_canvas_], {
        areaData
      } = this[_params_],
      min = height - (height / this.maxYNum * (areaData.min || 0)),
      max = height - (height / this.maxYNum * (areaData.max || 10))
    this[_ctx_].beginPath()
    this[_ctx_].fillStyle = '#3275fa1a'
    this[_ctx_].fillRect(this.toRpx(4), max, width - this.toRpx(8), min - max)
    this[_ctx_].save()
  }
  drawLine(data) {
    const {
      height,
      width
    } = this[_canvas_], {
        maxYNum
      } = this,
      maxXNum = data.length,
      curveData = []
    for (let i = 0; i < maxXNum; i++) {
      curveData.push({
        x: width / maxXNum * i + (width / maxXNum / 2),
        y: height - (height / maxYNum * data[i])
      })
    }
    this.drawCurve(curveData)

  }
  drawCurve(curveData, currentSegment = 1) {
    const {
      width,
      height
    } = this[_canvas_]
    if (currentSegment < curveData.length - 1) {
      this[_ctx_].clearRect(0, 0, width, height)
      this.drawXLine()
      this.drawAreaView()
      this[_ctx_].lineWidth = this.toRpx(2)
      const gradient = this[_ctx_].createLinearGradient(0, 0, width, 0);
      gradient.addColorStop(0, '#51D3F4'); // 起点颜色
      gradient.addColorStop(1, '#002EF1'); // 终点颜色
      this[_ctx_].strokeStyle = gradient;
      this[_ctx_].beginPath();
      this[_ctx_].moveTo(curveData[0].x, curveData[0].y);
      for (let i = 1; i <= currentSegment; i++) {
        const p1 = curveData[i];
        const p2 = curveData[i + 1];
        const cp1 = {
          x: p1.x + (p2.x - p1.x) * 0.5,
          y: p1.y
        };
        const cp2 = {
          x: p2.x - (p2.x - p1.x) * 0.5,
          y: p2.y
        };
        this[_ctx_].bezierCurveTo(cp1.x, cp1.y, cp2.x, cp2.y, p2.x, p2.y);
        this[_ctx_].shadowOffsetX = 0
        this[_ctx_].shadowOffsetY = this.toRpx(8)
        this[_ctx_].shadowBlur = this.toRpx(4)
        this[_ctx_].shadowColor = '#c5cee080'
      }
      // 处理最后一个点
      if (currentSegment == curveData.length - 2) {
        const lastPoint = curveData[curveData.length - 1];
        this[_ctx_].lineTo(lastPoint.x, lastPoint.y)
      }
      this[_ctx_].stroke();
      this[_ctx_].restore()
    }
    currentSegment += 1
    // 如果还有未绘制的曲线段,则请求下一帧动画
    if (currentSegment <= curveData.length) {
      // setTimeout(() => {
      this[_canvas_].requestAnimationFrame(() => {
        this.drawCurve(curveData, currentSegment)
      })
      //   this.drawCurve(curveData, currentSegment)
      // }, 1e3 / 30)
    }
  }
  toRpx(val) {
    return this[_pixelRatio_] * val
  }
}

export default DrawLineChart