⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jbarservlet.java

📁 提供 精通Java Web动态图表编程 一书的源代码, 大家赶快下载呀
💻 JAVA
字号:
// Fig. 8.3_01:  JBarServlet.java
// JFreeChart实例25: 利用JFreeChart编写的生成直方图的Servlet

package com.fatcat.webchart;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.awt.image.*;
import java.awt.*;
import javax.imageio.*;

import org.jfree.chart.*;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.entity.StandardEntityCollection;

public class JBarServlet extends HttpServlet
{
    // 创建一个 500X375 的图像
    int width = 500, height = 375;

    String chartTitle = "JFreeChart实例25: 垂直直方图Servlet版";

    String bookTitle[] =
    {
        "Python", "JAVA", "C#", "Perl", "PHP"
    };
    String category[] =
    {
        "第1周", "第2周", "第3周", "第4周"
    };


    // 处理 HTTP get 请求
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        // 清空缓冲区
        response.reset();

        // 注意这里的MIME类型
        response.setContentType("image/png");

        // 创建数据集
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        int bookSales = 0;

        for (int i = 0; i < bookTitle.length; i++)
        {
            for (int j = 0; j < category.length; j++)
            {
                bookSales = 1+(int)(Math.random() * 100);
                dataset.addValue(bookSales, bookTitle[i], category[j]);
            }
        }

        // 创建图表
        JFreeChart chart = ChartFactory.createBarChart(chartTitle,  // 图表标题
        "销售时间:2005年2月",  // 坐标标题
        "销售量",  // 坐标标题
        dataset,  // 定义绘制数据
        PlotOrientation.VERTICAL,  // 直方图的方向
        true,  // 定义图表是否包含图例
        true,  // 定义图表是否包含提示
        false  // 定义图表是否包含URL
        );

        ServletOutputStream out = response.getOutputStream();
        // 利用ChartUtilities类的writeChartAsPNG方法对图像进行编码
        ChartUtilities.writeChartAsPNG(out, chart, width, height);
    }

    // 处理 HTTP post 请求, 和doGet一样
    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        doGet(request, response);
    }
}


/**************************************************************************
 * (C) Copyright 2004-2005 by Jingkui Zhong(钟京馗) and Huan Tang(唐桓).  *
 * All Rights Reserved.                                                   *
 *                                                                        *
 * DISCLAIMER: The authors of this code have used their                   *
 * best efforts in preparing the code. These efforts include the          *
 * development, research, and testing of the theories and programs        *
 * to determine their effectiveness. The authors and publisher make       *
 * no warranty of any kind, expressed or implied, with regard to these    *
 * programs or to the documentation contained in these codes. The authors *
 * shall not be liable in any event for incidental or consequential       *
 * damages in connection with, or arising out of, the furnishing,         *
 * performance, or use of these programs.                                 *
  **************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -