jquery结合highcharts插件显示实时数据动态曲线图

时间:2022-06-24 03:33:24

效果如图所示:

jquery结合highcharts插件显示实时数据动态曲线图

js代码如下:

 $(document).ready(function() {
Highcharts.setOptions({
global: {
useUTC: false
},
colors:"#08c,#ff5a00".split(","),
symbols: ["circle","triangle"]
}); var options = {
chart: {
renderTo: 'container',
type: 'spline',
marginRight: 10,
events: {
load: function() {
// set up the updating of the chart each second
// var series = this.series[0];
$.each(this.series, function(idx, series) {
setInterval(function() {
var x = (new Date()).getTime(), // current time
y = Math.random();
series.addPoint([x, y], true, true);
}, 1000);
});
}
}
},
title: {
text: '实时宽带'
},
subtitle: {
text: " "
},
credits: {
enabled: false
},
xAxis: {
type: 'datetime',
tickPixelInterval: 80
},
yAxis: {
title: {
text: 'Kbps'
},
startOnTick: true, //为true时,设置min才有效
min: 0,
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
formatter: function() {
return '<b>' + this.series.name + '</b><br/>' +
Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) + '<br/>' + '<span style="color:#08c">' +
Highcharts.numberFormat(this.y, 2) + ' Kbps' + '</span>'; }
},
legend: {
enabled: true
},
exporting: {
enabled: false
},
series: [{
name: '实时下载速率',
data: (function() {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i; for (i = -19; i <= 0; i++) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
})()
}, {
name: '实时上行速率',
data: (function() {
// generate an array of random data
var data = [],
time = (new Date()).getTime(),
i; for (i = -19; i <= 0; i++) {
data.push({
x: time + i * 1000,
y: Math.random()
});
}
return data;
})()
}]
};
chart = new Highcharts.Chart(options);
});

html代码:

 <div id="container" style="min-width: 400px; height: 400px; margin: 0 auto"></div>