zoukankan      html  css  js  c++  java
  • 插件jfreechart+shh实现树状图 柱状图 折线图

     最近在做一个考试系统后台时,对于考生的成绩要进行分析,需要使用三种图像来进行分析,分别是饼状图(班级内成绩分布),柱状图(班级间比较),折线图(成绩波动),

    还好java对图形做了很好的支持,利用freechart框架非常简单就完成了,freechart真的很强大,几乎支持所有的图形,并且支持web和应用类

     1.struts.xml

    <!DOCTYPE struts PUBLIC     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"     "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>    
    
    <package name="jFreeChartDemonstration" extends="struts-default"         namespace="/">        
    
    <result-types>            
    
      <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult"></result-type>        
    
    </result-types>        
    
    <!-- 班级成绩分析饼状图 -->        
    
    <action name="showGraphAction" class="com.ht.action.exam.ExamResultAction">         
    
    <result name="success" type="freemarker">/WEB-INF/admin/showGraph.ftl</result>        
    
    </action>        
    
    <!-- 显示饼状图 -->        
    
    <action name="PieChartAction" class="com.ht.action.admin.exportGraph" method="getExportGraph">              
    
    <result type="chart">                   
    
    <param name="width">400</param>                   
    
    <param name="height">300</param>            
    
    </result>        
    
    </action>                        
    
    <!-- 班级间柱状图 -->        
    
    <action name="showSGraphAction" class="com.ht.action.exam.ExamResultAction">         
    
    <result name="success" type="freemarker">........</result>        <!--这里是要显示到的页面,在那个也面只要用img标签来显示即可,src中就是连接到该action中即可-->
    
    </action>        
    
    <!-- 柱状图 -->        
    
     <action name="columnarChartAction" class="com.ht.action.admin.exportGraph" method="getExportColumnarGraph">            
    
    <result type="chart">                   
    
    <param name="width">400</param>                   
    
    <param name="height">300</param>            
    
    </result>        
    
    </action>         
    
     <!--折线图选择 -->      
    
     <action name="showFoldlineAction" class="com.ht.action.admin.exportGraph">         
    
    <result name="success" type="freemarker">/WEB-INF/admin/showFoldline.ftl</result>        
    
    </action>        <!-- 显示折线图 -->        
    
    <action name="FoldlineAction" class="com.ht.action.admin.exportGraph" method="getFoldlineGraph">              
    
    <result type="chart">                   
    
    <param name="width">400</param>                   
    
    <param name="height">300</param>            
    
    </result>        
    
    </action>                     
    
    </package>
    
    </struts>

     4.action

    /*  
    
     * 生成饼状图  
    
    * */  
    
    public String getExportGraph(){    
    
    //设置数据         
    
     DefaultPieDataset data = new DefaultPieDataset();        
    
      data.setValue("90~100","2",4);         
    
    data.setValue("70~90","3",5);         
    
    data.setValue("60~70","5",2);         
    
     data.setValue("<60","5",4);              //生成JFreeChart对象         
    
     chart = ChartFactory.createPieChart("", data, true,true, false);         
    
    //炸开的饼图         
    
    PiePlot plot=(PiePlot)chart.getPlot();         
    
    plot.setExplodePercentdata.setValue("<60","5",4,0.2);         
    
     //不显示空数据和0数据         
    
    plot.setIgnoreZeroValues(true);         
    
    plot.setIgnoreNullValues(true);            
    
    return SUCCESS;  
    
    }  
    
    /*  
    
     * 生成柱状图  
    
    */ 
    
     public String getExportColumnarGraph(){          
    
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();           
    
            
    
    dataset.setValue(4,2, "90~100");   //人数,班级,分数           
    
    dataset.setValue(5,2, "70~90");           
    
     dataset.setValue(4,2, "60~70");           
    
     dataset.setValue(7,2, "<60");          
    
       // 设置标题样式           
    
     chart = ChartFactory.createBarChart3D("", "分数段", "人数", dataset,PlotOrientation.VERTICAL, true, true, false);           
    
     //chart.setTitle(new TextTitle("", new Font("黑体", Font.BOLD, 22)));           
    
    // 报表中间部分           
    
    CategoryPlot plot = (CategoryPlot) chart.getPlot();           
    
     // 设置水平方向的样式           
    
    CategoryAxis categoryAxis = plot.getDomainAxis();           
    
     categoryAxis.setLabelFont(new Font("微软雅黑", Font.BOLD, 12));           
    
     categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);          
    
      // 设置垂直方向的样式           
    
    NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();           
    
     numberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); //数字显示区域,只能为整数          
    
    numberAxis.setLabelFont(new Font("微软雅黑", Font.LAYOUT_NO_LIMIT_CONTEXT, 12));           
    
     // 获得报表下面部分           
    
    chart.getLegend();                    
    
     //设置每根柱子显示自己的信息          
    
     BarRenderer3D renderer=new  BarRenderer3D();          
    
    renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());          
    
    renderer.setBaseItemLabelsVisible(true);          
    
    renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT));           renderer.setItemLabelAnchorOffset(10D);          
    
    plot.setRenderer(renderer);                     
    
    return SUCCESS; 
    
     }  
    
     
    
    /*  
    
     * 生成折线图  
    
    * */
    
     public String getFoldlineGraph(){    
    
     double[][] data = new double[][]                                 
    
     {                                 
    
     { 34, 56, 78, 55, 67 },            //折线上的数据                           
    
    { 44, 67, 88, 99, 34 },                                 
    
    { 44, 53, 78, 78, 45 },                                
    
    };    
    
     String[] rowKeys = { "小红","咚咚","叮叮"};          //纵坐标          
    
     String[] columnKeys = { "模拟1", "模拟2", "模拟3", "模拟4", "模拟5" };  //横坐标显示
    
     
    
       CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
    
       createTimeXYChar("考生成绩波动图", "试卷名称", "分数段", dataset, "");
    
      return SUCCESS;  }     public CategoryDataset getBarData(double[][] data, String[] rowKeys,
    
        String[] columnKeys)
    
        {
    
         return DatasetUtilities.createCategoryDataset(rowKeys, columnKeys, data); 
    
        }     public void createTimeXYChar(String chartTitle, String x, String y,
    
          CategoryDataset xyDataset, String charName) {
    
         chart = ChartFactory.createLineChart(chartTitle, x, y,
    
         xyDataset, PlotOrientation.VERTICAL, true, true, false);
    
         Font font00 = new Font("微软雅黑",Font.LAYOUT_NO_LIMIT_CONTEXT,13);
    
         LegendTitle legend = chart.getLegend();
    
         legend.setItemFont(font00);//设置注释字体   
    
         chart.setTextAntiAlias(false);
    
         // 设置图标题的字体重新设置title
    
         Font font = new Font("微软雅黑", Font.LAYOUT_NO_LIMIT_CONTEXT, 15);
    
         TextTitle title = new TextTitle(chartTitle);
    
         title.setFont(font);
    
         chart.setTitle(title);
    
         CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
    
         // x轴分类轴网格是否可见
    
         categoryplot.setDomainGridlinesVisible(true);
    
         // y轴数据轴网格是否可见
    
         categoryplot.setRangeGridlinesVisible(true);
    
         categoryplot.setRangeGridlinePaint(Color.pink);// 虚线色彩
    
         categoryplot.setDomainGridlinePaint(Color.pink);// 虚线色彩
    
         categoryplot.setBackgroundPaint(Color.white);
    
         // 设置轴和面板之间的距离
    
         categoryplot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 0D));
    
         CategoryAxis domainAxis = categoryplot.getDomainAxis();
    
         domainAxis.setLabelFont(new Font("微软雅黑" , Font.LAYOUT_NO_LIMIT_CONTEXT  , 13));// 轴标题
    
         domainAxis.setTickLabelFont(new Font("微软雅黑" , Font.LAYOUT_NO_LIMIT_CONTEXT , 13));// 轴数值
    
         domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的  
    
         // 设置距离图片左端距离
    
         domainAxis.setLowerMargin(0.1);
    
         // 设置距离图片右端距离
    
         domainAxis.setUpperMargin(0.1);
    
         NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
    
         numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    
         numberaxis.setAutoRangeIncludesZero(true);
    
         numberaxis.setLabelFont(new Font("微软雅黑" , Font.LAYOUT_NO_LIMIT_CONTEXT , 13));
    
               //设置最高的一个值与图片顶端的距离
    
         numberaxis.setUpperMargin(0.15);
    
               //设置最低的一个值与图片底端的距离
    
               //numberaxis.setLowerMargin(0.15);
    
         // 获得renderer 
    
         LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot.getRenderer();
    
         lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见
    
         lineandshaperenderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见
    
         // 显示折点数据
    
         lineandshaperenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    
         lineandshaperenderer.setBaseItemLabelsVisible(true);
    
        } 
  • 相关阅读:
    X264参考手册
    X264码率控制总结
    x264_param_t结构体解释,设置及对应函数位置
    FFmpeg与libx264 x264接口对应关系源代码分析
    x264源代码学习1:概述与架构分析
    X264使用指南
    X264学习1:简介
    PHPMailer 使用 中文乱码
    laravel学习之路5缓存
    laravel学习之路4artisan
  • 原文地址:https://www.cnblogs.com/shenliang123/p/2225219.html
Copyright © 2011-2022 走看看