JFreeChart在Web中的 应用

把图表数据显示的jsp中.

首先我们建一个Servlet:

package  com;

import  java.awt.Color;
import  java.io.File;
import  java.io.IOException;

import  javax.servlet.ServletException;
import  javax.servlet.http.HttpServlet;
import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  org.jfree.chart.ChartUtilities;
import  org.jfree.chart.JFreeChart;
import  org.jfree.chart.labels.StandardPieToolTipGenerator;
import  org.jfree.chart.plot.PiePlot3D;
import  org.jfree.chart.urls.StandardPieURLGenerator;
import  org.jfree.data.general.DefaultPieDataset;

/** */ /**
 * 
 * 
@author zdw
 * 
 
*/

@SuppressWarnings(
" serial " )
public   class  ChartServlet  extends  HttpServlet
{
    
public ChartServlet()
    
{
        
super();
    }


    
public void init() throws ServletException
    
{
    }


    
private void createPieChart(String path) throws IOException
    
{
        
// 饼图数据源
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue(
"中国"44.4);
        dataset.setValue(
"美国"25.5);
        dataset.setValue(
"日本"18.5);
        dataset.setValue(
"刚果"4.6);
        dataset.setValue(
"英国"5.5);
        dataset.setValue(
"其他"0.5);
        
// 生成3D饼图
        PiePlot3D plot = new PiePlot3D(dataset);
        plot.setToolTipGenerator(
new StandardPieToolTipGenerator());
        
// 设置图片链接
        plot.setURLGenerator(new StandardPieURLGenerator(
                
"http://www.blogjava.net/supercrsky"));

        JFreeChart chart 
= new JFreeChart("人口统计表",
                JFreeChart.DEFAULT_TITLE_FONT, plot, 
true);
        chart.setBackgroundPaint(Color.gray);
        ChartUtilities.saveChartAsJPEG(
new File(path), chart, 800600);
    }


    
public void doGet(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException
    
{
        System.out.println(
"get begin");
        
// 得到当前网站在服务上的绝对路径
        String path = request.getSession().getServletContext().getRealPath("/");
        
// 保存到根目录
        createPieChart(path + "/chart.jpg");
        request.getRequestDispatcher(
"/index.jsp").forward(request, response);
    }


    
public void doPost(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException
    
{
        System.out.println(
"post begin");
        
this.doGet(request, response);
    }


    
public void destroy()
    
{
        
super.destroy();
    }


}


web.xml:
<? xml version="1.0" encoding="UTF-8" ?>
< web-app  version ="2.4"  
    xmlns
="http://java.sun.com/xml/ns/j2ee"  
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>
  
< servlet >
    
< servlet-name > ChartServlet </ servlet-name >
    
< servlet-class > com.ChartServlet </ servlet-class >
  
</ servlet >

  
< servlet-mapping >
    
< servlet-name > ChartServlet </ servlet-name >
    
< url-pattern > /servlet/ChartServlet </ url-pattern >
  
</ servlet-mapping >
  
< welcome-file-list >
    
< welcome-file > index.jsp </ welcome-file >
  
</ welcome-file-list >
</ web-app >

index.jsp:
<% @ page language="java" pageEncoding="gbk" %>
< html >
    
< head >
        
< title > 人口统计表 </ title >
    
</ head >
    
< body >
        
< hr  />
        
< img  src ="/jfreechart/chart.jpg"   />
    
</ body >
</ html >
在浏览器中输入:
http://localhost:8888/jfreechart/servlet/ChartServlet
就可以看生成的饼图效果了.

源码可以 点此下载 199551.html

々上善若水々 2008-05-09 16:45 发表评论
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值