我需要将多个变量作为折线图放到 highcharts 中。由于变量的数量非常多,我需要能够抓取数据并动态绘制图表。
我有多个服务器,我需要将所有服务器的 cpu 利用率放在一个带有不同线条的图表中。从 sql 查询中,我的数据看起来像这样:
ServerName DateTime CPU
ServerA 1/2/2013 12:00:00 30
ServerA 1/2/2013 01:00:00 20
ServerA 1/2/2013 02:00:00 50
ServerB 1/2/2013 12:00:00 30
ServerB 1/2/2013 01:00:00 30
ServerB 1/2/2013 02:00:00 5
ServerC 1/2/2013 01:00:00 10
serverC 1/2/2013 02:00:00 50
ServerC 1/2/2013 03:00:00 70
我正在查看下面的代码,但由于我看不到数据,因此我无法真正从中做出任何事情。如何使用 highstock 图表将上述 sql 查询的结果添加到多线系列图表中?
$(function() {
var seriesOptions = [],
yAxisOptions = [],
seriesCounter = 0,
names = ['MSFT', 'AAPL', 'GOOG'],
colors = Highcharts.getOptions().colors;
$.each(names, function(i, name) {
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename='+ name.toLowerCase() +'-c.json&callback=?', function(data) {
seriesOptions[i] = {
name: name,
data: data
};
// As we're loading the data asynchronously, we don't know what order it will arrive. So
// we keep a counter and create the chart when all the data is loaded.
seriesCounter++;
if (seriesCounter == names.length) {
createChart();
}
});
});
// create the chart when all data is loaded
function createChart() {
$('#container').highcharts('StockChart', {
chart: {
},
rangeSelector: {
selected: 4
},
yAxis: {
labels: {
formatter: function() {
return (this.value > 0 ? '+' : '') + this.value + '%';
}
},
plotLines: [{
value: 0,
width: 2,
color: 'silver'
}]
},
plotOptions: {
series: {
compare: 'percent'
}
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
valueDecimals: 2
},
series: seriesOptions
});
}
});