一杯清茶

          統計

          留言簿

          Oracle SQL/PLSQL

          PowerDesigner教程系列

          Struts2.0

          web開發

          三人行

          從事RCP開發的同行

          工作流和權限設置

          閱讀排行榜

          評論排行榜

          jfreechart 生成折線圖,餅圖,柱狀圖,堆棧柱狀圖

          轉載:http://hi.baidu.com/ahli/blog/item/3aec09f78df33626730eec11.html

          關鍵字: 報表 最近公司使用jfreechart來生成統計圖,使用了折線圖,餅圖等,因為用的1.0.8a版的jfreechart,與1.0.0版本的地方有很多不同,而且圖片生成清晰度比老版的高,如下是我做的demo.

          1.所需包
          (1) jfreechart-1.0.8a.jar
          (2) jcommon-1.0.12.jar

          2.運行環境
          JDK 1.5

          3.源代碼

          import java.awt.Color; 
          import java.awt.Font; 
          import java.io.File; 
          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.axis.CategoryAxis; 
          import org.jfree.chart.axis.CategoryLabelPositions; 
          import org.jfree.chart.axis.NumberAxis; 
          import org.jfree.chart.axis.ValueAxis; 
          import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 
          import org.jfree.chart.labels.StandardPieSectionLabelGenerator; 
          import org.jfree.chart.plot.CategoryPlot; 
          import org.jfree.chart.plot.PiePlot3D; 
          import org.jfree.chart.plot.PlotOrientation; 
          import org.jfree.chart.renderer.category.BarRenderer; 
          import org.jfree.chart.renderer.category.LineAndShapeRenderer; 
          import org.jfree.chart.renderer.category.StackedBarRenderer; 
          import org.jfree.chart.title.TextTitle; 
          import org.jfree.data.category.CategoryDataset; 
          import org.jfree.data.general.DatasetUtilities; 
          import org.jfree.data.general.DefaultPieDataset; 
          import org.jfree.data.general.PieDataset; 

          /** 
          * 實際取色的時候一定要16位的,這樣比較準確 

          @author new 
          */
           
          public class CreateChartServiceImpl 

          private static final String CHART_PATH = "E:/test/"

          public static void main(String[] args) 

          // TODO Auto-generated method stub 
          CreateChartServiceImpl pm = new CreateChartServiceImpl(); 
          // 生成餅狀圖 
          pm.makePieChart(); 
          // 生成單組柱狀圖 
          pm.makeBarChart(); 
          // 生成多組柱狀圖 
          pm.makeBarGroupChart(); 
          // 生成堆積柱狀圖 
          pm.makeStackedBarChart(); 
          // 生成折線圖 
          pm.makeLineAndShapeChart(); 
          }
           

          /** 
          * 生成折線圖 
          */
           
          public void makeLineAndShapeChart() 

          double[][] data = new double[][] 

          672766223540126 }
          325521210340106 }
          332256523240526 } }

          String[] rowKeys 
          = 
          "蘋果""梨子""葡萄" }
          String[] columnKeys 
          = 
          "北京""上海""廣州""成都""深圳" }
          CategoryDataset dataset 
          = getBarData(data, rowKeys, columnKeys); 
          createTimeXYChar(
          "折線圖""x軸""y軸", dataset, "lineAndShap.png"); 
          }
           

          /** 
          * 生成分組的柱狀圖 
          */
           
          public void makeBarGroupChart() 

          double[][] data = new double[][] 

          672766223540126 }
          325521210340106 }
          332256523240526 } }

          String[] rowKeys 
          = 
          "蘋果""梨子""葡萄" }
          String[] columnKeys 
          = 
          "北京""上海""廣州""成都""深圳" }
          CategoryDataset dataset 
          = getBarData(data, rowKeys, columnKeys); 
          createBarChart(dataset, 
          "x坐標""y坐標""柱狀圖""barGroup.png"); 
          }
           

          /** 
          * 生成柱狀圖 
          */
           
          public void makeBarChart() 

          double[][] data = new double[][] 

          672766223540126 } }

          String[] rowKeys 
          = 
          "蘋果" }
          String[] columnKeys 
          = 
          "北京""上海""廣州""成都""深圳" }
          CategoryDataset dataset 
          = getBarData(data, rowKeys, columnKeys); 
          createBarChart(dataset, 
          "x坐標""y坐標""柱狀圖""bar.png"); 
          }
           

          /** 
          * 生成堆棧柱狀圖 
          */
           
          public void makeStackedBarChart() 

          double[][] data = new double[][] 

          0.210.660.230.400.26 }
          0.250.210.100.400.16 } }

          String[] rowKeys 
          = 
          "蘋果""梨子" }
          String[] columnKeys 
          = 
          "北京""上海""廣州""成都""深圳" }
          CategoryDataset dataset 
          = getBarData(data, rowKeys, columnKeys); 
          createStackedBarChart(dataset, 
          "x坐標""y坐標""柱狀圖""stsckedBar.png"); 
          }
           

          /** 
          * 生成餅狀圖 
          */
           
          public void makePieChart() 

          double[] data = 
          991 }
          String[] keys 
          = 
          "失敗率""成功率" }

          createValidityComparePimChar(getDataPieSetByUtil(data, keys), 
          "餅狀圖"
          "pie2.png", keys); 
          }
           

          // 柱狀圖,折線圖 數據集 
          public CategoryDataset getBarData(double[][] data, String[] rowKeys, 
          String[] columnKeys) 

          return DatasetUtilities 
          .createCategoryDataset(rowKeys, columnKeys, data); 

          }
           

          // 餅狀圖 數據集 
          public PieDataset getDataPieSetByUtil(double[] data, 
          String[] datadescription) 


          if (data != null && datadescription != null

          if (data.length == datadescription.length) 

          DefaultPieDataset dataset 
          = new DefaultPieDataset(); 
          for (int i = 0; i < data.length; i++

          dataset.setValue(datadescription[i], data[i]); 
          }
           
          return dataset; 
          }
           

          }
           

          return null
          }
           

          /** 
          * 柱狀圖 

          *
          @param dataset 數據集 
          @param xName x軸的說明(如種類,時間等) 
          @param yName y軸的說明(如速度,時間等) 
          @param chartTitle 圖標題 
          @param charName 生成圖片的名字 
          @return 
          */
           
          public String createBarChart(CategoryDataset dataset, String xName, 
          String yName, String chartTitle, String charName) 

          JFreeChart chart 
          = ChartFactory.createBarChart(chartTitle, // 圖表標題 
          xName, // 目錄軸的顯示標簽 
          yName, // 數值軸的顯示標簽 
          dataset, // 數據集 
          PlotOrientation.VERTICAL, // 圖表方向:水平、垂直 
          true// 是否顯示圖例(對于簡單的柱狀圖必須是false) 
          false// 是否生成工具 
          false // 是否生成URL鏈接 
          ); 
          Font labelFont 
          = new Font("SansSerif", Font.TRUETYPE_FONT, 12); 
          /* 
          * VALUE_TEXT_ANTIALIAS_OFF表示將文字的抗鋸齒關閉, 
          * 使用的關閉抗鋸齒后,字體盡量選擇12到14號的宋體字,這樣文字最清晰好看 
          */
           
          // chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); 
          chart.setTextAntiAlias(false); 
          chart.setBackgroundPaint(Color.white); 
          // create plot 
          CategoryPlot plot = chart.getCategoryPlot(); 
          // 設置橫虛線可見 
          plot.setRangeGridlinesVisible(true); 
          // 虛線色彩 
          plot.setRangeGridlinePaint(Color.gray); 

          // 數據軸精度 
          NumberAxis vn = (NumberAxis) plot.getRangeAxis(); 
          // vn.setAutoRangeIncludesZero(true); 
          DecimalFormat df = new DecimalFormat("#0.00"); 
          vn.setNumberFormatOverride(df); 
          // 數據軸數據標簽的顯示格式 
          // x軸設置 
          CategoryAxis domainAxis = plot.getDomainAxis(); 
          domainAxis.setLabelFont(labelFont);
          // 軸標題 
          domainAxis.setTickLabelFont(labelFont);// 軸數值 

          // Lable(Math.PI/3.0)度傾斜 
          // domainAxis.setCategoryLabelPositions(CategoryLabelPositions 
          // .createUpRotationLabelPositions(Math.PI / 3.0)); 

          domainAxis.setMaximumCategoryLabelWidthRatio(
          0.6f);// 橫軸上的 Lable 是否完整顯示 

          // 設置距離圖片左端距離 
          domainAxis.setLowerMargin(0.1); 
          // 設置距離圖片右端距離 
          domainAxis.setUpperMargin(0.1); 
          // 設置 columnKey 是否間隔顯示 
          // domainAxis.setSkipCategoryLabelsToFit(true); 

          plot.setDomainAxis(domainAxis); 
          // 設置柱圖背景色(注意,系統取色的時候要使用16位的模式來查看顏色編碼,這樣比較準確) 
          plot.setBackgroundPaint(new Color(255255204)); 

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

          BarRenderer renderer 
          = new BarRenderer(); 
          // 設置柱子寬度 
          renderer.setMaximumBarWidth(0.05); 
          // 設置柱子高度 
          renderer.setMinimumBarLength(0.2); 
          // 設置柱子邊框顏色 
          renderer.setBaseOutlinePaint(Color.BLACK); 
          // 設置柱子邊框可見 
          renderer.setDrawBarOutline(true); 

          // // 設置柱的顏色 
          renderer.setSeriesPaint(0new Color(204255255)); 
          renderer.setSeriesPaint(
          1new Color(153204255)); 
          renderer.setSeriesPaint(
          2new Color(51204204)); 

          // 設置每個地區所包含的平行柱的之間距離 
          renderer.setItemMargin(0.0); 

          // 顯示每個柱的數值,并修改該數值的字體屬性 
          renderer.setIncludeBaseInRange(true); 
          renderer 
          .setBaseItemLabelGenerator(
          new StandardCategoryItemLabelGenerator()); 
          renderer.setBaseItemLabelsVisible(
          true); 

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

          FileOutputStream fos_jpg 
          = null
          try 

          isChartPathExist(CHART_PATH); 
          String chartName 
          = CHART_PATH + charName; 
          fos_jpg 
          = new FileOutputStream(chartName); 
          ChartUtilities.writeChartAsPNG(fos_jpg, chart, 
          500500true10); 
          return chartName; 
          }
           
          catch (Exception e) 

          e.printStackTrace(); 
          return null
          }
           
          finally 

          try 

          fos_jpg.close(); 
          }
           
          catch (Exception e) 

          e.printStackTrace(); 
          }
           
          }
           
          }
           

          /** 
          * 橫向圖 

          @param dataset 數據集 
          @param xName x軸的說明(如種類,時間等) 
          @param yName y軸的說明(如速度,時間等) 
          @param chartTitle 圖標題 
          @param charName 生成圖片的名字 
          @return 
          */
           
          public String createHorizontalBarChart(CategoryDataset dataset, 
          String xName, String yName, String chartTitle, String charName) 

          JFreeChart chart 
          = ChartFactory.createBarChart(chartTitle, // 圖表標題 
          xName, // 目錄軸的顯示標簽 
          yName, // 數值軸的顯示標簽 
          dataset, // 數據集 
          PlotOrientation.VERTICAL, // 圖表方向:水平、垂直 
          true// 是否顯示圖例(對于簡單的柱狀圖必須是false) 
          false// 是否生成工具 
          false // 是否生成URL鏈接 
          ); 

          CategoryPlot plot 
          = chart.getCategoryPlot(); 
          // 數據軸精度 
          NumberAxis vn = (NumberAxis) plot.getRangeAxis(); 
          //設置刻度必須從0開始 
          // vn.setAutoRangeIncludesZero(true); 
          DecimalFormat df = new DecimalFormat("#0.00"); 
          vn.setNumberFormatOverride(df); 
          // 數據軸數據標簽的顯示格式 

          CategoryAxis domainAxis 
          = plot.getDomainAxis(); 

          domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
          // 橫軸上的 
          // Lable 
          Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); 

          domainAxis.setLabelFont(labelFont);
          // 軸標題 
          domainAxis.setTickLabelFont(labelFont);// 軸數值 

          domainAxis.setMaximumCategoryLabelWidthRatio(
          0.8f);// 橫軸上的 Lable 是否完整顯示 
          // domainAxis.setVerticalCategoryLabels(false); 
          plot.setDomainAxis(domainAxis); 

          ValueAxis rangeAxis 
          = plot.getRangeAxis(); 
          // 設置最高的一個 Item 與圖片頂端的距離 
          rangeAxis.setUpperMargin(0.15); 
          // 設置最低的一個 Item 與圖片底端的距離 
          rangeAxis.setLowerMargin(0.15); 
          plot.setRangeAxis(rangeAxis); 
          BarRenderer renderer 
          = new BarRenderer(); 
          // 設置柱子寬度 
          renderer.setMaximumBarWidth(0.03); 
          // 設置柱子高度 
          renderer.setMinimumBarLength(30); 

          renderer.setBaseOutlinePaint(Color.BLACK); 

          // 設置柱的顏色 
          renderer.setSeriesPaint(0, Color.GREEN); 
          renderer.setSeriesPaint(
          1new Color(00255)); 
          // 設置每個地區所包含的平行柱的之間距離 
          renderer.setItemMargin(0.5); 
          // 顯示每個柱的數值,并修改該數值的字體屬性 
          renderer 
          .setBaseItemLabelGenerator(
          new StandardCategoryItemLabelGenerator()); 
          // 設置柱的數值可見 
          renderer.setBaseItemLabelsVisible(true); 

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

          FileOutputStream fos_jpg 
          = null
          try 

          isChartPathExist(CHART_PATH); 
          String chartName 
          = CHART_PATH + charName; 
          fos_jpg 
          = new FileOutputStream(chartName); 
          ChartUtilities.writeChartAsPNG(fos_jpg, chart, 
          500500true10); 
          return chartName; 
          }
           
          catch (Exception e) 

          e.printStackTrace(); 
          return null
          }
           
          finally 

          try 

          fos_jpg.close(); 
          }
           
          catch (Exception e) 

          e.printStackTrace(); 
          }
           
          }
           
          }
           

          /** 
          * 餅狀圖 

          @param dataset 數據集 
          @param chartTitle 圖標題 
          @param charName 生成圖的名字 
          @param pieKeys 分餅的名字集 
          @return 
          */
           
          public String createValidityComparePimChar(PieDataset dataset, 
          String chartTitle, String charName, String[] pieKeys) 

          JFreeChart chart 
          = ChartFactory.createPieChart3D(chartTitle, // chart 
          // title 
          dataset,// data 
          true,// include legend 
          truefalse); 

          // 使下說明標簽字體清晰,去鋸齒類似于 
          // chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);的效果 
          chart.setTextAntiAlias(false); 
          // 圖片背景色 
          chart.setBackgroundPaint(Color.white); 
          // 設置圖標題的字體重新設置title 
          Font font = new Font("隸書", Font.BOLD, 25); 
          TextTitle title 
          = new TextTitle(chartTitle); 
          title.setFont(font); 
          chart.setTitle(title); 

          PiePlot3D plot 
          = (PiePlot3D) chart.getPlot(); 
          // 圖片中顯示百分比:默認方式 

          // 指定餅圖輪廓線的顏色 
          // plot.setBaseSectionOutlinePaint(Color.BLACK); 
          // plot.setBaseSectionPaint(Color.BLACK); 

          // 設置無數據時的信息 
          plot.setNoDataMessage("無對應的數據,請重新查詢。"); 

          // 設置無數據時的信息顯示顏色 
          plot.setNoDataMessagePaint(Color.red); 

          // 圖片中顯示百分比:自定義方式,{0} 表示選項, {1} 表示數值, {2} 表示所占比例 ,小數點后兩位 
          plot.setLabelGenerator(new StandardPieSectionLabelGenerator( 
          "{0}={1}({2})", NumberFormat.getNumberInstance(), 
          new DecimalFormat("0.00%"))); 
          // 圖例顯示百分比:自定義方式, {0} 表示選項, {1} 表示數值, {2} 表示所占比例 
          plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator( 
          "{0}={1}({2})")); 

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

          // 指定圖片的透明度(0.0-1.0) 
          plot.setForegroundAlpha(0.65f); 
          // 指定顯示的餅圖上圓形(false)還橢圓形(true) 
          plot.setCircular(falsetrue); 

          // 設置第一個 餅塊section 的開始位置,默認是12點鐘方向 
          plot.setStartAngle(90); 

          // // 設置分餅顏色 
          plot.setSectionPaint(pieKeys[0], new Color(244194144)); 
          plot.setSectionPaint(pieKeys[
          1], new Color(144233144)); 

          FileOutputStream fos_jpg 
          = null
          try 

          // 文件夾不存在則創建 
          isChartPathExist(CHART_PATH); 
          String chartName 
          = CHART_PATH + charName; 
          fos_jpg 
          = new FileOutputStream(chartName); 
          // 高寬的設置影響橢圓餅圖的形狀 
          ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500230); 

          return chartName; 
          }
           
          catch (Exception e) 

          e.printStackTrace(); 
          return null
          }
           
          finally 

          try 

          fos_jpg.close(); 
          System.out.println(
          "create pie-chart."); 
          }
           
          catch (Exception e) 

          e.printStackTrace(); 
          }
           
          }
           

          }
           

          /** 
          * 判斷文件夾是否存在,如果不存在則新建 
          @param chartPath 
          */
           
          private void isChartPathExist(String chartPath) 

          File file 
          = new File(chartPath); 
          if (!file.exists()) 

          file.mkdirs(); 
          // log.info("CHART_PATH="+CHART_PATH+"create."); 
          }
           
          }
           

          /** 
          * 折線圖 

          @param chartTitle 
          @param x 
          @param y 
          @param xyDataset 
          @param charName 
          @return 
          */
           
          public String createTimeXYChar(String chartTitle, String x, String y, 
          CategoryDataset xyDataset, String charName) 


          JFreeChart chart 
          = ChartFactory.createLineChart(chartTitle, x, y, 
          xyDataset, PlotOrientation.VERTICAL, 
          truetruefalse); 

          chart.setTextAntiAlias(
          false); 
          chart.setBackgroundPaint(Color.WHITE); 
          // 設置圖標題的字體重新設置title 
          Font font = new Font("隸書", Font.BOLD, 25); 
          TextTitle title 
          = new TextTitle(chartTitle); 
          title.setFont(font); 
          chart.setTitle(title); 
          // 設置面板字體 
          Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); 

          chart.setBackgroundPaint(Color.WHITE); 

          CategoryPlot categoryplot 
          = (CategoryPlot) chart.getPlot(); 
          // x軸 // 分類軸網格是否可見 
          categoryplot.setDomainGridlinesVisible(true); 
          // y軸 //數據軸網格是否可見 
          categoryplot.setRangeGridlinesVisible(true); 

          categoryplot.setRangeGridlinePaint(Color.WHITE);
          // 虛線色彩 

          categoryplot.setDomainGridlinePaint(Color.WHITE);
          // 虛線色彩 

          categoryplot.setBackgroundPaint(Color.lightGray); 

          // 設置軸和面板之間的距離 
          // categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); 

          CategoryAxis domainAxis 
          = categoryplot.getDomainAxis(); 

          domainAxis.setLabelFont(labelFont);
          // 軸標題 
          domainAxis.setTickLabelFont(labelFont);// 軸數值 

          domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
          // 橫軸上的 
          // Lable 
          // 45度傾斜 
          // 設置距離圖片左端距離 
          domainAxis.setLowerMargin(0.0); 
          // 設置距離圖片右端距離 
          domainAxis.setUpperMargin(0.0); 

          NumberAxis numberaxis 
          = (NumberAxis) categoryplot.getRangeAxis(); 
          numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 
          numberaxis.setAutoRangeIncludesZero(
          true); 

          // 獲得renderer 注意這里是下嗍造型到lineandshaperenderer!! 
          LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot 
          .getRenderer(); 

          lineandshaperenderer.setBaseShapesVisible(
          true); // series 點(即數據點)可見 
          lineandshaperenderer.setBaseLinesVisible(true); // series 點(即數據點)間有連線可見 

          // 顯示折點數據 
          // lineandshaperenderer.setBaseItemLabelGenerator(new 
          // StandardCategoryItemLabelGenerator()); 
          // lineandshaperenderer.setBaseItemLabelsVisible(true); 

          FileOutputStream fos_jpg 
          = null
          try 

          isChartPathExist(CHART_PATH); 
          String chartName 
          = CHART_PATH + charName; 
          fos_jpg 
          = new FileOutputStream(chartName); 

          // 將報表保存為png文件 
          ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500510); 

          return chartName; 
          }
           
          catch (Exception e) 

          e.printStackTrace(); 
          return null
          }
           
          finally 

          try 

          fos_jpg.close(); 
          System.out.println(
          "create time-createTimeXYChar."); 
          }
           
          catch (Exception e) 

          e.printStackTrace(); 
          }
           
          }
           
          }
           

          /** 
          * 堆棧柱狀圖 

          @param dataset 
          @param xName 
          @param yName 
          @param chartTitle 
          @param charName 
          @return 
          */
           
          public String createStackedBarChart(CategoryDataset dataset, String xName, 
          String yName, String chartTitle, String charName) 

          // 1:得到 CategoryDataset 

          // 2:JFreeChart對象 
          JFreeChart chart = ChartFactory.createStackedBarChart(chartTitle, // 圖表標題 
          xName, // 目錄軸的顯示標簽 
          yName, // 數值軸的顯示標簽 
          dataset, // 數據集 
          PlotOrientation.VERTICAL, // 圖表方向:水平、垂直 
          true// 是否顯示圖例(對于簡單的柱狀圖必須是false) 
          false// 是否生成工具 
          false // 是否生成URL鏈接 
          ); 
          // 圖例字體清晰 
          chart.setTextAntiAlias(false); 

          chart.setBackgroundPaint(Color.WHITE); 

          // 2 .2 主標題對象 主標題對象是 TextTitle 類型 
          chart 
          .setTitle(
          new TextTitle(chartTitle, new Font("隸書", Font.BOLD, 
          25))); 
          // 2 .2.1:設置中文 
          // x,y軸坐標字體 
          Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); 

          // 2 .3 Plot 對象 Plot 對象是圖形的繪制結構對象 
          CategoryPlot plot = chart.getCategoryPlot(); 

          // 設置橫虛線可見 
          plot.setRangeGridlinesVisible(true); 
          // 虛線色彩 
          plot.setRangeGridlinePaint(Color.gray); 

          // 數據軸精度 
          NumberAxis vn = (NumberAxis) plot.getRangeAxis(); 
          // 設置最大值是1 
          vn.setUpperBound(1); 
          // 設置數據軸坐標從0開始 
          // vn.setAutoRangeIncludesZero(true); 
          // 數據顯示格式是百分比 
          DecimalFormat df = new DecimalFormat("0.00%"); 
          vn.setNumberFormatOverride(df); 
          // 數據軸數據標簽的顯示格式 
          // DomainAxis (區域軸,相當于 x 軸), RangeAxis (范圍軸,相當于 y 軸) 
          CategoryAxis domainAxis = plot.getDomainAxis(); 

          domainAxis.setLabelFont(labelFont);
          // 軸標題 
          domainAxis.setTickLabelFont(labelFont);// 軸數值 

          // x軸坐標太長,建議設置傾斜,如下兩種方式選其一,兩種效果相同 
          // 傾斜(1)橫軸上的 Lable 45度傾斜 
          // domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); 
          // 傾斜(2)Lable(Math.PI 3.0)度傾斜 
          // domainAxis.setCategoryLabelPositions(CategoryLabelPositions 
          // .createUpRotationLabelPositions(Math.PI / 3.0)); 

          domainAxis.setMaximumCategoryLabelWidthRatio(
          0.6f);// 橫軸上的 Lable 是否完整顯示 

          plot.setDomainAxis(domainAxis); 

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

          // Renderer 對象是圖形的繪制單元 
          StackedBarRenderer renderer = new StackedBarRenderer(); 
          // 設置柱子寬度 
          renderer.setMaximumBarWidth(0.05); 
          // 設置柱子高度 
          renderer.setMinimumBarLength(0.1); 
          //設置柱的邊框顏色 
          renderer.setBaseOutlinePaint(Color.BLACK); 
          //設置柱的邊框可見 
          renderer.setDrawBarOutline(true); 

          // // 設置柱的顏色(可設定也可默認) 
          renderer.setSeriesPaint(0new Color(204255204)); 
          renderer.setSeriesPaint(
          1new Color(255204153)); 

          // 設置每個地區所包含的平行柱的之間距離 
          renderer.setItemMargin(0.4); 

          plot.setRenderer(renderer); 
          // 設置柱的透明度(如果是3D的必須設置才能達到立體效果,如果是2D的設置則使顏色變淡) 
          // plot.setForegroundAlpha(0.65f); 

          FileOutputStream fos_jpg 
          = null
          try 

          isChartPathExist(CHART_PATH); 
          String chartName 
          = CHART_PATH + charName; 
          fos_jpg 
          = new FileOutputStream(chartName); 
          ChartUtilities.writeChartAsPNG(fos_jpg, chart, 
          500500true10); 
          return chartName; 
          }
           
          catch (Exception e) 

          e.printStackTrace(); 
          return null
          }
           
          finally 

          try 

          fos_jpg.close(); 
          }
           
          catch (Exception e) 

          e.printStackTrace(); 
          }
           
          }
           
          }
           

          posted on 2009-05-26 10:59 一杯清茶 閱讀(5164) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 博罗县| 广安市| 福州市| 伊宁市| 郑州市| 顺义区| 逊克县| 武冈市| 云林县| 恩平市| 思南县| 博客| 邯郸市| 哈尔滨市| 分宜县| 娄底市| 安陆市| 安溪县| 微博| 宝鸡市| 宾川县| 微山县| 凌海市| 乌苏市| 佛山市| 青龙| 乐陵市| 唐海县| 梅州市| 乌恰县| 江门市| 英吉沙县| 卢氏县| 洛隆县| 红桥区| 游戏| 阳城县| 兴安盟| 依兰县| 瑞昌市| 吴江市|