春风博客

春天里,百花香...

导航

<2008年4月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

统计

公告

MAIL: junglesong@gmail.com
MSN: junglesong_5@hotmail.com

Locations of visitors to this page

常用链接

留言簿(11)

随笔分类(224)

随笔档案(126)

个人软件下载

我的其它博客

我的邻居们

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

JFreeChat四种图表的绘制例程

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,
                
truetrue);
        
        
// 设定图片标题
        chart.setTitle(new TextTitle("2007世界总GDP排名"new Font("隶书", Font.ITALIC, 15)));

        
// 设定背景
        chart.setBackgroundPaint(Color.white);
        
        
// 创建文件并将图片地址传递到页面
        String filename = ServletUtilities.saveChartAsPNG(chart, 500300,
                
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,
                
truetrue);
        
        
// 设定图片标题
        chart.setTitle(new TextTitle("2007世界总GDP排名"new Font("隶书", Font.ITALIC, 15)));

        
// 设定背景
        chart.setBackgroundPaint(Color.white);
        
        
// 创建文件并将图片地址传递到页面
        String filename = ServletUtilities.saveChartAsPNG(chart, 500300,
                
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(
200222100);
        chinaht.put(
200314100);
        chinaht.put(
200427100);
        chinaht.put(
200538100);
        chinaht.put(
200629000);
        chinaht.put(
200790100);
        
        
for (Integer i:chinaht.keySet()) {
            chinaTs.add(
new TimeSeriesDataItem(new Day(11, i),new Double(chinaht.get(i))));            
        }


        
// 创建时间数据源,每一个TimeSeries在图上是一条曲线
        TimeSeries japanTs = new TimeSeries("日本");
        Map
<Integer, Integer> japanht = new LinkedHashMap<Integer, Integer>();
        japanht.put(
200043900);
        japanht.put(
200363500);
        japanht.put(
200443400);
        japanht.put(
200563200);
        japanht.put(
200653100);
        japanht.put(
200732900);
        
        
for (Integer i:japanht.keySet()) {
            japanTs.add(
new TimeSeriesDataItem(new Day(11, i),new Double(japanht.get(i))));            
        }


        
// 创建时间数据源,每一个TimeSeries在图上是一条曲线
        TimeSeries usaTs = new TimeSeries("美国");
        Map
<Integer, Integer> usaht = new LinkedHashMap<Integer, Integer>();
        usaht.put(
2001100000);
        usaht.put(
2003159800);
        usaht.put(
2004109700);
        usaht.put(
2005129600);
        usaht.put(
20069800);
        usaht.put(
2007149800);
        
        
for (Integer i:usaht.keySet()) {
            usaTs.add(
new TimeSeriesDataItem(new Day(11, 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, 
truetruefalse);

        
// 创建文件并将图片地址传递到页面
        String filename = ServletUtilities.saveChartAsPNG(chart, 500300,
                
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, falsefalse,
                
false);
        
        
// 设定图片标题
        chart.setTitle(new TextTitle("2007世界总GDP排名"new Font("隶书", Font.ITALIC, 15)));

        
// 设定背景
        chart.setBackgroundPaint(Color.white);
        
        
// 创建文件并将图片地址传递到页面
        String filename = ServletUtilities.saveChartAsPNG(chart, 500300,
                
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

posted on 2008-04-08 13:00 sitinspring 阅读(3587) 评论(6)  编辑  收藏 所属分类: Web开发

评论

# re: JFreeChat四种图表的绘制例程 2008-05-30 13:46 we

不是时间曲线图如何制作?能不能些个例子谢谢!  回复  更多评论   

# re: JFreeChat四种图表的绘制例程 2010-08-09 13:43 zhangnian

我按你这样写,为什么图片不显示  回复  更多评论   

# re: JFreeChat四种图表的绘制例程 2010-08-09 13:44 zhangnian

可以把那个曲线图给我份完整的代码吗,谢谢!714378662@QQ.com  回复  更多评论   

# re: JFreeChat四种图表的绘制例程 2010-08-09 14:06 何杨

http://www.blogjava.net/Files/sitinspring/JFreeChartSample20080408130142.rar  回复  更多评论   

# re: JFreeChat四种图表的绘制例程[未登录] 2010-08-11 11:55 杨超

@zhangnian
在你的web.xml 里加上:  回复  更多评论   

# re: JFreeChat四种图表的绘制例程[未登录] 2010-08-11 11:55 杨超


<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>
org.jfree.chart.servlet.DisplayChart
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/displayChart</url-pattern>
</servlet-mapping>  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
sitinspring(http://www.blogjava.net)原创,转载请注明出处.