微信小程序绘制echart饼图

微信小程序绘制echart饼图

问题背景

小程序开发过程经常会使用到画图表的场景,本文将介绍微信小程序开发过程如何使用Echarts组件绘制饼图。

问题分析

GitHub地址: https://github.com/ecomfe/echarts-for-weixin
该项目是 Apache ECharts (incubating) 的微信小程序版本,开发者可以通过熟悉的 ECharts 配置方式,快速开发图表,满足各种可视化需求。

问题解决

(1)下载GitHub仓项目,拷贝 ec-canvas 目录到我们自己项目中。
在这里插入图片描述

(2)需要引入的页面json文件引入该组件。

  "usingComponents": {
    "custom-tab-bar": "/custom-tab-bar",
    "ec-canvas": "../../component/ec-canvas/ec-canvas"
  },
  "navigationBarTitleText": "分类列表",
  "navigationStyle": "custom",
  "enablePullDownRefresh": true
}

(3)需要使用的页面wxml文件中,使用该组件

    <ec-canvas force-use-old-canvas="true" type="2d" style="width: 300rpx; height: 300rpx;" id="mychart-pie" ec="{{ ec }}">
    </ec-canvas>

(4)页面对应js文件中,import ec-canvas组件并进行初始化和数据赋值。

import * as echarts from '../../component/ec-canvas/echarts';
const util = require('../../utils/util')

const app = getApp();

// pages/healdata/healthydata.ts
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ...
    ec: {
      lazyLoad: true
    },
    tagList: [{
      value: 55,
    }, {
      value: 20,
    }, {
      value: 10,
    }, {
      value: 20,
    }, {
      value: 38,
    }]
    ,
    ...
  },

  onLoad: function () {
    ...
    this.echartsComponnet = this.selectComponent('#mychart-pie'); 
  },



   //初始化图表
   init_echarts() {
    if (this.echartsComponnet) {
      console.log('init_echarts this.echartsComponnet')
      this.echartsComponnet.init((canvas, width, height, dpr) => {
        // 初始化图表
        let chart = echarts.init(canvas, null, {
          width: width,
          height: height,
          devicePixelRatio: dpr
        });
        this.setOption(chart);
        // 注意这里一定要返回 chart 实例,否则会影响事件处理等
        return chart;
      });
    }
  },
  setOption: function (Chart) {
    Chart.clear(); // 清除
    Chart.setOption(this.getOption()); //获取新数据
  },
  getOption: function () {
    var that = this;
    var option = {
      // title: {
      //   text: '存量客户分布',
      //   x: 'center',
      //   textStyle: {
      //     fontSize: 14,
      //     fontWeight: 'normal',
      //   },
      // },
      tooltip: {
        show: true,
        formatter: "{a} <br/>{b} : {c} ({d}%)"
      },
      calculable: true,
      // legend: {
      //   bottom: 0,
      //   data: that.data.tagList,
      //   icon: 'circle',
      //   itemWidth: 10,
      //   itemHeight: 10
      // },
      color: ["#108ee9", "#ff9900", "#10cfc8"],
      series: [{
        type: 'pie',
        tooltip: {
          trigger: 'item',
          triggerOn: 'click',
          formatter: "{c} ({d}%)"
        },
        center: ['50%', '45%'],
        radius: ['50%', '90%'],
        data: that.data.tagList,
        itemStyle: {
          normal: {
            label: {
              show: true,
              position: 'inner',
              formatter: "{d}%"
            },
            labelLine: {
              show: false
            }
          },
        },
      }]
    };
    return option;
  },
})

运行结果如下所示:
在这里插入图片描述

问题总结

本文初步介绍了微信小程序开发过程如何使用Echarts组件绘制饼图,有兴趣的同学可以进一步深入研究。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序开发中,可以使用 ECharts 这个图表库来绘制饼图。下面是一个简单的示例代码,用于在小程序绘制饼图: 1. 在 wxml 文件中引入 ECharts 的 canvas 组件: ```html <view class="chart-container"> <ec-canvas id="pieChart" canvas-id="pieCanvas"></ec-canvas> </view> ``` 2. 在 js 文件中引入 ECharts 并初始化饼图: ```javascript import * as echarts from '../../ec-canvas/echarts'; Page({ onLoad: function (options) { this.initPieChart(); }, initPieChart: function () { this.pieChart = this.selectComponent('#pieChart'); this.pieChart.init((canvas, width, height, dpr) => { const chart = echarts.init(canvas, null, { width: width, height: height, devicePixelRatio: dpr // 像素密度 }); canvas.setChart(chart); const option = { tooltip: { trigger: 'item', formatter: '{a} <br/>{b}: {c} ({d}%)' }, legend: { orient: 'vertical', left: 10, data: ['类别1', '类别2', '类别3', '类别4', '类别5'] }, series: [ { name: '访问来源', type: 'pie', radius: ['50%', '70%'], avoidLabelOverlap: false, label: { show: false, position: 'center' }, emphasis: { label: { show: true, fontSize: '30', fontWeight: 'bold' } }, labelLine: { show: false }, data: [ { value: 335, name: '类别1' }, { value: 310, name: '类别2' }, { value: 234, name: '类别3' }, { value: 135, name: '类别4' }, { value: 1548, name: '类别5' } ] } ] }; chart.setOption(option); return chart; }); } }); ``` 这是一个简单的示例,你可以根据自己的需求来修改数据和样式。注意,在使用 ECharts 绘制图表时,需要在小程序的 `app.json` 文件中添加 `ec-canvas` 组件的路径。 希望对你有所帮助!如有更多问题,请继续提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值