jqplot通過ajax動態畫折線圖的方法及思路_javascript技巧

來源:互聯網
上載者:User

效果如圖所示,每個五秒鐘圖會移動一次(其實是重新畫了一張圖),能顯示所監控的cpu資訊。

pastCpuInfomation函數主要用來顯示一張折線圖

updateCpuPic函數把5秒前的圖去掉,重新根據現有資料畫一張圖。

updateCpuInfomation函數 把最新的點加入儲存折線的數組中

再接著在介面中弄兩個定時器,讓他們每個5秒執行一次updateCpuPic,每個1分鐘執行一次updateCpuInfomation,圖畫就動起來了。

PS:代碼中執行好多操作前都會在伺服器中擷取下伺服器的目前時間 ,ajax寫得有點亂,我也不知道規不規範,實現動態圖的方法好像也不是太好,最好是能直接更新線的資料的,希望朋友們多多指教!畫表的代碼已經標紅(30行到60行)

複製代碼 代碼如下:

var past_cpu_service_statistics_line = new Array();
var plot;
function pastCpuInfomation() {
    // 曆史cpu資料
    // 本地時間

    $.ajax({
        type: "POST",
        contentType: "application/x-www-form-urlencoded;charset=UTF-8",
        url: globalObj.path + '/server/getServerCurrentTime.htm',
        success: function(currentTime){
            console.log("取到伺服器時間"+currentTime);
            //擷取曆史cpu資料
            $.ajax({
                type: "POST",
                contentType: "application/x-www-form-urlencoded;charset=UTF-8",
                url: globalObj.path + '/server/getPastCpuMonitorData.htm',
                // startTime endtime 是偽資料,時間在後台擷取
                data: "hostName=" + "login.cheos.cn",
                success: function(result){

                    for (key in result) {
                        // 去得到時間轉化為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監控',

                                    }
                                },
                                highlighter: {
                                    show: true,
                                    sizeAdjust : 7.5
                                },
                                cursor: {
                                    show: false
                                }
                            });
 </span>
                },
                error: function(textStatus){
                    alert("擷取監控資訊失敗!");
                }
            });
            //擷取曆史cpu資料ajax結束
        },
        error: function(textStatus){
            alert("取曆史cpu資料時候擷取伺服器時間失敗!");

        }
    });    

}
function updateCpuPic() {
    console.log("正在更新cpu視圖");
//先取得伺服器時間,用來畫橫座標
    $.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("擷取到伺服器時間:"+result+"------"+transferTime);
        //操作圖表
            //如果已經存在圖表,則摧毀
            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監控',

                    }
                },
                highlighter: {
                    show: true,
                    sizeAdjust: 7.5
                },
                cursor: {
                    show: false
                },
                replot: {
                    resetAxes: true
                }
            });

        },
        error: function(textStatus){
            alert("擷取伺服器時間失敗!");

        }
    });

 

}

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){
            // 更新一次資料
            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資訊失敗!");

        }
    });

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.