Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add an event listener to a Highcharts object *AFTER* I've created it

Tags:

highcharts

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.

like image 745
Kai Keliikuli Avatar asked Jun 24 '13 14:06

Kai Keliikuli


People also ask

How do I add an event listener to just once?

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.

How do you add an event listener to an inside function?

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.

How can I tell if Highcharts are loaded?

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.


1 Answers

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.

like image 62
NT3RP Avatar answered Oct 17 '22 23:10

NT3RP