Reputation: 13
I have ChartPanel with all Listners implemented on it. On mouse dragging event I am changing the coordinates on my Annotation to redraw as to show dragging action. It is working alright but the coordinates are little messed up. Actually the coordinated I am getting are in context with ChartPanel which are then converting to XYPlots's Axes values therefor the Annotation is drawing at Strange location.
Upvotes: 0
Views: 595
Reputation: 6229
You need to convert the MouseEvent
XY coordinates into chart coordinates using java2DToValue()
.
ChartPanel
mouse handling code
Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
XYPlot xyPlot = chartPanel.getChart().getXYPlot();
// event is the MouseEvent from one of the MouseEvent functions
// store the location and use it later to place the annotation
java.awt.Point clickLocation = new Point(event.getX(), event.getY());
Annotation code
double x = xyPlot.getDomainAxis().java2DToValue(clickLocation.getX(), dataArea, xyPlot.getDomainAxisEdge());
double y = xyPlot.getRangeAxis().java2DToValue(clickLocation.getY(), dataArea, xyPlot.getRangeAxisEdge());
String text = Double.toString(x) + " " + Double.toString(y);
XYTextAnnotation annotation = new XYTextAnnotation(text, x, y);
plot.addAnnotation(annotation);
Upvotes: 1