circle.js 3.42 KB
//index.js
//注意!基础库【2.9.0】起支持 2d 模式,如不显示请检查基础库版本!
import uCharts from '../../../../../utils/u-charts/u-charts.js';
import { getSystemInfoSync, getSystemInfo } from '~/utils/systemInfo'
var uChartsInstance = {};
Component({
  properties: {
    width: {
      type: Number,
      value: {}
    },
    isLabel:{
        type: Boolean,
        value: false
    },
    Id: {
      type: String
    },
    ratioList: {
      type:Object,
      observer(n) {
        this.setRatio()
      }
    }
  },
  data: {
    pixelRatio: 2,
  },

  methods: {
    setRatio() {
      const {
        width,
        isLabel
      } = this.data
      //这里的第一个 750 对应 css .charts 的 width
      const cWidth = (isLabel?700:width) / 750 * getSystemInfoSync().windowWidth;
      //这里的 500 对应 css .charts 的 height
      const cHeight = width / 750 * getSystemInfoSync().windowWidth;
      const pixelRatio = getSystemInfoSync().pixelRatio;
      this.setData({
        cWidth,
        cHeight,
        pixelRatio
      });
      this.getServerData();
    },
    getServerData() {
      //模拟从服务器获取数据时的延时
        //模拟服务器返回数据,如果数据格式和标准格式不同,需自行按下面的格式拼接
        const data = this.data.ratioList.map(i=>{
          return {
            "labelShow": true,
            "value": i.value,
          }
        })
        let res = {
          series: [{
            data
          }]
        };
        this.drawCharts(this.data.Id, res);
    },
    drawCharts(id, data) {
      const query = wx.createSelectorQuery().in(this);
      query.select('#' + id).fields({
        node: true,
        size: true
      }).exec(res => {
        if (res[0]) {
          const canvas = res[0].node;
          const ctx = canvas.getContext('2d');
          canvas.width = res[0].width * this.data.pixelRatio;
          canvas.height = res[0].height * this.data.pixelRatio;
          const color = this.data.ratioList.map(i=>i.color)
          uChartsInstance[id] = new uCharts({
            type: "ring",
            context: ctx,
            width: this.data.cWidth * this.data.pixelRatio,
            height: this.data.cHeight * this.data.pixelRatio,
            pixelRatio: this.data.isLabel?this.data.pixelRatio:1,
            series: data.series,
            animation: true,
            rotate: false,
            rotateLock: false,
            background: "#FFFFFF",
            color,
            fontSize: 16,
            fontColor: color,
            padding: [5, 5, 5, 5],
            dataLabel: this.data.isLabel?true:false,
            enableScroll: false,
            legend: {
              show: false
            },
            // title: {
            //     name: "收益率",
            //     fontSize: 15,
            //     color: "#666666"
            // },
            // subtitle: {
            //     name: "70%",
            //     fontSize: 25,
            //     color: "#7cb5ec"
            // },
            extra: {
              tooltip: {
                showBox: false
              },
              ring: {
                ringWidth: this.data.isLabel?60:this.data.width / 4,
                activeOpacity: 0.5,
                activeRadius: 10,
                offsetAngle: 0,
              }
            }
          })
        } else {
          console.log("[uCharts]: 未获取到 context");
        }
      });
    },
  }

})