Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

java - jfreechart customize piechart to show absolute values and percentages

How can this compilable minimal code snippet example, which uses JFreeChart as plotting API, adapted in order to show both absoulte values AND percentages? I couldn't extract this information neither from any code snippet on the internet nor from the JFreechart manual itself. The code snippet produces a pie chart showing only percentages. The absolute values in my case also matter, so i need to display them right under the percentages.

Here is the code: (Note it lacks the imports)

public class MyMinimalPieChartExample {
    public static void main(String[] args) {
    DefaultPieDataset dataset = new DefaultPieDataset();
    dataset.setValue("some data 1",99);
    dataset.setValue("some data 2", 77);

    //third adaption
    JFreeChart someChart = ChartFactory.createPieChart(
            "some chart header", dataset,
            true, true, false);
    PiePlot illegalLegalRestPiePlot4 = (PiePlot) someChart.getPlot();
    illegalLegalRestPiePlot4.setSectionPaint("some data 1", new Color(0, 255, 0));
    illegalLegalRestPiePlot4.setSectionPaint("some data 2",
            new Color(255, 0, 0));
    PiePlot plot4 = (PiePlot) someChart.getPlot();
    plot4.setExplodePercent("some data 1", 0.4);
    plot4.setSimpleLabels(true);

    PieSectionLabelGenerator generator = new StandardPieSectionLabelGenerator(
            "{0} = {2}", new DecimalFormat("0"), new DecimalFormat("0.00%"));
    plot4.setLabelGenerator(generator);

    try {
        ChartUtilities.saveChartAsJPEG(new File("C:/myMinimalPieChartExample.jpeg"),
                someChart, 1200, 1000);
    } catch (Exception e) {
        System.err.println("couldn't write chart");
    }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the MessageFormat symbol {1} for the absolute section value.

See StandardPieSectionLabelGenerator for details.

pie plot

public class MyMinimalPieChartExample {

    private static final String KEY1 = "Datum 1";
    public static final String KEY2 = "Datum 2";

    public static void main(String[] args) {
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue(KEY1, 99);
        dataset.setValue(KEY2, 77);

        JFreeChart someChart = ChartFactory.createPieChart(
            "Header", dataset, true, true, false);
        PiePlot plot = (PiePlot) someChart.getPlot();
        plot.setSectionPaint(KEY1, Color.green);
        plot.setSectionPaint(KEY2, Color.red);
        plot.setExplodePercent(KEY1, 0.10);
        plot.setSimpleLabels(true);

        PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator(
            "{0}: {1} ({2})", new DecimalFormat("0"), new DecimalFormat("0%"));
        plot.setLabelGenerator(gen);

        JFrame f = new JFrame("Test");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new ChartPanel(someChart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(400, 300);
            }
        });
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...