Blog Stats
Posts - 14
Articles - 11
Comments - 1
Trackbacks - 0
随笔档案
2009年11月 (5)
2009年10月 (9)
文章档案
2009年10月 (2)
2009年9月 (9)
Infernu的Google site
Infernus-JXH
在JSP页面查数据库显示统计图。
jsp页面:
<%
@page pageEncoding
=
"
GBK
"
%>
<%
@page import
=
"
org.jfree.data.category.CategoryDataset
"
%>
<%
@page import
=
"
org.jfree.data.general.DatasetUtilities
"
%>
<%
@page import
=
"
org.jfree.chart.JFreeChart
"
%>
<%
@page import
=
"
org.jfree.chart.ChartFactory
"
%>
<%
@page import
=
"
org.jfree.chart.plot.PlotOrientation
"
%>
<%
@page import
=
"
java.awt.Font
"
%>
<%
@page import
=
"
org.jfree.chart.plot.CategoryPlot
"
%>
<%
@page import
=
"
org.jfree.chart.axis.NumberAxis
"
%>
<%
@page import
=
"
org.jfree.chart.axis.CategoryAxis
"
%>
<%
@page import
=
"
org.jfree.chart.servlet.ServletUtilities
"
%>
<
jsp:useBean id
=
"
bean
"
class
=
"
com.tsinghuait.beans.DbBean
"
/>
<%
String[] productNames
=
bean.getProductNames();
//
产品名称系列
String[] months
=
bean.getMonths();
//
横坐标
double
[][] amounts
=
new
double
[productNames.length][months.length];
//
用从数据库读到productNames, months的长度做二维数组
for
(
int
i
=
0
; i
<
amounts.length; i
++
)
{
for
(
int
j
=
0
; j
<
amounts[
0
].length; j
++
)
{
amounts[i][j]
=
bean.getAmount(productNames[i], months[j]);
//
利用bean的getAmount方法查到数据赋给amounts[][]
//
System.out.print(amounts[i][j] + "\t");
}
//
System.out.println();
}
CategoryDataset data
=
DatasetUtilities.createCategoryDataset(productNames, months, amounts);
//
(X轴数据,Y轴数据,填充柱状图所要的实际数据);
JFreeChart chart
=
ChartFactory.createBarChart3D(
"
农产品产量图
"
,
"
月份
"
,
"
产量
"
,
data, PlotOrientation.VERTICAL,
true
,
false
,
false
);
//
利用工厂模式创建JFreeChart的对象chart,使用其的createBarChart3D方法画出3D柱状图,参数如下:
//
图形的标题、X轴标题、Y轴标题、data就是CategoryDataset类的实例对象、图表的方向(横/竖)、显示标题、启用热键、启用超键接。
chart.getTitle().setFont(
new
Font(
"
黑体
"
, Font.PLAIN,
20
));
//
此代码处理jfreeChart的中文乱码问题
chart.getLegend().setItemFont(
new
Font(
"
宋体
"
, Font.BOLD,
12
));
CategoryPlot plot
=
(CategoryPlot) chart.getPlot();
NumberAxis na
=
(NumberAxis) plot.getRangeAxis();
CategoryAxis ca
=
plot.getDomainAxis();
na.setLabelFont(
new
Font(
"
宋体
"
, Font.PLAIN,
14
));
ca.setLabelFont(
new
Font(
"
楷体_gb2312
"
, Font.ITALIC,
14
));
ca.setTickLabelFont(
new
Font(
"
宋体
"
, Font.BOLD,
14
));
String filename
=
ServletUtilities.saveChartAsJPEG(chart,
400
,
300
, session);
//
创建jpg图片,并将名字保存到filename
String url
=
request.getContextPath()
+
"
/servlet/a?filename=
"
+
filename;
//
使用request.getContextPath()的到主地址,做字符串连接,使用传参数的方法得到新的地址
out.println(url);
%>
<
img src
=
"
<%= url%>
"
/>
bean中的数据库查询
public
String[] getProductNames()
{
String[] series
=
null
;
try
{
Statement stmt
=
con.createStatement();
ResultSet rs
=
stmt.executeQuery(
"
select distinct name from t_product
"
);
rs.last();
series
=
new
String[rs.getRow()];
rs.first();
rs.relative(
-
1
);
for
(
int
i
=
0
; rs.next(); i
++
)
{
series[i]
=
rs.getString(
1
);
}
}
catch
(SQLException e)
{
e.printStackTrace();
}
return
series;
}
public
String[] getMonths()
{
String[] months
=
null
;
try
{
Statement stmt
=
con.createStatement();
ResultSet rs
=
stmt.executeQuery(
"
select distinct name from t_month
"
);
rs.last();
months
=
new
String[rs.getRow()];
rs.first();
rs.relative(
-
1
);
for
(
int
i
=
0
; rs.next(); i
++
)
{
months[i]
=
rs.getString(
1
);
}
}
catch
(SQLException e)
{
e.printStackTrace();
}
return
months;
}
public
int
getAmount(String name, String month)
{
int
amount
=
0
;
//
Add your code here
try
{
PreparedStatement ps
=
con.prepareStatement(
"
select amount from t_product, t_month where t_product.name = ? and t_month.name = ?
"
+
"
and t_product.month = t_month.id
"
);
ps.setString(
1
, name);
ps.setString(
2
, month);
ResultSet rs
=
ps.executeQuery();
if
(rs.next())
{
amount
=
rs.getInt(
1
);
}
}
catch
(SQLException e)
{
e.printStackTrace();
}
return
amount;
}
posted on 2009-10-24 01:35
Infernus
阅读(1293)
评论(0)
编辑
收藏
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
知识库
C++博客
博问
管理
Powered by:
.Text
and
ASP.NET
- Copyright © Infernus