Highcharts ajax擷取json對象動態產生報表產生 .__js

來源:互聯網
上載者:User

http://blog.csdn.net/wsk7860/article/details/8751061


最近做個項目,專案經理想做一個統計報表,在網上查看些資料就選用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();

 

圖片為效果圖:

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.