ECharts 和微信小程序官方團隊合作,提供了 ECharts 的微信小程序版本,ECharts圖表技術在國內應該是最好的了,能結合到小程序應用真的是極好。
准備:小程序開發環境,下載ECharts組件,gitHub地址:https://github.com/ecomfe/echarts-for-weixin
操作過程:
1,把ec-canvas拷貝到項目中(可以不是根目錄,但是后續引用的時候,注意更改文件地址)
2,對應頁面json文件引入組件
{ "usingComponents": { "ec-canvas": "../../ec-canvas/ec-canvas" } }
3,對應頁面js文件
import * as echarts from '../../ec-canvas/echarts'; function initChart(canvas, width, height) { const chart = echarts.init(canvas, null, { width: width, height: height }); 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; } Page({ data: { ec: { onInit: initChart } }, onReady() { } });
4,wxml文件
<!--index.wxml--> <view class="container"> <ec-canvas id="mychart-dom-line" canvas-id="mychart-line" ec="{{ ec }}"></ec-canvas> </view>
5,頁面樣式
/**app.wxss**/ .container { position: absolute; top: 0; bottom: 0; left: 0; right: 0; display: flex; flex-direction: column; align-items: center; justify-content: space-between; box-sizing: border-box; }
6,效果圖