Home Forums Chart Support How can I use PHP MySQL Dynamic data Reply To: How can I use PHP MySQL Dynamic data

#23824

@gereby,

The issue seems to be with the scope of the variables – chart, please refer this stackoverflow thread for more info on scope of a variable in JavaScript. Please find the working code-snippet below.

var updateInterval = 100;
var chart;
var dps;
$(document).ready(function() {
	$.getJSON("testdata.php", function(result) {
		dps = result;
		chart = new CanvasJS.Chart("chartContainer", {
			title: {
				text: "Test",
			},
			axisY: {
				minimum: 0,
				maximum: 1000
			},
			data: [{
				type: "stackedBar",
				dataPoints: dps
			}]
		});

		chart.render();
	});

	var updateChart = function() {
		$.getJSON("testdata.php", function(result) {
			dps.splice(0, dps.length);
			$.each(result, function(index, value) {
				dps.push(value);
			});
		});

		chart.render();
	};

	setInterval(function() {
		updateChart()
	}, updateInterval);
});

Please take a look at this php file for complete code.


Vishwas R
Team CanvasJS