小程序中如何使用echarts图表

1.在https://github.com/ecomfe/echarts-for-weixin下载插件
小程序中如何使用echarts图表

2.将下载的包解压后,把里面的ec-canvas文件夹放进自己的小程序里(与pages同级)
小程序中如何使用echarts图表
小程序中如何使用echarts图表
3.在对应的json中引用ec-canvas
小程序中如何使用echarts图表
“usingComponents”: {
“ec-canvas”:"…/…/ec-canvas/ec-canvas"
}

4.在对应的js中引用echarts,例如我引用到了自己新建的home.js中
小程序中如何使用echarts图表
5.//引用echarts
import * as echarts from ‘…/…/ec-canvas/echarts’;

6.在app.wxss中写公共样式(不懂可以看图中的注解)
小程序中如何使用echarts图表
7.在相对应的wxml文件中调用组件(根据自己的需求进行调用,我调用的是折线图)
小程序中如何使用echarts图表

8.在对应的wxss中写基本样式(根据自己的需求适当调整宽、高比例)

小程序中如何使用echarts图表
9.在对应的js中写相应的代码
小程序中如何使用echarts图表
小程序中如何使用echarts图表
小程序中如何使用echarts图表
小程序中如何使用echarts图表
小程序中如何使用echarts图表
//上图代码如下:
function initChart(canvas, width, height, dpr) {
const chart = echarts.init(canvas, null, {
width: width,
height: height,
devicePixelRatio: dpr // new
});
canvas.setChart(chart);

var option = {
title: {
text: ‘测试下面legend的红色区域不应被裁剪’,
left: ‘center’
},
color: ["#37A2DA", “#67E0E3”, “#9FE6B8”],
legend: {
data: [‘A’, ‘B’, ‘C’],
top: 50,
left: ‘center’,
backgroundColor: ‘red’,
z: 100
},
grid: {
containLabel: true
},
tooltip: {
show: true,
trigger: ‘axis’
},
xAxis: {
type: ‘category’,
boundaryGap: false,
data: [‘周一’, ‘周二’, ‘周三’, ‘周四’, ‘周五’, ‘周六’, ‘周日’],
// show: false
},
yAxis: {
x: ‘center’,
type: ‘value’,
splitLine: {
lineStyle: {
type: ‘dashed’
}
}
// show: false
},
series: [{
name: ‘A’,
type: ‘line’,
smooth: true,
data: [18, 36, 65, 30, 78, 40, 33]
}, {
name: ‘B’,
type: ‘line’,
smooth: true,
data: [12, 50, 51, 35, 70, 30, 20]
}, {
name: ‘C’,
type: ‘line’,
smooth: true,
data: [10, 30, 31, 50, 40, 20, 10]
}]
};

chart.setOption(option);
return chart;
}

10.效果图如下:
小程序中如何使用echarts图表

图表所需的数据以及设置图表类型,可参考

https://echarts.apache.org/examples/zh/index.html