我有以下代码,可在鼠标单击事件的 XYPlot 中生成注释。
_chartPanel.addChartMouseListener(new ChartMouseListener() {
@Override
public void chartMouseClicked(ChartMouseEvent cme)
{
Rectangle2D dataArea = _chartPanel.getScreenDataArea();
XYPlot plot = (XYPlot) cme.getChart().getPlot();
plot.clearAnnotations();
ValueAxis xAxis = plot.getDomainAxis();
ValueAxis yAxis = plot.getRangeAxis();
double x = xAxis.java2DToValue(cme.getTrigger().getX(), dataArea, RectangleEdge.BOTTOM);
double y = yAxis.java2DToValue(cme.getTrigger().getY(), dataArea, RectangleEdge.BOTTOM);
if (!xAxis.getRange().contains(x)) {
x = Double.NaN;
}
DecimalFormat df = new DecimalFormat("#.##");
df.setRoundingMode(RoundingMode.CEILING);
XYPointerAnnotation pt = new XYPointerAnnotation("Lat: " + df.format(y) + "\n Lon: " + df.format(x), x, y, 0.2);
//pt.setBackgroundPaint(Color.red);
pt.setArrowPaint(Color.red);
pt.setFont(_font);
pt.setPaint(Color.red);
plot.addAnnotation(pt);
}
除了 Y 轴之外,一切正常(参见下面的示例)。当我单击系列点(红色箭头)时,注释出现在另一个位置(该位置相对于 X 轴是正确的,但相对于 Y 轴是不正确的)。
最佳答案
当您调用 java2dToValue()
计算 y 值时,您正在指定 RectangleEdge.BOTTOM
作为轴位置,但它应该是 RectangleEdge。左
。
关于java - XY 图 : Annotation is placed incorrectly with respect to Y axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35266151/