timeline.js 3.32 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 = 750 / 750 * getSystemInfoSync().windowWidth;
      //这里的 500 对应 css .charts 的 height
      const cHeight = 500 / 750 * getSystemInfoSync().windowWidth;
      this.setData({
        cWidth,
        cHeight
      });
      this.getServerData();
    },
    getServerData() {
      let _max = 0;
      const {viewCustomerPortrait:{browseDistribution}} = this.data,
      categories = browseDistribution.map((i,k)=>{
        return k%2==0?i.name:''
      }),
      data = browseDistribution.map(i=>{
        _max = Math.max(_max, i.value)
        return i.value
      })
      //模拟从服务器获取数据时的延时
      setTimeout(() => {
        //模拟服务器返回数据,如果数据格式和标准格式不同,需自行按下面的格式拼接
        let res = {
          categories,
          series: [{
            data
          }, ]
        };
        this.drawCharts('MkatSdTACyZKZRwWnLaWPTKfZrUmkBlh', res,_max);
      }, 500);
    },
    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]) {
          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({
            type: "line",
            context: ctx,
            width: this.data.cWidth * this.data.pixelRatio,
            height: this.data.cHeight * this.data.pixelRatio,
            categories: data.categories,
            series: data.series,
            animation: true,
            pixelRatio: this.data.pixelRatio,
            background: "#FFFFFF",
            color: ["#35C376"],
            padding: [15, 15, 0, 0],
            enableScroll: false,
            legend: {
              show: false
            },
            xAxis: {
              disableGrid: true,
              axisLineColor: '#fff',
              fontColor: '#A6A6A6',
              fontSize: '10'
            },
            yAxis: {
              gridType: "dash",
              dashLength: 2,
              data: [{
                axisLine: false,
                fontColor: '#A6A6A6',
                max: _max
              }]
            },
            extra: {
              tooltip: {
                showBox: false
              },
              line: {
                type: "straight",
                width: 2,
                activeType: "none"
              }
            }
          });
        } else {
          console.error("[uCharts]: 未获取到 context");
        }
      });
    },
    tap(e) {
      uChartsInstance[e.target.id].touchLegend(e);
      uChartsInstance[e.target.id].showToolTip(e);
    }
  }

})