微信小程序echarts图表

  1. 下载echarts微信版,github地址
  2. 将下载文件中的ec-canvas目录放入微信小程序项目(page同级即可)
  3. 页面.json文件引入
    {
    	"usingComponents": {
    		"ec-canvas": "../../ec-canvas/ec-canvas"
    	}
    }
    
  4. 页面.wxml文件使用
    <ec-canvas class="chart" id="chart1" canvas-id="chart1" ec="{{ec1}}"></ec-canvas>
    <ec-canvas class="chart" id="chart2" canvas-id="chart2" ec="{{ec2}}"></ec-canvas>
    
  5. 页面.js文件引入并使用
import * as echarts from '../../ec-canvas/echarts';
//echarts实例
let chart1 = null
let chart2 = null
Page({
	data: {
	    ec1: {
	      //图表初始化
	      onInit: (canvas, width, height, dpr) => {
	        chart1 = echarts.init(canvas, null, {
	          width: width,
	          height: height,
	          devicePixelRatio: dpr // new
	        });
	        canvas.setChart(chart1)
	        return chart1
	      }
	    },
	    ec2: {
	      onInit: (canvas, width, height, dpr) => {
	        chart2 = echarts.init(canvas, null, {
	          width: width,
	          height: height,
	          devicePixelRatio: dpr // new
	        });
	        canvas.setChart(chart2)
	        return chart2
	      }
	    }
	},
	onLoad(options) {
		//图表初始化为异步操作,直接显示会报错
		setTimeout(() => {
			this.showChart1()
			this.showChart2()
		}, 1000)
	},
	showChart1() {
		// echarts正常配置
		let option = {}
		if (chart1) {
			chart1.setOption(option)
		}
	},
	showChart2() {
		let option = {}
		if (chart2) {
			chart2.setOption(option)
		}
	}
})
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值