consume.js 3.4 KB
//index.js
//注意!基础库【2.9.0】起支持 2d 模式,如不显示请检查基础库版本!
import uCharts from '../../../../../utils/u-charts/u-charts.js';
import { getSystemInfoSync, getSystemInfo } from '~/utils/systemInfo'
const view = require('../view.js')
var uChartsInstance = {};

Component({
  behaviors: [view],
  data: {
    cWidth: 750,
    cHeight: 500,
    pixelRatio: 2,
  },


  methods: {
    getRatio() {
      //这里的第一个 750 对应 css .charts 的 width
      const cWidth = 700 / 750 * getSystemInfoSync().windowWidth;
      //这里的 500 对应 css .charts 的 height
      const cHeight = 480 / 750 * getSystemInfoSync().windowWidth;
      const pixelRatio = getSystemInfoSync().pixelRatio;
      this.setData({
        cWidth,
        cHeight,
        pixelRatio
      });
      this.getServerData();
    },
    getServerData() {
      let _max = 0;
      const {
        viewCustomerPortrait: {
          productDistribution
        }
      } = this.data,
        categories = (productDistribution || []).map(i => i.name),
        data = (productDistribution || []).map((i, k) => {
          _max = Math.max(_max, i.value)
          return {
            value: i.value,
            color: k % 2 == 0 ? '#3582EE' : '#5CF0D8'
          }
        })
      let res = {
        categories,
        series: [{
          name: "目标值",
          textColor: '#A6A6A6',
          data
        }, ]
      };
      this.drawCharts('afMCYQMEmXXVAjNQFJvvfxbLSHuxNEOL', res, _max);
    },
    drawCharts(id, data, _max) {
      _max = Math.ceil(_max / 5) * 5
      const query = wx.createSelectorQuery().in(this);
      query.select('#' + id).fields({
        node: true,
        size: true
      }).exec(res => {
        if (res[0]) {
          console.log(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;
          uChartsInstance[id] = new uCharts({
            animation: true,
            background: "#FFFFFF",
            canvas2d: true,
            categories: data.categories,
            color: ["#3582EE"],
            context: ctx,
            extra: {
              tooltip: {
                showBox: false
              },
              column: {
                type: "group",
                width: 15,
                activeBgColor: "#000000",
              }
            },
            height: this.data.cHeight * this.data.pixelRatio,
            legend: {
              show: false,
            },
            padding: [15, 15, 5, 5],
            pixelRatio: this.data.pixelRatio,
            series: data.series,
            type: "column",
            width: this.data.cWidth * this.data.pixelRatio,
            xAxis: {
              disableGrid: true,
              axisLineColor: '#fff',
              fontColor: '#A6A6A6',
              fontSize: 10,
              marginTop: 4,
              rotateLabel:true,
              rotateAngle: 45
            },
            yAxis: {
              disableGrid: true,
              fontColor: '#A6A6A6',
              data: [{
                axisLine: false,
                fontColor: '#A6A6A6',
                min: 0,
                max: _max
              }]
            }
          });
        } else {
          console.error("[uCharts]: 未获取到 context");
        }
      });
    }
  }

})