JFreeChart饼状图展示百分比

JFreeChart饼状图显示百分比
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%")表示小数点后保留两位

1 楼 hellodajun 2012-02-27  
JFreeChart饼状图展示百分比
不错,挺好用的
2 楼 hellodajun 2012-02-27  
多谢,多谢