jfreechart customize piechart to show absolute values and percentages

前端 未结 1 409
被撕碎了的回忆
被撕碎了的回忆 2020-11-30 13:43

How can this compilable minimal code snippet example, which uses JFreeChart as plotting API, adapted in order to show both absoulte values AND percentages?

相关标签:
1条回答
  • 2020-11-30 14:24

    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);
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题