jfreechart自定义饼图以显示绝对值和百分比 [英] jfreechart customize piechart to show absolute values and percentages

查看:449
本文介绍了jfreechart自定义饼图以显示绝对值和百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个可编辑的最小代码片段示例如何使用JFreeChart作为绘图API,以便显示 absoulte值和百分比
我无法从互联网上的任何代码段或JFreechart手册中提取此信息。代码段生成一个仅显示百分比的饼图。在我的情况下绝对值也很重要,所以我需要在百分比下显示它们。

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.

这是代码:(注意它缺少导入)

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");
    }
    }
}


推荐答案

使用 MessageFormat 符号 {1} 作为绝对部分值。

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

请参阅 StandardPieSectionLabelGenerator 了解详情。

See StandardPieSectionLabelGenerator for details.

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);

    }
}

这篇关于jfreechart自定义饼图以显示绝对值和百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆