项目中的jfreechart终于可以使用了
项目环境:struts2+spring+ibatis+Maven
首先下载jfreechart,可参考JFreeChart的介绍
我使用maven的,所以在pom.xml中添加如下依赖
<dependency> <groupId>jfree</groupId> <artifactId>jfreechart</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>jfree</groupId> <artifactId>jcommon</artifactId> <version>1.0.16</version> </dependency>
写一个小实例进行测试:
public String createReport() throws IOException{ //设置数据集 DefaultPieDataset dataset = new DefaultPieDataset(); dataset.setValue(“初中高级程序员”, 0.55); dataset.setValue(“项目经理”, 0.1); dataset.setValue(“系统分析师”, 0.1); dataset.setValue(“软件架构师”, 0.1); dataset.setValue(“其他”, 0.2); //通过工厂类生成JFreeChart对象 JFreeChart chart = ChartFactory.createPieChart3D(“IT行业职业分布图”, dataset, true, false, false); PiePlot pieplot = (PiePlot) chart.getPlot(); pieplot.setLabelFont(new Font(“宋体”, 0, 12)); //pieplot.setExplodePercent(); //标题字体 Font font = new Font(“SimSun”, 10, 20); TextTitle textTitle = chart.getTitle(); textTitle.setFont(font); textTitle.setPaint(Color.BLUE); //联想细节 LegendTitle legend = chart.getLegend(); legend.setItemFont(new Font(“宋体”, Font.PLAIN, 15)); legend.setItemPaint(Color.BLUE); //没有数据的时候显示的内容 pieplot.setNoDataMessage(“无数据显示”); pieplot.setCircular(false); pieplot.setLabelGap(0.02D); String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, getSession()); FileOutputStream fos_jpg = null; try { fos_jpg = new FileOutputStream(“D:\\fruit.jpg”); ChartUtilities.writeChartAsJPEG(fos_jpg,1.0f,chart,400,300,null); } finally { try { fos_jpg.close(); } catch (Exception e) {} } return “success”; }
如下图: