我有一种情况我在哪里画堆积柱形图,目前停留在一个点,我需要订购按照特定的顺序串联在同一天多个数据点的情况下。问题是,该系列顺序可以是每天不同的 - 我有下面的代码和例子 -
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'Stacked column chart'
},
xAxis: {
categories: ['Apples', 'Oranges', 'Pears', 'Grapes', 'Bananas']
},
yAxis: {
min: 0,
title: {
text: 'Total fruit consumption'
},
stackLabels: {
enabled: true,
style: {
fontWeight: 'bold',
color: (Highcharts.theme && Highcharts.theme.textColor) ||
'gray'
}
}
},
legend: {
align: 'right',
x: -30,
verticalAlign: 'top',
y: 25,
floating: true,
backgroundColor: (Highcharts.theme && Highcharts.theme.background2)
|| 'white',
borderColor: '#CCC',
borderWidth: 1,
shadow: false
},
tooltip: {
headerFormat: '<b>{point.x}</b><br/>',
pointFormat: '{series.name}: {point.y}<br/>Total:
{point.stackTotal}'
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true,
color: (Highcharts.theme &&
Highcharts.theme.dataLabelsColor) || 'white'
}
}
},
series: [{
name: 'John',
data: [5, 3, 4, 7, 2]
}, {
name: 'Mary',
data: [2, 2, 3, 2, 1]
}, {
name: 'Kevin',
data: [3, 4, 4, 2, 5]
}]
});
的jsfiddle:http://jsfiddle.net/arj_ary/4kz7rdpn/2/
要求:在上面的jsfiddle,每个类别我想最低的数据点在顶部出现。所以对于苹果我想玛丽在顶部,因为它具有2同样,对于桔子和梨的值。
这能实现呢?任何帮助表示赞赏。
它可以使用这种方法来实现:
1)创建一个函数,其排序列:
SVGElement.attr
设定的新计算的y位置(point.graphic是SVGElement实例)。执行相同的point.dataLabel
和设置一个新的位置,以point.tooltipPos
阵列(否则工具提示会被错误地放置)。function sortColumns() {
var chart = this,
pointsByCat = {},
bottomY,
shapeArgs;
chart.series.forEach(function(serie) {
serie.points.forEach(function(point, index) {
if (pointsByCat[point.category] === undefined) {
pointsByCat[point.category] = [];
}
pointsByCat[point.category].push(point);
});
});
Highcharts.objectEach(pointsByCat, function(points, key) {
shapeArgs = points[points.length - 1].shapeArgs;
bottomY = shapeArgs.y + shapeArgs.height;
points.sort(function(a, b) {
return b.y - a.y;
});
points.forEach(function(point) {
if (point.series.visible) {
point.graphic.attr({
y: bottomY - point.shapeArgs.height
});
point.dataLabel.attr({
y: bottomY - point.shapeArgs.height / 2 - point.dataLabel.height / 2
});
point.tooltipPos[1] = bottomY - point.shapeArgs.height;
bottomY = bottomY - point.shapeArgs.height;
}
});
});
}
2)上述功能设定为回调chart.events.load
和chart.events.redraw
事件:
chart: {
type: 'column',
events: {
load: sortColumns,
redraw: sortColumns
}
}
演示:
API参考: