簡易代碼之家

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            157 Posts :: 2 Stories :: 57 Comments :: 0 Trackbacks

          jFreeChart是Java開發(fā)中常用的統(tǒng)計(jì)類組件,主要包括柱狀圖,餅狀圖等。下面我們介紹一下jFreeChart最簡單的用法。

          首先需要導(dǎo)入jFreeChart的jar包,放在項(xiàng)目web\WEB-INF\lib文件夾下。然后我們以最簡潔的代碼實(shí)現(xiàn)一個統(tǒng)計(jì)功能。

          1.柱狀圖

          import java.awt.Color;
          import java.awt.Font;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;

          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.NumberAxis;
          import org.jfree.chart.plot.CategoryPlot;
          import org.jfree.chart.plot.PlotOrientation;
          import org.jfree.chart.title.TextTitle;
          import org.jfree.data.category.DefaultCategoryDataset;

          public class BarChartTest {
              
              
          public static void main(String[] args) {
                  DefaultCategoryDataset dataset 
          = new DefaultCategoryDataset();
                  
                  dataset.addValue(
          20"企業(yè)備案數(shù)""北京局");
                  dataset.addValue(
          18"企業(yè)備案數(shù)""上海局");
                  dataset.addValue(
          16"企業(yè)備案數(shù)""天津局");
                  dataset.addValue(
          15"企業(yè)備案數(shù)""重慶局");
                  dataset.addValue(
          45"企業(yè)備案數(shù)""山東局");
                  
                  JFreeChart chart 
          = ChartFactory.createBarChart("企業(yè)備案圖""直屬局""企業(yè)備案數(shù)",dataset, PlotOrientation.HORIZONTAL, truefalsefalse);
                  
                  
          /***************A start*********/
                  
          //設(shè)置標(biāo)題字體樣式
                  TextTitle textTitle = chart.getTitle();
                  textTitle.setFont(
          new Font("黑體", Font.PLAIN, 20));
                  
          //設(shè)置柱狀體顏色
                  CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
                  categoryplot.getRenderer().setSeriesPaint(
          0new Color(228,109,10));
                  NumberAxis numberaxis 
          = (NumberAxis) categoryplot.getRangeAxis();
                  CategoryAxis domainAxis 
          = categoryplot.getDomainAxis();
                  
          //設(shè)置X軸坐標(biāo)上的字體樣式
                  domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
                  
          //設(shè)置X軸的標(biāo)題字體樣式
                  domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12));
                  
          //設(shè)置Y軸坐標(biāo)上的字體樣式
                  numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
                  
          //設(shè)置Y軸的標(biāo)題字體樣式
                  numberaxis.setLabelFont(new Font("黑體", Font.PLAIN, 12));
                  
          //設(shè)置圖片最底部字體樣式
                  if (chart.getLegend() != null{
                      chart.getLegend().setItemFont(
          new Font("宋體", Font.PLAIN, 12));
                  }

                  
          /***************A end*********/
                  
          try {
                      ChartUtilities.writeChartAsPNG(
          new FileOutputStream("D:\\barChart.jpg"), chart, 400200);
                  }
           catch (FileNotFoundException e) {
                      e.printStackTrace();
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }

              }

          }

          生成的文件顯示效果如下:

          2.餅狀圖
          import java.awt.Color;
          import java.awt.Font;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          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.labels.StandardPieSectionLabelGenerator;
          import org.jfree.chart.plot.PiePlot;
          import org.jfree.data.general.DefaultPieDataset;

          public class PieChartTest {
              
              
          public static void main(String[] args){
                  DefaultPieDataset pieDataset 
          = new DefaultPieDataset();
                  pieDataset.setValue(
          "北京局",20);
                  pieDataset.setValue(
          "上海局",18);
                  pieDataset.setValue(
          "天津局",16);
                  pieDataset.setValue(
          "重慶局",15);
                  pieDataset.setValue(
          "山東局",45);
                  
                  JFreeChart chart 
          = ChartFactory.createPieChart3D("企業(yè)備案圖",pieDataset, truefalsefalse);
                  
          /***************A start*********/
                  
          //設(shè)置標(biāo)題字體樣式
                  chart.getTitle().setFont(new Font("黑體",Font.BOLD,20));
                  
          //設(shè)置餅狀圖里描述字體樣式
                  PiePlot piePlot= (PiePlot) chart.getPlot();
                  piePlot.setLabelFont(
          new Font("黑體",Font.BOLD,10));
                  
          //設(shè)置顯示百分比樣式
                  piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator( 
                          (
          "{0}({2})"), NumberFormat.getNumberInstance(), 
                          
          new DecimalFormat("0.00%"))); 
                  
          //設(shè)置統(tǒng)計(jì)圖背景
                  piePlot.setBackgroundPaint(Color.white);
                  
          //設(shè)置圖片最底部字體樣式
                  chart.getLegend().setItemFont(new Font("黑體",Font.BOLD,10));
                  
          /***************A end*********/
                  
          try {
                      ChartUtilities.writeChartAsPNG(
          new FileOutputStream("D:\\pieChart.jpg"), chart, 400300);
                  }
           catch (FileNotFoundException e) {
                      e.printStackTrace();
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }

              }


          }


          生成的文件顯示效果如下:

          其中以上兩段代碼中的“A”代碼塊中的內(nèi)容是為了解決圖片生成過程中遇到的亂碼問題,在實(shí)際開發(fā)中可以寫到一個公共類中,此時應(yīng)注意服務(wù)器的操作系統(tǒng)上是否缺少上述代碼中所用到的字體。關(guān)于jFreeChart詳細(xì)參數(shù)的使用請參考官方文檔。
          posted on 2010-07-21 19:48 Jakin.zhou 閱讀(1974) 評論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 新巴尔虎左旗| 龙海市| 绥滨县| 孟村| 沐川县| 鲁甸县| 石柱| 浪卡子县| 宣威市| 麻阳| 龙州县| 宁明县| 长垣县| 仪陇县| 安吉县| 东光县| 璧山县| 崇明县| 永德县| 景德镇市| 长子县| 靖州| 荆门市| 特克斯县| 涡阳县| 若尔盖县| 疏附县| 宣城市| 渝中区| 奈曼旗| 浙江省| 平原县| 宁陵县| 蒙山县| 德保县| 西华县| 图们市| 利辛县| 历史| 遵义市| 海门市|