1、下載 GitHub 上的 ecomfe/echarts-for-weixin 項目,Echarts微信版。
地址:https://github.com/ecomfe/echarts-for-weixin
2、將 ec-canvas 文件放置到小程序項目中
3、使用echarts(餅圖為例)
// json文件 "usingComponents": { "ec-canvas": "../../ec-canvas/ec-canvas" }
// wxml文件 <view class="containers"> <ec-canvas id="mychart-dom-bar" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas> </view>
// 外層用view包裹方便設置echarts元素的寬高
// js import * as echarts from '../../ec-canvas/echarts'; const app = getApp() function initChart(canvas, width, height, dpr) { const chart = echarts.init(canvas, null, { width: width, height: height, devicePixelRatio: dpr // 像素 }); canvas.setChart(chart); let option = { title: {}, tooltip: { trigger: 'item', formatter: '{b} : {c}' }, legend: { left: 'center', bottom: 20, textStyle: { fontSize: 8, color: '#ffffff' }, data: ['已聯系客戶', '未聯系客戶'] }, color: ['#FD7031','#00d6a5'], series: [{ name: '客戶', type: 'pie', radius: '60%', center: ["45%", "38%"], label: { position: 'inner', show: true, formatter: '{c}' }, data: [{ value: app.globalData.pieData? app.globalData.pieData[0] : 0, name: '已聯系客戶', formatter: '{c}', tooltip: { trigger: 'item', formatter: '{b} : {c}' } }, { value: app.globalData.pieData? app.globalData.pieData[1] : 0, name: '未聯系客戶', formatter: '{c}', tooltip: { trigger: 'item', formatter: '{b} : {c}' } } ], emphasis: { itemStyle: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } }, }] }; chart.setOption(option); return chart; } Page({ data: { ec: { onInit: initChart } } })
4、效果圖