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("无数据可供显示!"); // 没有数据的时候显示的内容 DecimalFormat df = new DecimalFormat("0.00%");//获得一个DecimalFormat对象,主要是设置小数问题,表示小数点后保留两位。 NumberFormat nf = NumberFormat.getNumberInstance();//获得一个NumberFormat对象 StandardPieSectionLabelGenerator sp = new StandardPieSectionLabelGenerator( "{0}:{2}", nf, df);//获得StandardPieSectionLabelGenerator对象,生成的格式,{0}表示section名,{1}表示section的值,{2}表示百分比。可以自定义 pieplot.setLabelGenerator(sp);//设置饼图显示百分比 File dir = new File(staticsSavePath); if (!dir.exists()) { dir.mkdir(); } String fName = String.valueOf(System.currentTimeMillis())+"pie.png"; File file = new File(staticsSavePath, fName); ChartUtilities.saveChartAsPNG(file, jfreechart, 400, 250);//生成一个png图片 // ChartUtilities.writeChartAsPNG(response.getOutputStream(), jfreechart, // 500, 270);生成输出流