我有ChartPanel与所有listener实现在它上面。在鼠标拖动事件中,我正在更改Annotation上的坐标以重新绘制以显示拖动动作。它工作得很好,但是坐标有点混乱。实际上,我得到的协调是在与ChartPanel的上下文中,然后转换为XYPlots的轴值,因此注释是在奇怪的位置绘制的。
您需要使用java2DToValue()
将MouseEvent
的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());
<<p> 注释代码/strong> 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);