賢仁居 George Gong
          It's never too late to learn
          posts - 32,comments - 16,trackbacks - 0

          以下代碼用到了大多數方法(附有注釋):

           private String createBarChart(String[] series,String[] categories,double[][] data,HttpSession session,PrintWriter printWriter){
                   
                 CategoryDataset dataset 
          = DatasetUtilities.createCategoryDataset(series, categories, data);
            
              JFreeChart chart 
          = ChartFactory.createBarChart3D("預算統計圖""預算科目","實際執行額",
                       dataset,PlotOrientation.VERTICAL,
          true,true,false);
                 
                 chart.setBackgroundPaint(Color.WHITE);   
          //設定背景色為白色
                 
                 CategoryPlot categoryPlot 
          = chart.getCategoryPlot();  //獲得 plot:CategoryPlot!!
                 
                 categoryPlot.setBackgroundPaint(Color.lightGray); 
          //設定圖表數據顯示部分背景色
                 
                 categoryPlot.setDomainGridlinePaint(Color.white); 
          //橫坐標網格線白色
                 categoryPlot.setDomainGridlinesVisible(true); //可見
                 
                 categoryPlot.setRangeGridlinePaint(Color.white); 
          //縱坐標網格線白色
               
                 CategoryAxis domainAxis 
          = categoryPlot.getDomainAxis();
                 
                 
          //設置 categoryAxis 垂直顯示
               domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(0.4));
               
               
          //設置哼坐標軸的標題字體,此處是“預算科目”
                  domainAxis.setLabelFont(new Font("SansSerif",Font.PLAIN,13));
                 
                 
          //設置距離圖片左端距離,參數為圖片的百分比
                 domainAxis.setLowerMargin(0.05);
                 
                 
          //設置距離圖片右端距離
                 domainAxis.setUpperMargin(0.05);
                     
                 
          //設置橫坐標的坐標值的字體
                 domainAxis.setTickLabelFont(new Font("SansSerif",Font.PLAIN,12));
                 
                 categoryPlot.setDomainAxis(domainAxis); 
                      
                 ValueAxis rangeAxis 
          = categoryPlot.getRangeAxis();
                 
          //設置最高的一個柱與圖片頂端的距離
                 rangeAxis.setUpperMargin(0.05);
                 
                 
          //設置最低的一個柱與圖片底端的距離
                 rangeAxis.setLowerMargin(0.05);
                 categoryPlot.setRangeAxis(rangeAxis); 
                 
                 
          //設置豎坐標標簽的旋轉角度
                 rangeAxis.setLabelAngle(0.05);
                 
                 
          //設置豎坐標軸的坐標值字體
          //       rangeAxis.setTickLabelFont(new Font("SansSerif",Font.PLAIN,12));
            
                 BarRenderer3D renderer
          =(BarRenderer3D) categoryPlot.getRenderer();
                 
                 renderer.setBaseOutlinePaint(Color.GREEN);
                 
          //設置 Wall 的顏色
                 renderer.setWallPaint(Color.PINK);
                 
                 
          //設置每個柱的顏色     
                 GradientPaint gradientpaint = new GradientPaint(0.0F0.0F, Color.blue,
                          
          0.0F0.0Fnew Color(0064)); //設定特定顏色
            GradientPaint gradientpaint1 = new GradientPaint(0.0F0.0F, Color.green,
                     
          0.0F0.0Fnew Color(0640));
           
                 renderer.setSeriesPaint(
          0, gradientpaint);
                 renderer.setSeriesPaint(
          1, gradientpaint1);
                 
                 
          //設置柱的 Outline 顏色
                 renderer.setSeriesOutlinePaint(0, Color.BLACK);
                 renderer.setSeriesOutlinePaint(
          1, Color.BLACK);
                 
          //設置每個category所包含的平行柱的之間距離
                 renderer.setItemMargin(0.1);
              
                 
          //顯示每個柱的數值,并修改該數值的字體屬性
                 renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
                 
                 
          //設置柱子上數值的字體
                 renderer.setItemLabelFont(new Font("SansSerif",Font.PLAIN,13));  
                 renderer.setItemLabelsVisible(
          true);
               
                 
          //設置柱子上數據的顏色
                 renderer.setItemLabelPaint(Color.RED);
                 
                 
          //設置bar的最小寬度,以保證能顯示數值
                 renderer.setMinimumBarLength(0.02);
                 
                 
          //最大寬度
                 renderer.setMaximumBarWidth(0.07);
                 
                 
                 
          //設置柱子上顯示的數據旋轉90度,最后一個參數為旋轉的角度值/3.14
                 ItemLabelPosition itemLabelPosition= new ItemLabelPosition(
                   ItemLabelAnchor.INSIDE12,TextAnchor.CENTER_RIGHT,
                   TextAnchor.CENTER_RIGHT,
          -1.57D);
                 
                 
          //設置不能在柱子上正常顯示的那些數值的顯示方式,將這些數值顯示在柱子外面
                 ItemLabelPosition itemLabelPositionFallback=new ItemLabelPosition(
                   ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT,
                   TextAnchor.HALF_ASCENT_LEFT,
          -1.57D);
                 
                 
          //設置正常顯示的柱子label的position
                 renderer.setPositiveItemLabelPosition(itemLabelPosition);
                 renderer.setNegativeItemLabelPosition(itemLabelPosition);
                 
                 
          //設置不能正常顯示的柱子label的position
                 renderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);
                 renderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);
                 
                 categoryPlot.setRenderer(renderer);
                 
          //設置柱子的透明度
                 categoryPlot.setForegroundAlpha(0.8f);
                 
                 
          //設置地區、銷量的顯示位置
          //       categoryPlot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
          //       categoryPlot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); 
                
                 StandardEntityCollection sec 
          = new StandardEntityCollection(); 
                 ChartRenderingInfo info 
          = new ChartRenderingInfo(sec);
                       
                 
          try {
                   ServletUtilities.setTempFilePrefix(
          "zcz");
                   ServletUtilities.setTempOneTimeFilePrefix(
          "zcz-onetime");
                  
                     filename 
          = ServletUtilities.saveChartAsPNG(chart, 926555, info, session);   
                     chartName
          =filename;
                     
                     System.out.println(
          "圖片的臨時保存路徑:"+System.getProperty("java.io.tmpdir"));
                 }
           catch (IOException ex) {
                     ex.printStackTrace();
                 }

                    
                     String myMap
          =ChartUtilities.getImageMap(filename, info);
                     printWriter.println(myMap);
              
               
          return filename;
              }
          posted on 2007-07-23 10:47 George Gong 閱讀(1356) 評論(2)  編輯  收藏 所屬分類: JAVA&&J2EE

          FeedBack:
          # re: jfreechart 柱型圖應用
          2007-12-03 15:58 | 羅夕
          謝謝分享  回復  更多評論
            
          # re: jfreechart 柱型圖應用[未登錄]
          2008-05-14 11:57 | a
          hao  回復  更多評論
            
          主站蜘蛛池模板: 岗巴县| 类乌齐县| 修武县| 齐齐哈尔市| 大埔县| 冷水江市| 资讯 | 宁陵县| 颍上县| 关岭| 辛集市| 商都县| 金乡县| 阳东县| 武穴市| 怀化市| 湖南省| 黄平县| 西安市| 崇州市| 清涧县| 岑溪市| 东丰县| 巴马| 寿光市| 阿克陶县| 灵川县| 九龙城区| 龙陵县| 甘南县| 凤庆县| 锦州市| 陆川县| 西畴县| 南京市| 门源| 健康| 西乌珠穆沁旗| 崇州市| 琼结县| 叶城县|