weixin_33719619 2013-03-14 11:06 采纳率: 0%
浏览 51

使用Ajax的动态Flot图表

I'm new to javascript and flot and hope someone can help me with this problem I'm having. What I'm trying to achieve it have a dynamic flot graph on a page.

The data for the series that is to be plotted is in a file that will get updated every few seconds. I want the flot graph to read the data file every few seconds and be updated with the new data.

Here's the code I've got which isn't working. The graph displays ok on page load, but just doesn't get updated every 5 seconds.

Any help is much appreciated.

   $(function () {

var dataFolder = "http://localhost/graphdata/";

/***********************************************************************
 *             Function to get series data from a file
 ***********************************************************************/
function getSeriesData(file) {
    var url= dataFolder + file;
    var data = null;
    $.ajax({
        async: false,
        url: url,
        method: 'GET',
        dataType: 'json',
        success: function(datasets){
            data = datasets;
        },
        error: function(error,text,http){
            alert(error + " " + text + " " + http);
        }
    });  

    return data;
}
    var plot = $.plot($("#placeholder"),
        [
            {label: "A", data: getSeriesData("dataA.txt")},
            {label: "B", data: getSeriesData("dataB.txt")},
            {label: "C", data: getSeriesData("dataC.txt")}
        ],
        {
            series: {
                lines: {
                    color: "red",
                    show: true
                },
                points: {
                    show: true
                },
                shadowSize: 0,
                hoverable: true
            },
            colors: ["red", "blue", "green"],
            yaxis: {
                min: 0,  ticks:5
            },
            xaxis: {
                mode: 'time',
                timeformat: '%H:%m',
                show: false
            },
            legend:{
                show: true
            },
            grid:{
                color: "green",
                show: true,
                backgroundColor: "white",
                hoverable: true
            }
        }
);

var updateInterval = 1000 * 5;
function update() {

    plot.setData([
                          {label: "A", data: getSeriesData("dataA.txt")},
                          {label: "B", data: getSeriesData("dataB.txt")},
                          {label: "C", data: getSeriesData("dataC.txt")}
                      ]);
    plot.setupGrid();
    plot.draw();

    setTimeout(update, updateInterval);
}
update();});
  • 写回答

3条回答 默认 最新

  • weixin_33737774 2013-03-14 16:20
    关注

    It seems to update correctly for me using numbers. Can you provide a few data examples?

    I changed the x axis and data pull but the updating worked fine.

    function getSeriesData() {
    
    var randomnumber=Math.floor(Math.random()*11)
    var randomnumber2=Math.floor(Math.random()*11)
    var data = [ 
                 [randomnumber, randomnumber2],
                [randomnumber +1, randomnumber2 +2],
                [randomnumber +3, randomnumber2 +4],
                [randomnumber +5, randomnumber2 +6],
                [randomnumber +7,  randomnumber2 +8],
    
    ];
    
    return data; 
    }
    

    fiddle - http://jsfiddle.net/EX6dv/1/

    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站