Java的統計圖表(JFreeChart的應用)


第一步、導入庫文件(jar包):
      jcommon-1.0.23.jar
      jfreechart-1.0.19.jar
第二步:配置web.xml文件將JFreeChart組件中已有的DisplayChart在web.xml文件進行聲明)
   
      <servlet-name>DisplayChart</servlet-name>
      <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
 
      <servlet-name>DisplayChart</servlet-name>
      <url-pattern>/DisplayChart</url-pattern>
 
第三步:創建數據集:
     1、設計數據庫(明確需要提取的數據)
    2、Dao層設計:實體類、查詢方法
     public int chart(String sql) {
       int count = 0;
       try {
           ps = DBUtils.getConn().prepareStatement(sql);
           ResultSet rs = ps.executeQuery();      
          while(rs.next())
           {
               count = rs.getInt(1);
           }
       } catch (SQLException e) {        
          e.printStackTrace();
       }
       return count;
    }   
  3、 創建一個類:用於生成不同類型的圖表
      public class ChartUtil {
 
            private static PieDataset getPieDataset() throws Exception{
        
        DefaultPieDataset dataset=new DefaultPieDataset();
        String s1="SELECT COUNT(*) FROM student WHERE Education='高中'";
        String s2="SELECT COUNT(*) FROM student WHERE Education='大專'";
        String s3="SELECT COUNT(*) FROM student WHERE Education='本科'";
        String s4="SELECT COUNT(*) FROM student WHERE Education='碩士'";
        IStuDAO dao=DaoFactory.getStudentDao();
        
        dataset.setValue("碩士",dao.select(s4));
        dataset.setValue("專科",dao.select(s2));
        dataset.setValue("本科",dao.select(s3));
        dataset.setValue("高中",dao.select(s1));
        
        return dataset;
    }
 
    public static JFreeChart getPieJFreeChart() throws Exception{
        
        // 以下是餅圖
        PieDataset dataset=getPieDataset();//獲得餅圖
        JFreeChart chart=ChartFactory.createPieChart("學歷分布情況",dataset,true,true,false);//調用工廠類創建餅圖
        
        PiePlot piePlot=(PiePlot)chart.getPlot();
        piePlot.setLabelFont(new Font("宋體",Font.PLAIN,14));//設置圖表字體
        
        TextTitle textTitle=chart.getTitle();
        textTitle.setFont(new Font("宋體",Font.BOLD,20));//設置標題
        
        LegendTitle legendTitle=chart.getLegend();
        legendTitle.setItemFont(new Font("宋體",Font.PLAIN,12));//設置圖例
        
        piePlot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1}人):{2}"));//當前類別的百分比
        
        //分離餅圖
        piePlot.setExplodePercent("本科",0.1);
        piePlot.setExplodePercent("專科",0.1);
        piePlot.setExplodePercent("高中",0.1);
        piePlot.setExplodePercent("碩士",0.1);
        
        //piePlot.setSimpleLabels(true);
        
        //piePlot.setForegroundAlpha(0.7f);//設置3D餅圖的透明度
        //piePlot.setDepthFactor(0.1f);//設置Z軸的高度,取值范圍在0~1之間,值越小,Z軸越低
        
        piePlot.setBackgroundPaint(Color.white);
        piePlot.setOutlineVisible(false);
        chart.setAntiAlias(false);
        
        return chart;
    }
        //以下是柱形圖
    public static JFreeChart getCatJFreeChart() throws Exception{
        
        CategoryDataset dataset=getCategoryDataset();
        JFreeChart chart=ChartFactory.createBarChart("年齡分布情況","年齡段","人數",dataset,PlotOrientation.VERTICAL,false,false,false);
        
        //圖表標題
        TextTitle textTitle=chart.getTitle();
        textTitle.setFont(new Font("宋體",Font.PLAIN,20));
        
        //圖表(柱形圖)
        CategoryPlot categoryPlot=chart.getCategoryPlot();
        CategoryAxis categoryAxis=categoryPlot.getDomainAxis();
        
        //設置柱形的顏色
        BarRenderer renderer=(BarRenderer)categoryPlot.getRenderer();
        renderer.setSeriesPaint(0,Color.RED);
        //X軸字體
        categoryAxis.setTickLabelFont(new Font("宋體",Font.PLAIN,14));
        
        //X軸標簽字體
        categoryAxis.setLabelFont(new Font("宋體",Font.PLAIN,14));
        //categoryAxis.setAxisLinePaint(Color.BLUE);//
        
        //Y軸字體
        ValueAxis valueAxis=categoryPlot.getRangeAxis();
        valueAxis.setLabelFont(new Font("宋體",Font.PLAIN,14));
        //valueAxis.setAxisLinePaint(Color.BLUE);*/
        return chart;
    }
    
    public static CategoryDataset getCategoryDataset() throws Exception{
        
        DefaultKeyedValues keyedValues=new DefaultKeyedValues();
        
        String s1="SELECT COUNT(*) FROM student WHERE Age<20";
        String s2="SELECT COUNT(*) FROM student WHERE Age between 20 and 25";
        String s3="SELECT COUNT(*) FROM student WHERE Age between 25 and 30";
        String s4="SELECT COUNT(*) FROM student WHERE Age between 30 and 35";
        String s5="SELECT COUNT(*) FROM student WHERE Age>35";
        IStuDAO dao=DaoFactory.getStudentDao();
        
        keyedValues.addValue("20歲以下",dao.select(s1));
        keyedValues.addValue("20~25歲",dao.select(s2));
        keyedValues.addValue("25~30歲",dao.select(s3));
        keyedValues.addValue("30~35歲",dao.select(s4));
        keyedValues.addValue("35歲以上",dao.select(s5));
        
        CategoryDataset dataset=DatasetUtilities.createCategoryDataset("年齡分布情況",keyedValues);
        
        return dataset;
    }
}
 
第四步、創建圖表對象
   
         String filename=null;
    
       //餅圖對象
      filename=ServletUtilities.saveChartAsJPEG(ChartUtil.getPieJFreeChart(),500, 350, session);
   
        //柱狀圖對象
      filename=ServletUtilities.saveChartAsJPEG(ChartUtil.getCatJFreeChart(),500, 350, session);
 
第五步、獲取圖片:
 
           String charUrl="DisplayChart?filename="+filename;
 
 
第六步、在web中顯示圖片:

          


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM