posts - 297,  comments - 1618,  trackbacks - 0
                昨天聽到“數據下探”這個名詞,結果一問才發現是這樣的一回事,就是在圖表上,例如柱狀圖上點擊一個柱子,跳轉到另一個圖表頁面。昨天看了一下JFreeChart,發現可以StandardCategoryURLGenerator類來實現這個功能,昨天做了個簡單的demo,效果還不錯,呵呵,共享一下。
               首先建立一個名為barSample的web項目,并將jfreechart1.0.6的相關包加入到其中,本人在該項目中加入的jar包如下:
               (1)gnujaxp.jar;
               (2)itext-2.0.2.jar;
               (3)jcommon-1.0.10.jar;
               (4)jfreechart-1.0.6.jar;
               (5)jfreechart-1.0.6-experimental.jar;
               (6)jfreechart-1.0.6-swt.jar;
               (7)servlet.jar;
               (8)swtgraphics2d.jar。
                并在web.xml中加入相關的配置,配置后的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>DisplayChart</servlet-name>
                  
          <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
              
          </servlet>
              
          <servlet-mapping>
                  
          <servlet-name>DisplayChart</servlet-name>
                  
          <url-pattern>/DisplayChart</url-pattern>
              
          </servlet-mapping>
          </web-app>

                  在WebRoot目錄下建立index.jsp,顯示第一個圖表,該柱狀圖的每根柱子都添加了鏈接,該頁面的代碼如下:
          <%@ page contentType="text/html;charset=GBK"%>
          <%@ page import="java.awt.Color,
                          org.jfree.chart.ChartFactory,
                          org.jfree.chart.JFreeChart,
                          org.jfree.chart.plot.PlotOrientation,
                          org.jfree.chart.servlet.ServletUtilities,
                          org.jfree.data.category.CategoryDataset,
                          org.jfree.data.general.
          *,
                          org.jfree.chart.plot.CategoryPlot,
                          org.jfree.chart.axis.CategoryAxis,
                          org.jfree.chart.axis.ValueAxis,
                          org.jfree.chart.renderer.category.BarRenderer3D,
                          org.jfree.chart.urls.StandardCategoryURLGenerator,
                          org.jfree.chart.
          *,
                          org.jfree.chart.entity.
          *,
                          org.jfree.chart.labels.
          *"%>
          <%
          java.io.PrintWriter pw
          =new java.io.PrintWriter(out);
          double[][] data = new double[][] {{672}{325}{332}{440}{550}{330}};
          String[] rowKeys 
          = {"蘋果""梨子""葡萄""桔子""西瓜""香蕉"};
          String[] columnKeys 
          = {""};
          CategoryDataset dataset 
          = DatasetUtilities.createCategoryDataset(
                  rowKeys,
                  columnKeys,
                  data);


          JFreeChart chart 
          = ChartFactory.createBarChart3D("水果銷量圖統計",
                            
          "水果",
                            
          "銷量",
                            dataset,
                            PlotOrientation.VERTICAL,
                            
          true,
                            
          false,
                            
          false);

          chart.setBackgroundPaint(Color.WHITE);
          CategoryPlot plot 
          = chart.getCategoryPlot();

          CategoryAxis domainAxis 
          = plot.getDomainAxis();
          plot.setDomainAxis(domainAxis);

          ValueAxis rangeAxis 
          = plot.getRangeAxis();
          //設置最高的一個 Item 與圖片頂端的距離
          rangeAxis.setUpperMargin(0.15);
          //設置最低的一個 Item 與圖片底端的距離
          rangeAxis.setLowerMargin(0.15);

          plot.setRangeAxis(rangeAxis);

          BarRenderer3D renderer 
          = new BarRenderer3D();
          renderer.setBaseOutlinePaint(Color.BLACK);
          //設置 Wall 的顏色
          renderer.setWallPaint(Color.gray);
          //設置每種水果代表的柱的顏色
          renderer.setSeriesPaint(0new Color(00255));
          renderer.setSeriesPaint(
          1new Color(200200255));
          renderer.setSeriesPaint(
          2, Color.GREEN);
          renderer.setSeriesPaint(
          3, Color.MAGENTA);
          renderer.setSeriesPaint(
          4, Color.GRAY);
          renderer.setSeriesPaint(
          5, Color.CYAN);

          //設置平行柱的之間距離
          renderer.setItemMargin(0.3);

          //顯示每個柱的數值
          renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
          renderer.setBaseItemLabelsVisible(
          true);

          renderer.setBaseItemLabelsVisible(
          true);
          renderer.setBaseItemURLGenerator(
          new StandardCategoryURLGenerator(
                  
          "detail.jsp""fruit"""));
          plot.setRenderer(renderer);

          //設置柱的透明度
          plot.setForegroundAlpha(0.5f);
          //設置地區、銷量的顯示位置
          //plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
          ChartRenderingInfo info = new ChartRenderingInfo(new
                              StandardEntityCollection());

          String filename 
          = ServletUtilities.saveChartAsPNG(chart, 800600, info, session);
          String graphURL 
          = request.getContextPath() + "/DisplayChart?filename=" + filename;
          ChartUtilities.writeImageMap(pw, filename, info, 
          true);
                        pw.flush();
          %>

          <table width="100%">
              
          <tr>
                  
          <td align="center">
                      
          <img src="<%= graphURL %>" width=800 height=600 border=0 usemap="#<%= filename %>" alt="">
                  
          </td>
              
          </tr>
          </table>
                 鏈接到的詳情頁面detail.jsp的內容如下所示:
          <%@ page contentType="text/html;charset=GBK"%>
          <%@ page import="java.awt.Color,
                          org.jfree.chart.ChartFactory,
                          org.jfree.chart.JFreeChart,
                          org.jfree.chart.plot.PlotOrientation,
                          org.jfree.chart.servlet.ServletUtilities,
                          org.jfree.data.category.CategoryDataset,
                          org.jfree.data.general.
          *,
                          org.jfree.chart.plot.CategoryPlot,
                          org.jfree.chart.axis.CategoryAxis,
                          org.jfree.chart.axis.ValueAxis,
                          org.jfree.chart.renderer.category.BarRenderer3D,
                          org.jfree.chart.labels.
          *"%>
          <%
          String fruit 
          = new String(request.getParameter("fruit").getBytes("ISO8859_1"), "utf-8");
          int count = 0;
          if ("蘋果".equals(fruit)) {
              count 
          = 100;
          }
           else if ("梨子".equals(fruit)) {
              count 
          = 26;
          }
           else if ("葡萄".equals(fruit)) {
              count 
          = -20;
          }
           else if ("桔子".equals(fruit)) {
              count 
          = -40;
          }
           else if ("西瓜".equals(fruit)) {
              count 
          = 80;
          }

          //String address = new String(request.getParameter("address").getBytes("ISO8859_1"), "utf-8");

          double[][] data = new double[][] {{672 + count}{325 + count}{332 + count}{440 + count},
                                            
          {550 + count}{330 + count}{435 + count}{553 + count},
                                            
          {335 + count}{443 + count}{378 + count}{733 + count}}
          ;
          String[] rowKeys 
          = {"一月份""二月份""三月份""四月份",
                              
          "五月份""六月份""七月份""八月份",
                              
          "九月份""十月份""十一月份""十二月份"}
          ;
          String[] columnKeys 
          = {""};
          CategoryDataset dataset 
          = DatasetUtilities.createCategoryDataset(
                  rowKeys,
                  columnKeys,
                  data);

          JFreeChart chart 
          = ChartFactory.createBarChart3D("2007年度" + fruit + "銷量圖",
                            
          "月份",
                            
          "銷量",
                            dataset,
                            PlotOrientation.VERTICAL,
                            
          true,
                            
          false,
                            
          false);

          chart.setBackgroundPaint(Color.WHITE);
          CategoryPlot plot 
          = chart.getCategoryPlot();

          CategoryAxis domainAxis 
          = plot.getDomainAxis();
          plot.setDomainAxis(domainAxis);

          ValueAxis rangeAxis 
          = plot.getRangeAxis();
          //設置最高的一個 Item 與圖片頂端的距離
          rangeAxis.setUpperMargin(0.15);
          //設置最低的一個 Item 與圖片底端的距離
          rangeAxis.setLowerMargin(0.15);

          plot.setRangeAxis(rangeAxis);

          BarRenderer3D renderer 
          = new BarRenderer3D();
          renderer.setBaseOutlinePaint(Color.BLACK);
          //設置 Wall 的顏色
          renderer.setWallPaint(Color.gray);
          //設置每種水果代表的柱的顏色
          renderer.setSeriesPaint(0new Color(00255));
          renderer.setSeriesPaint(
          1new Color(200200255));
          renderer.setSeriesPaint(
          2, Color.GREEN);
          renderer.setSeriesPaint(
          3, Color.MAGENTA);
          renderer.setSeriesPaint(
          4, Color.GRAY);
          renderer.setSeriesPaint(
          5, Color.CYAN);

          //設置平行柱的之間距離
          renderer.setItemMargin(0.3);

          //顯示每個柱的數值
          renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
          renderer.setBaseItemLabelsVisible(
          true);

          plot.setRenderer(renderer);
          //設置柱的透明度
          plot.setForegroundAlpha(0.5f);

          String filename 
          = ServletUtilities.saveChartAsPNG(chart, 800550null, session);
          String graphURL 
          = request.getContextPath() + "/DisplayChart?filename=" + filename;
          %>

          <table width="100%">
              
          <tr>
                  
          <td align="center">
                      
          <a href="index.jsp">返回</a>
                      
          <br>
                      
          <img src="<%= graphURL %>" width=800 height=550 border=0 usemap="#<%= filename %>" alt="">
                  
          </td>
              
          </tr>
          </table>

                 在Tomcat下部署barSample項目后,輸入地址:http://localhost:8080/barSample/,運行效果如下圖所示:

                 點擊第一個柱子(蘋果所在的柱子)后,鏈接到的頁面如下圖所示:

          posted on 2008-02-20 10:34 阿蜜果 閱讀(8347) 評論(12)  編輯  收藏 所屬分類: JFreeChart


          FeedBack:
          # re: 用JFreeChart實現數據下探(給柱狀圖的每根柱子加鏈接的例子)
          2008-02-20 11:30 | voff12
          不錯,請教一個問題。圖片放在session中,每次點擊會不會重新產生?有沒有緩存?怎樣最好的解少處理次數?
          另外可不可把數據和代碼處理與VIEW分開?  回復  更多評論
            
          # re: 用JFreeChart實現數據下探(給柱狀圖的每根柱子加鏈接的例子)
          2008-02-20 11:40 | 阿蜜果
          可以將數據和代碼處理與VIEW分開,網上有相關的例子。  回復  更多評論
            
          # re: 用JFreeChart實現數據下探(給柱狀圖的每根柱子加鏈接的例子)
          2008-02-20 19:37 | BeanSoft
          我來支持一下!  回復  更多評論
            
          # re: 用JFreeChart實現數據下探(給柱狀圖的每根柱子加鏈接的例子)[未登錄]
          2008-03-14 10:02 | haha
          柱圖中如何顯示百分比?  回復  更多評論
            
          # re: 用JFreeChart實現數據下探(給柱狀圖的每根柱子加鏈接的例子)
          2008-03-17 14:16 | jfreechart
          PiePlot pieplot = (PiePlot) chart.getPlot();
          pieplot.setLabelFont(new Font("宋體", 0, 12));

          //沒有數據的時候顯示的內容
          pieplot.setNoDataMessage("無數據顯示");
          pieplot.setCircular(false);
          pieplot.setLabelGap(0.02D);
          pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
          ("{0}: ({2})")));//算出百分比
            回復  更多評論
            
          # re: 用JFreeChart實現數據下探(給柱狀圖的每根柱子加鏈接的例子)
          2008-06-04 13:03 | manqinglb
          LZ看了你的例子 , 真的太感謝你了 , 對我有很大的幫助 , 謝謝謝謝!!  回復  更多評論
            
          # re: 用JFreeChart實現數據下探(給柱狀圖的每根柱子加鏈接的例子)
          2008-06-05 10:05 | 碩士生
          頁面老死掉是怎么回事啊?  回復  更多評論
            
          # re: 用JFreeChart實現數據下探(給柱狀圖的每根柱子加鏈接的例子)
          2008-06-05 10:11 | 碩士生
          第一個頁面老吧cup推到100%然后就卡住了,怎么辦啊?  回復  更多評論
            
          # re: 用JFreeChart實現數據下探(給柱狀圖的每根柱子加鏈接的例子)
          2008-11-27 14:32 | 星彤
          學習一下  回復  更多評論
            
          # re: 用JFreeChart實現數據下探(給柱狀圖的每根柱子加鏈接的例子)
          2010-03-10 15:52 | 時的合適的
          白癡,設置都錯了,還貼出來!!!  回復  更多評論
            
          # re: 蜜果私塾:用JFreeChart實現數據下探(給柱狀圖的每根柱子加鏈接的例子)
          2014-02-24 17:00 | 劉能
          gnujaxp.jar;這個包最好不要放進去,我放進去之后一直報錯  回復  更多評論
            
          # re: 蜜果私塾:用JFreeChart實現數據下探(給柱狀圖的每根柱子加鏈接的例子)[未登錄]
          2014-10-10 16:33 | james
          你好,請問有在餅狀圖上做數據下探的么?  回復  更多評論
            
          <2008年2月>
          272829303112
          3456789
          10111213141516
          17181920212223
          2425262728291
          2345678

                生活將我們磨圓,是為了讓我們滾得更遠——“圓”來如此。
                我的作品:
                玩轉Axure RP  (2015年12月出版)
                

                Power Designer系統分析與建模實戰  (2015年7月出版)
                
               Struts2+Hibernate3+Spring2   (2010年5月出版)
               

          留言簿(263)

          隨筆分類

          隨筆檔案

          文章分類

          相冊

          關注blog

          積分與排名

          • 積分 - 2296322
          • 排名 - 3

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 五原县| 聂荣县| 龙海市| 镇康县| 绥德县| 闽侯县| 吉首市| 理塘县| 宝清县| 湖北省| 瑞金市| 安顺市| 邵阳市| 舟曲县| 嘉兴市| 高碑店市| 辽中县| 徐闻县| 当阳市| 杭锦后旗| 泰州市| 西安市| 塔河县| 从江县| 靖安县| 通江县| 佛学| 万安县| 合江县| 葫芦岛市| 澄迈县| 侯马市| 福建省| 美姑县| 乌恰县| 拜城县| 阿图什市| 德保县| 咸宁市| 原阳县| 中方县|