[英]Highcharts: How to link legend to categories rather than to bar series?
Using Highcharts to display a bar chart, is there a built-in way to link legend items to categories rather than to series? 使用Highcharts显示条形图,是否有内置的方式将图例项目链接到类别而不是序列? As such I would see a chart with a bunch of bars, and when I click a legend item it would show or hide just the bar(s) associated with a single category.
这样,我将看到带有一堆条形图的图表,当我单击图例项时,它将仅显示或隐藏与单个类别关联的条形图。
Thanks 谢谢
There is not a built in way. 没有内置的方式。
You can fake it in a number of ways, however. 但是,您可以通过多种方式伪造它。
One way is to use a separate series for each category, set grouping
to false, dummy the x values to hover around the actual category value. 一种方法是对每个类别使用单独的系列,将
grouping
设置为false,将x值虚拟化以将其悬停在实际类别值上。
plotOptions: {
series: {
grouping: false,
pointRange: 0.2
}
},
series: [{
name: 'Group A',
data: [[-0.25,5],[0,7],[0.25,6]]
}]
Example: 例:
In the end, I found no out-of-the-box way to do this. 最后,我发现没有现成的方法可以做到这一点。 But I found a solution that works, as follows:
但是我找到了一个可行的解决方案,如下所示:
correlate each bar with a single series (see fiddle referenced above) 将每个小节与单个序列相关联(请参见上面引用的小提琴)
in legendItemClick: 在legendItemClick中:
Here is a very simple example of the above: 这是上面的一个非常简单的示例:
legendItemClick = function (e) {
var seriesClicked = e.currentTarget;
var chart = seriesClicked.chart;
var axis = chart.xAxis[0]
if (seriesClicked.visible) {
seriesClicked.hide();
chart.series[2].data[0].update( { x: 1 });
axis.setCategories(['Group A', 'Group C'], false)
axis.setExtremes(0, 1, true)
} else {
seriesClicked.show();
chart.series[2].data[0].update( { x: 2 });
axis.setCategories(['Group A', 'Group B', 'Group C'], false)
axis.setExtremes(0, 2, true)
}
return false;
}
And a fiddle: http://jsfiddle.net/dkent600/e6357a22/17/ 和一个小提琴: http : //jsfiddle.net/dkent600/e6357a22/17/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.