zoukankan      html  css  js  c++  java
  • Highcharts axja 获取json对象动态生成报表生成

    最近做个项目,项目经理想做一个统计报表,在网上查看些资料就选用Highchars 这里和大家分享下使用心得。 

      重点说明此代码是针对一个报表显示多个项对比显示。 

            直接贴代码:web端                

         <script type="text/javascript" src="js/jquery/jquery-1.7.2.js"></script>
          <script type="text/javascript" src="js/highcharts/highcharts.js"></script>
          <script type="text/javascript">    
               $(document).ready(function() {
                        var options = {
                              chart: {
                                           renderTo: 'container', //DIV容器ID
                                           type: 'column'//报表类型
                                         },
                                //报表名称
                                title:{
                                         text:'测试'
                                        },  
                                  / /补充说明
                          subtitle: {
                                          text: '报表说明'

                                         },
                              yAxis: {
                                           min: 0,
                                           title: {
                                                   text: '单位(mm)'
                                                   }
                                            },
                                    //x轴显示内容
                                  xAxis: {
                                        categories: [ ]
                                              },
                                    / /数据来源(多个对比的)        
                                     series: [{},{},{},{}]
                                    };
                                   //json url 地址这里我使用的servlet
                                    var url =  "http://127.0.0.1:8080/servlet/JsonServlet";
                                    $.getJSON(url,function(data) { 
                                           var i,len=data.length;         
                                            for( i=0;i<len;i++){

                                              //赋值 series
                                              options.series[i].data = data[i].list;    
                                              options.series[i].name = data[i].name;

                                               //对报表X轴显示名称赋值
                                               options.xAxis.categories[i]=data[i].year;
                                          }    
                                       var chart = new Highcharts.Chart(options);
                                 });
                          });  

      </script>
      <body>
        <div id="container"></div>     
      </body>

    后台servlet doget() 方法内容:

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");  
        JSONArray members = new JSONArray();
        PrintWriter out= response.getWriter();
        try {
         for(int i=1;i<5;i++){

       //构建JSON 对象
          JSONObject member = new JSONObject();

         //构建series所需参数
          member.put("name", "张飞"+i); //对应series.name
          JSONArray list = new JSONArray();//对应series.data
          for(int k=1;k<5;k++){
           list.put(k*100);
          }
          member.put("year", (2012 + i));//对应Y轴显示
          member.put("list", list);
          member.put("color", "#FF0022");//如需要可以设置柱状图颜色
          members.put(member);
         }
       
       out.write(members.toString());
       
      } catch (JSONException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
          
      out.flush();
      out.close();

    图片为效果图:

  • 相关阅读:
    Packetbeat协议扩展开发教程(2)
    C语言函数的定义和使用(2)
    C程序的函数说明使用和特点说明第一节
    C 利用移位运算符 把十进制转换成二进制
    while循环实现十进制转二进制
    C位运算符的使用
    C语言for 循环 9*9 实现九九乘法表
    C语言实现金字塔
    C语言实现的简单银行存取款程序 请输入如下数字命令
    C语言中一个字符对应一个ascii码;占一个1个字节8个二进制位;存到内存中也是用ascii的十进制的二进制表示
  • 原文地址:https://www.cnblogs.com/onetwo/p/5461067.html
Copyright © 2011-2022 走看看