[42] | 1 | <?php // content="text/plain; charset=utf-8"
|
---|
| 2 | // Example on how to treat and format timestamp as human readable labels
|
---|
| 3 | require_once ('jpgraph/jpgraph.php');
|
---|
| 4 | require_once ('jpgraph/jpgraph_line.php');
|
---|
| 5 |
|
---|
| 6 | // Number of "fake" data points
|
---|
| 7 | DEFINE('NDATAPOINTS',500);
|
---|
| 8 |
|
---|
| 9 | // Assume data points are sample every 10th second
|
---|
| 10 | DEFINE('SAMPLERATE',10);
|
---|
| 11 |
|
---|
| 12 | // Callback formatting function for the X-scale to convert timestamps
|
---|
| 13 | // to hour and minutes.
|
---|
| 14 | function TimeCallback($aVal) {
|
---|
| 15 | return Date('H:i', $aVal);
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | // Get start time
|
---|
| 19 | $start = time();
|
---|
| 20 | // Set the start time to be on the closest minute just before the "start" timestamp
|
---|
| 21 | $adjstart = floor($start / 60);
|
---|
| 22 |
|
---|
| 23 | // Create a data set in range (20,100) and X-positions
|
---|
| 24 | // We also apply a simple low pass filter on the data to make it less
|
---|
| 25 | // random and a little smoother
|
---|
| 26 | $data = array();
|
---|
| 27 | $xdata = array();
|
---|
| 28 | $data[0] = rand(20,100);
|
---|
| 29 | $xdata[0] = $adjstart;
|
---|
| 30 | for( $i=1; $i < NDATAPOINTS; ++$i ) {
|
---|
| 31 | $data[$i] = rand(20,100)*0.2 + $data[$i-1]*0.8;
|
---|
| 32 | $xdata[$i] = $adjstart + $i * SAMPLERATE;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | // Assume that the data points represents data that is sampled every 10s
|
---|
| 36 | // when determing the end value on the scale. We also add some extra
|
---|
| 37 | // length to end on an even label tick.
|
---|
| 38 | $adjend = $adjstart + (NDATAPOINTS+10)*10;
|
---|
| 39 |
|
---|
| 40 | $graph = new Graph(500,250);
|
---|
| 41 | $graph->SetMargin(40,20,30,50);
|
---|
| 42 |
|
---|
| 43 | // Now specify the X-scale explicit but let the Y-scale be auto-scaled
|
---|
| 44 | $graph->SetScale("intlin",0,0,$adjstart,$adjend);
|
---|
| 45 | $graph->title->Set("Example on TimeStamp Callback");
|
---|
| 46 |
|
---|
| 47 | // Setup the callback and adjust the angle of the labels
|
---|
| 48 | $graph->xaxis->SetLabelFormatCallback('TimeCallback');
|
---|
| 49 | $graph->xaxis->SetLabelAngle(90);
|
---|
| 50 |
|
---|
| 51 | // Set the labels every 5min (i.e. 300seconds) and minor ticks every minute
|
---|
| 52 | $graph->xaxis->scale->ticks->Set(300,60);
|
---|
| 53 |
|
---|
| 54 | $line = new LinePlot($data,$xdata);
|
---|
| 55 | $line->SetColor('lightblue');
|
---|
| 56 | $graph->Add($line);
|
---|
| 57 |
|
---|
| 58 | $graph->Stroke();
|
---|
| 59 | ?>
|
---|