Java 如何在JFreeChart中将ChartPanel坐标转换为XYPlot坐标

Java 如何在JFreeChart中将ChartPanel坐标转换为XYPlot坐标,java,jfreechart,chartpanel,Java,Jfreechart,Chartpanel,我在ChartPanel上实现了所有Listner。在鼠标拖动事件中,我将注释上的坐标更改为重绘,以显示拖动操作。工作正常,但坐标有点混乱。实际上,我得到的坐标是在ChartPanel的上下文中,它随后将转换为XYPlots的轴值,因此注释是在奇怪的位置绘制的。您需要使用java2DToValue()将MouseEventXY坐标转换为图表坐标 图表面板鼠标操作代码 Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlot

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

您需要使用
java2DToValue()
MouseEvent
XY坐标转换为图表坐标

图表面板
鼠标操作代码

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);
注释代码

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);