This idea was born from my need of handling (insert and show) a set of data almost from everywhere.
The first attempt I made was using an OpenOffice spreadsheet, shared via a free sharing service such as DropBox (sharing is actually useless since the only user was myself: I just needed a central place to retrieve and save these data).
Very soon I realized that this wasn’t working for several reasons:
- plotting capabilities of OpenOffice seemed poor when dealing with moderate/big quantities of data ( > 500 records)
- whenever I needed to see/edit the data I needed an internet enabled computer with OpenOffice installed; requirement may be lowered by exporting an image from the spreadsheet plot and inserting it in some static html page but I would have been forced to keep this image in sync with the data every time I changed them.
Then, someone suggested me to build a small web application to add new data and show them, I would only need an internet connection to do my job.
I also realized that advantages are several:
- no more need of using OpenOffice
- can view/add data from a mobile device
- great freedom data visualization
So I started working at this php web app and this is the result of my efforts.
The application at a glance
I built a single html page containing:
- an html form for data upload (data are written to a csv file)
- data plots using a custom php script that loads data and uses an external charting library to show them.
This is an image of the whole process:
When the web page is loaded, the form is shown and a first plot is built with existing data; then, when data are submitted, the page is reloaded and plots are updated.
The external charting library
At a first time I had a look at the famous GD library and soon I realized that I wanted some higher level library because I did not want to create it by myself.
After some searching I found three candidates:
The first two libraries seemed more suitable since they are free and extremely simple to be used, the third one, instead, has a more complex usage and has a “Professional” version (for commercial usage).
At the beginning I thought I could use one of the first two, because they produced better looking plots with fewer lines of code; but then I switched to jpGraph because it is more flexible, expecially in handling abscissa ticks and lables without showing them crowded.
These first attempts had a nice side-effect since forced me to use a better application design, isolating graphics and I/O code from the remainder.
These are the main application elements:
- a single web page containing form and plots
- a form “target” php file
- a php file handling I/O
- a php file that builds the plots using jpGraph and input data
let’s describe them one by one:
The web page
It’s important parts are:
- php declarations of URI and URL of various files. Note: paths are needed in case page and other php files reside in different folders; this can be useful to add the app to a CMS, see below.
URL are needed by html code, e.g.:
<img src=‘<?php echo $imageFileURL ?>’ />
URI are needed by php for accessing various files (data, plot images):
if ( file_exists($dataFileURI) )
- HTML form: along with the data fields, I added a “password” field that reasonably would be present in a “mature” application. Also, there are some hidden params to be passed to php routine: the data filename and the return address for the php form target (see below in the next item).
- PHP code invokes graphics wrapper that uses jpGraph. All three libraries support on the fly creation of images but I decided to create files to introduce an optimization, plot is rebuilt only if data are newer that images:
// optimization: if csv file is older than image do not rebuild them
So this part is very simple, if data exists and are newer than images read them and rebuild the plots images:
read($dataFileURI, $xvalues, $yvalues1, $yvalues2, $yvalues3, $comments);
buildPlotImage($imageFileURI, $xvalues, $yvalues1, $yvalues2, $yvalues3, $comments);
buildPlotImage($imageTailFileURI, $xvaluesS, $yvalues1S, $yvalues2S, $yvalues3S, $comments);
- I’m creating two plots: the latter shows only data “tail” using last 100 records (this value is arbitrary)
- (I know, I’m lazy and I do not want to change this part) textual comments are passed as params too, even if they are not used
- plot routines need file URI, i.e. related to server internal filesystem
Finally, two plots are shown:
<div style="text-align: center;">
<h2>Output – complete set</h2>
<img src="<?php echo $imageFileURL ?>" alt="" />
<img src="<?php echo $imageTailFileURL ?>" alt="" /></div>
<h2>Output – no data</h2>
this part is in case data are missing.
Form php target
This script simply passes data to I/O scripts. I could have written data to file directly but I wanted to add a little structure to the app separating I/O code. Data are retrieved from POST params, time is added in this phase, before sending data to I/O scripts. jpGraph library supports time in abscissa values almost automagically simply using them in the form of POSIX time().
Nothing special, here: a function to write a record (time, three data, textual description) and a second to read all data from file.
jpGraph calls on csv data build the plot. As I wrote, jpGraph is quite complex but has a good documentation and several examples. In order to use TrueType fonts I added them to app folder to avoid the hassles of finding it on the server or implementing workarounds in case they’re missing. This is how tell jpGraph where fonts are:
These are the steps that where necessary in order to use the app inside WordPress, I guess most of them hold for other CMS too:
- paths: php scripts, data files and images cannot reside in the same folder of the page content (which actually … does not exists since content is on a database)
- return address: it must be discovered dynamically for the same reason (no static page).
Here’s the result!
An example page is (mine is outside the CMS) here; I’m automatically filling the field that handles the password/authorization code (see above).
The complete source code (for a static web page) is : OnlinePlotExample.
You have to download the jpGraph library by yourself (it’s too big to be uploaded to the server) from here and put it in the “php” folder.
Future: wishes and improvements
This small app could be improved in countless ways, in particular design must be improved, e.g. removing all those php/html mixes and wrapping all php code inside a class.
Also, I guess a web app should perform a sort of installation to let the user choose:
- data filename and folder
- own password for adding new data
- the wanted number of series for each record
- custom abscissa values
- data validity ranges for data validation and for plotting (e.g. min and max plot values)
in this way I could also be possible to provide multi-user plotting capabilities.