• 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();

    图片为效果图:

  • 相关阅读:
    TQ2440之I2C操作
    TQ2440之流水灯
    k8s pod注入环境变量
    k8s pod基本概念
    k8s pod存在的意义
    vscode 前端常用插件
    docker swarm常用命令
    k8s pod重启策略和健康检查实现应用自动修复
    k8s init初始化容器应用
    k8s pod状态管理
  • 原文地址:https://www.cnblogs.com/onetwo/p/5461067.html
Copyright © 2020-2023  润新知