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中显示图片:

          

posted @ 2019-05-13 13:27  #独狼  阅读(2453)  评论(0编辑  收藏  举报