问题
I'm trying to draw a real time graph as my mysql table is constantly being inserted with values, like a moving graph referenced from http://kalanir.blogspot.com/2009/11/how-to-plot-moving-graphs-using-flot.html The values actually come from a carbon dioxide sensor which updates the value of the table with co2 values with positions id. I changed her Math.Random to the code below:
<?php $result = mysql_query("SELECT * FROM node1 ORDER BY id DESC LIMIT 1")or die(mysql_error());?>
<?php $row = mysql_fetch_array( $result );?>
var j = "<?php echo $row['co2'];?>";
var next = "<?php echo $row['id'];?>";
for (var i = 0; i < this.xscale - 1; i++)
{
this.array[i] = [i,this.array[i+1][1]]; // (x,y)
}
this.array[this.xscale - 1] = [this.xscale - 1,j];
However, when i run this code, the first value changes, after which it remains constant, even though the last row of the table is being updated. I heard it is because in php, the server is only polled once. Therefore i only get a constant reading of the first data. Is there any way in which i can make the graph update to the last value of the table? with ajax?
Thanks for your help
回答1:
Yes, you can use Periodic refresh (Polling) or HTTP Streaming.
Note that both of these options can be quite bandwidth demanding.
回答2:
you have to do some sort of polling. But even before you do that, 1. create a php file that retrieves all the important data from the db. 2. let that file echo/return that data in a formatted way. 3. have js function poll that file at intervals (a function that runs in setInterval() )
and yes.. there would be some bandwith issues but i think its manageable.
来源:https://stackoverflow.com/questions/3350056/real-time-graphing-with-flot-mysql-php