Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFreeChart饼图中的百分比精度问题_Java_Jfreechart - Fatal编程技术网

Java JFreeChart饼图中的百分比精度问题

Java JFreeChart饼图中的百分比精度问题,java,jfreechart,Java,Jfreechart,我在用图书馆的饼图来满足我的需要我想在饼图中显示总值的百分比。所以,我在谷歌上搜索。我找到了“垃圾神”的答案 现在根据他给出的答案,我创建了一个类,如下所示: import java.awt.Color; import java.awt.Dimension; import java.text.DecimalFormat; import javax.swing.JFrame; import org.jfree.chart.ChartFactory; import org.jfree.chart

我在用图书馆的饼图来满足我的需要
我想在饼图中显示总值的百分比。所以,我在谷歌上搜索。我找到了“垃圾神”的答案

现在根据他给出的答案,我创建了一个类,如下所示:

import java.awt.Color;
import java.awt.Dimension;
import java.text.DecimalFormat;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.PieSectionLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

public class TestPieChart {
    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, 45045); //49
        dataset.setValue(KEY2, 53955); //151

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

    }
}
它给出的输出如下:

其中百分比为55%46%,总计为101%
(因为它是两个值的上限,如54.5和55)

但同时,如果我将KEY1的值设为49,将KEY2设为151,那么它给出的准确结果如下:
(在这种情况下,两者的总和为100%)


所以,我的问题是:为什么JFreeChart对不同的值执行不同的操作
是否有任何解决方案可以解决此问题(总百分比不会超过100)?

标签生成器的抽象父级执行的百分比计算(参见)和格式化程序的默认舍入(讨论)都是正确的。如果需要,您可以在构造以下对象时通过指定不同的
percentFormat
来更改显示百分比的精度:

请注意
45.5%+54.5%=100%

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