jqplot通過ajax動態(tài)畫折線圖的方法及思路
效果如圖所示,每個(gè)五秒鐘圖會移動一次(其實(shí)是重新畫了一張圖),能顯示所監(jiān)控的cpu信息。
pastCpuInfomation函數(shù)主要用來顯示一張折線圖
updateCpuPic函數(shù)把5秒前的圖去掉,重新根據(jù)現(xiàn)有數(shù)據(jù)畫一張圖。
updateCpuInfomation函數(shù) 把最新的點(diǎn)加入存儲折線的數(shù)組中
再接著在界面中弄兩個(gè)定時(shí)器,讓他們每個(gè)5秒執(zhí)行一次updateCpuPic,每個(gè)1分鐘執(zhí)行一次updateCpuInfomation,圖畫就動起來了。
PS:代碼中執(zhí)行好多操作前都會在服務(wù)器中獲取下服務(wù)器的當(dāng)前時(shí)間 ,ajax寫得有點(diǎn)亂,我也不知道規(guī)不規(guī)范,實(shí)現(xiàn)動態(tài)圖的方法好像也不是太好,最好是能直接更新線的數(shù)據(jù)的,希望朋友們多多指教!畫表的代碼已經(jīng)標(biāo)紅(30行到60行)
var past_cpu_service_statistics_line = new Array();
var plot;
function pastCpuInfomation() {
// 歷史cpu數(shù)據(jù)
// 本地時(shí)間
$.ajax({
type: "POST",
contentType: "application/x-www-form-urlencoded;charset=UTF-8",
url: globalObj.path + '/server/getServerCurrentTime.htm',
success: function(currentTime){
console.log("取到服務(wù)器時(shí)間"+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ù),時(shí)間在后臺獲取
data: "hostName=" + "login.cheos.cn",
success: function(result){
for (key in result) {
// 去得到時(shí)間轉(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: '時(shí)間',
renderer: $.jqplot.DateAxisRenderer,
tickOptions: {
formatString: '%Y-%#m-%d %H:%M'
},
min : (currentTime -1000 * 60 * 60),
max: (currentTime),
// 橫(縱)坐標(biāo)顯示的最小值
// 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ù)時(shí)候獲取服務(wù)器時(shí)間失敗!");
}
});
}
function updateCpuPic() {
console.log("正在更新cpu視圖");
//先取得服務(wù)器時(shí)間,用來畫橫坐標(biāo)
$.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ù)器時(shí)間:"+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: '時(shí)間',
renderer: $.jqplot.DateAxisRenderer,
tickOptions: {
formatString: '%Y-%#m-%d %H:%M'
},
min: (result - 1000 * 60 * 60),
max: (result),
// 橫(縱)坐標(biāo)顯示的最小值
// ticks:['']
},
yaxis: {
label: 'cpu監(jiān)控',
}
},
highlighter: {
show: true,
sizeAdjust: 7.5
},
cursor: {
show: false
},
replot: {
resetAxes: true
}
});
},
error: function(textStatus){
alert("獲取服務(wù)器時(shí)間失敗!");
}
});
}
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 + "----更新時(shí)間:" + transferTime);
var onePoint = [transferTime, result[key] ];
past_cpu_service_statistics_line.push(onePoint);
}
// 更新圖表
// updateCpuPic();
},
error: function(textStatus){
alert("更新cpu信息失敗!");
}
});
}
- D3.js中data(), enter() 和 exit()的問題詳解
- JavaScript可視化圖表庫D3.js API中文參考
- D3.js 從P元素的創(chuàng)建開始(顯示可加載數(shù)據(jù))
- jQuery實(shí)現(xiàn)折線圖的方法
- php下實(shí)現(xiàn)折線圖效果的代碼
- asp.net畫曲線圖(折線圖)代碼 詳細(xì)注釋
- ASP生成柱型體,折線圖,餅圖源代碼提供了
- PHP中使用GD庫繪制折線圖 折線統(tǒng)計(jì)圖的繪制方法
- C#畫圖之餅圖折線圖的實(shí)現(xiàn)方法
- 基于d3.js實(shí)現(xiàn)實(shí)時(shí)刷新的折線圖
相關(guān)文章
node在兩個(gè)div之間移動,用ztree實(shí)現(xiàn)
本文介紹了“node在兩個(gè)div之間移動,用ztree實(shí)現(xiàn)”的方法,需要的朋友可以參考一下2013-03-03一個(gè)通過script自定義屬性傳遞配置參數(shù)的方法
編寫了一個(gè)js插件,要使用該插件需要先在html中引入該插件Js,然后再添加一個(gè)script標(biāo)簽,在里面調(diào)用,需要的朋友可以看看2014-09-09JS面向?qū)ο缶幊袒A(chǔ)篇(一) 對象和構(gòu)造函數(shù)實(shí)例詳解
這篇文章主要介紹了JS面向?qū)ο缶幊虒ο蠛蜆?gòu)造函數(shù),結(jié)合實(shí)例形式詳細(xì)分析了JS面向?qū)ο缶幊虒ο蠛蜆?gòu)造函數(shù)具體概念、原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-03-03mapboxgl實(shí)現(xiàn)帶箭頭軌跡線的代碼
這篇文章主要介紹了mapboxgl實(shí)現(xiàn)帶箭頭軌跡線的代碼,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01JS對象序列化成json數(shù)據(jù)和json數(shù)據(jù)轉(zhuǎn)化為JS對象的代碼
這篇文章主要介紹了JS對象序列化成json數(shù)據(jù)和json數(shù)據(jù)轉(zhuǎn)化為JS對象的代碼,需要的朋友可以參考下2017-08-08Js中使用hasOwnProperty方法檢索ajax響應(yīng)對象的例子
這篇文章主要介紹了Js中使用hasOwnProperty方法檢索ajax響應(yīng)對象的例子,本文介紹的技巧就是hasOwnProperty方法在ajax請求中的使用,需要的朋友可以參考下2014-12-12