jFreeChart初探

发表于:2007-07-04来源:作者:点击数: 标签:
//****************************************** //*author:rautinee* //*Email:rautinee@21cn.com* //*http://www.tryitsoft.com* //*本文可自由传播使用,但请务必保留此信息* //****************************************** jFreeChart也许对大家来说很陌生
//******************************************
//*             author:rautinee            *
//*         Email:rautinee@21cn.com        *
//*         http://www.tryitsoft.com       *
//*本文可自由传播使用,但请务必保留此信息  *
//******************************************

jFreeChart也许对大家来说很陌生,她是一款免费的、功能强大的统计图生成工具,可以直接生成PNG,JPG等各式的文件
为我们java项目的开发增加了一部利器。她的英文简介
JFreeChart is a free Java class library for generating charts, including:
pie charts;
line charts and horizontal/vertical bar charts (regular and stacked, with optional 3D-effect);
XY plots and scatter plots;
time series, high/low/open/close charts and candle stick charts;
combination charts;
Pareto charts;
Gantt charts;
bubble charts;
wind plots, meter charts and symbol charts;

正好我最近的一个项目用到她,于是就很粗略的看了一下,现在把它拿出来和大家分享,希望对大家的工作有所
帮助。

本文主要是讲解她在jsp上面的应用。例子采用jsp+javaBean结构实现

准备工作:首先我们需要什么?当然是jFreeChart的package了,可以在她的官方网站上面找到下载链接
http://www.jfree.org/jfreechart/,把包下下来,配置到tomcat当中,然后可以开始我们的Jfreechart之旅
了。

下面我要做的是一个饼状统计图,首先建立javaBean文件

文件名:Tryitshow.java

package com.tryit;

import java.io.*;
import java.awt.Insets;
import java.awt.Font;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Locale;
import java.text.NumberFormat;
import javax.servlet.http.HttpSession;
import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.axis.*;
import org.jfree.chart.renderer.VerticalBarRenderer;
import org.jfree.chart.renderer.StandardXYItemRenderer;
import org.jfree.chart.plot.*;
import org.jfree.chart.entity.*;
import org.jfree.chart.tooltips.*;
import org.jfree.chart.urls.*;
import org.jfree.chart.servlet.*;
import org.jfree.chart.TextTitle;
import java.net.URL;

public class Tryitshow
{
    public static String generatePieChart(String infoStr, String titles,HttpSession session, PrintWriter pw)
        {
            String filename = null;
            Font font;
            
            try {
                    //  建立PieDataSet
            DefaultPieDataset data = new DefaultPieDataset();
            String ss[]=infoStr.split(",");
            for (int n=0;n<ss.length;n++)
            {
                String ssPart[]=ss[n].split("#");
                int tt=Integer.parseInt(ssPart[1]);
                String mm=ssPart[0];
                data.setValue(mm,tt);
            }
        
                //  生成chart物件
            PiePlot plot = new PiePlot(data);
            plot.setInsets(new Insets(0, 5, 5, 5));
        
            plot.setToolTipGenerator(new StandardPieToolTipGenerator());
            
            font=new Font("黑体",Font.CENTER_BASELINE,20);//这个地方是设置统计图标题的字体和大小
            
            JFreeChart chart = new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true);
            
            TextTitle tt=new TextTitle(titles);
            tt.setFont(font);
            chart.setBackgroundPaint(java.awt.Color.white);//统计图片的底色
            chart.setTitle(tt);

            //  把生成的文件写入到临时的目录中
            ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
            filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session);
//选择存储成png格式的文件,当然你也可以使用saveChartAsJPEG的方法生成jpg图片

            //  把image map 写入到 PrintWriter
            ChartUtilities.writeImageMap(pw, filename, info);
            pw.flush();
            }
            catch (Exception ex) {
            System.out.println("error:"+ex.getMessage());
            }
            return filename;
            }
    }


接着是jsp文件
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import = "com.tryit.Tryitshow" %>
<%@ page import = "java.io.PrintWriter" %>
<%@ page import = "java.text.SimpleDateFormat" %>
<%@ page import = "java.text.ParseException" %>
<%@ page import = "java.util.ArrayList" %>
<%@ page import = "java.util.Date" %>
<%@ page import = "java.util.Iterator" %>
<%@ page import = "java.util.Locale" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<%
String filename = Tryitshow.generatePieChart("本科#100,专科#200","人才学历信息统计",session, new PrintWriter(out));

    String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
    %>
<BODY>
<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#<%= filename %>">
</BODY>
</HTML>

ok,大功告成,当然,你可以采用其他方式,比如你可以把DataSet的数据设置在xml文件中,从xml文件读取
统计数据中添加百分比等等,还可以选择生成柱状图、折线图等。

jFreeChart还有很多其他的用法,希望大家可以多多交流。


web.xml文件的配置

<web-app>
  <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>/servlet/DisplayChart</url-pattern>
    </servlet-mapping>
</web-app>



原文转自:http://www.ltesting.net