微信小程序使用echarts图表

4,876 阅读2分钟

前言

数据统计是我们经常使用的功能,我们一般在 pc 端使用的比较多,大多数用在管理系统中统计数据的分析,最近在做微信小程序的时候也遇到了相同的需求,把数据统计在小程序端以图表的形式展示,这里记录下自己的配置使用过程。

准备

首先百度的 echarts 没有提供小程序版本,这里找了个封装过可以用在微信端的仓库小程序版 echarts,通过这个链接下载最新的包。解压之后有个ec-canvas文件夹就是封装的组件,放到小程序的组件文件夹目录下,以供使 用。

├── ec-canvas
│   ├── ec-canvas.js
│   ├── ec-canvas.json
│   ├── ec-canvas.wxml
│   ├── ec-canvas.wxss
│   ├── echarts.min.js
│   └── wx-canvas.js

第一种使用方式

  1. 在需要使用的页面配置文件中引入该图表组件(路径相对于你组件文件夹的位置)
"usingComponents": {
    "ec-canvas": "../../ec-canvas/ec-canvas"
  }
  1. index.wxml 中,我们创建了一个 组件:
<view class="container">
  <ec-canvas ec="{{ ringEc }}"></ec-canvas>
</view>
  1. 在index.js中对图表进行初始化设置
// 相对于你解压的图表包的位置
import * as echarts from '../../../../components/ec-canvas/echarts';
// 定义接收图表实例
let initExpressChart = null;

Component({
  properties: {
    chartData: {
      type: Array,
      value: [],
    },
    num: {
      type: Number,
      value: 0,
    },
  },

  data: {
    // 初始化配置
    ringEc: {
      onInit(canvas, width, height, dpr) {
        initExpressChart = echarts.init(canvas, null, {
          width: width,
          height: height,
          devicePixelRatio: dpr,
        });
        canvas.setChart(initExpressChart);
        return initExpressChart;
      },
    },
  },
  // 监听数据变化重绘图表
  observers: {
    'chartData,num': function () {
      this.changeChart();
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
     // 重绘图表
    changeChart: function () {
      const { chartData, num } = this.data;
      setTimeout(() => {
        initExpressChart.setOption(
          {
            backgroundColor: '#ffffff',
            title: [
              {
                text: '中间显示类容',
                x: 'center',
                y: '40%',
                textStyle: {
                  color: '#999999',
                  fontSize: 12,
                  fontWeight: '100',
                },
              },
              {
                text: num,
                x: 'center',
                y: '48%',
                textStyle: {
                  fontSize: 16,
                  fontFamily: 'DINAlternate-Bold, DINAlternate',
                  foontWeight: '600',
                },
              },
            ],
            series: [
              {
                label: false,
                type: 'pie',
                center: ['50%', '50%'],
                radius: ['70%', '95%'],
                color: ['#FF6F4E', '#2FD7B2', '#FFD137'],
                data: chartData,
              },
            ],
          },
          true
        );
      }, 1000);
    },
  },
});

第二种使用方式

  1. 在需要使用的页面配置文件中引入该图表组件(路径相对于你组件文件夹的位置)
"usingComponents": {
    "ec-canvas": "../../ec-canvas/ec-canvas"
  }
  1. index.wxml 中,我们创建了一个 组件:
<view class="container">
  <ec-canvas id="mychart-dom-bar" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>
</view>
  1. 其中 ec 是一个我们在 index.js 中定义的对象,它使得图表能够在页面加载后被初始化并设置。index.js 的结构如下:
Page({
  data: {
    ec: {
      onInit: initChart
    }
  },
  onLoad(){
      // 在需要的地方获取dom
      this.echartsComponnet1 = this.selectComponent('#mychart-dom-bar1')
      this.init_echarts1({ value: res.data.rotateSpeed || 0, name: 'x1000' })
  }
  // 初始化
    init_echarts1 (data) {
      this.echartsComponnet1.init((canvas, width, height) => {
        // 初始化图表
        const chart = echarts.init(canvas, null, {
          width: width,
          height: height
        })
        this.chart = chart
        // setGaugeChartOption1获取到基础配置
        chart.setOption(setGaugeChartOption1(data))
        // 注意这里一定要返回 chart 实例,否则会影响事件处理等
        return chart
      })
    },
});