下面的代码使用AJAXJSON从mysql中提取数据,并绘制动态图表,效果很好,但当我的问题是更新图表时。
当我尝试更新图表时,我遇到以下错误:
未捕获引用错误:未定义g
这已经定义好了
所有这些代码都在文档中
//从服务器获取数据
$.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() {
在设置中,您可以使用以下内容定义图形:
var g=新的动态图(…
var
限制了g
的范围,因此在以后的调用中无法访问它。使g
变量全局可用,或在函数之间传递的变量中可用。