<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 120,  comments - 19,  trackbacks - 0

    自己最近學(xué)習(xí)了JfreeChart的使用,覺(jué)得這個(gè)冬冬的功能非常強(qiáng)大,總結(jié)一下。
    一、簡(jiǎn)單介紹一下
    ??? WW
    的發(fā)展使得基于因特網(wǎng)的應(yīng)用程序不再局限于靜態(tài)或者簡(jiǎn)單的動(dòng)態(tài)內(nèi)容提供。傳統(tǒng)的一些以軟件包形式發(fā)布應(yīng)用程序例如報(bào)表系統(tǒng)等都在逐漸搬到因特網(wǎng)上。但是這兩者之間有著天壤之別,雖然對(duì)于數(shù)據(jù)獲取、業(yè)務(wù)處理等方面基本類似,但是最大的差別在于用戶界面。為了能在web瀏覽器上顯示要求用戶界面使用 HTML以及圖片的方式來(lái)展現(xiàn)數(shù)據(jù),而傳統(tǒng)的一些利用操作系統(tǒng)本身的控件來(lái)開(kāi)發(fā)的用戶界面無(wú)法適應(yīng)琳瑯滿目的客戶端,因此在這里也變得無(wú)能為力。回到本文的題目上來(lái),為了創(chuàng)建一個(gè)可以在web瀏覽器上查看到圖表一般有兩種做法:第一種就是使用applet利用java本身對(duì)圖形的支持來(lái)顯示一個(gè)圖表;第二 種就是直接在web服務(wù)器端生成好圖表圖片文件后發(fā)送給瀏覽器。第一種方式顯然對(duì)于客戶端要求太高,隨著現(xiàn)在主流瀏覽器放棄對(duì)JAVA的支持后,這種方式 只適合一些局域網(wǎng)的應(yīng)用,而對(duì)于因特網(wǎng)的環(huán)境就顯得不太適合。因此我們下面將介紹一個(gè)JAVA的圖表引擎JFreeChart用來(lái)產(chǎn)生基于WEB的圖表。
    ??? JFreeChart
    項(xiàng)目簡(jiǎn)介 JFreeChart是開(kāi)放源代碼站點(diǎn)SourceForge.net上的一個(gè)JAVA項(xiàng)目,它主要用來(lái)各種各樣的圖表,這些圖表包括:餅圖、柱狀圖(普 通柱狀圖以及堆棧柱狀圖)、線圖、區(qū)域圖、分布圖、混合圖、甘特圖以及一些儀表盤(pán)等等。這些不同式樣的圖表基本上可以滿足目前的要求。

    二、JFreeChart獲取。
    ???????? JFreeChart JFreeChart公司在開(kāi)源網(wǎng)站SourceForge.net上的一個(gè)項(xiàng)目,該公司的主要產(chǎn)品有如下:
    ???????? 1.JFreeReport :報(bào)表解決工具
    ???????? 2.JFreeChart:Java 圖形解決方案Application/Applet/Servlet/Jsp
    ???????? 3.JCommon JFreeReportJFreeChart的公共類庫(kù)
    ???????? 4.JFreeDesigner JFreeReport的報(bào)表設(shè)計(jì)工具
    ???? 我們可以從jfree官方網(wǎng)站上獲取最新版本和相關(guān)資料(但是jfreedocument需要40美金才能獲取),
    ???? 獲取地址:http://www.jfree.org/jfreechart/index.html(同時(shí)可以獲得簡(jiǎn)明介紹)
    ???? 我們以當(dāng)前最新版本:jfreechart-1.0.1.zip為例子進(jìn)行說(shuō)明。

    三、JFreeChart配置安裝
    ???? 1 、解壓jfreechart-1.0.1.zip.zip到指定位置,其中sourcejfreechart的源碼,jfreechart-1.0.1-demo.jar ? 是例子程序,可以先運(yùn)行一下看看各種效果,就知道他的nb之處了。
    ??? 2
    、為了配置成功,我們需要關(guān)注的文件有如下三個(gè):
    ??? 設(shè)置classpath。加入下面三個(gè)jar包。
    ??? jfreechart-1.0.1.jar
    jcommon-1.0.0.jargnujaxp.jar
    ??? 加上第三個(gè)jar包有時(shí)web.xml會(huì)報(bào)錯(cuò),把它去掉就好了。
    ??????? 至此jfreechart的配置就完成了,下面就可以進(jìn)行jfreechart的開(kāi)發(fā)了。這里值得提出的是jfreechart的類
    ??????? 結(jié)構(gòu)設(shè)計(jì)前后兼容性不是很好,不同版本的jfreechart中類庫(kù)結(jié)構(gòu)可能不一樣,有時(shí)候可能需要查源碼。如果是中文顯示的時(shí)候可能依據(jù)觀感需要改變?cè)创a的字體。


    四、JFreeChart功能介紹
    ???? JFreeChart 目前是最好的java圖形解決方案,基本能夠解決目前的圖形方面的需求,主要包括如下幾個(gè)方面:
    ???? pie ? charts ? (2D ? and ? 3D) :餅圖(平面和立體)
    ???? bar ? charts ? (regular ? and ? stacked, ? with ? an ? optional ? 3D ? effect) :柱狀圖
    ???? line ? and ? area ? charts :曲線圖
    ???? scatter ? plots ? and ? bubble ? charts
    ???? time ? series, ? high/low/open/close ? charts ? and ? candle ? stick ? charts :時(shí)序圖
    ???? combination ? charts :復(fù)合圖
    ???? Pareto ? charts
    ???? Gantt ? charts :甘特圖
    ???? wind ? plots, ? meter ? charts ? and ? symbol ? charts
    ???? wafer ? map ? charts
    ???? ( 態(tài)圖表,餅圖(二維和三維) ? , ? 柱狀圖 ? ( 水平,垂直),線圖,點(diǎn)圖,時(shí)間變化圖,甘特圖, ? 股票行情圖,混和圖, ? 溫度計(jì)圖, ? 刻度圖等常用商用圖表)
    ???? 圖形可以導(dǎo)出成PNGJPEG格式,同時(shí)還可以與PDFEXCEL關(guān)聯(lián)
    ???? JFreeChart 核心類庫(kù)介紹:
    ???????????? 研究jfreechart源碼發(fā)現(xiàn)源碼的主要由兩個(gè)大的包組成:org.jfree.chart,org.jfree.data。其中前者主要與圖形
    ???? 本身有關(guān),后者與圖形顯示的數(shù)據(jù)有關(guān)。具體研究如果大家有興趣的話可以自己研究 ??
    ????????? 核心類主要有:
    ??????????? org.jfree.chart.JFreeChart :圖表對(duì)象,任何類型的圖表的最終表現(xiàn)形式都是在該對(duì)象進(jìn)行一些屬性的定制。JFreeChart引擎本身提供了一個(gè)工廠類用于創(chuàng)建不同類型的圖表對(duì)象
    ??????????? org.jfree.data.category.XXXDataSet: 數(shù)據(jù)集對(duì)象,用于提供顯示圖表所用的數(shù)據(jù)。根據(jù)不同類型的圖表對(duì)應(yīng)著很多類型的數(shù)據(jù)集對(duì)象類
    ??????????? org.jfree.chart.plot.XXXPlot :圖表區(qū)域?qū)ο螅旧线@個(gè)對(duì)象決定著什么樣式的圖表,創(chuàng)建該對(duì)象的時(shí)候需要AxisRenderer以及數(shù)據(jù)集對(duì)象的支持
    ??????????? org.jfree.chart.axis.XXXAxis :用于處理圖表的兩個(gè)軸:縱軸和橫軸
    ??????????? org.jfree.chart.render.XXXRender :負(fù)責(zé)如何顯示一個(gè)圖表對(duì)象
    ??????????? org.jfree.chart.urls.XXXURLGenerator: 用于生成Web圖表中每個(gè)項(xiàng)目的鼠標(biāo)點(diǎn)擊鏈接
    ??????????? XXXXXToolTipGenerator: 用于生成圖象的幫助提示,不同類型圖表對(duì)應(yīng)不同類型的工具提示類

    ??? 個(gè)人感覺(jué)JFreeChart可以滿足大部分圖片創(chuàng)建的需要,美中不足的是:對(duì)字體的設(shè)置做的不夠好,特別是使用中文的時(shí)候字體很不清晰。因?yàn)檫@個(gè)原因建議你自己去修改他的源代碼
    最好使用properties文件去設(shè)置字體.還有就是文檔要錢(qián)所以要多花點(diǎn)時(shí)間去看源代碼。或多上社區(qū).

    五.開(kāi)始開(kāi)發(fā)
    ??? 對(duì)JfreeChart有了初步了解并做好準(zhǔn)備工作之后,開(kāi)始作例子程序試驗(yàn)。在這里我只介紹餅圖的做法,而這張圖采用不同的方式進(jìn)行輸出,其他類型的圖片可以參考jfreechart提供的例子,做法都差不多。

    1) 直接生成圖片
    package com.jfreechart.picture.example;
    import java.awt.Color;
    import java.awt.Font;
    import java.io.FileOutputStream;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
    import org.jfree.chart.plot.PiePlot3D;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.title.TextTitle;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.general.DefaultPieDataset;
    import org.jfree.data.general.PieDataset;

    /**

    * 該類用于演示餅圖生成,生成圖片

    */
    public class PieChartPicture {

    public static void main(String[] args)

    ??? {
    ??? ? PieDataset dataset = getDataSet();
    ??? ? JFreeChart chart = ChartFactory.createPieChart3D(
    ?? ? ? "
    項(xiàng)目進(jìn)度分布",? // chart title
    ?? ??? ???
    dataset,??? ?????????// data
    ??? ??? ??? true,?????????????? // include legend
    ??? ??? ? ? true,
    ??? ??? ??? false
    ??? ??? ??? );
    ??? PiePlot3D? plot=(PiePlot3D)chart.getPlot();
    ??? //
    圖片中顯示百分比:默認(rèn)方式
    ??? //plot.setLabelGenerator(new ??? ????? StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
    // 圖片中顯示百分比:自定義方式,{0} 表示選項(xiàng), {1} 表示數(shù)值, {2} 表示所占比例 ,小數(shù)點(diǎn)后兩位
    ??? plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));

    // 圖例顯示百分比:自定義方式, {0} 表示選項(xiàng), {1} 表示數(shù)值, {2} 表示所占比例??? ??? ???? ??? plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));

    // 設(shè)置背景色為白色

    ?????? chart.setBackgroundPaint(Color.white);

    ?????? // 指定圖片的透明度(0.0-1.0)

    ?????? ?plot.setForegroundAlpha(1.0f);

    ?????? ?// 指定顯示的餅圖上圓形(false)還橢圓形(true)

    ?????? ?plot.setCircular(true);

    ?????? ?// 設(shè)置圖標(biāo)題的字體

    ?????? Font font = new Font(" 黑體",Font.CENTER_BASELINE,20);

    ?????? TextTitle title = new TextTitle(" 項(xiàng)目狀態(tài)分布");

    ?????? title.setFont(font);?

    ?????? chart.setTitle(title);

    ?????? FileOutputStream fos_jpg = null;

    ??? ??? try {

    ?????????? fos_jpg=new FileOutputStream("D:\\ 項(xiàng)目狀態(tài)分布.jpg");

    ?????????? ChartUtilities.writeChartAsJPEG(fos_jpg,100,chart,640,480,null);

    ?????????? fos_jpg.close();

    ?????? } catch (Exception e) {

    ?????????? // TODO: handle exception

    ?????? }

    ?}

    ?private static PieDataset getDataSet() {

    ??? ? ?????? DefaultPieDataset dataset = new DefaultPieDataset();

    ??? ??????? dataset.setValue(" 市場(chǎng)前期", new Double(10));

    ??? ??????? dataset.setValue(" 立項(xiàng)", new Double(15));

    ??? ??????? dataset.setValue(" 計(jì)劃", new Double(10));

    ??? ??????? dataset.setValue(" 需求與設(shè)計(jì)", new Double(10));

    ??? ??????? dataset.setValue(" 執(zhí)行控制", new Double(35));

    ??? ??????? dataset.setValue(" 收尾", new Double(10));

    ??? ??????? dataset.setValue(" 運(yùn)維",new Double(10));

    ??? ??????? return dataset;???????

    ??? ??? }
    }

    2) 采用servletstrutsaction方式輸出

    采用這種方式輸出,不用生成圖片。

    A servlet輸出

    package com.jfreechart.servlet.example;

    import java.io.IOException;

    import java.awt.Color;

    import java.awt.Font;

    import java.text.NumberFormat;

    import java.text.DecimalFormat;

    import javax.servlet.ServletException;

    import javax.servlet.ServletRequest;

    import javax.servlet.ServletResponse;

    import javax.servlet.http.HttpServlet;

    import org.jfree.chart.title.TextTitle;

    import org.jfree.chart.ChartFactory;

    import org.jfree.chart.ChartUtilities;

    import org.jfree.chart.JFreeChart;

    import org.jfree.data.general.DefaultPieDataset;

    import org.jfree.data.general.PieDataset;

    import org.jfree.chart.plot.*;

    import org.jfree.chart.labels.StandardPieSectionLabelGenerator;

    /**

    * 演示通過(guò)servlet直接輸出餅圖

    */

    public class PieByServlet extends HttpServlet{

    public void service(ServletRequest req, ServletResponse res)

    ??? throws ServletException, IOException

    ??? {

    ?????? res.setContentType("image/jpeg");

    ?????? PieDataset dataset = getDataSet();

    ?????? JFreeChart chart = ChartFactory.createPieChart3D(

    ??? ??????????? " 水果餅圖",? // chart title

    ??? ???????? ???dataset,???????????? // data

    ??? ??????????? true,?????????????? // include legend

    ??? ??????????? true,

    ??? ??????????? false

    ??? ??????? );

    ?????? PiePlot3D? plot=(PiePlot3D)chart.getPlot();?

    ?????? // 圖片中顯示百分比:默認(rèn)方式

    ?????? //plot.setLabelGenerator(new StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));

    // 圖片中顯示百分比:自定義方式,{0} 表示選項(xiàng), {1} 表示數(shù)值, {2} 表示所占比例 ,小數(shù)點(diǎn)后兩位

    ???? plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));

    // 圖例顯示百分比:自定義方式, {0} 表示選項(xiàng), {1} 表示數(shù)值, {2} 表示所占比例

    ????? plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));

    ?// 設(shè)置背景色為白色

    ?????? chart.setBackgroundPaint(Color.white);

    ?????? // 指定圖片的透明度(0.0-1.0)

    ?????? ?plot.setForegroundAlpha(1.0f);

    ?????? ?// 指定顯示的餅圖上圓形(false)還橢圓形(true)

    ?????? ?plot.setCircular(true);

    ?????? ?// 設(shè)置圖標(biāo)題的字體

    ?????? Font font = new Font(" 黑體",Font.CENTER_BASELINE,20);

    ?????? TextTitle title = new TextTitle(" 項(xiàng)目狀態(tài)分布");

    ?????? title.setFont(font);

    ??? ??? chart.setTitle(title);

    ?????? // 輸出圖片

    ?????? ChartUtilities.writeChartAsJPEG(res.getOutputStream(),100,chart,800,600,null);

    ??? }

    ??? /**

    ??? * 獲取一個(gè)演示用的簡(jiǎn)單數(shù)據(jù)集對(duì)象

    ??? * @return

    ??? */

    ??? private static DefaultPieDataset getDataSet() {

    ?????? DefaultPieDataset dataset = new DefaultPieDataset();

    ??? ??????? dataset.setValue(" 市場(chǎng)前期", new Double(10));

    ??? ??????? dataset.setValue(" 立項(xiàng)", new Double(15));

    ??? ??????? dataset.setValue(" 計(jì)劃", new Double(10));

    ??? ??????? dataset.setValue(" 需求與設(shè)計(jì)", new Double(10));

    ??? ??????? dataset.setValue(" 執(zhí)行控制", new Double(35));

    ??? ??????? dataset.setValue(" 收尾", new Double(10));

    ??? ??????? dataset.setValue(" 運(yùn)維",new Double(10));

    ??? ??????? return dataset;???????
    ??? }
    }

    B strutsaction方式輸出

    只將這條語(yǔ)句加上try catch即可,并返回null

    try{?????? ChartUtilities.writeChartAsJPEG(response.getOutputStream(),100,chart,800,600,null);

    ??? } catch (Exception e) {

    ????????????? // TODO: handle exception

    ??? }
    return null;

    其實(shí)采用這兩種方式與生成圖片的方式改動(dòng)并不大

    加上語(yǔ)句response.setContentType("image/jpeg");

    ChartUtilities.writeChartAsJPEG(new FileOutputStream("D:\\ 項(xiàng)目狀態(tài)分布.jpg");,100,chart,640,480,null);

    文件流改成response的輸出流就可以了

    ChartUtilities.writeChartAsJPEG(response.getOutputStream(),100,chart,800,600,null);

    ?

    ?3)jspservletjavabean方式

    1. Create ChartViewer servlet (ChartViewer.java)

    Code:

    /**

    ?* Need to produce some chart prior to this action call in a Java bean

    ?* Need a session attribute named "chartImage";

    ?*/

    package myapp.webwork.servlets;

    import java.io.*;

    import java.awt.image.*;

    import javax.servlet.*;

    import javax.servlet.http.*;

    import com.keypoint.PngEncoder;

    public class ChartViewer extends HttpServlet {

    ?public void init() throws ServletException {

    ? }

    ?
    //Process the HTTP Get request

    ? public void doGet(HttpServletRequest request, HttpServletResponse response)

    ???????? throws ServletException, IOException {

    ? // get the chart from session

    ?? HttpSession session = request.getSession();

    ?? BufferedImage chartImage = (BufferedImage) session.getAttribute("chartImage");

    // set the content type so the browser can see this as a picture

    ?? response.setContentType("image/png");

    // send the picture

    ?? PngEncoder encoder = new PngEncoder(chartImage, false, 0, 9);

    ?? response.getOutputStream().write(encoder.pngEncode());

    }

    //Process the HTTP Post request

    ? public void doPost(HttpServletRequest request, HttpServletResponse response)

    ???????? throws ServletException, IOException {

    ??? doGet(request, response);

    ? }

    //Process the HTTP Put request

    ? public void doPut(HttpServletRequest request, HttpServletResponse response)

    ???????? throws ServletException, IOException {

    ? }

    //Clean up resources

    ? public void destroy() {

    ? }

    }

    ?
    2. Compile and store the servlet under WEB-INF/classes

    <appname>WEB-INF/classes/myapp/webwork/servlets

    3. Create a servlet map in web.xml

    Code:

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE web-app

    ??? PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"

    ??? "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

    <web-app>

    ? <servlet>

    ??? <servlet-name>ChartViewer</servlet-name>

    ??? <servlet-class>myapp.webwork.servlets.ChartViewer</servlet-class>

    ? </servlet>

    ? <servlet-mapping>

    ??? <servlet-name>ChartViewer</servlet-name>

    ??? <url-pattern>/servlet/ChartViewer</url-pattern>

    ? </servlet-mapping>

    </web-app>

    4. Create a chart in a java bean (Pie3DDemo.java)

    Code:

    /**

    ?*

    ?* JFreeChart version 0.9.20

    ?* Called by Pie3DDemo.jsp

    ?*

    ?*/

    package myapp.webwork.beans;

    import java.io.*;

    import java.awt.*;

    import java.util.*;

    import java.awt.image.*;

    import org.jfree.data.*;

    import org.jfree.chart.*;

    import org.jfree.chart.plot.*;

    import org.jfree.chart.urls.*;

    import org.jfree.chart.entity.*;

    import javax.servlet.http.*;

    public class Pie3DDemo {

    ?public Pie3DDemo() {

    ? }

    private DefaultPieDataset getDataset() {

    ??? // categories...

    ??? String[] section = new String[] {

    ????? "Jan","Feb","Mar","Apr","May","Jun",

    ????? "Jul","Aug","Sep","Oct","Nov","Dec"

    ?? };

    ?// data...

    ??? double[] data = new double[section.length];

    ??? for (int i = 0; i < data.length; i++) {

    ??????? data[i] = 10 + (Math.random() * 10);

    ??? }

    // create the dataset...

    ??? DefaultPieDataset dataset = new DefaultPieDataset();

    ??? for (int i = 0; i < data.length; i++) {

    ??????? dataset.setValue(section[i], data[i]);

    ??? }

    ??? return dataset;

    ? }

    ?public String getChartViewer(HttpServletRequest request, HttpServletResponse response) {

    ??? DefaultPieDataset dataset = getDataset();

    ??? // create the chart...

    ??? JFreeChart chart = ChartFactory.createPie3DChart(

    ????????? "Pie3D Chart Demo",? // chart title

    ????????? dataset,???????????? // data

    ????????? true,??????????????? // include legend

    ????????? true,

    ????????? false

    ??? );

    ? // set the background color for the chart...

    ??? chart.setBackgroundPaint(Color.cyan);

    ??? PiePlot plot = (PiePlot) chart.getPlot();

    ??? plot.setNoDataMessage("No data available");

    ?// set drilldown capability...

    ??? plot.setURLGenerator(new StandardPieURLGenerator("Bar3DDemo.jsp","section"));

    ??? plot.setLabelGenerator(null);

    ? // OPTIONAL CUSTOMISATION COMPLETED.

    ? ChartRenderingInfo info = null;

    ??? HttpSession session = request.getSession();

    ??? try {

    ? //Create RenderingInfo object

    ????? response.setContentType("text/html");

    ????? info = new ChartRenderingInfo(new StandardEntityCollection());

    ????? BufferedImage chartImage = chart.createBufferedImage(640, 400, info);

    ? // putting chart as BufferedImage in session,

    ????? // thus making it available for the image reading action Action.

    ????? session.setAttribute("chartImage", chartImage);

    ???? PrintWriter writer = new PrintWriter(response.getWriter());

    ????? ChartUtilities.writeImageMap(writer, "imageMap", info);

    ????? writer.flush();

    ? }

    ??? catch (Exception e) {

    ?????? // handel your exception here

    ??? }

    ?String pathInfo = "http://";

    ??? pathInfo += request.getServerName();

    ??? int port = request.getServerPort();

    ??? pathInfo += ":"+String.valueOf(port);

    ?? ?pathInfo += request.getContextPath();

    ??? String chartViewer = pathInfo + "/servlet/ChartViewer";

    ??? return chartViewer;

    ? }

    }

    ?

    5. Compile and store the bean under WEB-INF/classes

    <appname>WEB-INF/classes/myapp/webwork/beans

    6. Create Pie3DDemo.jsp code

    Code:

    <html>

    <head>

    <title>Pie Chart Demo</title>

    </head>

    <jsp:useBeanid="myChart"scope="session"class="myapp.webwork.beans.Pie3DDemo" />

    <body>

    <h2>Pie Chart Demo</h2>

    <%String chartViewer = myChart.getChartViewer(request, response);%>

    <img src="<%=chartViewer%>" border=0 usemap="#imageMap">

    </body>

    </html>

    ?4)采用工具類,DisplayChart輸出
    我用了上面的幾個(gè)方法輸出圖片,發(fā)現(xiàn)頁(yè)面里只能輸出一個(gè)圖片
    不過(guò)下面的方法可以輸出多個(gè)圖片

    1 .添加工具類ChartUtil

    public class ChartUtil {

    //? 產(chǎn)生時(shí)間序列圖,返回圖片名稱

    ??? ?// 產(chǎn)生餅圖,返回圖片名稱

    ??? ? public? static String generatePieChart(DefaultPieDataset dataset,String title,int width,int height,HttpSession session, PrintWriter pw) {

    ?????????? String filename = null;

    ?????????? try {

    ????????????? if (session != null)

    ????????????? {

    ????????????????? ChartDeleter deleter = (ChartDeleter)session.getAttribute("JFreeChart_Deleter");

    ????????????????? session.removeAttribute("JFreeChart_Deleter");

    ????????????????? session.setAttribute("JFreeChart_Deleter", deleter);

    ????????????? }

    ????????????? ?JFreeChart chart = ChartFactory.createPieChart3D(

    ????????????? ????????? title,? // chart title

    ????????????? ????????? dataset,???????????? // data

    ????????????? ????????? true,??????????????? // include legend

    ????????????? ????????? true,

    ????????????? ??? ??????false

    ????????????? ??? );

    ? ? //? Write the chart image to the temporary directory

    ????????????? ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

    ????????????? //If the last parameter is null, the chart is a "one time"-chart and will be deleted after the first serving.

    ????????????? //If the last parameter is a session object, the chart remains until session time out.

    ????????????? filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);

    ? ? //? Write the image map to the PrintWriter

    ????????????? ChartUtilities.writeImageMap(pw, filename, info,true);

    ????????????? pw.flush();

    ?????????? } catch (Exception e) {

    ????????????? System.out.println("Exception - " + e.toString());

    ????????????? e.printStackTrace(System.out);

    ????????????? filename = "picture_error.png";

    ?????????? }

    ?????????? return filename;

    ?????? }

    }

    2 .在頁(yè)面里

    統(tǒng)計(jì)好dataset,傳到頁(yè)面。

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

    <%@ page import="com.jfreechart.util.ChartUtil"%>

    <%@ page import="org.jfree.data.general.DefaultPieDataset"%>

    <%@ page import = "java.io.PrintWriter" %>

    <%

    ??? DefaultPieDataset piedataset=(DefaultPieDataset)request.getAttribute("piedataset");

    %>

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

    <html>

    ? <head>

    ??? <base href="<%=basePath%>">

    ? ? <title>chartDemo.jsp</title>

    ??? <meta http-equiv="pragma" content="no-cache">

    ??? <meta http-equiv="cache-control" content="no-cache">

    ??? <meta http-equiv="expires" content="0">

    ??? <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    ??? <meta http-equiv="description" content="This is my page">

    ?? <!--

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

    ??? -->

    ? </head>

    <body>

    ??? <%

    ??? // ChartUtil工具類輸出

    ??? String p = ChartUtil.generatePieChart(piedataset," 項(xiàng)目收支線圖",500,300,null, new PrintWriter(out));

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

    ??? %>

    ??? <TABLE>

    ?????? <tr>

    ??? <td><img src="<%= p1 %>" width=500 height=300 border=0 usemap="#<%= p %>"></td>

    ??? </tr>

    ??? </TABLE>

    ? </body>

    </html>

    3 .在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>

    5) ApplicationFrame 方式

    package com.taiji.jfreechart.frame.example;

    import java.awt.Font;

    import javax.swing.JPanel;

    import org.jfree.chart.ChartFactory;

    import org.jfree.chart.ChartPanel;

    import org.jfree.chart.JFreeChart;

    import org.jfree.chart.plot.PiePlot;

    import org.jfree.data.general.DefaultPieDataset;

    import org.jfree.data.general.PieDataset;

    import org.jfree.ui.ApplicationFrame;

    import org.jfree.ui.RefineryUtilities;

    /**

    ?* A simple demonstration application showing how to create a pie chart using

    ?* data from a {@link DefaultPieDataset}.

    ?*/

    public class PieChartDemo1 extends ApplicationFrame {

    ? /**

    ???? * Default constructor.

    ???? *

    ???? * @param title? the frame title.

    ???? */

    ??? public PieChartDemo1(String title) {

    ??????? super(title);

    ??????? setContentPane(createDemoPanel());

    ??? }

    ? /**

    ???? * Creates a sample dataset.

    ???? *

    ???? * @return A sample dataset.

    ???? */

    ??? private static PieDataset createDataset() {

    ??????? DefaultPieDataset dataset = new DefaultPieDataset();

    ??????? dataset.setValue("One", new Double(43.2));

    ??????? dataset.setValue("Two", new Double(10.0));

    ??????? dataset.setValue("Three", new Double(27.5));

    ??????? dataset.setValue("Four", new Double(17.5));

    ??????? dataset.setValue("Five", new Double(11.0));

    ??????? dataset.setValue("Six", new Double(19.4));

    ??????? return dataset;???????

    ??? }

    ??/**

    ???? * Creates a chart.

    ???? *

    ???? * @param dataset? the dataset.

    ???? *

    ???? * @return A chart.

    ???? */

    ??? private static JFreeChart createChart(PieDataset dataset) {

    ??? JFreeChart chart = ChartFactory.createPieChart(

    ??????????? "Pie Chart Demo 1",? // chart title

    ??????????? dataset,????????? ???// data

    ??????????? true,?????????????? // include legend

    ??????????? true,

    ??????????? false

    ??????? );

    ? ? PiePlot plot = (PiePlot) chart.getPlot();

    ??????? plot.setSectionOutlinesVisible(false);

    ??????? plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));

    ??????? plot.setNoDataMessage("No data available");

    ??????? plot.setCircular(false);

    ??????? plot.setLabelGap(0.02);

    ??????? return chart;

    ???????

    ??? }

    ?/**

    ???? * Creates a panel for the demo (used by SuperDemo.java).

    ???? *

    ?? ??* @return A panel.

    ???? */

    ??? public static JPanel createDemoPanel() {

    ??????? JFreeChart chart = createChart(createDataset());

    ??????? return new ChartPanel(chart);

    ??? }

    ? /**

    ???? * Starting point for the demonstration application.

    ???? *

    ??? ?* @param args? ignored.

    ???? */

    ??? public static void main(String[] args) {

    ? ??? ? PieChartDemo1 demo = new PieChartDemo1("Pie Chart Demo 1");

    ??????? demo.pack();

    ??????? RefineryUtilities.centerFrameOnScreen(demo);

    ??????? demo.setVisible(true);

    ??? }

    }

    六. 可參考資料與網(wǎng)址

    官方網(wǎng)站

    http://www.jfree.org/jfreechart/index.html

    ?? 官方論壇

    http://www.jfree.org/phpBB2/index.php

    API 文檔

    http://www.jfree.org/jfreechart/api/gjdoc/index.html


    ???中文API

    ????
    http://blog.sina.com.cn/u/405da78d010000ap

    好了,嘮叨了這么多,寫(xiě)東西真tmd



    posted on 2006-08-16 10:44 阿成 閱讀(971) 評(píng)論(0)  編輯  收藏 所屬分類: Open source
    主站蜘蛛池模板: 免费女人高潮流视频在线观看| 亚洲一区二区三区影院| 亚洲人成电影青青在线播放| 久久国产免费一区二区三区 | 在线播放免费人成视频网站| 在线播放免费人成视频在线观看| 亚洲精品国产免费| 无码免费一区二区三区免费播放| 亚洲色精品aⅴ一区区三区| 四虎影视在线看免费观看| 国产一区视频在线免费观看| 亚洲日韩国产AV无码无码精品| 91成人免费观看网站| 亚洲欧洲国产综合| 中文字幕在线免费| 久久久无码精品亚洲日韩按摩| 日本不卡免费新一区二区三区| 亚洲精品色午夜无码专区日韩| 中国人免费观看高清在线观看二区| 亚洲免费在线观看| 一区二区三区在线免费观看视频| 亚洲国产成人精品91久久久| 日韩一区二区三区免费播放| 亚洲国产精品综合久久网络 | a毛片视频免费观看影院| 在线亚洲97se亚洲综合在线| 2022国内精品免费福利视频| 国产成人精品日本亚洲专区| 日韩电影免费在线观看网址| 亚洲国产精品无码久久九九| 黄色免费网址大全| 亚洲日本韩国在线| 亚洲阿v天堂在线2017免费| 国产亚洲自拍一区| 在线观看免费视频一区| 久久精品视频亚洲| 99视频有精品视频免费观看| 亚洲最大在线视频| 18禁超污无遮挡无码免费网站国产 | 亚洲电影免费在线观看| 免费无码成人AV在线播放不卡|