Real time graphing with flot, mysql, php

孤人 提交于 2019-12-12 02:15:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!