Highcharts 異步加載數(shù)據(jù)曲線圖表
以下實(shí)例演示了異步加載數(shù)據(jù)曲線圖表。這邊我們通過(guò) jQuery.getJSON() 方法從異步加載 csv 文件:
我們?cè)谇懊娴恼鹿?jié)已經(jīng)了解了 Highcharts 配置語(yǔ)法。接下來(lái)讓我們來(lái)看個(gè)完整實(shí)例:
導(dǎo)入 data.js 文件
異步加載數(shù)據(jù)需要引入以下js 文件:
<script src="http://code.highcharts.com/modules/data.js" rel="external nofollow" rel="external nofollow" ></script>
配置
X 軸
以每周為間隔設(shè)置 X 軸:
var xAxis = { tickInterval: 7 * 24 * 3600 * 1000, // 一周 tickWidth: 0, gridLineWidth: 1, labels: { align: 'left', x: 3, y: -3 } };
Y 軸
以每周為間隔設(shè)置 Y 軸:
配置兩個(gè) Y 軸:
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 } ];
plotOptions
plotOptions用于設(shè)置圖表中的數(shù)據(jù)點(diǎn)相關(guān)屬性。
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 } } }
實(shí)例
文件名:highcharts_line_ajax.html
<html> <head> <title>Highcharts 教程 | W3Cschool教程(w3cschool.cn)</title> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js" rel="external nofollow" ></script> <script src="http://code.highcharts.com/highcharts.js" rel="external nofollow" ></script> <script src="http://code.highcharts.com/modules/data.js" rel="external nofollow" rel="external nofollow" ></script> </head> <body> <div id="container" style="width: 550px; height: 400px; margin: 0 auto"></div> <script language="JavaScript"> $(document).ready(function() { var title = { text: 'Daily visits at www.highcharts.com' }; var subtitle = { text: 'Source: Google Analytics' }; var xAxis = { tickInterval: 7 * 24 * 3600 * 1000, // one week tickWidth: 0, gridLineWidth: 1, labels: { align: 'left', x: 3, y: -3 } }; var yAxis = [{ // left y axis title: { text: null }, labels: { align: 'left', x: 3, y: 16, format: '{value:.,0f}' }, showFirstLabel: false },{ // right y axis 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: true, 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>
以上實(shí)例輸出結(jié)果為:
更多建議: