request: ChartPanel.getEntityForPoint(x, y)

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Daniel van Enckevort

request: ChartPanel.getEntityForPoint(x, y)

Post by Daniel van Enckevort » Thu Oct 10, 2002 4:33 pm

Hi, I want to get the ChartEntity that a user has depressed a mouse button over. I cannot use ChartMouseListener.chartMouseClicked, because it only works for mouse clicks, not press or release.

Rather than adding further actions for ChartMouseEvents (i.e. press, release, etc) it would probably be more flexible to have a method on ChartPanel to get the ChartEntity for a given view co-ordinate.

I have extended ChartPanel with the following method and was wondering if you would consider adding some similar functionality to the main trunk?


public ChartEntity getEntityForPoint(int viewX, int viewY) {
Insets insets = getInsets();
int x = (int)((viewX-insets.left)/scaleX);
int y = (int)((viewY-insets.top)/scaleY);

//I guess someone could feasibly call this before the chart is drawn.
//allow the NullPointer or guard it? Dunno.
EntityCollection entities = this.info.getEntityCollection();
return entities != null ? entities.getEntity(x, y) : null;
}


Thanks alot
Dan

Dave Gilbert

Re: request: ChartPanel.getEntityForPoint(x, y)

Post by Dave Gilbert » Thu Oct 10, 2002 4:58 pm

Hi Daniel,

Thanks for the suggestion. I've added your method to the ChartPanel class for 0.9.4.

Regards,

DG.

barry

Re: request: ChartPanel.getEntityForPoint(x, y)

Post by barry » Fri Oct 11, 2002 9:39 pm

Dan,

I have a requirenment to draw lines on demand on a chart using the mouse. The user selects a starting point (left mouse press) and then rubbers to another spot on the chart and then releases the mouse. The line will stay inplace. (see www.bigcharts.com - JAVACHARTS)

1) Can this be done?
2) If so, any suggestions



BT

Daniel van Enckevort

Re: request: ChartPanel.getEntityForPoint(x, y)

Post by Daniel van Enckevort » Mon Oct 14, 2002 6:56 pm

Hmm looks like the code that translates a screen coordinate to a Java2D coordinate is useful in it own right.

At the moment the only way to do it is to extend ChartPanel, since ChartPanel.scaleX is protected.

Dave, maybe another 2 methods on ChartPanel (and refactor them into the code at the top of this thread)?

BTW, I have no idea whether the choice of Point and Point2D below is right.
Maybe everything should be Point2D (even though screen coords always are integer)?

public Point translateJava2DToScreen(Point2D java2DPoint){
Insets insets = getInsets();
int x = (int) (java2DPoint.getX() * scaleX + insets.left);
int y = (int) (java2DPoint.getY() * scaleY + insets.top);
return new Point(x,y);
}

public Point2D translateScreenToJava2D(Point2D screenPoint){
Insets insets = getInsets();
double x = (screenPoint.getX()-insets.left)/scaleX;
double y = (screenPoint.getY()-insets.top)/scaleY;
return new Point.Double(x,y);
}

Cheers
Dan

Daniel van Enckevort

Re: request: ChartPanel.getEntityForPoint(x, y)

Post by Daniel van Enckevort » Mon Oct 14, 2002 6:56 pm

barry:
if you simply want to draw a line on the screen, look at the java tutorials, I guess there may be some example paint program or something. Just override a paint method to draw a line after it has done all the normal stuff.

However, I assume you actually want to create a graph that displays your mouse lines?
To do this, use the above code to get the Java2D point for a mouse press. Once you have this you can get both axes as instances of ValueAxis (assuming you have an XYPlot) and use ValueAxis.translateJava2DtoValue()

Now you have an acutal datapoint you can add to an XYDataset and make a line plot using data you have defined with the mouse.

You may find this does not give you enough perfomance for "rubber-banding" if this is the case, you could do the rubber band display by overriding paint. As soon as the user releases the mouse button then you can convert the rubber band start and end locations to actual data values and add them to the dataset as described above.

Good luck, hope that gives you an idea of what to do

Dan

btahlor

Re: request: ChartPanel.getEntityForPoint(x, y)

Post by btahlor » Wed Oct 16, 2002 1:24 am

good advise, I will let you know how it comes out

Barry

Locked