I'm making a website that shows data from a MySQL database in Morris graphs. Basically I have a database that gets new measurements every minute, and I'm trying to show these changes live without having to reload the whole page. I have shortened my code for this question, but here's basically what I have:
PHP code:
<?php
while($measurement = mysqli_fetch_array($result)){
$data += $measurement['data'];
}
?>
And the script:
function data() {
var ret = [];
ret.push({
y: 'Today',
a: <?php echo $data; ?>
});
return ret;
}
var graph = Morris.Bar({
element: 'graph',
data: data(),
xkey: 'y',
ykeys: ['a'],
labels: ['random label']
});
function update() {
graph.setData(data());
}
setInterval(update, 60000);
The graph is then shown in a div with the id "graph". So the problem is that the update function doesn't update the graph with new data as $data doesn't get updated. I've heard that I can somehow create a PHP function and continuously run that using Ajax and have it update my $data variable, but I have no idea how to do that.
In order for the graph to update I have to reload the whole page, and this works fine using a meta tag that resfreshes the page every 60 seconds, but it seems like a bad solution.
I have also tried to put the code in a separate PHP file and run that using this code:
var auto_updater = setInterval(
(function () {
$("#graph").load("data.php");
}), 60000);
This also works fine, but the problem is that it redraws the whole graph and that causes the scroll bar on my site to go crazy. What I want is to update the data variable in Morris.Bar, not the whole thing. Any help would be appreciated.