etmvc+jQuery EasyUI Highcharts實現柱狀圖

來源:互聯網
上載者:User

基於JQuery easyUI 使用Highcharts實現了資料柱狀圖的顯示,前段顯示主要使用Highchartst這個圖表庫,後端使用etmvc架構從資料庫擷取資料,前後端資料互動採用Json資料格式,主要代碼如下: 前段HTML: [html]  <div style="padding:5px;">      <fieldset>       <div>          <div style="margin: 0 1px">          <div id="containerliuliang" style="min-width: 368px; height: 368px; margin: 0 auto"></div>          </div>       </div>      </fieldset>  </div>   前段JS:[html]  <script type="text/javascript">         /*擷取json資料開始*/         //定義變數         $(document).ready(function () {             var jsonXData = [];             var jsonyD1 = [];             var jsonyD2 = [];           //擷取資料             $.ajax({ url: '/ciccpsMember/memberfee/memberfeeByYear',                 cache: false,                 async: false,                 success: function (data) {                     var json = eval("(" + data + ")");                     if (json.Rows.length > 0) {                         for (var i = 0; i < json.Rows.length; i++) {                             var rows = json.Rows[i];                             var Year = rows.year;                             var ShouldPay = rows.shouldPay;                             var TruePay = rows.truePay;                             jsonXData.push(Year); //賦值                             jsonyD1.push(ShouldPay);                             jsonyD2.push(TruePay);                         } //for                         var chart;                         chart = new Highcharts.Chart({                             chart: {                                 renderTo: 'containerliuliang',//放置圖表的容器                                 plotBackgroundColor: null,                                 plotBorderWidth: null,                                 defaultSeriesType: 'column'   //圖表類型line, spline, area, areaspline, column, bar, pie , scatter                              },                             title: {                                 text: '近年會費繳納情況',                                 style: { font: 'normal 13px 宋體' }                             },                             xAxis: {//X軸資料                                 categories: jsonXData,                                 lineWidth: 2,                                 labels: {                                     rotation: -45, //字型傾斜                                     align: 'right',                                     style: { font: 'normal 13px 宋體' }                                 }                             },                             yAxis: {//Y軸顯示文字                                 lineWidth: 2,                                 title: {                                     text: '金額/萬元'                                 }                             },                             tooltip: {                                 formatter: function () {                                     return '<b>' + this.x + '</b><br/>' +                                  this.series.name + ': ' + this.y+'萬元';                                 }                             },                             plotOptions: {                                 column: {                                     dataLabels: {                                         enabled: true                                     },                                     enableMouseTracking: true//是否顯示title                                 }                             },                             series: [{                                 name: '應繳',                                 data: jsonyD1                             }, {                                 name: '實繳',                                 data: jsonyD2                             }]                         });                         //$("tspan:last").hide(); //把廣告刪除掉                     } //if                 }             });         });      </script>   後端代碼:[java]  /**  * 近幾年會費  */  public JsonView memberfeeByYear() throws Exception {        db connection = new db();      String sql = "select * from  memberfeesview";      ResultSet rs = connection.executeQuery(sql);      List<Map<String, Object>> _list = new ArrayList<Map<String, Object>>();      if (rs != null) {          try {              while (rs.next()) {                  /* 擷取資訊 */                  Map<String, Object> _map = new HashMap<String, Object>();                  _map.put("year", rs.getString("year"));                  _map.put("shouldPay", rs.getFloat("shouldPay"));                  _map.put("truePay", rs.getFloat("truePay"));                  _list.add(_map);              }          } catch (Exception e) {              e.printStackTrace();          } finally {              try {                  rs.close();              } catch (Exception e) {                  e.printStackTrace();              }              connection.closed();          }      }            Map<String, Object> result = new HashMap<String, Object>();      result.put("RowCount",_list.size());      result.put("Rows", _list);      JsonView view = new JsonView(result);      view.setContentType("text/html;charset=utf-8");      return view;  }   Json資料格式:[plain]  {"Rows":[{"truePay":30.0,"shouldPay":140.0,"year":"2013"},{"truePay":140.0,"shouldPay":140.0,"year":"2012"},{"truePay":134.5,"shouldPay":140.0,"year":"2011"},{"truePay":141.0,"shouldPay":140.0,"year":"2010"},{"truePay":85.0,"shouldPay":86.5,"year":"2009"}],"RowCount":5}   運行: 先介紹到這,其他圖可結合Highcharts相關的教程來實現,附帶 多個JS+Json柱狀圖餅圖折線圖樣本 裡面有多個不同的JS表徵圖庫的範例程式碼! 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.