Home Forums Chart Support How to update values in line chart from MySQL DB

How to update values in line chart from MySQL DB

Viewing 2 posts - 1 through 2 (of 2 total)
  • #34969

    I have this code in php file and works fine with fetching the values from MySQl DB but I need it to update the values every 2 seconds:
    Code

    #34987

    @mohsaid,

    You can update datapoints in the chart for every 2 seconds by changing chart-options & calling chart.render() within setInterval.

    setInterval(function() {
    	chart.options.data[0].dataPoints.push({x: 10, y: 20});
    	chart.render();
    }, updateInterval);

    To update the same with the data from the database, you need to read data from database & return it from PHP (service.php), parse it to the format accepted by CanvasJS & render the chart. Please find the code-snippet below.

    var updateChart = function() {
    	$.getJSON("service.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 sample project for complete working code.

    Also refer to this forum thread and PHP Gallery Page for more examples along with working code. You can also download PHP sample that you can run locally from our download page.

    PHP Chart with Data from Database


    Vishwas R
    Team CanvasJS

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.