circular.js 5.44 KB
// packageEquity/pages/aiInspection/components/circular/circular.js
const Arc_Background = [
    '#F8F8FA',
    '#F5F7FA',
    '#F0F2F5',
    '#EBEDF2',
    '#E4E8EE'
  ],
  Arc_Data_Background = [
    '#CCE6FF',
    '#A6CCFF',
    '#80ACFF',
    '#4D8AFF',
    '#196AFF'
  ],
  Start_Angle = 0.79 * Math.PI,
  Angle = Math.PI / 3.5
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    num: Number
  },
  lifetimes: {
    ready() {
      this.init()
    }
  },
  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    init() {
      try {
        this._pixelRatio_ = 2
        const width = this.toRpx(120),
          canvas = wx.createOffscreenCanvas({
            type: '2d',
            width,
            height: width,
            compInst: true
          }),
          ctx = canvas.getContext('2d')
        ctx.lineWidth = this.toRpx(2)
        ctx.strokeStyle = '#fff'
        this.ctx = ctx
        this.arcInfo = {
          x: width / 2,
          y: width / 2,
          innerRadius: this.toRpx(58),
          outerRadius: this.toRpx(34),
          lineWidth: this.toRpx(22),
          arcRadius: this.toRpx(46)
        }
        this.canvas = canvas
        this.drawBackground()
      } catch (error) {
        console.log(error)
        wx.$util.showToast(error || '出错了,请重试')
      }
    },
    drawBackground() {
      const {
        ctx,
        arcInfo: {
          x,
          y,
          innerRadius,
          outerRadius
        }
      } = this
      let startAngle = Start_Angle
      for (let i = 0; i < 5; i++) {
        ctx.beginPath()
        ctx.arc(x, y, innerRadius, startAngle, startAngle + Angle)
        ctx.arc(x, y, outerRadius, startAngle + Angle, startAngle, true)
        if (i == 0) ctx.closePath()
        ctx.fillStyle = Arc_Background[i]
        ctx.fill()
        ctx.stroke()
        startAngle += Angle
      }
      ctx.save()
      this.drawTips()
    },
    drawData() {
      const {
        num
      } = this.data
      this.part = Math.ceil(num / 20)
      if (num != null) this.fillArc(Start_Angle, 0, num == 0 ? Start_Angle : Start_Angle + Angle)
    },
    drawTips(data) {
      let {
        x,
        y,
      } = this.arcInfo
      x = x / 2
      this.ctx.clearRect(x + 10, y / 2 + 20, this.toRpx(52), this.toRpx(30))
      this.ctx.clearRect(x + 10, y / 2 + 60, this.toRpx(50), this.toRpx(30))
      this.drawText({
        text: data != null ? data : '···',
        x,
        y: y / 2,
        fontSize: 30,
        color: '#196AFF',
        fontWeight: 600,
        textAlign: 'center',
        textBaseline: 'middle'
      })
      this.drawText({
        text: data != null ? '分数' : '计算中…',
        x,
        y: (y + 44) / 2,
        fontSize: 10,
        color: '#196AFF',
        fontWeight: 400,
        textAlign: 'center',
        textBaseline: 'middle'
      })
    },

    fillArc(startAngle, index, endAngle) {
      const {
        ctx,
        part,
        arcInfo: {
          x,
          y,
          lineWidth,
          arcRadius
        },
        data: {
          num
        }
      } = this,
      a = startAngle
      if(num == 0) return this.drawTips(num)
      let b = startAngle + Math.PI / 70
      ctx.beginPath()
      ctx.strokeStyle = Arc_Data_Background[index]
      ctx.lineWidth = lineWidth
      ctx.arc(x, y, arcRadius, a, b)
      ctx.stroke()
      ctx.save()
      this.drawImg()
      setTimeout(() => {
        if (index == part - 1) {
          const blockNum = Start_Angle + (Angle * index),
            remainder = (endAngle - blockNum) / 20 * ((num % 20) == 0 ? 20 : num % 20) + blockNum
          if (b == remainder) {
            this.refactor(remainder)
            this.drawTips(num)
            return
          }
        }
        if (b <= endAngle) this.fillArc(b, index, endAngle)
        else {
          if (index < part - 1) {
            this.fillArc(endAngle, index + 1, startAngle + Angle)
          } else this.drawTips(num)
        }
      }, 10)
    },
    refactor(remainder) {
      const {
        ctx,
        part,
        arcInfo: {
          x,
          y,
          lineWidth,
          arcRadius
        }
      } = this
      let startAngle = Start_Angle
      for (let i = 0; i < part; i++) {
        ctx.beginPath()
        ctx.strokeStyle = Arc_Data_Background[i]
        ctx.lineWidth = lineWidth
        if (i == part - 1) {
          ctx.arc(x, y, arcRadius, startAngle, remainder)
        } else {
          ctx.arc(x, y, arcRadius, startAngle, startAngle + Angle)
        }
        // ctx.fillStyle = Arc_Data_Background[i]
        ctx.stroke()
        startAngle += Angle
      }
      ctx.save()
      this.drawImg()
    },
    drawText(params) {
      const {
        ctx
      } = this
      let {
        x,
        y,
        text,
        color,
        fontSize,
        textAlign,
        fontWeight,
        textBaseline
      } = params,
      [, font] = ctx.font.split('px')
      x = this.toRpx(x)
      y = this.toRpx(y)
      ctx.beginPath()
      ctx.font = `${fontWeight || 400} ${this.toRpx(fontSize || 12)}px ${font}`
      ctx.textAlign = textAlign || 'left'
      ctx.textBaseline = textBaseline || 'top'
      ctx.fillStyle = color
      ctx.fillText(text, x, y)
      ctx.save()
      this.drawImg()
    },
    drawImg() {
      let url = this.canvas.toDataURL('image/png')
      this.setData({
        url
      })
    },
    toRpx(val) {
      return this._pixelRatio_ * val
    },
  }
})