如何将事件侦听器添加到Highcharts对象* AFTER *我已创建它 [英] How do I add an event listener to a Highcharts object *AFTER* I've created it

查看:199
本文介绍了如何将事件侦听器添加到Highcharts对象* AFTER *我已创建它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个事件监听器被创建后添加到一个highcharts对象中。我可以在声明中添加一个。当我尝试使用chrome控制台排除附加侦听器发布声明的位置时,我不能。

解决方案

如果你想要在图表创建完成后添加事件侦听器,文档提供了一些有关可以通过框架事件绑定将事件添加到实例中。如果你的框架是 jQuery ,你可以运行 $(chart).bind('load',someFunction);




部分(在同一页上)甚至提到了您可能会在图表渲染后绑定事件,但这是一种更具侵入性的解决方案。我已经修改了下面的代码示例,并进行了一些修改:

pre code $ General $全局事件侦听器
var globalCallback =函数(图表){

//特定事件监听器
Highcharts.addEvent(chart.container,'click',function(e){
e = chart.pointer.normalize );
console.log('点击图表'+ e.chartX +','+ e.chartY);
});

//特定事件监听器
Highcharts.addEvent()函数(e){
console.log('Set extremes to' + e.min +','+ e.max);
});
}

//将`globalCallback`添加到highcharts回调列表
Highcharts.Chart.prototype.callbacks.push(globalCallback);

您也可以查看他们的JSFiddle例子


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.

解决方案

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.

这篇关于如何将事件侦听器添加到Highcharts对象* AFTER *我已创建它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆