最基本实现
目录结构,需要ec-canvas文件夹,然后在chart.js中引入
chart.js:
- js中的Page data中的内容返回给wxml,让其显示
- initChart中let option 以上是固定的内容
- let option填的是具体的数据,x轴、y轴还有数值(可以option提取出来写一个方法)
- chart.setOption,将数据融入到chart中最后再返回chart
import * as echarts from '../../ec-canvas/echarts';
function initChart(canvas, width, height, dpr) {
const chart = echarts.init(canvas, null, {
width: width,
height: height,
devicePixelRatio: dpr // new
});
canvas.setChart(chart);
let option = {
xAxis: {
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
},
yAxis: {
type: 'value'
},
series: [{
data: [150, 230, 224, 218, 135, 147, 260],
type: 'bar'
}]
};
chart.setOption(option);
return chart;
}
Page({
data: {
ec: {
onInit: initChart
}
}
});
<view class="container">
<view class="my-chart">
<ec-canvas id="mychart-dom-recite" canvas-id="mychart-recite" ec="{{ec}}"></ec-canvas>
</view>
</view>