asprin
asprin

Reputation: 9833

Highcharts Dynamic Chart (Updating over certain interval) PHP+MySql Example

I'm trying to use this demo to make it work with PHP & MySql so that after certain interval I get updated values from the database.

When I use

// using random value using javascript
setInterval(function() {
        var x = (new Date()).getTime(), // current time
        var y = Math.random();
        series.addPoint([x, y], true, true);
        }, 5000);

I get

Normal chart

But when I try putting an Ajax function like

var t;
setInterval(function() {
    var x = (new Date()).getTime(), // current time
    $.ajax({
        type:'post',
        url:'blahblah.php',
        success:function(data)
        {
             t = data;   // data will be a random numeric value
        }
    });        
    series.addPoint([x, t], true, true);
    }, 5000);

The chart turns into Wrong chart

blahblah.php

echo rand(10, 99);

I'm not able to determine why the line disappears on every ajax call. Also the plotting of the points is also not correct. The returned values are always greater than 10, but as you can see in the second chart, the plotting range is -2 to 8

I don't seem to find an appropriate mysql example to work with this kind chart (auto updating after certain interval)

Are there any other alternatives to fetch data from the server and use that value inside the setInterval function?

Upvotes: 0

Views: 3671

Answers (2)

Paweł Fus
Paweł Fus

Reputation: 45079

Put series.addPoint([x, t], true, true); inside success function - otherwise 't' will be undefined which will mess with chart.

Upvotes: 1

Vinicius
Vinicius

Reputation: 1

I think you're always plotting the values in the same timestamp. And make sure the value in 't' is numeric.

Upvotes: 0

Related Questions