Highcharts - Bar Chart & Column Chart

时间:2021-03-01 00:17:03

1. 条形图(Bar Chart)需要的数据格式类型如下:

["Luke Skywalker", "Darth Vader", "Yoda", "Princess Leia"]
[2, 4, 1, 1]

2. Bar Chart代码示例:

$(function () {
$('#container').highcharts({
chart: {
type: 'bar'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
plotOptions: {
series: {
allowPointSelect: true
}
},
yAxis:{
min: 0,
title: {
text: 'Sales'
},
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
}); // the button action
$('#button').click(function () {
var selectedPoints = $('#container').highcharts().getSelectedPoints();
alert('You selected ' + selectedPoints.length + ' points');
});
});

效果图:

Highcharts - Bar Chart & Column Chart

3. 柱状图(Column Chart)与条形图(Bar Chart)的区别是Chart的type属性不同,一个是column,一个是bar,但使用的数据以及格式设置都一样;将条目2中的Chart的type属性改为column,就可以得到如下效果:

Highcharts - Bar Chart & Column Chart

4. 如果Bar Chart需要在同一个category中显示不同状态的结果(StackedBar),数据格式就需要做相应修改:

["Luke Skywalker", "Princess Leia", "Yoda", "Darth Vader"]
[{"Completed", [1, 0, 0, 1]},{"Active", [1, 1, 1, 2]},{"Pipeline", [0, 0, 0, 1]}]

代码如下,重要的是在plotOptions中设置series的stacking属性:

$(function () {
$('#container').highcharts({
chart: {
type: 'bar'
},
xAxis: {
categories: ['One', 'Two', 'Three', 'Four', 'Five']
}, plotOptions: {
series: {
stacking: 'normal'
}
},
series: [
// first stack
{
name: 'A',
data: [29.9, 71.5, 106.4, 129.2, 144.0],
stack: 0
}, {
name: 'B',
data: [30, 176.0, 135.6, 148.5, 216.4],
stack: 0
// second stack
}, {
name: 'C',
data: [106.4, 129.2, 144.0, 29.9, 71.5],
stack: 1
}, {
name: 'D',
data: [148.5, 216.4, 30, 176.0, 135.6],
stack: 1
}
]
});
});

效果如下:

Highcharts - Bar Chart & Column Chart

5. 如果需要在Column Chart中显示StackedColumn,可以对条目4中的代码做简单修改即可,重要的是在plotOptions中设置column的stacking属性:

$(function () {
$('#container').highcharts({
chart: {
type: 'column'
}, xAxis: {
categories: ['One', 'Two', 'Three', 'Four', 'Five']
}, plotOptions: {
column: {
stacking: 'normal'
}
}, series: [
// first stack
{
data: [29.9, 71.5, 106.4, 129.2, 144.0],
stack: 0
}, {
data: [30, 176.0, 135.6, 148.5, 216.4],
stack: 0
// second stack
}, {
data: [106.4, 129.2, 144.0, 29.9, 71.5],
stack: 1
}, {
data: [148.5, 216.4, 30, 176.0, 135.6],
stack: 1
}
]
});
});

效果如图:

Highcharts - Bar Chart & Column Chart

6.