四、JSP源程序:
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.awt.*" %>
<%@ page import="org.jfree.chart.*" %>
<%@ page import="org.jfree.chart.plot.*" %>
<%@ page import="org.jfree.chart.servlet.ServletUtilities" %>
<%@ page import="org.jfree.chart.entity.StandardEntityCollection" %>
<%@ page import="org.jfree.chart.renderer.category.BarRenderer3D" %>
<%@ page import="org.jfree.data.category.DefaultCategoryDataset" %>
<%@ page import="org.jfree.data.general.DefaultPieDataset" %>
<%@ page import="org.jfree.chart.renderer.category.BarRenderer3D" %>
<html>
<head><title>Jfree图表</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
</head>
<body>
<div align="cente"><P><font size="4" face="楷体_GB2312"><strong><font color="#0099ff">招生统计信息</font></strong></font></p>
</div>
<%
//设置生成饼状图
int i=0;
String[] area={"南京市","镇江市","常州市","无锡市","苏州市","南通市"};
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:aaa");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("select * from test");
//按地区进行统计
PrintWriter pw=null;
DefaultPieDataset dataset=new DefaultPieDataset();
//设置数据源
while(rs.next()){
String dq=rs.getString("dq");
dataset.setValue(area[i],Integer.parseInt(dq));
//添加数据,参数包括名称和数值
i++;
}
rs.close();
sql.close();
con.close();
JFreeChart chart=ChartFactory.createPieChart("饼型图",dataset,true,true,true);
chart.setTitle("统计图");//设置图片标题
ChartRenderingInfo info=new ChartRenderingInfo(new StandardEntityCollection());
String filename=ServletUtilities.saveChartAsJPEG(chart,300,200,info,session);
//设置生成图片,包括图片的大小,长度是300,宽是200
//out.println(filename);
session.setAttribute("file_name1",filename);
String URL= request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
session.setAttribute("file_url1",URL);
}
catch(SQLException e){
out.println(e);
}
%>
<%
//生成柱状图
try{
int j=0;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:aaa");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("select * from test");
DefaultCategoryDataset dataset=new DefaultCategoryDataset();//设置数据源
while(rs.next()){
dataset.addValue(Integer.parseInt(rs.getString("dq")),"学校",area[j]);
//添加数据,参数包括名称和数值
j++;
}
JFreeChart chart1=ChartFactory.createBarChart3D("统计图","招生地区","招生人数",
dataset,PlotOrientation.VERTICAL,false,false,false);
CategoryPlot plot=chart1.getCategoryPlot();
BarRenderer3D renderer1=new BarRenderer3D();
renderer1.setItemLabelsVisible(true);//显示每个柱子上的数据
renderer1.setItemMargin(0.1);//设置每个柱子之间的距离
plot.setRenderer(renderer1);//让上面对柱子的设置生效
String filename=ServletUtilities.saveChartAsJPEG(chart1,300,200,session);
//生成图片,包括图片的大小
session.setAttribute("file_name2",filename);
String URL=request.getContextPath()+"/servlet/DisplayChart?filename="+filename;
session.setAttribute("file_url2",URL);
}catch(SQLException e){}
%>
<center><img src="<%=session.getAttribute("file_url1")%>" width=300 height=200 border=0 usemap="#<%=session.getAttribute("file_name1") %>">
<img src="<%=session.getAttribute("file_url2") %>" width=300 height=200 border=0 usemap="#<%=session.getAttribute("file_name2")%>"></center>
</html>