|
Posted on 2008-10-27 23:50 梦与桥 阅读(665) 评论(0) 编辑 收藏 所属分类: jsp程序设计
1、JavaReport输出一维数据展现:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="com.javareport.beans.*"%>
<%@ page extends="com.javareport.http.WebReportEngine"%>
<%!
public Report createReport(HttpServletRequest request) throws Exception{
//图片类型数组
int[] chartType = new int[]{
Chart.CHART_PIE3D,Chart.CHART_STACKBAR3D,
Chart.CHART_CURVE,Chart.CHART_LINE,
Chart.CHART_POINT,Chart.CHART_INVERTED_CURVE,
Chart.CHART_INVERTED_LINE,Chart.CHART_INVERTED_STACKBAR};
//单元数据的显示标签字符串数组
String[] labels = new String[] {"太阳","星星","月亮","她的眼睛"};
//实例化报表对象
Report report = new Report();
//在页眉中添加文本信息内容
report.addHeaderText("各种光源在我心中的位置统计");
//在报表的页眉添加一条横直线
report.addHeaderSeparator(1);
//在页尾添加一条横直线
report.addFooterSeparator(1);
//在页尾添加文本信息内容
report.addFooterText("第{P}页, 共{N}页");
//循环输出各种类型的图片
for (int i = 0; i < chartType.length; i++) {
try {
//实例化一个图表对象
Chart chart = new Chart((Number[][])getData(request));
//设置图表中的单元数据的显示的标签
chart.setLabels(labels);
//设置统计图的类型
chart.setStyle(chartType[i]);
//设置统计图中显示的时候把具体的数值也显示出来
chart.setShowValue(true);
//在报表中添加文本信息内容
report.addText("报表中常见的报表统计图表("+i+"): ");
//在报表中添加图表信息内容
report.addChart(chart);
//在报表中添加换行符号
report.addBreak();
report.addBreak();
report.addBreak();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
return report;
}
//读者可根据需要设置数组的值,或从数据库中取出值放入数组中以动态显示数据
public Double[][] getData(HttpServletRequest request){
Double[][] data = new Double[1][4];
data[0][0] = new Double(67);
data[0][1] = new Double(82);
data[0][2] = new Double(74);
data[0][3] = new Double(100);
return data;
}
//定制Web报表在页面首部显示的工具栏为标准的样式,增加一个"返回"按钮,返回到首页
public String getToolbarScript(HttpServletRequest request){
return "<a href=\"../index.htm\"><img src=\""+request.getRequestURI()+
"?op=Resource&name=/resource/back.gif\" border=\"0\" alt=\"返回\"></a>";
}
%>
附:二维数据图形与一维数据图形的区别是在数据展现上加大了数据的展现量,在同一个单元数据标签处可显示属于同一个单元的多个数据。修改改getData()方法的内容如下:
public Double[][] getData(HttpServletRequest request){
Double[][] data = new Double[4][4];
data[0][0] = new Double(67);data[0][1] = new Double(82);
data[0][2] = new Double(74);data[0][3] = new Double(100);
data[1][0] = new Double(67);data[1][1] = new Double(82);
data[1][2] = new Double(74);data[1][3] = new Double(100);
data[2][0] = new Double(67);data[2][1] = new Double(82);
data[2][2] = new Double(74);data[2][3] = new Double(100);
data[3][0] = new Double(67);data[3][1] = new Double(82);
data[3][2] = new Double(74);data[3][3] = new Double(100);
return data;
}
2、JavaReport输出报表:
<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.awt.*"%>
<%@ page import="com.javareport.beans.*"%>
<%@ page extends="com.javareport.http.WebReportEngine"%>
<%!
public Report createReport(HttpServletRequest request) throws Exception{
//实例化报表对象
Report report = new Report();
//在页眉中添加文本信息内容
report.addHeaderText("报表输出示例");
//在报表的页眉添加一条横直线
report.addHeaderSeparator(1);
//在页尾添加一条横直线
report.addFooterSeparator(1);
//在页尾添加文本信息内容
report.addFooterText("第{P}页, 共{N}页");
//在报表中添加文本信息内容
report.addText("销售情况一览表(合并表格):");
//在报表中添加换行符号
report.addBreak();
//在报表中添加表格
report.addTable(getTable());
//在报表中添加换行符号
report.addBreak();
return report;
}
//------得到销售情况一览表(合并表格)对象------
public Table getTable(){
String[][] data = getTotalData();
Table table = new Table(data);
table.setAlignment(Table.H_CENTER + Table.V_CENTER);
table.setColAutoSize(true);
table.setRowBackground(0,Color.LIGHT_GRAY);
table.setRowBackground(1,Color.LIGHT_GRAY);
table.setColBackground(0,Color.LIGHT_GRAY);
table.setRowBackground(7,new Color(255,255,128));
table.setHeaderRowCount(2);
table.setHeaderColCount(1);
table.setRowBorder(table.LINE_THIN);
table.setColBorder(table.LINE_THIN);
table.setCellSpan(0,0,new Dimension(1,2));
table.setCellSpan(0,1,new Dimension(2,1));
table.setCellSpan(0,3,new Dimension(2,1));
table.setCellSpan(0,3,new Dimension(2,1));
return table;
}
//生成销售情况数据,实际工程中一般从数据库中获取
public String[][] getData(){
String[][] data = new String[6][4];
data[0][0] = "区域"; data[0][1] = "第一季度"; data[0][2] = "第二季度"; data[0][3] = "第三季度";
data[1][0] = "华南地区"; data[1][1] = "¥2,000,000";
data[1][2] = "¥2,500,000"; data[1][3] = "¥2,200,000";
data[2][0] = "华东地区"; data[2][1] = "¥6,000,000";
data[2][2] = "¥4,500,000"; data[2][3] = "¥4,800,000";
data[3][0] = "华中地区"; data[3][1] = "¥500,000";
data[3][2] = "¥400,000"; data[3][3] = "¥700,000";
data[4][0] = "华北地区"; data[4][1] = "¥3,000,000";
data[4][2] = "¥3,200,000"; data[4][3] = "¥2,500,000";
data[5][0] = "东北地区"; data[5][1] = "¥4,000,000";
data[5][2] = "¥5,000,000"; data[5][3] = "¥4,400,000";
return data;
}
//得到销售汇总统计数据,实际工程中一般从数据库中获取
public String[][] getTotalData(){
String[][] data = new String[8][5];
data[0][0] = "区域"; data[0][1] = "上半年"; data[0][3] = "下半年";
data[1][1] = "第一季度"; data[1][2] = "第二季度"; data[1][3] = "第三季度";data[1][4] = "第四季度";
data[2][0] = "华南地区"; data[2][1] = "¥2,000,000"; data[2][2] = "¥2,500,000";
data[2][3] = "¥2,200,000";data[2][4] = "¥0";
data[3][0] = "华东地区"; data[3][1] = "¥6,000,000"; data[3][2] = "¥4,500,000";
data[3][3] = "¥4,800,000";data[3][4] = "¥0";
data[4][0] = "华中地区"; data[4][1] = "¥500,000"; data[4][2] = "¥400,000";
data[4][3] = "¥700,000";data[4][4] = "¥0";
data[5][0] = "华北地区"; data[5][1] = "¥3,000,000"; data[5][2] = "¥3,200,000";
data[5][3] = "¥2,500,000";data[5][4] = "¥0";
data[6][0] = "东北地区"; data[6][1] = "¥4,000,000"; data[6][2] = "¥5,000,000";
data[6][3] = "¥4,400,000";data[6][4] = "¥0";
data[7][0] = "总计"; data[7][1] = "¥15,500,000"; data[7][2] = "¥15,600,000";
data[7][3] = "¥14,600,000";data[7][4] = "¥0";
return data;
}
//定制Web报表在页面首部显示的工具栏为标准的样式,增加一个"返回"按钮,返回到首页
public String getToolbarScript(HttpServletRequest request){
return "<a href=\"../index.htm\"><img src=\""+request.getRequestURI()+
"?op=Resource&name=/resource/back.gif\" border=\"0\" alt=\"返回\"></a>";
}
%>
注:实际工程中,报表的数据往往来自于数据库,需要在getTotalData()方法中编写代码从数据库中得到统计数据,统计方法可以是使用特定的SQL语句或取出数据后在Java语句中统计。输出报表还有一种更直接的方法,就是用SQL语句查询出数据库中的数据后,得到一个ResultSet对象,如rs,再用如语句:RsTable rsTable=new RsTable(rs),重载如下方法:public RsTable getTable(HttpServletRequest request)throws Exception将返回值为rsTable,即可将数据库查询的结果作二维表格中的数据直接输出,不必再转换成二维数组中的再数据再输出到报表。
|