微信小程序?qū)崿F(xiàn)折線圖的示例代碼
插件地址:https://github.com/xiaolin3303/wx-charts/blob/master/dist/wxcharts.js
微信小程序折線圖效果:

首先需要引入一個折線圖的插件:

XXX.xml 的代碼:
<canvas style="width: 400px; height: 500px;" canvas-id="yueEle" binderror="canvasIdErrorCallback" ></canvas>
注意 canvas-id="yueEle" 要與 JS中的 canvasId: 'yueEle' 一樣。才能顯示圖。

然后在XXX.js中配置:
1.在頂部寫入:

2.寫一個獨立的方法(或者直接寫在onload里面):
我這里寫的方法名是 getMothElectro ,我后面試在onload里面調(diào)用了的。

getMothElectro:function(){
var windowWidth = 320;
try {
var res = wx.getSystemInfoSync();
windowWidth = res.windowWidth;
} catch (e) {
console.error('getSystemInfoSync failed!');
}
yuelineChart = new wxCharts({ //當月用電折線圖配置
canvasId: 'yueEle',
type: 'line',
categories: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31'], //categories X軸
animation: true,
// background: '#f5f5f5',
series: [{
name: '總用電量',
//data: yuesimulationData.data,
data: [1, 6, 9, 1, 0, 2, 9, 2, 8, 6, 0, 9, 8, 0, 3, 7, 3, 9, 3, 8, 9, 5, 4, 1, 5, 8, 2, 4, 9, 8, 7],
format: function (val, name) {
return val.toFixed(2) + 'kWh';
}
}, {
name: '電池供電量',
data: [0, 6, 2, 2, 7, 6, 2, 5, 8, 1, 8, 4, 0, 9, 7, 2, 5, 2, 8, 2, 5, 2, 9, 4, 4, 9, 8, 5, 5, 5, 6],
format: function (val, name) {
return val.toFixed(2) + 'kWh';
}
},
{
name: '光伏供電量',
data: [6, 4, 9, 7, 1, 4, 5, 1, 1, 8, 8, 6, 6, 2, 2, 2, 0, 5, 5, 8, 8, 8, 8, 9, 0, 4, 6, 9, 2, 1, 6],
format: function (val, name) {
return val.toFixed(2) + 'kWh';
}
},
{
name: '市電供電量',
data: [0, 4, 3, 3, 1, 7, 7, 7, 9, 9, 3, 3, 0, 0, 5, 6, 0, 4, 1, 2, 0, 1, 3, 9, 2, 5, 1, 8, 3, 4, 2],
format: function (val, name) {
return val.toFixed(2) + 'kWh';
}
}],
xAxis: {
disableGrid: true
},
yAxis: {
title: '當月用電(kWh)',
format: function (val) {
return val.toFixed(2);
},
max: 20,
min: 0
},
width: windowWidth,
height: 200,
dataLabel: false,
dataPointShape: true,
extra: {
lineStyle: 'curve'
}
});
}
3.然后在onload里面在調(diào)用一次:

4.在增加一個觸摸顯示功能:


yueTouchHandler: function (e) { //當月用電觸摸顯示
console.log(daylineChart.getCurrentDataIndex(e));
yuelineChart.showToolTip(e, { //showToolTip圖表中展示數(shù)據(jù)詳細內(nèi)容
background: '#7cb5ec',
format: function (item, category) {
return category + '日 ' + item.name + ':' + item.data
}
});
},
這樣就OK了!
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
JavaScript實現(xiàn)獲取某個元素相鄰兄弟節(jié)點的prev與next方法
這篇文章主要介紹了JavaScript實現(xiàn)獲取某個元素相鄰兄弟節(jié)點的prev與next方法,涉及JavaScript基于函數(shù)的判定及調(diào)用previousSibling與nextSibling的相關(guān)技巧,需要的朋友可以參考下2016-01-01

