Does somebody know why those 2 snippets doesn’t work the same way at all?
In the first snippet I declare a function within a JSON:
plotOptions : {
series : {
events: {
legendItemClick: function () {
var visibility = this.visible ? 'visible' : 'hidden';
if (this.visible) {
this.chart.container.style.height = '100px';
this.chart.reflow();
}
else {
this.chart.container.style.height = '400px';
this.chart.reflow();
}
//this.chart.collapse();
}
}
}
},
See the snippet here: first snippet on jsfiddle
N.B.: it works better on chrome if you save the files' content on your hard drive.
If you click on the legend you will see it will collapse the graph (the function is called). In the second snippet I declare the function later on:
plotOptions : {
series : {
events: {
}
}
},
…
myChart.options.plotOptions.series.events.legendItemClick = function () {
var visibility = this.visible ? 'visible' : 'hidden';
if (this.visible) {
this.chart.container.style.height = '100px';
this.chart.reflow();
}
else {
this.chart.container.style.height = '400px';
this.chart.reflow();
}
//this.chart.collapse();
};
See the snippet here:second snippet on jsfiddle
In that case if you click on the legend the function will NOT been called and the graph won’t collapse.
I guess the answer is somewhere in highcharts.js, but where? I don’t see what to do when looking at the Highcharts.Legend API documentation: https://api.highcharts.com/class-reference/Highcharts.Legend#toc0
Is there a way to modify a call to legendItemClick after the creation of the chart object?
p.s.: by the way, I see exactly the same thing when looking at myChart.options.plotOptions.series.events.legendItemClick in the debugger for the 2 snippets: