I have a plotline for each series in a column chart. Currently the series only displays when the chart is visible, but I would like to add the functionality where clicking on a legend item shows that item and its plotline but hides all of the others series/plotlines. Here is a fiddle with the current code: https://jsfiddle.net/nhrmc/5d46bL2f/
Fiddle can be found here: 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
}
]
});
I expect to click "Series 2" in the legend and show it along with the associated plotline and hide Series 1 and its associated plotline. So only one series/plotline combination should ever display.
In legendItemClick
callback function you can hide all of the others series and plotlines:
plotOptions: {
series: {
events: {
legendItemClick: function() {
this.chart.series.forEach(function(s) {
if (s !== this && s.visible) {
s.hide();
}
});
return !this.visible ? true : false
}
}
}
}
Live demo: https://jsfiddle.net/BlackLabel/os86px7v/
API Reference: https://api.highcharts.com/highcharts/plotOptions.series.events.legendItemClick