I was looking for a solution to get a plotBand by id to hide it with a series. Based on the snippet of r00tGER I made a function to call:
/**
* Get the plotBand or plotLine with given id out of all plotBands and plotLines of one Axis
* @param id The id of the plotBand or plotLine you are looking for
* @param axis The axis where the plotBand/plotLine is bound
*/
function getPlotBandOrLineById(id, axis) {
// loop through all plotBands/plotLines and check their id
for (var i = 0; i < axis.plotLinesAndBands.length; i++) {
if (axis.plotLinesAndBands[i].id === id) {
return axis.plotLinesAndBands[i];
}
}
}
With that function you can hide and show a plotBand (or plotLine) with their series:
$(function () {
$('#chart_container').highcharts('StockChart', {
...
series: [
{
name: 'Carbs',
...
events: {
show: function () {
var plotBand = getPlotBandOrLineById('goal_carbs', this.chart.yAxis[0]);
plotBand.hidden = false;
plotBand.svgElem.show();
},
hide: function () {
var plotBand = getPlotBandOrLineById('goal_carbs', this.chart.yAxis[0]);
plotBand.hidden = true;
plotBand.svgElem.hide();
},
},
},
],
...
});
});