如何将一个事件仅与一个对象关联? jqplot
我使用 jqplot 绘制一些图表。它是一个很棒的工具,但它缺少每个图表的简单点击处理程序选项。
它的插件(如荧光笔、可拖动和光标)通过将自身添加到 jqplot.eventListenerHooks(eventListenerHooks.push(['jqplotClick',callback]); 例如。或 'jqplotMouseDown' 来注册从 jqplot 画布捕获点击/鼠标事件的兴趣。 的也可用。
或这样 然后我这样做
$.jqplot.eventListenerHooks.push(['jqplotClick', myFunc]);
果然,无论我点击绘图,myFunc 都会被调用,并带有 event
、neighbour
、datapos
和 gridpos
。邻居是最有趣的,如果点击它,它包含我的数据点。这是我需要在网格位置附近创建一个弹出窗口并包含有关数据点的附加信息的数据。
但问题是,如果我在同一页面上有两个图表,并且想要为每个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这没有详细记录,但您可以找到有关它的讨论 这里。
基本上,您需要在调用 jqplot 创建实际图表之前为图表创建事件处理程序。为此,您需要执行以下操作:
这只是一个简单的事件处理程序,用于检查您单击的位置附近是否存在相邻数据点。请参阅此处的工作示例: http://jsfiddle.net/andrewwhitaker/PtyFG/
更新:如果您只想监听特定绘图上的事件,您可以执行以下操作:
将
#chartdiv
替换为您想要监听的图表的 ID的事件。该示例已更新。更新 2: 看起来您可以将常规
bind
事件与绘图事件结合使用:使用此方法的示例,带有 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:
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:
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:Example using this method, with 2 charts:
http://jsfiddle.net/andrewwhitaker/PtyFG/5/
Hope that helps!