当前位置:编程学习 > JSP >>

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 NA

上一个:基于weblogic 的EJB 学习笔记
下一个:你是高手吗?这些关于异常的问题你注意到了吗??

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,