JFreeChart 饼图中的百分比精度问题
Percentage Accuracy issue in JFreeChart Pie-chart
我正在使用由@trashgod 回答的 JFreeChart library for my need.
And i want to display percentage with total values in pie-chart. So, i searched over google. And i found this solution 饼图。
现在根据他给出的答案,我创建了 class,如下所示:
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)?
标签生成器的抽象父级执行的百分比计算,已见 here, and the formatter's default rounding, discussed here, are both correct. If desired, you can change the precision of the displayed percentage by specifying a different percentFormat
when constructing the StandardPieSectionLabelGenerator
:
PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator(
"{0}: {1} ({2})", new DecimalFormat("0"), new DecimalFormat("0.0%"));
注意 45.5% + 54.5% = 100%
.
我正在使用由@trashgod 回答的 JFreeChart library for my need.
And i want to display percentage with total values in pie-chart. So, i searched over google. And i found this solution 饼图。
现在根据他给出的答案,我创建了 class,如下所示:
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)?
标签生成器的抽象父级执行的百分比计算,已见 here, and the formatter's default rounding, discussed here, are both correct. If desired, you can change the precision of the displayed percentage by specifying a different percentFormat
when constructing the StandardPieSectionLabelGenerator
:
PieSectionLabelGenerator gen = new StandardPieSectionLabelGenerator(
"{0}: {1} ({2})", new DecimalFormat("0"), new DecimalFormat("0.0%"));
注意 45.5% + 54.5% = 100%
.