Highcharts:带有情节的系列。一次只显示一个系列

Highcharts: Series with plotlines. Show only one series at a time

我在柱形图中为每个系列绘制了一条情节线。目前该系列仅在图表可见时显示,但我想添加点击图例项目显示该项目及其绘图线但隐藏所有其他项目的功能 series/plotlines。这是带有当前代码的 fiddle:https://jsfiddle.net/nhrmc/5d46bL2f/

Fiddle 可以在这里找到:https://jsfiddle.net/nhrmc/5d46bL2f/

var chart = Highcharts.chart('container', {
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
},
yAxis: {
plotLines: [{
  value: 50,
  color: 'red',
  width: 2,
  id: 'plot-line-1'
}]
},

series: [{
events: {
  show: function() {
    chart.yAxis[0].addPlotLine({
      value: 7,
      color: 'red',
      width: 2,
      id: 'plot-line-1'
    });
  },
  hide: function() {
    chart.yAxis[0].removePlotLine('plot-line-1')
  }
},
 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],
type: 'column',
}, {
events: {
  show: function() {
    chart.yAxis[0].addPlotLine({
      value: 75,
      color: 'blue',
      width: 2,
      id: 'plot-line-2'
    });
  },
  hide: function() {
    chart.yAxis[0].removePlotLine('plot-line-2')
  }
},
data: [20, 70, 100, 125, 140, 176.0, 132, 145, 21.4, 191, 96, 54],
type: 'column',
visible: false
}
]
});

我希望单击图例中的 "Series 2" 并将其与关联的情节线一起显示,并隐藏系列 1 及其关联的情节线。因此只能显示一个 series/plotline 组合。

legendItemClick回调函数中,您可以隐藏所有其他系列和情节:

plotOptions: {
    series: {
        events: {
            legendItemClick: function() {
                this.chart.series.forEach(function(s) {
                    if (s !== this && s.visible) {
                        s.hide();
                    }
                });

                return !this.visible ? true : false
            }
        }
    }
}

现场演示: https://jsfiddle.net/BlackLabel/os86px7v/

API参考:https://api.highcharts.com/highcharts/plotOptions.series.events.legendItemClick