简体   繁体   English

如何在JFree折线图中添加注释

[英]How to add annotation in jfree line chart

Im using jfree charts for my application. 我在我的应用程序中使用jfree图表。 I need a chart with stroke point values appear above the x axis label. 我需要一个图表,其笔触点值显示在x轴标签上方。

Expected 预期

在此处输入图片说明

And i need to remove the tick mark in between the values(54% and 2008). 而且我需要删除值之间的刻度线(54%和2008)。 I have tried the below code to get the annotation, 我已经尝试过以下代码来获取注释,

final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(23, "Line", "2008");
        dataset.addValue(145, "Line", "2009");
        dataset.addValue(245, "Line", "2010");
        dataset.addValue(322, "Line", "2011");
        final JFreeChart chart = ChartFactory.createLineChart(
                "", // chart title
                "", // domain axis label
                "", // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                false, // include legend
                false, // tooltips
                false // urls
                );

        chart.setBackgroundPaint(Color.WHITE);
        chart.setBorderVisible(true);
        chart.setBorderPaint(Color.decode("#EEEEEE"));
        chart.setPadding(new RectangleInsets(10, 10, 5, 5));

        final CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.WHITE);
        plot.setRangeZeroBaselinePaint(Color.RED);
        plot.setOutlineVisible(false);
        plot.setRangeGridlinePaint(Color.white);
        plot.setDomainGridlinePaint(Color.BLUE);        

        final CategoryAxis categoryAxis = (CategoryAxis) plot.getDomainAxis();
        categoryAxis.setAxisLineVisible(false);
        categoryAxis.setTickMarksVisible(false);
        categoryAxis.setTickLabelFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 8));        

        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setVisible(false);
        rangeAxis.setLabelPaint(Color.BLUE);
        rangeAxis.setRangeWithMargins(21, 600);

        DecimalFormat format = new DecimalFormat("###,###");

        StandardCategoryItemLabelGenerator labelGenerator = new StandardCategoryItemLabelGenerator("{2}", format);
        final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        renderer.setBaseShapesVisible(true);
        renderer.setBaseShapesFilled(true);
        renderer.setSeriesStroke(0, new BasicStroke(1.5f));
        renderer.setSeriesItemLabelsVisible(0, true);
        renderer.setBaseItemLabelGenerator(labelGenerator);
        renderer.setBaseItemLabelFont(new java.awt.Font("Arial", java.awt.Font.PLAIN, 9));
        Shape circle = new Ellipse2D.Double(-2, -2, 4, 4);
        renderer.setSeriesShape(0, circle);        
        plot.getRenderer().setSeriesPaint(0, Color.decode("#0066CC"));        
        //       CategoryAnnotation categoryAnnotation=new CategoryLineAnnotation(Male1, 248, Male2, 216, null, null);
//        XYLineAnnotation annotation=new XYLineAnnotation(8, 0, 8, 24, new BasicStroke(2.0f), Color.blue);
//        plot.addAnnotation( (CategoryAnnotation) annotation);

        try {
            ChartUtilities.saveChartAsPNG(new File("E:\\jfreeLinechart.png"), chart, 290, 95);
            System.out.println("=====chart=====");
        } catch (Exception e) {
            e.printStackTrace();
        }

from the above code im getting the following chart 从上面的代码即时获取以下图表

Actual 实际

在此处输入图片说明

Please help me to get the expected image in jfree line chart. 请帮助我在jfree折线图中获得期望的图像。

Using the method setBasePositiveItemLabelPosition you can set the base positive item label position. 使用方法setBasePositiveItemLabelPosition可以设置基本的正项目标签位置。 Try this: 尝试这个:

renderer.setBasePositiveItemLabelPosition(
                   new ItemLabelPosition(ItemLabelAnchor.OUTSIDE8, TextAnchor.CENTER));

Using OUTSIDE , INSIDE , or CENTER you can specify where the label will be placed respecting the item. 使用OUTSIDEINSIDECENTER可以指定相对于项目放置标签的位置。

I have found the solution. 我找到了解决方案。 For the expected Chart you have to create the dummy series values. 对于预期的图表,您必须创建虚拟序列值。 And use this code. 并使用此代码。

public String createLineChart() throws IOException {
        System.out.println("Line chart");
        final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        dataset.addValue(14, "Line", "2008");
        dataset.addValue(18, "Line", "2009");
        dataset.addValue(22, "Line", "2010");

        dataset.addValue(13, "Line2", "2008");
        dataset.addValue(13, "Line2", "2009");
        dataset.addValue(13, "Line2", "2010");
        final JFreeChart chart = ChartFactory.createLineChart(
                "", // chart title
                "", // domain axis label
                "", // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                false, // include legend
                false, // tooltips
                false // urls
        );

        chart.setBackgroundPaint(Color.WHITE);
        final CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.WHITE);
        plot.setRangeZeroBaselinePaint(Color.RED);
        plot.setOutlineVisible(false);
        plot.setRangeGridlinePaint(Color.white);
        plot.setDomainGridlinePaint(Color.BLUE);

        final CategoryAxis categoryAxis = (CategoryAxis) plot.getDomainAxis();
        categoryAxis.setAxisLineVisible(false);
        categoryAxis.setTickMarksVisible(false);
        categoryAxis.setMaximumCategoryLabelLines(2);

        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setVisible(false);
        rangeAxis.setLabelPaint(Color.BLUE);
        rangeAxis.setRange(13, 23);
        rangeAxis.setTickUnit(new NumberTickUnit(20));

        final LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();
        renderer.setBaseShapesVisible(true);
        renderer.setBaseShapesFilled(true);
        renderer.setSeriesStroke(0, new BasicStroke(2.0f));
        renderer.setSeriesItemLabelsVisible(1, Boolean.TRUE);
        Shape circle = new Ellipse2D.Double(-3, -3, 6, 6);
        renderer.setSeriesShape(0, circle);
        renderer.setSeriesShape(1, null);
        renderer.setBaseItemLabelGenerator(new CustomLabelGenerator());
//        MyGenerator generator=new MyGenerator();
//        renderer.setSeriesItemLabelGenerator(1, generator);
        renderer.setBaseItemLabelsVisible(true);
        plot.getRenderer().setSeriesPaint(0, Color.decode("#0066CC"));
        plot.getRenderer().setSeriesPaint(1, Color.WHITE);

//        ValueAxis range = plot.getRangeAxis();
//        range.setUpperMargin(0.20);
        try {
            ChartUtilities.saveChartAsPNG(new File("/media/root/668ea9a3-d26c-4896-a2f0-756dfb532756/jfreeLinechart.png"), chart, 400, 200);
            System.out.println("=====chart=====");
        } catch (IOException e) {
            System.out.println("Line chart :" + e);
        }
        xyLineChart();
        return "success";
    }

    static class CustomLabelGenerator extends AbstractCategoryItemLabelGenerator implements CategoryItemLabelGenerator {

            public CustomLabelGenerator() {
                super("", NumberFormat.getCurrencyInstance());
            }

            public String generateLabel(CategoryDataset dataset, int series, int category) {

                String result = null;
                if (series == 1) {
                    series = 0;
                    Number value = dataset.getValue(series, category);
                    result = value.toString();
                    System.out.println("===========result===============" + series + "====category======" + category);
                }
                return result;
            }

            public String generateRowLabel(CategoryDataset arg0, int arg1) {
                return null;
            }

            public String generateColumnLabel(CategoryDataset arg0, int arg1) {
                return null;
            }
        }

You will get the appropriate chart... 您将获得适当的图表...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM