Featured post
jquery - How to associate an event with only one object? jqplot -
im using jqplot draw charts. great tool, lacks simple clickhandler option each chart.
its plugins highlighter, draggable , cursor register interest in capturing click/mouse-events jqplot canvas adding jqplot.eventlistenerhooks (eventlistenerhooks.push(['jqplotclick', callback]); example. or 'jqplotmousedown' or such available.
after making plot usual $.jqplot(target, data, options);
$.jqplot.eventlistenerhooks.push(['jqplotclick', myfunc]);
and sure enough myfunc gets called wherever click on plot, event
, neighbour
, datapos
, gridpos
. neighbour interesting, contains data point if there click on it. data need make popup close gridpos aditional information on datapoint.
but problem if have 2 charts on same page , want register different callbacks each jqplot. now, when register second myfunc2, clicks on second plot go through myfunc aswell!
do need make changes jqplot? directions, whatsoever?
thanks
this isn't well-documented, can find discussion here.
basically, need create event handlers chart before calling jqplot create actual chart. that, need 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 simple event handler checks see if there's neighboring data point near clicked. see working example here: http://jsfiddle.net/andrewwhitaker/ptyfg/
update: if want listen events on plot, 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 replace #chartdiv
whatever id of chart want listen events is. example has been updated.
update 2: looks can use regular bind
event plot events:
$("#chartdiv").bind("jqplotclick", function(ev, gridpos, datapos, neighbor) { if (neighbor) { alert('x:' + neighbor.data[0] + ' y:' + neighbor.data[1]); } });
example using method, 2 charts:
http://jsfiddle.net/andrewwhitaker/ptyfg/5/
hope helps!
- Get link
- X
- Other Apps
Comments
Post a Comment