XYPlot:注释相对于Y轴放置不正确

时间:2016-02-08 09:28:16

标签: java jfreechart

我有以下代码在鼠标点击事件的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的位置是不正确的。)

enter image description here

1 个答案:

答案 0 :(得分:2)

当您致电java2dToValue()计算y值时,您为轴位置指定RectangleEdge.BOTTOM,但它应为RectangleEdge.LEFT