java 從零開始,學習筆記之基礎入門(四十四)

統計圖(JFreeChart)

JSP內嵌代碼單獨實現

(在web.xml配置如下代碼)

  <servlet>

    <servlet-name>DisplayChart</servlet-name>

    <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>

  </servlet>

  <servlet-mapping>

    <servlet-name>DisplayChart</servlet-name>

    <url-pattern>/servlet/DisplayChart</url-pattern>

  </servlet-mapping>

 

 

Index.jsp

<%@ page language="java"import="java.util.*"pageEncoding="utf-8"%>

<%@pageimport="org.jfree.data.category.*"%>

<%@pageimport="org.jfree.chart.JFreeChart"%>

<%@pageimport="org.jfree.chart.ChartFactory"%>

<%@pageimport="org.jfree.chart.plot.PlotOrientation"%>

<%@pageimport="org.jfree.chart.servlet.ServletUtilities"%>

<%@pageimport="org.jfree.chart.title.TextTitle"%>

<%@pageimport="java.awt.Font"%>

<%@pageimport="org.jfree.chart.axis.CategoryAxis"%>

<%@pageimport="org.jfree.chart.plot.CategoryPlot"%>

<%@pageimport="org.jfree.chart.axis.ValueAxis"%>

<%

    //  request.setCharacterEncoding("utf-8");

//  response.setCharacterEncoding("utf-8");

//

//  //數據結果集

//  DefaultCategoryDataset dataset = new DefaultCategoryDataset();

//  dataset.addValue(200,"廣州","蘋果");

//  dataset.addValue(100,"廣州","梨子");

// 

//  //創建JFreeChart

//  JFreeChart chart = ChartFactory.createBarChart("水果銷售統計圖","水果","銷售",dataset,PlotOrientation.VERTICAL,false,false,false);

// 

//  //生成一張圖片,以供頁面中的img標籤使用

//  String pngName=ServletUtilities.saveChartAsPNG(chart,500,300,null,session);   

//  System.out.println("圖片名稱"+pngName);

//  String grapgURL=request.getContextPath()+"/servlet/DisplayChart?filename="+pngName;

   

   

    //第二種寫法 

       //注意在web.xml文件中進行配置

       DefaultCategoryDataset dataset = new DefaultCategoryDataset();

       dataset.addValue(300, "廣州","蘋果");

       dataset.addValue(200, "廣州","梨子");

       dataset.addValue(500, "廣州","葡萄");

       dataset.addValue(340, "廣州","芒果");

       dataset.addValue(280, "廣州","荔枝");

 

       JFreeChart chart = ChartFactory.createBarChart3D("水果銷量統計圖",

                         "水果",//x軸名稱

                         "銷量",//y軸名稱

                         dataset, // 繪圖數據集

                         PlotOrientation.VERTICAL, // 柱形圖繪製方向

                         false,// 顯示圖例

                         false,// 顯示圖例名稱

                         false);// 生成鏈接

                        

       TextTitle textTitle=chart.getTitle();

       textTitle.setFont(new Font("黑體",Font.PLAIN,20));

 

       //對圖像區域的字體進行設置

       CategoryPlot plot=chart.getCategoryPlot();

       //橫向圖像與對象   

       CategoryAxis domainAxis=plot.getDomainAxis();

       domainAxis.setVisible(true);

       //plot.setDomainAxes(domainAxis);

       //橫向座標

       domainAxis.setTickLabelFont(new Font("黑體",Font.PLAIN,15));    //橫向標題

       domainAxis.setLabelFont(new Font("黑體",Font.PLAIN,20));

       //縱向圖像域對象

       ValueAxis rAxis=plot.getRangeAxis();     

       //縱向座標

       rAxis.setTickLabelFont(new Font("黑體",Font.PLAIN,20));

       //縱向標題

       rAxis.setLabelFont(new Font("黑體",Font.PLAIN,15));

       String filename =ServletUtilities.saveChartAsJPEG(chart, 500, 300,null, session);

        //String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, session);

       String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;

   

%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

   

    <title>My JSP 'index.jsp' starting page</title>

    <metahttp-equiv="pragma"content="no-cache">

    <metahttp-equiv="cache-control"content="no-cache">

    <metahttp-equiv="expires"content="0">   

    <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

    <metahttp-equiv="description"content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css"href="styles.css">

    -->

  </head>

 

  <body>

    This is my JSP page. <br>

    <imgsrc="<%=graphURL%>"/>

  </body>

</html>

 

 

 

Servlet實現:

Servlet中的代碼

ChartAction.java

package com.ibm.action;

 

import java.awt.Font;

import java.io.IOException;

import java.io.PrintWriter;

 

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.axis.CategoryAxis;

import org.jfree.chart.axis.ValueAxis;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.chart.title.TextTitle;

import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.data.general.DefaultPieDataset;

 

public class ChartAction extends HttpServlet {

 

    public void doGet(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

 

       response.setContentType("text/html");

       PrintWriter out = response.getWriter();

       out

              .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

       out.println("<HTML>");

       out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");

       out.println("  <BODY>");

       out.print("    This is ");

       out.print(this.getClass());

       out.println(", using the GET method");

       out.println("  </BODY>");

       out.println("</HTML>");

       out.flush();

       out.close();

    }

 

    public void doPost(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

 

       response.setContentType("text/html");

       PrintWriter out = response.getWriter();

       out

              .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");

       out.println("<HTML>");

       out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");

       out.println("  <BODY>");

       out.print("    This is ");

       out.print(this.getClass());

       out.println(", using the POST method");

       out.println("  </BODY>");

       out.println("</HTML>");

       out.flush();

       out.close();

    }

   

    @Override

    protected void service(HttpServletRequest request, HttpServletResponse response)

           throws ServletException, IOException {

           response.setContentType("image/jpeg");

           //指定數據結果集對象

           DefaultCategoryDataset dataset = getDataSet();

          

           //利用ChartFactory創建一個圖標對象 並指定默認格式

           JFreeChart chart = ChartFactory.createBarChart3D("水果銷量統計圖",

                      "水果",//x軸名稱

                      "銷量",//y軸名稱

                      dataset, // 繪圖數據集

                      PlotOrientation.VERTICAL,// 柱形圖繪製方向

                      false,// 顯示圖例

                      false,// 顯示圖例名稱

                      false);// 生成鏈接

          

           TextTitle textTitle=chart.getTitle();

           textTitle.setFont(new Font("黑體",Font.PLAIN,20));

          

           //對圖像區域的字體進行設置

           CategoryPlot plot=chart.getCategoryPlot();

           //橫向圖像與對象   

           CategoryAxis domainAxis=plot.getDomainAxis();

           domainAxis.setVisible(true);

           //plot.setDomainAxes(domainAxis);

           //橫向座標

           domainAxis.setTickLabelFont(new Font("黑體",Font.PLAIN,15));      //橫向標題

           domainAxis.setLabelFont(new Font("黑體",Font.PLAIN,20));

           //縱向圖像域對象

           ValueAxis rAxis=plot.getRangeAxis();     

           //縱向座標

           rAxis.setTickLabelFont(new Font("黑體",Font.PLAIN,20));

           //縱向標題

           rAxis.setLabelFont(new Font("黑體",Font.PLAIN,15));

          

           //創建一張圖片

           //圖片流數據最終將在客戶端img標籤中被捕獲到

           ChartUtilities.writeChartAsJPEG(response.getOutputStream(), 0.99f,

           chart, 600, 450, null);

    }

   

    /**

     * * 獲取一個簡單數據集對象 *

     */

    private static DefaultCategoryDataset getDataSet() {

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    dataset.addValue(300, "廣州","蘋果");

    dataset.addValue(200, "廣州","梨子");

    dataset.addValue(500, "廣州","葡萄");

    dataset.addValue(340, "廣州","芒果");

    dataset.addValue(280, "廣州","荔枝");

    return dataset;

    }

}

 

頁面部分

<%@ page language="java"import="java.util.*"pageEncoding="utf-8"%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

   

    <title>My JSP 'index.jsp' starting page</title>

    <metahttp-equiv="pragma"content="no-cache">

    <metahttp-equiv="cache-control"content="no-cache">

    <metahttp-equiv="expires"content="0">   

    <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

    <metahttp-equiv="description"content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css"href="styles.css">

    -->

  </head>

 

  <body>

    This is my JSP page. <br>

    <imgsrc="ChartAction"/>

  </body>

</html>

 

 

 

Struts實現:

Web.xml

Web.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appversion="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">

  <welcome-file-list>

    <welcome-file>index.jsp</welcome-file>

  </welcome-file-list>

 

  <servlet>

    <servlet-name>DisplayChart</servlet-name>

    <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>

  </servlet>

  <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>ChartAction</servlet-name>

    <servlet-class>com.ibm.action.ChartAction</servlet-class>

  </servlet>

 

 

  <servlet-mapping>

    <servlet-name>DisplayChart</servlet-name>

    <url-pattern>/servlet/DisplayChart</url-pattern>

  </servlet-mapping>

  <servlet-mapping>

    <servlet-name>ChartAction</servlet-name>

    <url-pattern>/ChartAction</url-pattern>

  </servlet-mapping>

 

  <filter>

    <filter-name>filterDispatcher</filter-name>

    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>

  </filter>

 

  <filter-mapping>

    <filter-name>filterDispatcher</filter-name>

    <url-pattern>/*</url-pattern>

  </filter-mapping>

 

</web-app>

 

 

Struts.xml

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEstruts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <packagename="chartpackage"extends="struts-default">

    <!-- 視圖類型聲明 -->

    <result-types>

        <result-typename="chart"class="org.apache.struts2.dispatcher.ChartResult"></result-type>

     </result-types>

   

    <actionname="chart"class="com.ibm.action.ChartOperAction">

    <!-- 以圖表的方式展示結果 -->

        <resultname="success"type="chart">

            <paramname="width">400</param>

            <paramname="height">300</param>

        </result>         

    </action>

    </package>

</struts>

 

 

ChartOperAction.java

package com.ibm.action;

 

import java.awt.Font;

 

import org.jfree.chart.ChartFactory;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.CategoryAxis;

import org.jfree.chart.axis.ValueAxis;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.plot.PiePlot;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.chart.title.TextTitle;

import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.data.general.DefaultPieDataset;

 

public class ChartOperAction {

   

    //注意名稱

    JFreeChart chart;

   

    public String execute(){

    //指定數據結果集對象

       DefaultCategoryDataset dataset = getDataSet();

   

    //利用ChartFactory創建一個圖標對象(創建餅狀圖)

//  chart=new JFreeChart("Moderation Function",JFreeChart.DEFAULT_TITLE_FONT,new PiePlot(data),false);

 

      

    //利用ChartFactory創建一個圖標對象 並指定默認格式(創建柱狀圖)

    chart = ChartFactory.createBarChart3D("水果銷量統計圖",

                  "水果",//x軸名稱

                  "銷量",//y軸名稱

                  dataset, // 繪圖數據集

                  PlotOrientation.VERTICAL,// 柱形圖繪製方向

                  false,// 顯示圖例

                  false,// 顯示圖例名稱

                  false);// 生成鏈接    

   

    TextTitle textTitle=chart.getTitle();

    textTitle.setFont(new Font("黑體",Font.PLAIN,20));

   

    //對圖像區域的字體進行設置

    CategoryPlot plot=chart.getCategoryPlot();

    //橫向圖像與對象   

    CategoryAxis domainAxis=plot.getDomainAxis();

    domainAxis.setVisible(true);

    //plot.setDomainAxes(domainAxis);

    //橫向座標

    domainAxis.setTickLabelFont(new Font("黑體",Font.PLAIN,15));   //橫向標題

    domainAxis.setLabelFont(new Font("黑體",Font.PLAIN,20));

    //縱向圖像域對象

    ValueAxis rAxis=plot.getRangeAxis();     

    //縱向座標

    rAxis.setTickLabelFont(new Font("黑體",Font.PLAIN,20));

    //縱向標題

    rAxis.setLabelFont(new Font("黑體",Font.PLAIN,15));

   

    return "success";

    }

   

    /**

     * * 獲取一個簡單數據集對象 *

     */

    private static DefaultCategoryDataset getDataSet() {

   

    //創建柱狀圖結果集

    DefaultCategoryDataset dataset = new DefaultCategoryDataset();

    dataset.addValue(300, "廣州","蘋果");

    dataset.addValue(200, "廣州","梨子");

    dataset.addValue(500, "廣州","葡萄");

    dataset.addValue(340, "廣州","芒果");

    dataset.addValue(280, "廣州","荔枝");

   

    //餅狀圖結果集

//  DefaultPieDataset dataset = new DefaultPieDataset();

//  dataset.setValue("蘋果", 100);

//  dataset.setValue("梨子", 200);

//  dataset.setValue("葡萄", 300);

//  dataset.setValue("香蕉", 400);

//  dataset.setValue("荔枝", 500);

    return dataset;

    }

 

    public JFreeChart getChart() {

       returnchart;

    }

 

}

 

頁面部分

<%@ page language="java"import="java.util.*"pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPEHTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <basehref="<%=basePath%>">

   

    <title>My JSP 'chartoperaction.jsp' starting page</title>

   

    <metahttp-equiv="pragma"content="no-cache">

    <metahttp-equiv="cache-control"content="no-cache">

    <metahttp-equiv="expires"content="0">   

    <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">

    <metahttp-equiv="description"content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css"href="styles.css">

    -->

 

  </head>

 

  <body>

    <imgsrc="chart.do"/>

  </body>

</html>

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章