I have a continuously generated data (text file) generated by a program on the server. I want to plot the data as a real-time graph just like powergrid does. This was my approach:
As the data is generated continuously on the server in a text file, I wrote a PHP script which reads that file(get_file_contents), outputs the data points and plot the graph using sparkline jQuery plugin. But the problem is that it reads the file all at once. Moreover, the text file keeps on growing. Can anyone suggest me a better approach?
As you’re talking about using a Javascript plotting solution you do the following:
XMLHttpRequest) and passing the last-known filesize of your text file as a parameter.This procedure involves server-side as well as client-side programming but can be accomplished easily.
The following is a sample polling script that requires a
indexparamater that tells the script from which position to read the text file and returns a JSON-encoded list of plot points and the new index pointer.The corresponding Javascript/jQuery snippet could be:
Please be careful that this is only an example and that it lacks all error checking (e.g. concurrent calls to
pollData()on the same page will be problematic).