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

时间:2014-08-01 11:16:50

标签: java jfreechart chartpanel

我有ChartPanel,其上实现了所有Listners。在鼠标拖动事件中,我正在更改我的注释上的坐标以重绘以显示拖动操作。它工作正常,但坐标很乱。实际上,我得到的协调是在ChartPanel的上下文中,然后转换为XYPlots的Axes值,Annotation正在Strange位置绘制。

1 个答案:

答案 0 :(得分:1)

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