官网:
http://www.jfree.org/
主要步骤:
1.导包
activation.jar
jcommon-1.0.8.jar
jfreechart-1.0.6.jar
2.设定数据源
3.向数据源中插值
4.使用ChartFactory来创建JFreeChart
JFreeChart chart = ChartFactory.create...
5.创建文件
注意:只支持png和JPEG格式
JFreeChart可以创建的所有图形:
http://www.jfree.org/jfreechart/samples.html
以饼状图为例
1:将生成图片保存至某个文件夹中
代码如下:
1 package com.test;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.io.File;
6 import java.io.IOException;
7 import java.util.Hashtable;
8 import java.util.Map;
9 import org.jfree.chart.ChartFactory;
10 import org.jfree.chart.ChartUtilities;
11 import org.jfree.chart.JFreeChart;
12 import org.jfree.chart.title.TextTitle;
13 import org.jfree.data.general.DefaultKeyedValuesDataset;
14
15 /**
16 * 生成饼状图,并将该图保存到指定文件夹中
17 * @author Ying-er
18 * @time 2010-2-5上午11:32:27
19 */
20 public class TestMain{
21 public static void main(String[] args){
22 Map<String,Integer> ht=new Hashtable<String,Integer>();
23 ht.put("美国", 139800);
24 ht.put("日本", 52900);
25 ht.put("德国", 32800);
26 ht.put("中国", 30100);
27 ht.put("英国", 25700);
28 ht.put("法国", 25200);
29 ht.put("意大利", 20900);
30 ht.put("西班牙", 14100);
31 ht.put("加拿大", 13600);
32 ht.put("俄罗斯", 11400);
33
34 float sum=0;
35 for(Integer i:ht.values()){
36 sum+=i;
37 }
38
39 // 设定数据源
40 DefaultKeyedValuesDataset piedata = new DefaultKeyedValuesDataset();
41
42 // 向数据源中插值,第一个参数为名称,第二个参数是double数
43 for(String nation:ht.keySet()){
44
45 String temp = String.valueOf(((float)ht.get(nation))/sum*100);
46
47 if(temp.indexOf(".")>=0){
48 String xiaoshu ="00";
49 String[] arr = temp.split("[.]");
50 if(arr[1].length()>1){
51 xiaoshu = arr[1].substring(0, 2);
52 }
53 temp = arr[0]+"."+xiaoshu;
54 }
55 piedata.setValue(nation+" "+temp+"%", ((float)ht.get(nation))/sum*100);
56 }
57
58 // 使用ChartFactory来创建JFreeChart
59 JFreeChart chart = ChartFactory.createPieChart("2007世界总GDP排名", piedata, true,
60 true, true);
61
62 // 设定图片标题
63 chart.setTitle(new TextTitle("2007世界总GDP排名", new Font("隶书", Font.ITALIC, 15)));
64
65 // 设定背景
66 chart.setBackgroundPaint(Color.white);
67
68 /**
69 * 设定保存的路径
70 */
71 String rfName = "test.jpeg";
72 String fileName = "d:/"+rfName;
73 try {
74 /**
75 * 第一个参数为文件名
76 * 第二个参数为质量,取值从0f-1.0f之间
77 * 第三个参数为哪个chart创建图片
78 * 4:宽度
79 * 5:高度
80 */
81 /**
82 * 注意:只支持JPEG和png格式
83 */
84 ChartUtilities.saveChartAsJPEG(new File(fileName), 0.9f, chart, 600,600);
85 } catch (IOException e) {
86 // TODO Auto-generated catch block
87 e.printStackTrace();
88 }
89 }
90 }
2.将生成图片放到页面上
所执行Servlet代码:
1 package com.linying.action;
2
3 import java.awt.Color;
4 import java.awt.Font;
5 import java.util.Hashtable;
6 import java.util.Map;
7
8 import javax.servlet.RequestDispatcher;
9 import javax.servlet.ServletException;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import org.jfree.chart.ChartFactory;
15 import org.jfree.chart.JFreeChart;
16 import org.jfree.chart.servlet.ServletUtilities;
17 import org.jfree.chart.title.TextTitle;
18 import org.jfree.data.general.DefaultKeyedValuesDataset;
19
20 /**
21 * 饼图
22 * @author Ying-er
23 *
24 * @time 2009-11-12下午08:03:39
25 *
26 */
27 public class PieChartServlet extends HttpServlet {
28 private static final long serialVersionUID = 56890894234786L;
29
30 public void doPost(HttpServletRequest request, HttpServletResponse response)
31 throws ServletException, java.io.IOException {
32 request.setCharacterEncoding("UTF-8");
33
34 Map<String,Integer> ht=new Hashtable<String,Integer>();
35 ht.put("美国", 139800);
36 ht.put("日本", 52900);
37 ht.put("德国", 32800);
38 ht.put("中国", 30100);
39 ht.put("英国", 25700);
40 ht.put("法国", 25200);
41 ht.put("意大利", 20900);
42 ht.put("西班牙", 14100);
43 ht.put("加拿大", 13600);
44 ht.put("俄罗斯", 11400);
45
46 float sum=0;
47 for(Integer i:ht.values()){
48 sum+=i;
49 }
50
51 // 设定数据源
52 DefaultKeyedValuesDataset piedata = new DefaultKeyedValuesDataset();
53
54 // 向数据源中插值,第一个参数为名称,第二个参数是double数
55 for(String nation:ht.keySet()){
56
57 String temp = String.valueOf(((float)ht.get(nation))/sum*100);
58
59 if(temp.indexOf(".")>=0){
60 String xiaoshu ="00";
61 String[] arr = temp.split("[.]");
62 if(arr[1].length()>1){
63 xiaoshu = arr[1].substring(0, 2);
64 }
65 temp = arr[0]+"."+xiaoshu;
66 }
67 piedata.setValue(nation+" "+temp+"%", ((float)ht.get(nation))/sum*100);
68 }
69
70 // 使用ChartFactory来创建JFreeChart
71 JFreeChart chart = ChartFactory.createPieChart("2007世界总GDP排名", piedata, true,
72 true, true);
73
74 // 设定图片标题
75 chart.setTitle(new TextTitle("2007世界总GDP排名", new Font("隶书", Font.ITALIC, 15)));
76
77 // 设定背景
78 chart.setBackgroundPaint(Color.white);
79
80 // 创建文件并将图片地址传递到页面
81 String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
82 null, request.getSession());
83 String graphURL = request.getContextPath()
84 + "/displayChart?filename=" + filename;
85 request.setAttribute("graphURL", graphURL);
86
87 // 标识
88 request.setAttribute("msg", "饼图效果");
89
90 // 页面转向
91 RequestDispatcher dispatcher = request
92 .getRequestDispatcher("/web/page/chart.jsp");
93 dispatcher.forward(request, response);
94 return;
95 }
96
97 public void doGet(HttpServletRequest request, HttpServletResponse response)
98 throws ServletException, java.io.IOException {
99 doPost(request, response);
100 }
101 }
102
chart.jsp代码:
1 <%@ page contentType="text/html; charset=UTF-8"%>
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
3 <html>
4 <head>
5 <title>JFreeChart图表效果</title>
6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
7 <link rel="stylesheet" rev="stylesheet" href="web/css/style.css"
8 type="text/css" />
9 </head>
10
11 <body>
12 <div id="bodyDiv">
13 <div id="header">
14 <jsp:include page="/web/page/branch/header.jsp"/>
15 </div>
16 <div id="sidebar">
17 <jsp:include page="/web/page/branch/sidebar.jsp"/>
18 </div>
19 <div id="content">
20 <table border=0 align="center">
21 <%
22 out.print("<tr>");
23 String graphURL=(String)request.getAttribute("graphURL");
24 out.print("<td colspan=4 align=\"center\"><img src=\""+graphURL+"\"/></td>");
25 out.print("</tr>");
26 %>
27 </table>
28 </div>
29 <div id="footer">
30 <jsp:include page="/web/page/branch/footer.jsp"/>
31 </div>
32 </div>
33 </body>
34 </html>
35
36
37
38
posted on 2010-02-05 15:27
Ying-er 阅读(574)
评论(0) 编辑 收藏