我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

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
就可以看生成的饼图效果了.

源码可以点此下载

posted on 2008-05-09 16:45 々上善若水々 阅读(3297) 评论(0)  编辑  收藏 所属分类: opensource


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


网站导航: