乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      JfreeChart學(xué)習(xí)總結(jié) -花香蝶自來-學(xué)無止境 -JavaEye技術(shù)社區(qū)

       ShangShujie 2007-06-01

      自己最近學(xué)習(xí)了JfreeChart的使用,覺得這個(gè)冬冬的功能非常強(qiáng)大,總結(jié)一下。主要介紹JfreeChart生成圖片的幾種方式

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

      二、JFreeChart獲取。

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

      三、JFreeChart配置安裝
      1 、解壓jfreechart-1.0.1.zip.zip到指定位置,其中source是jfreechart的源碼,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.jar、gnujaxp.jar
      加上第三個(gè)jar包有時(shí)web.xml會(huì)報(bào)錯(cuò),把它去掉就好了。
      至此jfreechart的配置就完成了,下面就可以進(jìn)行jfreechart的開發(fā)了。這里值得提出的是jfreechart的類
      結(jié)構(gòu)設(shè)計(jì)前后兼容性不是很好,不同版本的jfreechart中類庫結(jié)構(gòu)可能不一樣,有時(shí)候可能需要查源碼。如果是中文顯示的時(shí)候可能依據(jù)觀感需要改變源碼的字體。

      四、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)出成PNG和JPEG格式,同時(shí)還可以與PDF和EXCEL關(guān)聯(lián)
      JFreeChart 核心類庫介紹:
      研究jfreechart源碼發(fā)現(xiàn)源碼的主要由兩個(gè)大的包組成:org.jfree.chart,org.jfree.data。其中前者主要與圖形
      本身有關(guān),后者與圖形顯示的數(shù)據(jù)有關(guān)。具體研究如果大家有興趣的話可以自己研究 。
      核心類主要有:
      org.jfree.chart.JFreeChart :圖表對象,任何類型的圖表的最終表現(xiàn)形式都是在該對象進(jìn)行一些屬性的定制。JFreeChart引擎本身提供了一個(gè)工廠類用于創(chuàng)建不同類型的圖表對象
      org.jfree.data.category.XXXDataSet: 數(shù)據(jù)集對象,用于提供顯示圖表所用的數(shù)據(jù)。根據(jù)不同類型的圖表對應(yīng)著很多類型的數(shù)據(jù)集對象類
      org.jfree.chart.plot.XXXPlot :圖表區(qū)域?qū)ο螅旧线@個(gè)對象決定著什么樣式的圖表,創(chuàng)建該對象的時(shí)候需要Axis、Renderer以及數(shù)據(jù)集對象的支持
      org.jfree.chart.axis.XXXAxis :用于處理圖表的兩個(gè)軸:縱軸和橫軸
      org.jfree.chart.render.XXXRender :負(fù)責(zé)如何顯示一個(gè)圖表對象
      org.jfree.chart.urls.XXXURLGenerator: 用于生成Web圖表中每個(gè)項(xiàng)目的鼠標(biāo)點(diǎn)擊鏈接
      XXXXXToolTipGenerator: 用于生成圖象的幫助提示,不同類型圖表對應(yīng)不同類型的工具提示類

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

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

      1) 直接生成圖片

      代碼
      1. public class PieChartPicture {  
      2. public static void main(String[] args)   
      3. {  
      4.  PieDataset dataset = getDataSet();  
      5.  JFreeChart chart = ChartFactory.createPieChart3D(  
      6.     " 項(xiàng)目進(jìn)度分布"// chart title  
      7.     dataset,// data  
      8.     true,// include legend  
      9.     true,  
      10.     false  
      11.    );  
      12.   PiePlot3D  plot=(PiePlot3D)chart.getPlot();  
      13.     // 圖片中顯示百分比:默認(rèn)方式  
      14.     //plot.setLabelGenerator(new           StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));  
      15. // 圖片中顯示百分比:自定義方式,{0} 表示選項(xiàng), {1} 表示數(shù)值, {2} 表示所占比例 ,小數(shù)點(diǎn)后兩位  
      16.  plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));   
      17. // 圖例顯示百分比:自定義方式, {0} 表示選項(xiàng), {1} 表示數(shù)值, {2} 表示所占比例                  
      18.  plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}={1}({2})"));   
      19. // 設(shè)置背景色為白色   
      20. chart.setBackgroundPaint(Color.white);   
      21. // 指定圖片的透明度(0.0-1.0)   
      22.  plot.setForegroundAlpha(1.0f);   
      23. // 指定顯示的餅圖上圓形(false)還橢圓形(true)   
      24. plot.setCircular(true);   
      25. // 設(shè)置圖標(biāo)題的字體   
      26. Font font = new Font(" 黑體",Font.CENTER_BASELINE,20);   
      27. TextTitle title = new TextTitle(" 項(xiàng)目狀態(tài)分布");   
      28. title.setFont(font);    
      29. chart.setTitle(title);   
      30. FileOutputStream fos_jpg = null;   
      31. try {   
      32.      fos_jpg=new FileOutputStream("D:\\ 項(xiàng)目狀態(tài)分布.jpg");   
      33.      ChartUtilities.writeChartAsJPEG(fos_jpg,100,chart,640,480,null);   
      34.      fos_jpg.close();   
      35. catch (Exception e) {   
      36.  }   
      37. }   
      38. private static PieDataset getDataSet() {   
      39. DefaultPieDataset dataset = new DefaultPieDataset();   
      40. dataset.setValue(" 市場前期"new Double(10));   
      41. dataset.setValue(" 立項(xiàng)"new Double(15));   
      42. dataset.setValue(" 計(jì)劃"new Double(10));   
      43. dataset.setValue(" 需求與設(shè)計(jì)"new Double(10));   
      44. dataset.setValue(" 執(zhí)行控制"new Double(35));   
      45. dataset.setValue(" 收尾"new Double(10));   
      46. dataset.setValue(" 運(yùn)維",new Double(10));   
      47. return dataset;          
      48. }  
      49. }   

      2) 采用servlet和struts的action方式輸出
      采用這種方式輸出,不用生成圖片。
      A .servlet輸出

      代碼
      1. public class PieByServlet extends HttpServlet{   
      2. public void service(ServletRequest req, ServletResponse res)   
      3. throws ServletException, IOException   
      4. {   
      5.    res.setContentType("image/jpeg");   
      6.    PieDataset dataset = getDataSet();   
      7.    JFreeChart chart = ChartFactory.createPieChart3D(   
      8.    " 水果餅圖"// chart title   
      9.    dataset,// data   
      10.    true// include legend   
      11.    true,   
      12.    false );   
      13.    //設(shè)置圖表屬性  
      14. // 輸出圖片   
      15.  ChartUtilities.writeChartAsJPEG(res.getOutputStream(),100,chart,800,600,null);   
      16. }   

      B .struts的action方式輸出
      只將這條語句加上try catch即可,并返回null。
      代碼
      1. try{         
      2. ChartUtilities.writeChartAsJPEG(response.getOutputStream(),100,chart,800,600,null);   
      3.  } catch (Exception e) {   
      4. }  
      5. return null;   

      其實(shí)采用這兩種方式與生成圖片的方式改動(dòng)并不大
      加上語句response.setContentType("image/jpeg");
      ChartUtilities.writeChartAsJPEG(new FileOutputStream("D:\\ 項(xiàng)目狀態(tài)分布.jpg");,100,chart,640,480,null);
      文件流改成response的輸出流就可以了
      hartUtilities.writeChartAsJPEG(response.getOutputStream(),100,chart,800,600,null);

      3)jsp+servlet+javabean方式
      1. Create ChartViewer servlet

      代碼
      1. public class ChartViewer extends HttpServlet {   
      2. public void init() throws ServletException {  
      3. }   
      4. //Process the HTTP Get request  
      5. public void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {   
      6. // get the chart from session  
      7. HttpSession session = request.getSession();   
      8. BufferedImage chartImage = (BufferedImage) session.getAttribute("chartImage");   
      9. // set the content type so the browser can see this as a picture   
      10. response.setContentType("image/png");   
      11. // send the picture   
      12. PngEncoder encoder = new PngEncoder(chartImage, false09);   
      13. response.getOutputStream().write(encoder.pngEncode());  
      14. }   
      15. //Process the HTTP Post request   
      16. public void doPost(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {   
      17. doGet(request, response);   
      18. }   
      19. //Process the HTTP Put request   
      20. public void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {   
      21. }   
      22. //Clean up resources   
      23. public void destroy() { }   
      24. }   

      2. Create a servlet map in web.xml

      代碼
      1. <?xml version="1.0" encoding="UTF-8"?>   
      2. <web-app>   
      3.  <servlet>   
      4.  <servlet-name>ChartViewer</servlet-name>   
      5.  <servlet-class>myapp.webwork.servlets.ChartViewer</servlet-class>   
      6. </servlet>   
      7. <servlet-mapping>   
      8. <servlet-name>ChartViewer</servlet-name>   
      9. <url-pattern>/servlet/ChartViewer</url-pattern>   
      10. </servlet-mapping>   
      11. </web-app>   

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

       

      代碼
      1. public class Pie3DDemo {   
      2. private DefaultPieDataset getDataset() {   
      3. // categories...   
      4. String[] section = new String[] { "Jan","Feb","Mar","Apr","May","Jun""Jul","Aug","Sep","Oct","Nov","Dec" };   
      5. // data...   
      6. double[] data = new double[section.length];   
      7.  for (int i = 0; i < data.length; i++) {   
      8.      data[i] = 10 + (Math.random() * 10);   
      9. }   
      10. // create the dataset...   
      11. DefaultPieDataset dataset = new DefaultPieDataset();   
      12. for (int i = 0; i < data.length; i++) {   
      13.        dataset.setValue(section[i], data[i]);   
      14. }   
      15. return dataset;   
      16. }   
      17. public String getChartViewer(HttpServletRequest request, HttpServletResponse response) {   
      18.  DefaultPieDataset dataset = getDataset();   
      19. // create the chart...   
      20.  JFreeChart chart = ChartFactory.createPie3DChart(   
      21.   "Pie3D Chart Demo",  // chart title   
      22.   dataset,             // data   
      23.   true,                // include legend   
      24.   true,   
      25.   false   
      26.  );   
      27. // set the background color for the chart...   
      28. chart.setBackgroundPaint(Color.cyan);   
      29. PiePlot plot = (PiePlot) chart.getPlot();   
      30. plot.setNoDataMessage("No data available");   
      31. // set drilldown capability...   
      32.  plot.setURLGenerator(new StandardPieURLGenerator("Bar3DDemo.jsp","section"));   
      33. plot.setLabelGenerator(null);   
      34. // OPTIONAL CUSTOMISATION COMPLETED.   
      35. ChartRenderingInfo info = null;   
      36. HttpSession session = request.getSession();   
      37. try {   
      38. //Create RenderingInfo object   
      39.  response.setContentType("text/html");   
      40.  info = new ChartRenderingInfo(new StandardEntityCollection());   
      41.  BufferedImage chartImage = chart.createBufferedImage(640400, info);   
      42. // putting chart as BufferedImage in session,   
      43. // thus making it available for the image reading action Action.   
      44. session.setAttribute("chartImage", chartImage);   
      45. PrintWriter writer = new PrintWriter(response.getWriter());   
      46. ChartUtilities.writeImageMap(writer, "imageMap", info);   
      47. writer.flush();   
      48. catch (Exception e) { }   
      49.   
      50. String pathInfo = "http://";   
      51. pathInfo += request.getServerName();   
      52. int port = request.getServerPort();   
      53. pathInfo += ":"+String.valueOf(port);   
      54. pathInfo += request.getContextPath();   
      55. String chartViewer = pathInfo + "/servlet/ChartViewer";   
      56. return chartViewer;   
      57. }   

      6. 頁面

      代碼
      1.    
      2. <html>   
      3. <head>   
      4. <title>Pie Chart Demo</title>   
      5. </head>   
      6. <jsp:useBeanidjsp:useBeanid="myChart"scope="session"class="myapp.webwork.beans.Pie3DDemo" />   
      7. <body>   
      8. <h2>Pie Chart Demo</h2>   
      9. <%String chartViewer = myChart.getChartViewer(request, response);%>   
      10. <img src="<%=chartViewer%>" border=0 usemap="#imageMap">   
      11. </body>   
      12. </html>   

      4)采用工具類ChartUtil和DisplayChart(jfreechart的servlet)輸出
      我用了上面的幾個(gè)方法輸出圖片,發(fā)現(xiàn)頁面里只能輸出一個(gè)圖片,
      不過下面的方法可以輸出多個(gè)圖片,而且是幾種方式中最簡單的一個(gè),推薦使用。

      這種方式和上面的三種比較類似,是將javabean里的生成圖片的方法寫的一個(gè)工具類ChartUtil里面。

      1 .添加工具類ChartUtil
      public class ChartUtil {
      // 產(chǎn)生時(shí)間序列圖,返回圖片名稱

      代碼
      1. public  static String generatePieChart(DefaultPieDataset dataset,String title,int width,int height,HttpSession session, PrintWriter pw) {   
      2.   
      3. String filename = null;   
      4.  try {   
      5.  if (session != null)   
      6.  {   
      7. ChartDeleter deleter = (ChartDeleter)session.getAttribute("JFreeChart_Deleter");   
      8.  session.removeAttribute("JFreeChart_Deleter");   
      9.  session.setAttribute("JFreeChart_Deleter", deleter);   
      10. }   
      11. JFreeChart chart = ChartFactory.createPieChart3D(   
      12. title,  // chart title   
      13. dataset, // data   
      14. true,  // include legend   
      15. true,   
      16. false );   
      17. //  Write the chart image to the temporary directory   
      18. ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());   
      19. //If the last parameter is null, the chart is a "one time"-chart and will be deleted after the first serving.   
      20. //If the last parameter is a session object, the chart remains until session time out.   
      21. filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);   
      22. //  Write the image map to the PrintWriter   
      23.  ChartUtilities.writeImageMap(pw, filename, info,true);   
      24.  pw.flush();   
      25. catch (Exception e) {   
      26.  System.out.println("Exception - " + e.toString());   
      27.  e.printStackTrace(System.out);   
      28.  filename = "picture_error.png"; }   
      29. return filename;   
      30. }   
      31. }   

      2、在action里統(tǒng)計(jì)數(shù)據(jù),設(shè)置好數(shù)據(jù)集dataset。傳到頁面

      3、 在頁面里取出
      DefaultPieDataset piedataset=(DefaultPieDataset)request.getAttribute("piedataset");
      // 用ChartUtil工具類產(chǎn)生圖片

      代碼
      1. String p = ChartUtil.generatePieChart(piedataset," 項(xiàng)目收支線圖",500,300,nullnew PrintWriter(out));   
      2. String p1 = request.getContextPath() + "/servlet/DisplayChart?filename=" + p;   

      通過以下方式輸出
      代碼
      1. <td><img src="<%= p1 %>" width=500 height=300 border=0 usemap="#<%= p %>"></td>  

      4、在web.xml中添加

      代碼
      1. <servlet>  
      2.   <servlet-name>DisplayChart</servlet-name>  
      3.   <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>  
      4. </servlet>  
      5. <servlet-mapping>  
      6.     <servlet-name>DisplayChart</servlet-name>  
      7.     <url-pattern>/servlet/DisplayChart</url-pattern>  
      8. </servlet-mapping>  

      5) ApplicationFrame 方式

      代碼
      1. public class PieChartDemo1 extends ApplicationFrame {   
      2. public PieChartDemo1(String title) {   
      3. super(title);   
      4. setContentPane(createDemoPanel());   
      5. }   
      6. private static JFreeChart createChart(PieDataset dataset) {   
      7. JFreeChart chart = ChartFactory.createPieChart(   
      8. .......  
      9. return chart;   
      10. }   
      11.   
      12. public static JPanel createDemoPanel() {   
      13. JFreeChart chart = createChart(createDataset());   
      14. return new ChartPanel(chart);   
      15. }   
      16.   
      17. public static void main(String[] args) {   
      18. PieChartDemo1 demo = new PieChartDemo1("Pie Chart Demo 1");   
      19. demo.pack();   
      20. RefineryUtilities.centerFrameOnScreen(demo);   
      21. demo.setVisible(true);   
      22. }   
      23. }   

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

      官方網(wǎng)站

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

      官方論壇

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

      API 文檔

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

      中文API

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

      好了,嘮叨了這么多,自己寫了一些,從網(wǎng)上找了一些,真tmd的很累啊。

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多