If you want to enable the desired functionality, you must change the Series.showInLegend
flag by using the Series.update()
method whenever a onchange
event occurs on the checkbox. Additionally, remember to call the setVisible()
function on that series to toggle its visibility on the plot. Below is an example code snippet demonstrating how this can be achieved:
Start by defining checkboxes as follows:
<input id="series1" type="checkbox" checked>Installation<br/>
<input id="series2" type="checkbox" checked>Manufacturing<br/>
<input id="series3"type="checkbox" checked>Sales and Distribution<br/>
Then iterate through each of these checkboxes and attach an onchange
event handler. Within this function, update the corresponding series' showInLegend
property based on the presence of the legendItem
in the series object. See the code snippet below for more clarity:
var checkboxes = ['series1', 'series2', 'series3']
checkboxes.forEach((elem, i) => {
var checkbox = document.getElementById(elem)
checkbox.onchange = function() {
chart.series[i].update({
showInLegend: chart.series[i].legendItem ? false : true
})
chart.series[i].setVisible()
}
})
For a live demonstration, visit: https://jsfiddle.net/uuwu48cm/