jqplot通過ajax動態(tài)畫折線圖的方法及思路

效果如圖所示,每個五秒鐘圖會移動一次(其實是重新畫了一張圖),能顯示所監(jiān)控的cpu信息。
pastCpuInfomation函數(shù)主要用來顯示一張折線圖
updateCpuPic函數(shù)把5秒前的圖去掉,重新根據(jù)現(xiàn)有數(shù)據(jù)畫一張圖。
updateCpuInfomation函數(shù) 把最新的點加入存儲折線的數(shù)組中
再接著在界面中弄兩個定時器,讓他們每個5秒執(zhí)行一次updateCpuPic,每個1分鐘執(zhí)行一次updateCpuInfomation,圖畫就動起來了。
PS:代碼中執(zhí)行好多操作前都會在服務(wù)器中獲取下服務(wù)器的當前時間 ,ajax寫得有點亂,我也不知道規(guī)不規(guī)范,實現(xiàn)動態(tài)圖的方法好像也不是太好,最好是能直接更新線的數(shù)據(jù)的,希望朋友們多多指教!畫表的代碼已經(jīng)標紅(30行到60行)
var past_cpu_service_statistics_line = new Array();
var plot;
function pastCpuInfomation() {
// 歷史cpu數(shù)據(jù)
// 本地時間
$.ajax({
type: "POST",
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
url: globalObj.path + '/server/getServerCurrentTime.htm',
success: function(currentTime){
console.log("取到服務(wù)器時間"+currentTime);
//獲取歷史cpu數(shù)據(jù)
$.ajax({
type: "POST",
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
url: globalObj.path + '/server/getPastCpuMonitorData.htm',
// startTime endtime 是偽數(shù)據(jù),時間在后臺獲取
data: "hostName=" + "login.cheos.cn",
success: function(result){
for (key in result) {
// 去得到時間轉(zhuǎn)化為int
var intKey = parseInt(key);
var transferTime = new Date(intKey);
console.log("transferTime:"+ key + "----resut:" + transferTime);
var onePoint = [transferTime, result[key] ];
past_cpu_service_statistics_line.push(onePoint);
}
<span style="color:#ff0000;"> // 歷史cpu情況表
plot= $.jqplot('cpuHistory',[ past_cpu_service_statistics_line ],
{
axes: {
xaxis: {
label: '時間',
renderer: $.jqplot.DateAxisRenderer,
tickOptions: {
formatString: '%Y-%#m-%d %H:%M'
},
min : (currentTime -1000 * 60 * 60),
max: (currentTime),
// 橫(縱)坐標顯示的最小值
// ticks:['']
},
yaxis: {
label: 'cpu監(jiān)控',
}
},
highlighter: {
show: true,
sizeAdjust : 7.5
},
cursor: {
show: false
}
});
</span>
},
error: function(textStatus){
alert("獲取監(jiān)控信息失敗!");
}
});
//獲取歷史cpu數(shù)據(jù)ajax結(jié)束
},
error: function(textStatus){
alert("取歷史cpu數(shù)據(jù)時候獲取服務(wù)器時間失敗!");
}
});
}
function updateCpuPic() {
console.log("正在更新cpu視圖");
//先取得服務(wù)器時間,用來畫橫坐標
$.ajax({
type: "POST",
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
url: globalObj.path + '/server/getServerCurrentTime.htm',
success: function(result){
var intKey =parseInt(result);
var transferTime = new Date(intKey);
console.log("獲取到服務(wù)器時間:"+result+"------"+transferTime);
//操作圖表
//如果已經(jīng)存在圖表,則摧毀
if (plot) {
// plot.destory();
$("#cpuHistory").unbind();
$("#cpuHistory").empty();
plot= null;
}
//重新畫圖表
plot= $.jqplot('cpuHistory',[ past_cpu_service_statistics_line ], {
axes: {
xaxis: {
label: '時間',
renderer: $.jqplot.DateAxisRenderer,
tickOptions: {
formatString: '%Y-%#m-%d %H:%M'
},
min: (result - 1000 * 60 * 60),
max: (result),
// 橫(縱)坐標顯示的最小值
// ticks:['']
},
yaxis: {
label: 'cpu監(jiān)控',
}
},
highlighter: {
show: true,
sizeAdjust: 7.5
},
cursor: {
show: false
},
replot: {
resetAxes: true
}
});
},
error: function(textStatus){
alert("獲取服務(wù)器時間失敗!");
}
});
}
function updateCpuInfomation() {
$.ajax({
type: "POST",
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
url: globalObj.path + '/server/getLatestCpuMonitorData.htm',
data: "hostName=" + "login.cheos.cn",
success: function(result){
// 更新一次數(shù)據(jù)
for (key in result) {
var intKey = parseInt(key);
var transferTime = new Date(intKey);
console.log("----------------更新cpu信息---:" + key + "----更新時間:" + transferTime);
var onePoint = [transferTime, result[key] ];
past_cpu_service_statistics_line.push(onePoint);
}
// 更新圖表
// updateCpuPic();
},
error: function(textStatus){
alert("更新cpu信息失敗!");
}
});
}
相關(guān)文章
js用Date對象的setDate()函數(shù)對日期進行加減操作
在某個日期上加減天數(shù)來說,其實只要調(diào)用Date對象的setDate()函數(shù)就可以了,具體方法如下2014-09-09
bootstrap-treeview自定義雙擊事件實現(xiàn)方法
這篇文章主要介紹了bootstrap-treeview自定義事件雙擊事件實現(xiàn)方法,需要的朋友可以參考下2016-01-01
JS函數(shù)動態(tài)傳遞參數(shù)的方法分析【基于arguments對象】
這篇文章主要介紹了JS函數(shù)動態(tài)傳遞參數(shù)的方法,結(jié)合實例形式分析了javascript通過arguments對象獲取傳遞參數(shù)的相關(guān)操作技巧,需要的朋友可以參考下2019-06-06

