在Highcharts中动态附加onload或重绘事件函数

Jon*_*man 6 javascript jquery highcharts

我需要在Highcharts中动态追加onload或redraw事件函数,我知道在配置步骤中进行,如:

$('#container').highcharts({
   chart: {
       events: {
          load: function(event) {
              function1();
              function2();
              function3();
          },
          redraw: function(event) {
              functionA();
              functionB();
              functionC();
          }
       }        
    },
    xAxis: {
    },
    series: [{
        data: [29.9, 71.5]     
    }]
});
Run Code Online (Sandbox Code Playgroud)

但是我需要在配置图表之后这样做(因为我无法访问配置图表步骤,图表通过包装器来到我的代码中)并且我需要做这样的事情:

//The wrapper simulation
^
|
|
/////////////////////////////////////////
//$('#container').highcharts({         //
//    chart: {                         //
//       events: {                     //
//          load: function(event) {    //
//              function1();           //
//          },                         //
//          redraw: function(event) {  //
//              functionA();           //
//          }                          //< "I dont't have access to this code"
//       }                             //  
//    },                               //
//    xAxis: {                         //
//    },                               // 
//    series: [{                       //
//        data: [29.9, 71.5]           //   
//    }]                               // 
//});                                  //
/////////////////////////////////////////

//I only have the container id in my hands, I can get the highchart object:
$('#container').highcharts();

//And I need to do something like this:
appendOnLoadEvent(function2);
appendOnLoadEvent(function3);

appendOnRedrawEvent(functionB);
appendOnRedrawEvent(functionC);
Run Code Online (Sandbox Code Playgroud)

这可能吗?

lio*_*nel 12

最简单的方法是创建第一个示例中描述的事件,并在这些函数中执行数组中的所有必要事件处理程序.

就像是:

  var redrawCallbacks = [];

  $(...).highcharts({
  chart: {
      events: {
          redraw: function(event) {
              for (var i = 0; i < redrawCallbacks.length; ++i) redrawCallbacks[i].call(this, event);
          }
      }
  }});
Run Code Online (Sandbox Code Playgroud)

查看完整示例:http://jsfiddle.net/5S6hF/2/

更新
假设您使用的是默认的jQuery适配器,您可以稍后使用以下内容定义事件:

$(chart).on('redraw', function(){ alert('new event handler'); });
Run Code Online (Sandbox Code Playgroud)

请参阅http://jsfiddle.net/5S6hF/6/上的演示