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

最佳答案

您需要使用MouseEventjava2DToValue() 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);

10-08 12:01