The important thing in life is to have a great aim , and the determination

          常用鏈接

          統計

          IT技術鏈接

          保險相關

          友情鏈接

          基金知識

          生活相關

          最新評論

          jfreechart 生成折線圖,餅圖,柱狀圖,堆棧柱狀圖(轉自javaEye)

          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[][]
          {
          { 672, 766, 223, 540, 126 },
          { 325, 521, 210, 340, 106 },
          { 332, 256, 523, 240, 526 } };
          String[] rowKeys =
          { "蘋果", "梨子", "葡萄" };
          String[] columnKeys =
          { "北京", "上海", "廣州", "成都", "深圳" };
          CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
          createTimeXYChar("折線圖", "x軸", "y軸", dataset, "lineAndShap.png");
          }

          /**
          * 生成分組的柱狀圖
          */
          public void makeBarGroupChart()
          {
          double[][] data = new double[][]
          {
          { 672, 766, 223, 540, 126 },
          { 325, 521, 210, 340, 106 },
          { 332, 256, 523, 240, 526 } };
          String[] rowKeys =
          { "蘋果", "梨子", "葡萄" };
          String[] columnKeys =
          { "北京", "上海", "廣州", "成都", "深圳" };
          CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
          createBarChart(dataset, "x坐標", "y坐標", "柱狀圖", "barGroup.png");
          }

          /**
          * 生成柱狀圖
          */
          public void makeBarChart()
          {
          double[][] data = new double[][]
          {
          { 672, 766, 223, 540, 126 } };
          String[] rowKeys =
          { "蘋果" };
          String[] columnKeys =
          { "北京", "上海", "廣州", "成都", "深圳" };
          CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
          createBarChart(dataset, "x坐標", "y坐標", "柱狀圖", "bar.png");
          }

          /**
          * 生成堆棧柱狀圖
          */
          public void makeStackedBarChart()
          {
          double[][] data = new double[][]
          {
          { 0.21, 0.66, 0.23, 0.40, 0.26 },
          { 0.25, 0.21, 0.10, 0.40, 0.16 } };
          String[] rowKeys =
          { "蘋果", "梨子" };
          String[] columnKeys =
          { "北京", "上海", "廣州", "成都", "深圳" };
          CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
          createStackedBarChart(dataset, "x坐標", "y坐標", "柱狀圖", "stsckedBar.png");
          }

          /**
          * 生成餅狀圖
          */
          public void makePieChart()
          {
          double[] data =
          { 9, 91 };
          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(255, 255, 204));

          // 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(0, new Color(204, 255, 255));
          renderer.setSeriesPaint(1, new Color(153, 204, 255));
          renderer.setSeriesPaint(2, new Color(51, 204, 204));

          // 設置每個地區所包含的平行柱的之間距離
          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, 500, 500, true, 10);
          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(1, new Color(0, 0, 255));
          // 設置每個地區所包含的平行柱的之間距離
          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, 500, 500, true, 10);
          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
                  true, false);

          // 使下說明標簽字體清晰,去鋸齒類似于
          // 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(false, true);

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

          // // 設置分餅顏色
          plot.setSectionPaint(pieKeys[0], new Color(244, 194, 144));
          plot.setSectionPaint(pieKeys[1], new Color(144, 233, 144));

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

          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, true, true, false);

          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, 500, 510);

          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(0, new Color(204, 255, 204));
          renderer.setSeriesPaint(1, new Color(255, 204, 153));

          // 設置每個地區所包含的平行柱的之間距離
          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, 500, 500, true, 10);
          return chartName;
          }
          catch (Exception e)
          {
          e.printStackTrace();
          return null;
          }
          finally
          {
          try
          {
          fos_jpg.close();
          }
          catch (Exception e)
          {
          e.printStackTrace();
          }
          }
          }

          }

          posted on 2009-11-12 15:25 鴻雁 閱讀(953) 評論(0)  編輯  收藏 所屬分類: IT技術相關

          主站蜘蛛池模板: 金堂县| 滕州市| 扬州市| 凉城县| 出国| 仁怀市| 青岛市| 九寨沟县| 吉林市| 济南市| 资中县| 措勤县| 辽中县| 余干县| 呼伦贝尔市| 道真| 临洮县| 武胜县| 玉山县| 新绛县| 任丘市| 榆中县| 仪陇县| 满洲里市| 噶尔县| 常州市| 尼玛县| 依安县| 鹤庆县| 乌拉特前旗| 伊通| 将乐县| 民勤县| 无为县| 佛冈县| 华阴市| 丹凤县| 巩留县| 石林| 海淀区| 化德县|