1.平面饼图
Servlet代码:
package com.sitinspring.action;
import java.awt.Color;
import java.awt.Font;
import java.util.Hashtable;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultKeyedValuesDataset;
/** *//**
* 用于创建饼图的Servlet
*
* @author sitinspring
*
* @date 2008-2-12
*/
public class PieChartServlet extends HttpServlet {
private static final long serialVersionUID = 56890894234786L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
request.setCharacterEncoding("UTF-8");
Map<String,Integer> ht=new Hashtable<String,Integer>();
ht.put("美国", 139800);
ht.put("日本", 52900);
ht.put("德国", 32800);
ht.put("中国", 30100);
ht.put("英国", 25700);
ht.put("法国", 25200);
ht.put("意大利", 20900);
ht.put("西班牙", 14100);
ht.put("加拿大", 13600);
ht.put("俄罗斯", 11400);
float sum=0;
for(Integer i:ht.values()){
sum+=i;
}
// 设定数据源
DefaultKeyedValuesDataset piedata = new DefaultKeyedValuesDataset();
// 向数据源中插值,第一个参数为名称,第二个参数是double数
for(String nation:ht.keySet()){
piedata.setValue(nation, ((float)ht.get(nation))/sum*100);
}
// 使用ChartFactory来创建JFreeChart
JFreeChart chart = ChartFactory.createPieChart("2007世界总GDP排名", piedata, true,
true, true);
// 设定图片标题
chart.setTitle(new TextTitle("2007世界总GDP排名", new Font("隶书", Font.ITALIC, 15)));
// 设定背景
chart.setBackgroundPaint(Color.white);
// 创建文件并将图片地址传递到页面
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
null, request.getSession());
String graphURL = request.getContextPath()
+ "/displayChart?filename=" + filename;
request.setAttribute("graphURL", graphURL);
// 标识
request.setAttribute("msg", "饼图效果");
// 页面转向
RequestDispatcher dispatcher = request
.getRequestDispatcher("/web/page/chart.jsp");
dispatcher.forward(request, response);
return;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
doPost(request, response);
}
}
页面代码:
<%@ page contentType="text/html; charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>JFreeChart图表效果</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" rev="stylesheet" href="web/css/style.css"
type="text/css" />
</head>
<body>
<div id="bodyDiv">
<div id="header">
<jsp:include page="/web/page/branch/header.jsp"/>
</div>
<div id="sidebar">
<jsp:include page="/web/page/branch/sidebar.jsp"/>
</div>
<div id="content">
<table border=0 align="center">
<%
out.print("<tr>");
String graphURL=(String)request.getAttribute("graphURL");
out.print("<td colspan=4 align=\"center\"><img src=\""+graphURL+"\"/></td>");
out.print("</tr>");
%>
</table>
</div>
<div id="footer">
<jsp:include page="/web/page/branch/footer.jsp"/>
</div>
</div>
</body>
</html>
2.3d饼图
Servlet代码:
package com.sitinspring.action;
import java.awt.Color;
import java.awt.Font;
import java.util.Hashtable;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultKeyedValuesDataset;
/** *//**
* 用于创建3D饼图的Servlet
*
* @author sitinspring
*
* @date 2008-2-12
*/
public class Pie3DChartServlet extends HttpServlet {
private static final long serialVersionUID = 56890894234786L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
request.setCharacterEncoding("UTF-8");
Map<String,Integer> ht=new Hashtable<String,Integer>();
ht.put("美国", 139800);
ht.put("日本", 52900);
ht.put("德国", 32800);
ht.put("中国", 30100);
ht.put("英国", 25700);
ht.put("法国", 25200);
ht.put("意大利", 20900);
ht.put("西班牙", 14100);
ht.put("加拿大", 13600);
ht.put("俄罗斯", 11400);
float sum=0;
for(Integer i:ht.values()){
sum+=i;
}
// 设定数据源
DefaultKeyedValuesDataset piedata = new DefaultKeyedValuesDataset();
// 向数据源中插值,第一个参数为名称,第二个参数是double数
for(String nation:ht.keySet()){
piedata.setValue(nation, ((float)ht.get(nation))/sum*100);
}
// 使用ChartFactory来创建JFreeChart
JFreeChart chart = ChartFactory.createPieChart3D("2007世界总GDP排名", piedata, true,
true, true);
// 设定图片标题
chart.setTitle(new TextTitle("2007世界总GDP排名", new Font("隶书", Font.ITALIC, 15)));
// 设定背景
chart.setBackgroundPaint(Color.white);
// 创建文件并将图片地址传递到页面
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
null, request.getSession());
String graphURL = request.getContextPath()
+ "/displayChart?filename=" + filename;
request.setAttribute("graphURL", graphURL);
// 标识
request.setAttribute("msg", "3D饼图效果");
// 页面转向
RequestDispatcher dispatcher = request
.getRequestDispatcher("/web/page/chart.jsp");
dispatcher.forward(request, response);
return;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
doPost(request, response);
}
}
3.折线图
Servlet代码:
package com.sitinspring.action;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.TimeSeriesDataItem;
/** *//**
* 用于创建折线图的Servlet
*
* @author sitinspring
*
* @date 2008-2-12
*/
public class CurveChartServlet extends HttpServlet {
private static final long serialVersionUID = 56890894234786L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
request.setCharacterEncoding("UTF-8");
// 曲线图标题
String title = "趋势分析";
// 曲线图X轴提示
String domain = "年份";
// 曲线图Y轴提示
String range = "GDP";
// 创建时间数据源,每一个TimeSeries在图上是一条曲线
TimeSeries chinaTs = new TimeSeries("中国");
Map<Integer, Integer> chinaht = new LinkedHashMap<Integer, Integer>();
chinaht.put(2002, 22100);
chinaht.put(2003, 14100);
chinaht.put(2004, 27100);
chinaht.put(2005, 38100);
chinaht.put(2006, 29000);
chinaht.put(2007, 90100);
for (Integer i:chinaht.keySet()) {
chinaTs.add(new TimeSeriesDataItem(new Day(1, 1, i),new Double(chinaht.get(i))));
}
// 创建时间数据源,每一个TimeSeries在图上是一条曲线
TimeSeries japanTs = new TimeSeries("日本");
Map<Integer, Integer> japanht = new LinkedHashMap<Integer, Integer>();
japanht.put(2000, 43900);
japanht.put(2003, 63500);
japanht.put(2004, 43400);
japanht.put(2005, 63200);
japanht.put(2006, 53100);
japanht.put(2007, 32900);
for (Integer i:japanht.keySet()) {
japanTs.add(new TimeSeriesDataItem(new Day(1, 1, i),new Double(japanht.get(i))));
}
// 创建时间数据源,每一个TimeSeries在图上是一条曲线
TimeSeries usaTs = new TimeSeries("美国");
Map<Integer, Integer> usaht = new LinkedHashMap<Integer, Integer>();
usaht.put(2001, 100000);
usaht.put(2003, 159800);
usaht.put(2004, 109700);
usaht.put(2005, 129600);
usaht.put(2006, 9800);
usaht.put(2007, 149800);
for (Integer i:usaht.keySet()) {
usaTs.add(new TimeSeriesDataItem(new Day(1, 1, i),new Double(usaht.get(i))));
}
// 时间曲线数据集合
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(chinaTs);
dataset.addSeries(japanTs);
dataset.addSeries(usaTs);
// 时间曲线元素
JFreeChart chart = ChartFactory.createTimeSeriesChart(title, domain,
range, dataset, true, true, false);
// 创建文件并将图片地址传递到页面
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
null, request.getSession());
String graphURL = request.getContextPath() + "/displayChart?filename="
+ filename;
request.setAttribute("graphURL", graphURL);
// 标识
request.setAttribute("msg", "折线图效果");
// 页面转向
RequestDispatcher dispatcher = request
.getRequestDispatcher("/web/page/chart.jsp");
dispatcher.forward(request, response);
return;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
doPost(request, response);
}
}
4.柱图
Servlet代码:
package com.sitinspring.action;
import java.awt.Color;
import java.awt.Font;
import java.util.Hashtable;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
/** *//**
* 用于创建柱图的Servlet
*
* @author sitinspring
*
* @date 2008-2-12
*/
public class ColumnChartServlet extends HttpServlet {
private static final long serialVersionUID = 56890894234786L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
request.setCharacterEncoding("UTF-8");
Map<String,Integer> ht=new Hashtable<String,Integer>();
ht.put("美国", 139800);
ht.put("日本", 52900);
ht.put("德国", 32800);
ht.put("中国", 30100);
ht.put("英国", 25700);
ht.put("法国", 25200);
ht.put("意大利", 20900);
ht.put("西班牙", 14100);
ht.put("加拿大", 13600);
ht.put("俄罗斯", 11400);
float sum=0;
for(Integer i:ht.values()){
sum+=i;
}
// 设定数据源
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
// 向数据源中插值,第一个参数为名称,第二个参数是double数
int i=0;
for(String nation:ht.keySet()){
i++;
dataset.addValue((float)ht.get(nation), String.valueOf(i), nation);
}
// 使用ChartFactory来创建JFreeChart
JFreeChart chart = ChartFactory.createBarChart3D("收支统计图", "国家",
"数额", dataset, PlotOrientation.VERTICAL, false, false,
false);
// 设定图片标题
chart.setTitle(new TextTitle("2007世界总GDP排名", new Font("隶书", Font.ITALIC, 15)));
// 设定背景
chart.setBackgroundPaint(Color.white);
// 创建文件并将图片地址传递到页面
String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
null, request.getSession());
String graphURL = request.getContextPath()
+ "/displayChart?filename=" + filename;
request.setAttribute("graphURL", graphURL);
// 标识
request.setAttribute("msg", "3D柱图效果");
// 页面转向
RequestDispatcher dispatcher = request
.getRequestDispatcher("/web/page/chart.jsp");
dispatcher.forward(request, response);
return;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
doPost(request, response);
}
}
例程下载(请在lib中加入jcommon-1.0.8.jar和jfreechart-1.0.6.jar):
http://www.blogjava.net/Files/sitinspring/JFreeChartSample20080408130142.rar