JFreeChar_03

将生成的图表移到浏览器上

为了将生成的图表直接传给客户端浏览器,只需要将前面两个例子中的文件流换成是通过HttpServletResponse对象获取到的,这里就是用简单的sevlet,
输出流,详细代码清单如下:

/**
 * 柱状图
 */
package com.ll.JFreeChat.Demo1;

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

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
* 该类用于演示最简单的柱状图生成
* @author Mr liu
*/
public class ZhuZhuang extends HttpServlet{
    
    
    private static final long serialVersionUID = 1L;
    public void doGet(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException{
        
        res.setContentType("image/jpeg");
    CategoryDataset dataset=getDataSet();
    JFreeChart chart=ChartFactory.createBarChart3D(
            "水果产量图",//图标标题
            "水果",//目录轴的显示标签
            "产量",//数值轴的显示标签
            dataset,//数据集
            PlotOrientation.VERTICAL,//图表的方向:水平、垂直
            true,//是否显示图例(对于简单的柱状图必须是false)
            false,//是否生成工具
            false//是否生成URL链接
            );
    ChartUtilities.writeChartAsJPEG(res.getOutputStream(),
            1.0f,chart,400,300,null);    
}

/**
 * 获取一个演示用的简单数据集对象
 * @return
 */
public CategoryDataset getDataSet(){
    DefaultCategoryDataset dataset=new DefaultCategoryDataset();
    dataset.addValue(100, "", "苹果");
    dataset.addValue(200, "", "梨子");
    dataset.addValue(300, "", "葡萄");
    dataset.addValue(400, "", "香蕉");
    dataset.addValue(500, "", "栗子");
    return dataset;
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
this.doGet(request, response);
}
}
web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>ZhuZhuang</servlet-name>
    <servlet-class>com.ll.JFreeChat.Demo1.ZhuZhuang</servlet-class>
  </servlet>

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


index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="ZhuZhuang">获取图片</a>

</body>
</html>

然后图片就会立刻出现,没大问题

你可能感兴趣的:(JFreeChar_03)