A sparkline is an inline graphic for quickly representing a small amount of data. I was inspired by
http://sparkline.org but re-implemented this in JpGraph because I was already using the JpGraph libraries in my project.
Sadly, I can't post an image (as the forum won't let me!), but here's the code I've used:
<?php
// This code developed by Jon "The Nice Guy" Spriggs, and is released under the GNU General Public License version 3.
// This is release 1.0
require_once("../libs/jpgraph/jpgraph.php");
require_once("../libs/jpgraph/jpgraph_line.php");
$v=0; // Value (about to be set by Data)
$t=NULL; // Top (Max)
$b=NULL; // Bottom (Min)
$l=0; // Last record and counter
foreach(explode(',', $_GET['graph']) as $v) { //Value
$l++;
$line[$l]=$v+0.0;
if($t==NULL OR $t<=$v) {$t=$v;}
if($b==NULL OR $b>=$v) {$b=$v;}
}
function MaxMinLastCallback($XVal, $YVal) {
global $t,$b,$l;
if($XVal==$t && $_GET['mark']!=FALSE) {return(array(5,'red','red'));}
elseif($XVal==$b && $_GET['mark']!=FALSE) {return(array(5,'blue','blue'));}
elseif($YVal==$l-1 && $_GET['mark']!=FALSE) {return(array(5,'black','black'));}
else {return(array(0,'',''));}
}
if(isset($_GET['debug'])) {
foreach($line as $key=>$value) {
echo "$key => $value (";
$data=MaxMinLastCallback($value, $key);
$set=0;
foreach($data as $data_key=>$data_value) {
echo "$data_value";
if($set=1) {echo ",";}
$set=1;
}
echo ")<br>";
}
} else {
$graph = new Graph(100, 15);
$graph->SetFrame(false);
$graph->SetScale("lin");
$graph->SetMargin(0,0,0,1); // Need to have a margin of 1 at the bottom, otherwise it doesn't render properly.
$graph->xaxis->Hide();
$graph->yaxis->Hide();
$lineplot=new LinePlot($line); // This is the line of data
$lineplot->mark->SetType(MARK_X);
$lineplot->mark->SetWidth(0);
$lineplot->mark->SetCallbackYX("MaxMinLastCallback");
$lineplot->mark->Show();
$graph->Add($lineplot);
$graph->Stroke();
}
?>
Call this using: jpspark.php?graph=6,6,14,13,4,10,9,2,2,10,10,16,13,18,1,16,5,6,7,19,8,4,10