Determining axis values at a mouse poisition

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
javageek
Posts: 11
Joined: Tue Apr 14, 2009 2:26 pm

Determining axis values at a mouse poisition

Post by javageek » Wed Apr 29, 2009 2:01 am

I am trying to come up with the code to take the location of a mouse pointer and determine the value of the domain and value axis that cooresponds to that location.

I set up a ChartMouseListener on the ChartPanel and can pick up the X and Y coordinates from the MouseEvent trigger in the chartMouseMoved method. But I can't figure out how to get from X,Y to the axis values. I also expect that the pointer can be between points. Which means I have to also pick the closest value to any specific point. I have searched and searched the API and can't find anything that obviously does either function for me. I sure could use a suggestion.

What I am trying to accomplish is to pick up the proper axis data values and display them at the crosshair lines.

sarsipius
Posts: 15
Joined: Fri Apr 10, 2009 1:23 pm

Re: Determining axis values at a mouse poisition

Post by sarsipius » Wed Apr 29, 2009 7:27 am

This is what I do to get the domain/range coordinates:

Code: Select all

double x = plot.getDomainAxis().java2DToValue(cp.translateScreenToJava2D(evt.getTrigger().getPoint()).getX(),cp.getChartRenderingInfo().getPlotInfo().getDataArea(),plot.getDomainAxisEdge());

double y = plot.getRangeAxis().java2DToValue(cp.translateScreenToJava2D(evt.getTrigger().getPoint()).getY(),cp.getChartRenderingInfo().getPlotInfo().getDataArea(),plot.getRangeAxisEdge());
Hope this will help you

javageek
Posts: 11
Joined: Tue Apr 14, 2009 2:26 pm

Re: Determining axis values at a mouse poisition

Post by javageek » Wed Apr 29, 2009 10:29 pm

That helped a lot. I am now able to get the value of the x and y axis. Thank you very much!!!

One more question. Is it possible to also get the index of the domain axis instead of the actual value?

sarsipius
Posts: 15
Joined: Fri Apr 10, 2009 1:23 pm

Re: Determining axis values at a mouse poisition

Post by sarsipius » Thu Apr 30, 2009 7:41 am

I'm not sure if this is what you want ::

Code: Select all

int rowIndex = dataset.getRowIndex(((CategoryItemEntity)evt.getEntity()).getRowKey());
int columnIndex = dataset.getColumnIndex(((CategoryItemEntity)evt.getEntity()).getColumnKey());

Locked