如何将一个事件仅与一个对象关联? jqplot

发布于 2024-10-08 15:49:36 字数 648 浏览 7 评论 0原文

我使用 jqplot 绘制一些图表。它是一个很棒的工具,但它缺少每个图表的简单点击处理程序选项。

它的插件(如荧光笔、可拖动和光标)通过将自身添加到 jqplot.eventListenerHooks(eventListenerHooks.push(['jqplotClick',callback]); 例如。或 'jqplotMouseDown' 来注册从 jqplot 画布捕获点击/鼠标事件的兴趣。 的也可用。

或这样 然后我这样做

$.jqplot.eventListenerHooks.push(['jqplotClick', myFunc]);

果然,无论我点击绘图,myFunc 都会被调用,并带有 eventneighbourdataposgridpos。邻居是最有趣的,如果点击它,它包含我的数据点。这是我需要在网格位置附近创建一个弹出窗口并包含有关数据点的附加信息的数据。

但问题是,如果我在同一页面上有两个图表,并且想要为每个 jqplot 注册不同的回调。现在,当我注册第二个 myFunc2 时,第二个图上的所有点击也都会通过 myFunc!

我需要对 jqplot 进行更改吗?有什么指示吗?

谢谢

Im using jqplot to draw some charts. It is a great tool, but it lacks a simple clickhandler option for each chart.

Its plugins like highlighter, draggable and cursor register their interest in capturing click/mouse-events from the jqplot canvas by adding themselves to jqplot.eventListenerHooks (eventListenerHooks.push(['jqplotClick', callback]); for example. or 'jqplotMouseDown' or such are also available.

After making my plot with the usual $.jqplot(target, data, options);
then I do this

$.jqplot.eventListenerHooks.push(['jqplotClick', myFunc]);

and sure enough myFunc gets called wherever I click on the plot, with event, neighbour, datapos and gridpos. Neighbour is the most interesting, it contains my data point if there was a click on it. This is the data I need to make a popup close to the gridpos with aditional information on the datapoint.

But the problem is if I have two charts on the same page and want to register different callbacks for each jqplot. As it is now, when I register the second myFunc2, all the clicks on the second plot go through the myFunc aswell!

Do I need to make changes to jqplot? Any directions, whatsoever?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猥琐帝 2024-10-15 15:49:36

这没有详细记录,但您可以找到有关它的讨论 这里

基本上,您需要在调用 jqplot 创建实际图表之前为图表创建事件处理程序。为此,您需要执行以下操作:

var handler = function(ev, gridpos, datapos, neighbor, plot) {
    if (neighbor) {
        alert('x:' + neighbor.data[0] + ' y:' + neighbor.data[1]);
    }
};

$.jqplot.eventListenerHooks.push(['jqplotClick', handler]);

这只是一个简单的事件处理程序,用于检查您单击的位置附近是否存在相邻数据点。请参阅此处的工作示例: http://jsfiddle.net/andrewwhitaker/PtyFG/

更新:如果您只想监听特定绘图上的事件,您可以执行以下操作:

var handler = function(ev, gridpos, datapos, neighbor, plot) {
    if (plot.targetId === "#chartdiv") {
        if (neighbor) {
            alert('x:' + neighbor.data[0] + ' y:' + neighbor.data[1]);
        }
    }
    else if (plot.targetId === "#chart2div") {
        ....
    }
    // etc...
};

#chartdiv 替换为您想要监听的图表的 ID的事件。该示例已更新。

更新 2: 看起来您可以将常规 bind 事件与绘图事件结合使用:

$("#chartdiv").bind("jqplotClick", function(ev, gridpos, datapos, neighbor) {
    if (neighbor) {
        alert('x:' + neighbor.data[0] + ' y:' + neighbor.data[1]);
    }
});

使用此方法的示例,带有 2 个图表:

http://jsfiddle.net/andrewwhitaker/PtyFG/5/

希望有帮助!

This isn't well-documented, but you can find a discussion about it here.

Basically, you need to create event handlers for your chart before calling jqplot to create the actual chart. To do that, you need to do something like this:

var handler = function(ev, gridpos, datapos, neighbor, plot) {
    if (neighbor) {
        alert('x:' + neighbor.data[0] + ' y:' + neighbor.data[1]);
    }
};

$.jqplot.eventListenerHooks.push(['jqplotClick', handler]);

That's just a simple event handler that checks to see if there's a neighboring data point near where you clicked. See a working example here: http://jsfiddle.net/andrewwhitaker/PtyFG/

Update: If you want to only listen to events on a certain plot, you could do something like this:

var handler = function(ev, gridpos, datapos, neighbor, plot) {
    if (plot.targetId === "#chartdiv") {
        if (neighbor) {
            alert('x:' + neighbor.data[0] + ' y:' + neighbor.data[1]);
        }
    }
    else if (plot.targetId === "#chart2div") {
        ....
    }
    // etc...
};

Where you would replace #chartdiv with whatever the Id of the chart you want to listen to events for is. The example has been updated.

Update 2: Looks like you can use a regular bind event with the plot events:

$("#chartdiv").bind("jqplotClick", function(ev, gridpos, datapos, neighbor) {
    if (neighbor) {
        alert('x:' + neighbor.data[0] + ' y:' + neighbor.data[1]);
    }
});

Example using this method, with 2 charts:

http://jsfiddle.net/andrewwhitaker/PtyFG/5/

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文