如何在jfreechart中添加形状到折线图

时间:2016-06-14 17:56:39

标签: jfreechart

编辑在最后添加工作图像。

我需要在我生成的折线图中添加某种注释。 What I want to do红线是我使用XYLineChart和XYLineAndShapeRenderer成功绘制图表的数据。正方形和文本是我想要添加但无法正常工作的样本。图表的数据可以每秒快速更新一次。

以下是我创建图表的方式:

private JFreeChart createChart(XYDataset dataset) {
    final JFreeChart result = ChartFactory.createXYLineChart(
            TITLE, // chart title
            XAXIS, // domain axis label (x-axis)
            YAXIS, // range axis label (y-axis)
            dataset, // data
            PlotOrientation.VERTICAL, // orientation
            false, // include legend
            true, // tooltips?
            false // URLs?
    );

    customizeChart(result);

    return result;
}

private void customizeChart(final JFreeChart result) {
    // NOW DO SOME OPTIONAL CUSTOMIZATION OF THE CHART...
    // set the background color for the chart...
    result.setBackgroundPaint(null); //makes background transparent.

    // get a reference to the plot for further customisation...
    XYPlot plot = (XYPlot) result.getPlot();
    plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
    plot.setRangePannable(false);

    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer(true, true);
// I've also tried to use a spline renderer
//        XYSplineRenderer renderer = new XYSplineRenderer();
    renderer.setSeriesShapesVisible(0, false);
    renderer.setBaseShapesVisible(true);
    plot.setRenderer(renderer);

    // set the range axis to display integers only...
    NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
    rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    rangeAxis.setTickUnit(new NumberTickUnit(10.0)); //otherwise tick units will randomly change to be 5's sometimes.

    //set the max/min values
    rangeAxis.setUpperBound(MAX);
    rangeAxis.setLowerBound(MIN);
//        rangeAxis.setInverted(true);
    rangeAxis.setInverted(false);

    //get the other axis for work in the next step.
    NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
    domainAxis.setAutoRange(true);

    domainAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

    //do some font stuff
    Font axisFont = new Font("Tahoma", Font.BOLD, 12);
    Font tickFont = new Font("Tahoma", Font.PLAIN, 11);
    Font titleFont = new Font("Tahoma", Font.BOLD, 18);
    result.getTitle().setFont(titleFont);
    rangeAxis.setLabelFont(axisFont);
    rangeAxis.setTickLabelFont(tickFont);
    domainAxis.setLabelFont(axisFont);
    domainAxis.setTickLabelFont(tickFont);
}

以下是我现在想要绘制的内容:

XYPlot plot = chart.getXYPlot();
            XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();

            BasicStroke stroke = new BasicStroke(2.0f);
            Rectangle2D.Double rect = new Rectangle2D.Double(10, 10, 10, 20);
//                renderer.addAnnotation(new XYShapeAnnotation(
//                        rect, stroke, Color.GREEN), Layer.BACKGROUND);
            plot.addAnnotation(new XYShapeAnnotation(
                    rect, stroke, Color.GREEN));

            Ellipse2D.Double circle = new Ellipse2D.Double(8, 8, 20, 20);
            renderer.addAnnotation(new XYShapeAnnotation(
                    circle, stroke, blue), Layer.BACKGROUND);

            plot.drawAnnotations((Graphics2D) chartPanel.getGraphics(), chartPanel.getBounds(), chartPanel.getChartRenderingInfo().getPlotInfo());

// This line does draw, but immediately disappears
            Graphics2D g2 = (Graphics2D) chartPanel.getGraphics();
            Line2D line = new Line2D.Double(0, 100, 500, 350);
            g2.draw(line);

感谢。

使用工作图像进行编辑: Working Sample

1 个答案:

答案 0 :(得分:1)

  

我想我尝试添加的圆和矩形可能不在可见的图表坐标空间中。

可能有助于注意org.jfree.chart.annotations通常要求在模型(数据集)坐标中指定坐标,而不是视图(绘图)坐标。