一:首先要导入所需要的包:
?? jfreechart-1.0.14-experimental.jar、 jfreechart-1.0.14-swt.jar、jfreechart-1.0.14.jar(等等)
二:在web.xml 中,增加如下配置:
?? <!-- 报表开始 -->
?<servlet>?
???? <servlet-name>DisplayChart</servlet-name>?
???? <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>?
</servlet>?
<servlet-mapping>?
??? <servlet-name>DisplayChart</servlet-name>?
?<url-pattern>/servlet/DisplayChart</url-pattern>
?</servlet-mapping>
?<!-- 报表结束 -->
三:在后台编写生成饼状图或者柱状图的代码
?1、饼状图:
? DefaultPieDataset dataset=new DefaultPieDataset();//填充饼状图的数据 集合
?ConnectionProvider cp=((SessionFactoryImplementor)this.lgispEquipmentService.getEntityDao().getHibernateTemplate().getSessionFactory()).getConnectionProvider();
??? try{
?
//sql 语句
?String pointSql=" select count(*) as count,point.point_type from lgisp_point as point , lgisp_equipment as equip, lgisp_terminal as ter? where point.equipment_id=equip.EQUIPMENT_ID and equip.terminal_id=ter.terminal_id group by point.point_type";
?
?????Statement smt=cp.getConnection().createStatement();
?????ResultSet rs=smt.executeQuery(pointSql);//查询获得结果集
?????double equipCount=0;//设备总数目
??? ?HashMap hashMap=new HashMap();
?????while(rs.next()){
??? ??double count=Double.parseDouble(rs.getString(1));
??? ??int type=Integer.parseInt(rs.getString(2));
??? ??equipCount+=count;
??? ??hashMap.put(type, count);
??? ?}
??? ?List<TerManuFacturer> equipManuList=(List<TerManuFacturer>) request.getSession().getAttribute("pointManuList");
??? ?for(TerManuFacturer equip:equipManuList){
??? ??double rate=Double.parseDouble((hashMap.get(equip.getId())).toString())/ equipCount;
??????????? dataset.setValue(equip.getManuName(), rate);
??? ?}//?以上的代码是为生成图的dataset 准备数据
???? //通过工厂类生成JFreeChart对象
??? ?JFreeChart chart=ChartFactory.createPieChart("设备――厂家分配图", dataset, true, false, false);
??? ?PiePlot piePlot=(PiePlot)chart.getPlot();
??? ?//防止乱码,多标题重新设备字体(乱码问题借鉴http://chengyue2007.iteye.com/blog/424954?得到解决)
??? ?TextTitle textTitle=chart.getTitle();
??? ?textTitle.setFont(new Font("黑体",Font.PLAIN,20));
??? ?piePlot.setLabelFont(new Font("宋体",0,12));
??? ?
??? ?//没有数据时显示的内容
??? ?piePlot.setNoDataMessage("无数据显示");
??? ?piePlot.setCircular(false);
??? ?piePlot.setLabelGap(0.02D);
??? ?OutputStream ostream=response.getOutputStream();
??? ?ChartUtilities.writeChartAsJPEG(ostream, chart,500, 500);
??? ?ostream.flush();
??? ?ostream.close();
??? return "输出图的页面";
2、柱状图
?
? DefaultCategoryDataset dataset = new DefaultCategoryDataset();?
?ConnectionProvider cp =((SessionFactoryImplementor)this.reportService.getEntityDao().getHibernateTemplate().getSessionFactory()).getConnectionProvider();
??try{
???Statement smt = cp.getConnection().createStatement();
???ResultSet rs = smt.executeQuery("sql 语句);
???while(rs.next()){????
????dataset.addValue(Double.parseDouble(rs.getString(1)),rs.getString(2), "")?;
???}
???? JFreeChart chart = ChartFactory.createBarChart3D(
???????????? "图标测试", // 图表标题
???????????? "班级", // 目录轴的显示标签
???????????? "分数", // 数值轴的显示标签
???????????? dataset, // 数据集
???????????? PlotOrientation.VERTICAL, // 图表方向:水平、垂直
???????????? true, // 是否显示图例(对于简单的柱状图必须是false)
???????????? false, // 是否生成工具
???????????? false // 是否生成URL链接
???????????? );
???
???? OutputStream ostream = response.getOutputStream();
???? //设定图片的宽为400,高为300,并输出至ostream?????
???? ChartUtilities.writeChartAsJPEG(ostream, chart, 800, 600);
???? ostream.flush();
???? ostream.close();
return"输出图的页面;
?
这样既可完成,这是最基本的,未作进一步的改进。
?
?
?
?