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