Overcharts addEvent override function

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

/**
         * 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 the default function, and at the same time I do not want to change highcharts.js.

I want to assign it through an external function, is there a 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);
}
+3
source share
6 answers

Unfortunately, I do not know why addEvent packaging does not work (although it should!), But you can overwrite or wrap importEvents for a point, do the same 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/

+1
source
0

:

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

    orig(el, event, fn);
  };
})(Highcharts.addEvent);

Highcharts wrap,

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

    addEvent(el, event, fn);
  });
})(Highcharts);
0

addEvent 1126 , , , , addEvent , , , plotOptions/series/point/events.

, :

(function (H) {
    Highcharts.Chart.prototype.callbacks.push(function (chart) {
        H.addEvent(chart.container, 'click', function() { alert('click!') });
    });
}(Highcharts));

, :

(function (H) {
    Highcharts.Chart.prototype.callbacks.push(function (chart) {    
        H.addEvent(chart.series[0].data, 'click', "(function() { alert (this.y);})");
    });
}(Highcharts));

:

(function (H) {
    H.wrap(H, "addEvent", function (addEvent, el, event, fn) {
        if (typeof fn == "string") {
            try {
                var evalfn = eval(fn);
                addEvent(el, event, evalfn);
            } catch (e) {
                if (e instanceof SyntaxError) {
                    console.log(e.message);
                }
            }
        } else {
            addEvent(el, event, fn);
        }
    });
})(Highcharts);

, , , , .

: http://jsfiddle.net/8wRAs/

0

Highcharts.addEvent Highcharts.wrap() , Highcharts addEvent. Highcharts.addEvent .


, , addEvent window.HighchartsAdapter. JavaScript , window.HighchartsAdapter , .

addEvent , Highcharts , JS: (demo)

<script src="http://code.highcharts.com/highcharts.js"></script>
<script>
    // Highcharts detects if it has been loaded more than once
    // and throws an error to alert developers
    // We work around this by unsetting the Highcharts object
    window.Highcharts = null;

    window.HighchartsAdapter.addEvent = function (el, event, fn) {
        // add your custom code here

        $(el).bind(event, fn);
    };
</script>
<script src="http://code.highcharts.com/highcharts.js"></script>

HTTP- JS , 304, .


, , . Highcharts ( API) , / . , , .

0

Pay attention to these lines of code in the highcharts.js file:

// check for a custom HighchartsAdapter defined prior to this file
var globalAdapter = win.HighchartsAdapter,
    adapter = globalAdapter || {};

// Initialize the adapter
if (globalAdapter) {
    globalAdapter.init.call(globalAdapter, pathAnim);
}


// Utility functions. If the HighchartsAdapter is not defined, adapter is an empty object
// and all the utility functions will be null. In that case they are populated by the
// default adapters below.

As addEventindicated in window.HighchartsAdapter, you need to create a custom HighchartsAdapterone to override it. An example of a custom highcharts adapter on github .

0
source

All Articles