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/

    评论

报告相同问题?

悬赏问题

  • ¥15 前台报错,加我微信帮我解决一下,15043457399
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题