微信小程序主流的图表工具
基于 Canvas,体积小
注意:wx-charts 插件无法在组件内使用。
1. 直接拷贝编译好的文件 dist/wxcharts-min.js 到项目中(js下载地址)
import wxCharts from '../../utils/wxcharts-min.js'
2. wxml 中定义
canvas-id 与 new wxCharts({canvasId: ''}) 中的 canvasId 值一致。
1. 饼图 pie
let chart = new wxCharts({
animation: true,
canvasId: 'pieCanvas',
type: 'pie',
series: [{
name: '成交量1',
data: 15,
}, {
name: '成交量2',
data: 35,
}, {
name: '成交量3',
data: 78,
}],
width: 300,
height: 300,
})
2. 圆环图 ring
let chart = new wxCharts({
animation: true,
canvasId: 'ringCanvas',
type: 'ring',
extra: {
ringWidth: 25,
pie: {
offsetAngle: -45
}
},
title: {
name: '70%',
color: '#7cb5ec',
fontSize: 25
},
subtitle: {
name: '收益率',
color: '#666666',
fontSize: 15
},
series: [{
name: '成交量1',
data: 15,
stroke: false
}, {
name: '成交量2',
data: 35,
stroke: false
}, {
name: '成交量3',
data: 78,
stroke: false
}, {
name: '成交量4',
data: 63,
stroke: false
}],
disablePieStroke: true,
width: 300,
height: 300,
dataLabel: false,
legend: false,
padding: 0
})
3. 线图 line
let chart = new wxCharts({
canvasId: 'lineCanvas',
type: 'line',
categories: ['2016-1', '2016-2', '2016-3', '2016-4', '2016-5', '2016-6', '2016-7', '2016-8', '2016-9', '2016-10'],
animation: true,
series: [{
name: '成交量1',
data: [12,10,18,16,19,13,11,10,15,14],
format: function (val, name) {
return val.toFixed(2) + '万';
}
}, {
name: '成交量2',
data: [2, 0, 0, 3, null, 4, 0, 0, 2, 0],
format: function (val, name) {
return val.toFixed(2) + '万';
}
}],
xAxis: {
disableGrid: true
},
yAxis: {
title: '成交金额 (万元)',
format: function (val) {
return val.toFixed(2);
},
min: 0
},
width: 300,
height: 300,
dataLabel: false,
dataPointShape: true,
extra: {
lineStyle: 'curve'
}
})
4. 柱状图 column
let chart = new wxCharts({
canvasId: 'columnCanvas',
type: 'column',
animation: true,
categories: ['2012', '2013', '2014', '2015'],
series: [{
name: '成交量',
data: [15, 20, 45, 37],
format: function (val, name) {
return val.toFixed(2) + '万';
}
}],
yAxis: {
format: function (val) {
return val + '万';
},
title: 'Column',
min: 0
},
xAxis: {
disableGrid: false,
type: 'calibration'
},
extra: {
column: {
width: 15
}
},
width: 300,
height: 200,
});
5. 区域图 area
let chart = new wxCharts({
canvasId: 'areaCanvas',
type: 'area',
categories: ['1', '2', '3', '4', '5', '6'],
animation: true,
series: [{
name: '成交量1',
data: [32, 45, 0, 56, 33, 34],
format: function (val) {
return val.toFixed(2) + '万';
}
}, {
name: '成交量2',
data: [15, 20, 45, 37, 4, 80],
format: function (val) {
return val.toFixed(2) + '万';
},
}],
yAxis: {
title: '成交金额 (万元)',
format: function (val) {
return val.toFixed(2);
},
min: 0,
fontColor: '#8085e9',
gridColor: '#8085e9',
titleFontColor: '#f7a35c'
},
xAxis: {
fontColor: '#7cb5ec',
gridColor: '#7cb5ec'
},
extra: {
legendTextColor: '#cb2431'
},
width: 300,
height: 250
});
6. 雷达图 radar
let chart = new wxCharts({
canvasId: 'radarCanvas',
type: 'radar',
categories: ['1', '2', '3', '4', '5', '6'],
series: [{
name: '成交量1',
data: [90, 110, 125, 95, 87, 122]
}],
width: 300,
height: 200,
extra: {
radar: {
max: 150
}
}
});
数据列表series每项参数说明
ring 图表相关配置
radar 图表相关配置
pie、ring 图表相关配置
touchHandler (e) {
let index = this.data.chart.getCurrentDataIndex(e);
console.log('index', index)
}
chart.addEventListener('renderComplete', () => {
// your code here
});
https://github.com/xiaolin3303/wx-charts