我的漫漫程序之旅

          專注于JavaWeb開發(fā)
          隨筆 - 39, 文章 - 310, 評論 - 411, 引用 - 0
          數(shù)據(jù)加載中……

          JFreeChart在Web中的 應(yīng)用

          把圖表數(shù)據(jù)顯示的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
              
          {
                  
          // 餅圖數(shù)據(jù)源
                  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());
                  
          // 設(shè)置圖片鏈接
                  plot.setURLGenerator(new StandardPieURLGenerator(
                          
          "http://www.aygfsteel.com/supercrsky"));

                  JFreeChart chart 
          = new JFreeChart("人口統(tǒng)計表",
                          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");
                  
          // 得到當(dāng)前網(wǎng)站在服務(wù)上的絕對路徑
                  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>人口統(tǒng)計表</title>
              
          </head>
              
          <body>
                  
          <hr />
                  
          <img src="/jfreechart/chart.jpg" />
              
          </body>
          </html>
          在瀏覽器中輸入:
          http://localhost:8888/jfreechart/servlet/ChartServlet
          就可以看生成的餅圖效果了.

          源碼可以點(diǎn)此下載

          posted on 2008-05-09 16:45 々上善若水々 閱讀(3317) 評論(0)  編輯  收藏 所屬分類: opensource

          主站蜘蛛池模板: 上蔡县| 永安市| 忻州市| 行唐县| 肇州县| 二手房| 合阳县| 兰西县| 黑水县| 临江市| 沙坪坝区| 朝阳市| 东莞市| 秦安县| 永和县| 平武县| 堆龙德庆县| 霞浦县| 岳阳县| 潢川县| 吉安市| 商水县| 崇仁县| 张掖市| 腾冲县| 无为县| 怀安县| 蓝山县| 广德县| 名山县| 荆门市| 绥芬河市| 商都县| 仪陇县| 金寨县| 南岸区| 新晃| 扬州市| 乐昌市| 贺州市| 玛多县|