user2853858
user2853858

Reputation: 11

How to get the mouse's position in jqPlot

I would like to write a code that gives the coordinates of the place the user clicks his mouse on a jqPlot graph. Using the option

cursor: { show: true, tooltipLocation:'sw', }

I can SEE the location of the mouse on the graph on the screen. However, I cannot USE it. Do you know how to get it?

Writing:

$('#chartdiv').bind('jqplotClick',function (event) {

 alert( 'The mouse cursor is at ('+event.pageX+','+event.pageY+').');
        } 

);

gives me the coordinates of the mouse on the whole screen, and if I knew how to get the coordinates of the top left point of the axes of the graph, that would have solved the problem. But I don't manage to do it. Is it possible in jqPlot?

Thank you very much for your help, and best wishes, Daphne

Upvotes: 1

Views: 810

Answers (2)

Benjamin Ziepert
Benjamin Ziepert

Reputation: 1754

This did the trick for me:

$('#chartdiv').bind('jqplotClick', function(event, seriesIndex, pointIndex, data) {
    var x = pointIndex.xaxis;
    var y = pointIndex.yaxis;
});

Source: jqPlot - synchronize cursor across multiple charts

Upvotes: 1

AnthonyLeGovic
AnthonyLeGovic

Reputation: 2335

I think you can find it using :

event.originalEvent.layerX 
event.originalEvent.layerY

According to my testing : event.pageX and event.pageY gives you coordinates according to the div including your graphic (i.e. graphic + title + offset...). event.screenX and event.screenY gives you coordinates according to the full screen. event.originalEvent.layerX and event.originalEvent.layerY gives you coordinates according to only graphic div.

See working example here

Upvotes: 1

Related Questions