getting coordinates with XYSeries

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Zanton
Posts: 9
Joined: Thu Jul 13, 2006 1:14 pm

getting coordinates with XYSeries

Post by Zanton » Thu Jul 13, 2006 1:22 pm

I have a XYSeries chart and I want to get the coordinates of a point when I'm clicking on it. I implemented the MouseClicked() method and I use event.getTrigger().getX() and event.getTrigger().getY() to get the Java2D screen coordinates. I know I have yet to use the translateJava2DtoValue() (or maybe Java2DtoValue() ? ) method to get the corresponding coordinates on the axis, but in the post I read ( can't post url because I'm new ) it's said that it's an axis classes methods, and I don't know how to have access to this method. I probably need to get the axis, but I don't know how to.

Thanks for helping.

Edit : ok I found it I think (using plot and getDomainAxis). By the way, the method Java2DtoValue() needs (double java2DValue, Rectangle2D area, RectangleEdge edge) and I don't know to what correspond Rectangle2D and RectangleEdge. Any clue ?

gribas
Posts: 32
Joined: Thu Jan 26, 2006 5:34 pm

Post by gribas » Thu Jul 13, 2006 2:04 pm

Hello,

try this:

Code: Select all

public void chartMouseClicked(ChartMouseEvent event) {
	if (null == event) return;

	// screen coordinates
	int screenX = event.getTrigger().getX();
	int screenY = event.getTrigger().getY();

	// data coordinates
	ChartEntity entity =  event.getEntity();
	if (entity instanceof XYItemEntity) {
	    XYItemEntity itemEntity = (XYItemEntity) entity;
	    XYDataset xyDataset = itemEntity.getDataset();
	    int seriesIndex = itemEntity.getSeriesIndex();	    
	    int itemIndex = itemEntity.getItem();
	    
	    double xDataValue = xyDataset.getXValue(seriesIndex,itemIndex);
	    double yDataValue = xyDataset.getYValue(seriesIndex,itemIndex);
	    
	    System.out.printf("The screen coordinates [%d,%d] contain the data point [%f,%f].", 
                screenX,screenY,xDataValue,yDataValue);
	}
	
}


Zanton
Posts: 9
Joined: Thu Jul 13, 2006 1:14 pm

Post by Zanton » Thu Jul 13, 2006 2:09 pm

Yes it works !
Thank you !

Edit : to what corresponds the Item obtained with getItem ? I thought it was the index of the (x,y) in the series but it seems not.

Locked