1. 程式人生 > >Highcharts 非同步載入資料曲線圖表

Highcharts 非同步載入資料曲線圖表

一 程式碼

<html>
<head>
   <title>Highcharts 非同步載入資料曲線圖表</title>
   <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
   <script src="http://code.highcharts.com/highcharts.js"></script>
    <!--非同步載入資料需要引入以下js 檔案-->
   <script src="http://code.highcharts.com/modules/data.js"></script>
</head>
<body>
<div id="container" style="width: 550px; height: 400px; margin: 0 auto"></div>
<script language="JavaScript">
$(document).ready(function() {
   var title = {
      text: '每日訪問量www.highcharts.com'
   };
   var subtitle = {
      text: 'Google分析'
   };
   var xAxis = {
      tickInterval: 7 * 24 * 3600 * 1000, //以每週為間隔設定 X 軸:
      tickWidth: 0,
      gridLineWidth: 5, //網格(豎線)寬度
      //設定X軸各分類名稱的樣式style
      labels: {
         align: 'left',
         x: 3,
         y: -3
      }
   };
   var yAxis = [{ // 左邊y軸
         //標題為空
         title: {
            text: null
         },
         //左標籤樣式
         labels: {
            align: 'left',
            x: 3,
            y: 16,
            format: '{value:.,0f}'
         },
         showFirstLabel: false
      },{ // 右邊y軸
         linkedTo: 0,
         gridLineWidth: 0,
         opposite: true,
         title: {
            text: null
         },
         labels: {
            align: 'right',
            x: -3,
            y: 16,
            format: '{value:.,0f}'
         },
         showFirstLabel: false
      }
   ];

   var tooltip = {
      shared: true, //是否顯示所有的交叉線資料
      crosshairs: true  //十字交叉線
   }

   var legend = {
      align: 'left',  //對齊方式
      verticalAlign: 'top',
      y: 20,
      floating: false,   //是否可以浮動
      borderWidth: 0
   };

   //用於設定圖表中的資料點相關屬性
   var plotOptions = {
      series: {
         cursor: 'pointer',
         point: {
            events: {
               click: function (e) {
                  hs.htmlExpand(null, {
                     pageOrigin: {
                        x: e.pageX || e.clientX,
                        y: e.pageY || e.clientY
                     },
                     headingText: this.series.name,
                     maincontentText: Highcharts.dateFormat('%A, %b %e, %Y', this.x)
                        + ':<br/> ' + this.y + ' visits',
                     width: 200
                  });
               }
            }
         },
         marker: {
            lineWidth: 1
         }
      }
   }

   var series =  [{
         name: 'All visits',
         lineWidth: 4,
         marker: {
            radius: 4
         }
      }, {
         name: 'New visitors'
      }]

   var json = {};

   json.title = title;
   json.subtitle = subtitle;
   json.xAxis = xAxis;
   json.yAxis = yAxis;
   json.tooltip = tooltip;
   json.legend = legend;
   json.series = series;
   json.plotOptions = plotOptions;
   //資料來源
   $.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=analytics.csv&callback=?', function (csv) {
      var data = {
         csv: csv
      };
      json.data = data;
      $('#container').highcharts(json);
   });
});
</script>
</body>
</html>

二 執行結果