我有ChartPanel,其上实现了所有Listners。在鼠标拖动事件中,我正在更改我的注释上的坐标以重绘以显示拖动操作。它工作正常,但坐标很乱。实际上,我得到的协调是在ChartPanel的上下文中,然后转换为XYPlots的Axes值,Annotation正在Strange位置绘制。
答案 0 :(得分:1)
您需要使用MouseEvent
将java2DToValue()
XY坐标转换为图表坐标。
ChartPanel
鼠标处理代码
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());
注释代码
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);