用jfreechat编写图形化统计数据程序

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>弄了很长时间的Jfreechat ,今天终于有点眉目了, 和大家共

享一下:
1.首先你先有开发环境eclipse+Access;
2.下载需要的Jfreechat包:
http://prdownloads.sourceforge.net/jfreechart/jfreechart-1.0.0.zip?download

解压之后把lib下的包复制到你的项目下lib中。
3.写web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"


xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"


xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee


http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>
<servlet-name>DisplayChart</servlet-name>


<servlet-class>org.jfree.chart.servlet.DisplayChart</ser

vlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart</url-pattern>
</servlet-mapping>
</web-app>
4.写index.jsp文件:
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ 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" %>

<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>
</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");

//按地区进行统计
DefaultPieDataset dataset=new

DefaultPieDataset();
//设置数据源
while(rs.next()){
String dq=rs.getString("number");



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);
}
%>
<hr>
<br>
<%


//生成柱状图
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("number")

),"学校",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?file

name="+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")%>"></cent

er>
</html>

你可能感兴趣的:(sql,servlet,jdbc,jfreechart,sun)