报表代码实例大全:
本实例采用Cewolf+JFreeChart做成。Cewolf是基于JFreechart的,利用JFreechart的绘制引擎,将最终的图片写入客户端.在服务器端不会创建任何文件。
其中这些实例中的web.xml的配置都是一样的,所以配置一次后就不再动它了
后面的每一个实例我都会给出java类和jsp页面的代码,并附上对应的显示出来的图形(这些实例中有些来自网上,我也都一并做过并成功实现)
#########################*************************######################
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>CewolfServlet</servlet-name>
<servlet-class>de.laures.cewolf.CewolfRenderer</servlet-class>
<!-- sets storage implementation -->
<init-param>
<param-name>storage</param-name>
<param-value>
de.laures.cewolf.storage.TransientSessionStorage
</param-value>
</init-param>
<!-- sets overlib.js location relative to webapp -->
<init-param>
<param-name>overliburl</param-name>
<param-value>/etc/overlib.js</param-value>
</init-param>
<!-- turn on or off debugging logging -->
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>4</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CewolfServlet</servlet-name>
<url-pattern>/adChart/*</url-pattern>
</servlet-mapping>
</web-app>
时间序列图
package cewolf;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import de.laures.cewolf.DatasetProducer;
public class AdDateLineDataset implements DatasetProducer, Serializable {
private Number[] data;
private Date[] dateCategories;
public AdDateLineDataset() {
super();
data = new Number[] { 11, 33, 27 };
DateFormat df = DateFormat.getDateTimeInstance();
try {
dateCategories = new Date[] { df.parse("2008-03-5 08:00:00"),
df.parse("2008-03-5 10:00:00"),
df.parse("2008-03-5 15:00:00") };
} catch (ParseException e) {
e.printStackTrace();
}
}
public AdDateLineDataset(Date[] d, Number[] a) {
}
public Object produceDataset(Map params) {
TimeSeries ts1 = new TimeSeries("My Title",
org.jfree.data.time.Hour.class);
for (int i = 0; i < data.length; i++) {
ts1.add(new org.jfree.data.time.Hour(dateCategories[i]), data[i]);
}
TimeSeriesCollection collect = new TimeSeriesCollection(ts1);
return collect;
}
public String getProducerId() {
return "TimeDataProducer";
}
public boolean hasExpired(Map params, Date since) {
return true;
}
}
JSP页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.util.Date"%>
<%@taglib uri='/WEB-INF/tlds/cewolf.tld' prefix='cewolf'%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>test</title>
</head>
<body>
<jsp:useBean id="data1" class="cewolf.AdDateLineDataset" />
<cewolf:chart id="pageChart2" title="时间序列图" type="timeseries"
xaxislabel="时间" yaxislabel="数量">
<cewolf:data>
<cewolf:producer id="data1" />
</cewolf:data>
</cewolf:chart>
<cewolf:img chartid="pageChart2" renderer="/adChart" width="500"
height="500" />
</body>
</html>
[img]https://p-blog.csdn.net/images/p_blog_csdn_net/igoder/a.png[/img]
折线图
package cewolf;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import de.laures.cewolf.DatasetProduceException;
import de.laures.cewolf.DatasetProducer;
import de.laures.cewolf.links.CategoryItemLinkGenerator;
import de.laures.cewolf.tooltips.CategoryToolTipGenerator;
/**
* An example data producer.
*
* @author Guido Laures
*/
public class PageViewCountData implements DatasetProducer,
CategoryToolTipGenerator, CategoryItemLinkGenerator, Serializable {
private static final Log log = LogFactory.getLog(PageViewCountData.class);
// These values would normally not be hard coded but produced by
// some kind of data source like a database or a file
private final String[] categories = { "mon", "tue", "wen", "thu", "fri",
"sat", "sun" };
private final String[] seriesNames = { "cewolfset.jsp", "tutorial.jsp",
"testpage.jsp", "performancetest.jsp" };
/**
* Produces some random data.
*/
public Object produceDataset(Map params) throws DatasetProduceException {
log.debug("producing data.");
DefaultCategoryDataset dataset = new DefaultCategoryDataset() {
/**
* @see java.lang.Object#finalize() 当垃圾回收器确定不存在对该对象的更多引用时,
* 由对象的垃圾回收器调用此方法
*/
protected void finalize() throws Throwable {
super.finalize();
log.debug(this + " finalized.");
}
};
for (int series = 0; series < seriesNames.length; series++) {
int lastY = (int) (Math.random() * 1000 + 1000);
for (int i = 0; i < categories.length; i++) {
final int y = lastY + (int) (Math.random() * 200 - 100);
lastY = y;
dataset.addValue(y, seriesNames[series], categories[i]);
}
}
return dataset;
}
/**
* This producer's data is invalidated after 5 seconds. By this method the
* producer can influence Cewolf's caching behaviour the way it wants to.
*/
public boolean hasExpired(Map params, Date since) {
log.debug(getClass().getName() + "hasExpired()");
return (System.currentTimeMillis() - since.getTime()) > 5000;
}
/**
* Returns a unique ID for this DatasetProducer
*/
public String getProducerId() {
return "PageViewCountData DatasetProducer";
}
/**
* Returns a link target for a special data item.
*/
public String generateLink(Object data, int series, Object category) {
return seriesNames[series];
}
/**
* @see java.lang.Object#finalize()
*/
protected void finalize() throws Throwable {
super.finalize();
log.debug(this + " finalized.");
}
/**
* @see org.jfree.chart.tooltips.CategoryToolTipGenerator#generateToolTip(CategoryDataset,
* int, int)
*/
public String generateToolTip(CategoryDataset arg0, int series, int arg2) {
return seriesNames[series];
}
}
JSP页面
<%@page contentType="text/html;charset=utf8"%>
<%@taglib uri='/WEB-INF/tlds/cewolf.tld' prefix='cewolf'%>
<HTML>
<BODY>
<H1>
Page View Statistics
</H1>
<HR>
<jsp:useBean id="pageViews" class="cewolf.PageViewCountData" />
<cewolf:chart id="line" title="折线图" type="line" xaxislabel="Page"
yaxislabel="Views">
<cewolf:data>
<cewolf:producer id="pageViews" />
</cewolf:data>
</cewolf:chart>
<p>
<cewolf:img chartid="line" renderer="/adChart" width="500"
height="400" />
<P>
</BODY>
</HTML>
[img]https://p-blog.csdn.net/images/p_blog_csdn_net/igoder/b.png[/img]
垂直柱条图
package cewolf;
import java.util.Date;
import java.util.Map;
import org.jfree.data.category.DefaultCategoryDataset;
import de.laures.cewolf.DatasetProduceException;
import de.laures.cewolf.DatasetProducer;
public class VerticalBarData implements DatasetProducer {
String chartTitle = "垂直直方图";
String name[] = { "张三", "李四", "王五", "刘六", "赵七" };
String category[] = { "第1周", "第2周", "第3周", "第4周" };
@Override
public String getProducerId() {
return null;
}
@Override
public boolean hasExpired(Map arg0, Date arg1) {
return true;
}
@Override
public Object produceDataset(Map arg0) throws DatasetProduceException {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
// 往数据集中添加绘制图表所需的数据
int bookSales;
for (int i = 0; i < name.length; i++) {
for (int j = 0; j < category.length; j++) {
// 随机生成销售量
bookSales = 50 + (int) (Math.random() * 50);
dataset.addValue(bookSales, name[i], category[j]);
}
}
return dataset;
}
}
JSP页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="java.util.Date"%>
<%@taglib uri='/WEB-INF/tlds/cewolf.tld' prefix='cewolf'%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>test</title>
</head>
<body>
<jsp:useBean id="data1" class="cewolf.VerticalBarData" />
<cewolf:chart id="verticalBar" title="金牌公司推销人员业绩" type="verticalBar"
xaxislabel="销售时间:2005年4月" yaxislabel="销售量">
<cewolf:data>
<cewolf:producer id="data1" />
</cewolf:data>
</cewolf:chart>
<cewolf:img chartid="verticalBar" renderer="/adChart" width="500"
height="375" />
</body>
</html>
[img]https://p-blog.csdn.net/images/p_blog_csdn_net/igoder/c.png[/img]
饼图
package cewolf;
import java.util.Date;
import java.util.Map;
import org.jfree.data.general.DefaultPieDataset;
import de.laures.cewolf.DatasetProduceException;
import de.laures.cewolf.DatasetProducer;
public class PieData implements DatasetProducer {
@Override
public String getProducerId() {
return null;
}
@Override
public boolean hasExpired(Map arg0, Date arg1) {
return true;
}
@Override
public Object produceDataset(Map arg0) throws DatasetProduceException {
DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
defaultpiedataset.setValue("One", new Double(43.200000000000003D));
defaultpiedataset.setValue("Two", new Double(10D));
defaultpiedataset.setValue("Three", new Double(27.5D));
defaultpiedataset.setValue("Four", new Double(17.5D));
return defaultpiedataset;
}
}
JSP页面
<%@ page language="java" pageEncoding="UTF-8" %>
<%@page import="java.util.Date"%>
<%@taglib uri='/WEB-INF/tlds/cewolf.tld' prefix='cewolf' %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>test</title>
</head>
<body>
<jsp:useBean id="data1" class="cewolf.PieData"/>
<cewolf:chart
id="pieChart"
title="饼图"
type="pie" >
<cewolf:data>
<cewolf:producer id="data1" />
</cewolf:data>
</cewolf:chart>
<cewolf:img
chartid="pieChart"
renderer="/adChart"
width="500"
height="375"/>
</body>
</html>
[img]https://p-blog.csdn.net/images/p_blog_csdn_net/igoder/d.png[/img]
柱状图
package cewolf;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;
import org.jfree.data.category.DefaultIntervalCategoryDataset;
import de.laures.cewolf.DatasetProduceException;
import de.laures.cewolf.DatasetProducer;
public class Tu implements DatasetProducer, Serializable{
String categories[] = {"mon","tues","sun","xxx"};
String seriesNames [] = {"show","click","ctr"};
public String getProducerId() {
return "CategoryDataProducer";
}
public boolean hasExpired(Map arg0, Date arg1) {
return true;
}
public Object produceDataset(Map arg0) throws DatasetProduceException {
//以下是测试数据
Number[][] datasets = new Number[4][3];
datasets[0][0] = new Integer(201);
datasets[0][1] = new Integer(2);
datasets[0][2] = new Integer(3);
datasets[1][0] = new Integer(11);
datasets[1][1] = new Integer(22);
datasets[1][2] = new Integer(33);
datasets[2][0] = new Integer(44);
datasets[2][1] = new Integer(55);
datasets[2][2] = new Integer(66);
datasets[3][0] = new Integer(77);
datasets[3][1] = new Integer(88);
datasets[3][2] = new Integer(99);
//参数1:横轴 参数2:纵轴
DefaultIntervalCategoryDataset ds = new DefaultIntervalCategoryDataset(
categories,seriesNames, datasets,datasets);
return ds;
}
}
JSP页面
<%@ page language="java" pageEncoding="UTF-8"%>
<%@taglib uri='/WEB-INF/tlds/cewolf.tld' prefix='cewolf'%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>test</title>
</head>
<body>
<%
cewolf.Tu tu = new cewolf.Tu();
pageContext.setAttribute("data1", tu);
{ // <cewolf:cha
%>
<cewolf:chart id="pageChart2" title="日期柱状图"
type="stackedHorizontalBar" xaxislabel="时间" yaxislabel="数量">
<cewolf:data>
<cewolf:producer id="data1" />
</cewolf:data>
</cewolf:chart>
<cewolf:img chartid="pageChart2" renderer="/adChart" width="500"
height="500" />
</body>
</html>
[img]https://p-blog.csdn.net/images/p_blog_csdn_net/igoder/e.png[/img]