weixin_33670713 2019-09-13 15:22 采纳率: 0%
浏览 29

用Ajax更新Dygraph

I have below code which pull data from mysql using ajax json and draw Dygraph chart which it works fine but when my problem is updating the chart.

When I try to update the graph I am getting this error:

Uncaught ReferenceError: g is not defined

which is already defined

all these code are in document ready fucntion

// get data from server

  $.ajax({
        type: 'POST',
        url: 'php/proccess.php',
        data: {
            type: "jobgraph",
            job: job
        },
        dataType: "json",
        success: function(response) {
            //console.log(response);
            var i = 0;
            $.each(response, function(key, value) {

                test[i] = [new Date(value[0]), value[1] / 1000, value[2]];
                i++;
            });


            if (test) {
                document.addEventListener("mousewheel", function() {
                    lastClickedGraph = null;
                }, false);
                document.addEventListener("click", function() {
                    lastClickedGraph = null;
                }, false);


                if (response) {
                    var g = new Dygraph(document.getElementById("noroll"),

                        test, {
                            labels: ["Date", "Voltage", "Temp"],
                            digitsAfterDecimal: 3,

                            interactionModel: {
                                'mousedown': downV3,
                                'mousemove': moveV3,
                                'mouseup': upV3,
                                'click': clickV3,
                                'dblclick': referesh,
                                'mousewheel': scrollV3
                            }

                        }
                    );

                }

            }


        },
        error: function(jqXHR, textStatus, errorThrown) {
            if (jqXHR.status == 500) {
                alert('Internal error: ' + jqXHR.responseText);
            } else {
                console.log(errorThrown);
            }
        },


    });
   // update Data every 2 second by pulling new data from mysql server   

  window.intervalId = setInterval(function() {

        $.ajax({
            type: 'POST',
            url: 'php/proccess.php',
            data: {
                type: "jobgraph",
                job: job
            },
            dataType: "json",
            success: function(response) {
                //console.log(response);
                var i = 0;
                $.each(response, function(key, value) {

                    test[i] = [new Date(value[0]), value[1] / 1000, value[2]];
                    i++;
                });


                //console.log(test);


                g.updateOptions({
                    'file': test
                });




            },
            error: function(jqXHR, textStatus, errorThrown) {
                if (jqXHR.status == 500) {
                    alert('Internal error: ' + jqXHR.responseText);
                } else {
                    console.log(errorThrown);
                }
            },


        });


    }, 2000); </pre>

//end update
  • 写回答

1条回答 默认 最新

  • weixin_33695450 2019-09-13 15:34
    关注

    In your setup, you define the graph with:

    var g = new Dygraph(...

    var limits the scope of g, so it's not accessible in your later call. Make the g variable available globally, or in a variable that you pass between functions.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决