java – jfreechart自定义饼图以显示绝对值和百分比
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – jfreechart自定义饼图以显示绝对值和百分比,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2326字,纯文字阅读大概需要4分钟。
内容图文
![java – jfreechart自定义饼图以显示绝对值和百分比](/upload/InfoBanner/zyjiaocheng/712/51d57d06f065498986aa1797095d8016.jpg)
这个可编辑的最小代码片段示例如何使用JFreeChart作为绘图API,以便显示absoulte值和百分比?
我无法从互联网上的任何代码段或JFreechart手册本身中提取此信息.代码段生成一个仅显示百分比的饼图.在我的情况下绝对值也很重要,所以我需要在百分比下显示它们.
这是代码:(注意它没有导入)
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}作为绝对节值.
有关详情,请参见StandardPieSectionLabelGenerator.
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);
}
}
内容总结
以上是互联网集市为您收集整理的java – jfreechart自定义饼图以显示绝对值和百分比全部内容,希望文章能够帮你解决java – jfreechart自定义饼图以显示绝对值和百分比所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。