我有一种情况,我正在绘制一个堆积的柱状图,当前停留在一个点,在同一天有多个数据点的情况下,我需要按特定顺序对系列进行排序。问题是,每天的系列顺序可能会有所不同-我在下面有代码和示例-
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中,对于每个类别,我希望最低的数据点显示在顶部。因此,对于苹果,我希望Mary在顶部,因为它的值为2。类似,对于Oranges和Pears。
这可以实现吗?任何帮助表示赞赏。
答案 0 :(得分:2)
可以使用这种方法完成:
1)创建一个对列进行排序的函数:
SVGElement.attr
方法(point.graphic是SVGElement实例)设置新计算的y位置。对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参考: