简体   繁体   English

Highcharts:如何将图例链接到类别而不是酒吧系列?

[英]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: 但是我找到了一个可行的解决方案,如下所示:

  1. correlate each bar with a single series (see fiddle referenced above) 将每个小节与单个序列相关联(请参见上面引用的小提琴)

  2. in legendItemClick: 在legendItemClick中:

    • manually show or hide the series associated with the clicked-on legend item 手动显示或隐藏与单击的图例项关联的系列
    • given visibility of each category, recompute the indices of each visible category and update each visible bar series data[0].x with the new index of the category with which it is associated (use point.update()) 给定每个类别的可见性,请重新计算每个可见类别的索引,并使用与其相关联的类别的新索引来更新每个可见条形系列数据[0] .x(使用point.update())
    • call xAxis.setCategories with a list of the categories you want to be currently visible 调用xAxis.setCategories,其中包含您希望当前可见的类别列表
    • call xAxis.setExtremes with the new extremes for the category indices 使用类别索引的新极端调用xAxis.setExtremes

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM