jfreechart-1.0.1中设置饼图默认的Label是传入的数值,但往往我们想显示的是Label的百分比,如何设置值为“n%”呢?在旧包里是可以直接设置的,而jfreechart-1.0.1包把设置放到了StandardPieSectionLabelGenerator.java的构造方法里面了。
具体设置如下:
JFreeChart chart = ChartFactory.createPieChart3D(title, // 图表标题
dataset, true, // 是否显示图例
false, false);
PiePlot pieplot = (PiePlot) chart.getPlot(); //通过JFreeChart 对象获得
pieplot.setNoDataMessage("无数据可供显示!"); // 没有数据的时候显示的内容
pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
("{0}: ({2})"), NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
其中new StandardPieSectionLabelGenerator(
("{0}: ({1},{2})"), NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%"))
的("{0}: ({1},{2})")是生成的格式,{0}表示section名,{1}表示section的值,{2}表示百分比。可以自定义。而new DecimalFormat("0.00%")表示小数点后保留两位
JFreeChart饼图百分比设置
本文介绍如何在JFreeChart-1.0.1中将饼图的Label设置为百分比形式(n%)。通过调整StandardPieSectionLabelGenerator的具体参数实现自定义展示效果。
1030

被折叠的 条评论
为什么被折叠?



