0

我有 ChartPanel,上面实现了所有 Listner。在鼠标拖动事件中,我正在更改注释上的坐标以重绘以显示拖动动作。它工作正常,但坐标有点混乱。实际上,我得到的协调是在 ChartPanel 的上下文中,然后转换为 XYPlots 的 Axes 值,因此注释在奇怪的位置绘制。

4

1 回答 1

1

您需要使用 将MouseEventXY 坐标转换为图表坐标java2DToValue()

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);
于 2014-08-01T11:50:15.857 回答