Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

highcharts override addEvent function

in highcharts javascript file line number 1126 there is a function addEvent

/**
         * Add an event listener
         * @param {Object} el A HTML element or custom object
         * @param {String} event The event type
         * @param {Function} fn The event handler
         */
        addEvent: function (el, event, fn) {
            $(el).bind(event, fn);
        },

I want to call my custom function instead of default function, and at the same time i don't want to change highcharts.js.

I want to assign it through external function, is there any way I can achieve this??

my new code will be

addEvent: function (el, event, fn) {
    if(typeof(fn)=="string"){
        try {
            eval(fn); 
        } catch (e) {
        if (e instanceof SyntaxError) {
            console.log(e.message);
        }
        }

    }
    $(el).bind(event, fn);
}
like image 243
Anup Singh Avatar asked May 14 '26 06:05

Anup Singh


1 Answers

Unfortuantely I don't know why wrapping addEvent doesn't work (while should!), but you can overwrite or wrap importEvents for point, to do similar thing (but only for points):

(function (H) {
    H.wrap(H.Point.prototype, 'importEvents', function () {
        if (!this.hasImportedEvents) {
            var point = this,
                options = H.merge(point.series.options.point, point.options),
                events = options.events,
                eventType;

            point.events = events;

            for (eventType in events) {
                if (typeof events[eventType] == "string") {
                    try {
                        events[eventType] = eval(events[eventType]);
                    } catch (e) {
                        if (e instanceof SyntaxError) {
                            console.log(e.message);
                        }
                    }
                }
                H.addEvent(point, eventType, events[eventType]);
                this.hasImportedEvents = true;
            }
        }
    });
}(Highcharts));

Demo: http://jsfiddle.net/cXS5u/

like image 140
Paweł Fus Avatar answered May 15 '26 19:05

Paweł Fus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!