读书人

jfreechart饼图设立Label为百分比的方

发布时间: 2012-09-24 13:49:41 作者: rapoo

jfreechart饼图设置Label为百分比的方法

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);生成输出流

读书人网 >开源软件

热点推荐