- 下载echarts依赖包并拷贝到指定位置 components
- 导入echarts自定义组件
{
"usingComponents": {
"ec-canvas": "/components/ec-canvas/ec-canvas"
}
}
- 模板中wxml添加标签组件
- 必须提供一个容器view 并且指定高度
- ec-canvas 标签需要设置样式:宽度和高度
<view class="container">
<ec-canvas id="mychart-dom-bar" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>
</view>
ec-canvas {
width: 750rpx;
height: 640rpx;
}
- 图表的js逻辑
index.js
// echarts图表的用法
import * as echarts from '../../components/ec-canvas/echarts.js'
function initChart(canvas, width, height, dpr) {
const chart = echarts.init(canvas, null, {
width: width,
height: height,
devicePixelRatio: dpr // 像素
});
canvas.setChart(chart);
var option = {
color: ['#3398DB'],
tooltip: {
trigger: 'axis',
// 坐标轴指示器,坐标轴触发有效
axisPointer: {
// 默认为直线,可选为:'line' | 'shadow'
type: 'shadow'
}
},
grid: {
left: '3%',
right: '4%',
bottom: '3%',
containLabel: true
},
xAxis: [
{
type: 'category',
data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
axisTick: {
alignWithLabel: true
}
}
],
yAxis: [
{
type: 'value'
}
],
series: [
{
name: '直接访问',
type: 'bar',
barWidth: '60%',
data: [10, 52, 200, 334, 390, 330, 220]
}
]
}
chart.setOption(option);
return chart;
}
Page({
data: {
ec: {
onInit: initChart
}
}
});