I'm trying to add an event listener to a highcharts object after it's been created. I can add one during declaration. When I try to use the chrome console to sort out where to attach a listener post declaration I can't.
Using the once option We can pass an object as an argument to the addEventListener method and specify that the event is only handled once. This is achieved by passing the property once to the object. If we set once to true, the event will only be fired once.
With the addEventListener() method you can specify the propagation type by using the "useCapture" parameter: addEventListener(event, function, useCapture); The default value is false, which will use the bubbling propagation, when the value is set to true, the event uses the capturing propagation.
To determine that chart is fully loaded you can use load event, because load event is invoked when all elements are drown (API reference https://api.highcharts.com/highcharts/chart.events.load). However when you are using series animation this runs in a timeout, then load event is invoked before animation will finish.
If you want to add an event listener after the chart is already created, the documentation provides some insight regarding extending highcharts:
Events can be added to the instance through framework event binding. If your framework is jQuery, you can for example run
$(chart).bind('load', someFunction);
There is even some mention in the "Hooking up to Chart.init" section (on the same page) as to how you might bind an event after the chart has rendered, but it is a more invasive solution. I've adapted the code sample below with some modifications:
// General, global event listener
var globalCallback = function (chart) {
// Specific event listener
Highcharts.addEvent(chart.container, 'click', function (e) {
e = chart.pointer.normalize();
console.log('Clicked chart at ' + e.chartX + ', ' + e.chartY );
});
// Specific event listener
Highcharts.addEvent(chart.xAxis[0], 'afterSetExtremes', function (e) {
console.log('Set extremes to ' + e.min + ', ' + e.max);
});
}
// Add `globalCallback` to the list of highcharts callbacks
Highcharts.Chart.prototype.callbacks.push(globalCallback);
You can also take a look at their example JSFiddle for this scenario.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With