##php
////////////////////////////////////////////////////
// Created By Alti 14/11/2000 altimir@hotmail.com//
////////////////////////////////////////////////////
// Significantly modified by Chris Gillings 2007-12
////////////////////////////////////////////////////
class graph {
var $image;
var $filename="graph.gif";
var $debug=0;
var $Bheight=600;
var $Bwidth=800;
var $datay=array();
var $nbhour=24;
var $nbday=31;
var $nbx=31;
var $nby=24;
var $labelX=array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,"");
var $labelY=array(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24);
var $value;
var $colour=array(
'background_colour'=>0,
'black'=>0,
'white'=>0,
'ycolour'=>0,
'xcolour'=>0,
'text_colour'=>0,
);
var $hour_colour=array();
var $title="Temperature Range";
var $title_labelY="Hour";
var $title_labelX="Day";
// Initialise graph skeleton
function graph($Bwidth,$Bheight) {
$this->Bwidth=$Bwidth;
$this->Bheight=$Bheight;
$this->image=ImageCreateTrueColor($this->Bwidth,$this->Bheight);
$this->colour['background_colour']=ImageColorAllocate($this->image,255,255,255);
$this->colour['white']=ImageColorAllocate($this->image,255,255,255);
$this->colour['black']=ImageColorAllocate($this->image,0,0,0);
$this->colour['ycolour']=ImageColorAllocate($this->image,228,238,244);
$this->colour['xcolour']=ImageColorAllocate($this->image,234,245,252);
$this->colour['xycolour']=ImageColorAllocate($this->image,206,225,237);
$this->colour['text_colour']=ImageColorAllocate($this->image,0,0,0);
imagefilledrectangle( $this->image, 0, 0, $this->Bwidth - 2,$this->Bheight - 2, $this->colour['white'] );
imagerectangle( $this->image, 0, 0, $this->Bwidth,$this->Bheight, $this->colour['black'] );
}
# Set colour triplets
function colouris($num,$red,$green,$blue) {
$this->hour_colour[$num]=ImageColorAllocate($this->image,$red,$green,$blue);
}
function create_graph() {
$autoscale=0;
$lenY=0;
for ($ii=0;$ii<=$this->nby;$ii++) {
$var=$this->labelY[$ii];
$lenY=($lenY>strlen($var))?$lenY:strlen($var);
};
$departx=$lenY*(5)+50;
$departy=60;
// width and height for the area of the graphics
$height=$this->Bheight-2*$departy;
$y=$height/($this->nby);
$height=$y*$this->nby;
$width=$this->Bwidth-2*$departx;
$x=$width/($this->nbx);
$width=$x*$this->nbx;
$StartY=$this->Bheight-$departy;
// title of graph
ImageString($this->image,
3,
($this->Bwidth/2)-strlen($this->title)*5/2,
20,
$this->title,
$this->colour['text_colour']);
// title label X
ImageString($this->image,
2,
$width/2+$departx-strlen($this->title_labelX)*5/2,
$this->Bheight-20,
$this->title_labelX,
$this->colour['text_colour']);
// title label Y
ImageStringUp($this->image,
2,
10,
$StartY-$height/2+strlen($this->title_labelY)*5/2,
$this->title_labelY,
$this->colour['text_colour']);
// Draw the X axis labels
$sub=0;
$i=0;
for($p=0;$p<=$this->nbx;$p++) {
//if ($sub<=$p) {
if ($sub<$p) {
$NewX=$departx+$x*$sub+7;
// Alternate positions of label - up/down
$i=($i==0)?1:0;
if ($i==1) { $YLineDelta = 5; $YStartDelta = 12; }
else { $YLineDelta = 8; $YStartDelta = 22; }
$espaceX=strlen($this->labelX[$sub])*sqrt(3);
ImageLine($this->image,
$NewX,
$StartY,
$NewX,
$StartY+$YLineDelta,
$this->colour['black']);
imageString($this->image,
1,
$NewX-$espaceX,
$StartY+$YStartDelta,
$this->labelX[$sub],
$this->colour['black']);
$sub++;
}
}
// Draw the Y axis labels
for($p=0;$p<=$this->nby;$p++) {
imageString($this->image,
1,
$departx-strlen($this->labelY[$p])*5-5,
$StartY-$y*$p-5,
$this->labelY[$p],
$this->colour['black']);
}
$m=1;
$ncolours=count($this->hour_colour);
if ( $autoscale ) {
// find max and min temperatures from data
$maxval=-1000;
$minval=1000;
for ($i=0;$i<32;$i++) {
for ($j=0;$j<25;$j++) {
if ($this->datay[$j][$i]>-100) {
$maxval=max($maxval,$this->datay[$j][$i]);
$minval=min($minval,$this->datay[$j][$i]);
}
}
}
} else {
// fix max and min temperatures
$maxval=41;
$minval=0;
}
// Calculate increment through colour space for colour to temperature mapping
$nvalues=($maxval-$minval);
$norm=intval($ncolours/($maxval-$minval))-1;
//if ( $debug = 1 ) { echo "$nvalues, $ncolours, $maxval, $minval, $norm
";}
// Draw legend showing colours for temperature values
$LegX=560;
$LegY=440;
$height=intval(350/$norm);
$height=intval(360/$nvalues);
for ($i=$nvalues;$i>0;$i--) {
$Xs1=$LegX;
$Ys1=$LegY-$i*$height;
$Xs2=$LegX;
$Ys2=$LegY-($i+1)*$height;
$Xi2=$LegX+$x;
$Yi2=$LegY-($i+1)*$height;
$Xi1=$LegX+$x;
$Yi1=$LegY-$i*$height;
ImageFilledPolygon($this->image,array($Xi1,$Yi1,$Xi2,$Yi2,$Xs2,$Ys2,$Xs1,$Ys1),4,$this->hour_colour[$i*$norm]);
imageString($this->image,1,$Xi2+$x/2,$Yi2,$minval+$i-1,$this->colour['black']);
}
// Draw the colour squares
for ($l=0;$l<($this->nbday);$l++) {
$h=(($l+1)<=$this->nbday)?($l+1):($l);
for ($j=0;$j<$this->nbhour;$j++) {
$kk=(($j+1)<=$this->nbhour)?($j+1):($j);
//if ( $debug = 1) { echo "$j, $h
"; }
/* $value is the index of the colour array */
$value=($this->datay[$j][$l]-$minval+1)*$norm;
// Calculate the coordinates of the coloured square
$Xs1=$departx+$h*$x;
$Ys1=$StartY-($j*$y);
$Xs2=$departx+$h*$x;
$Ys2=$StartY-($kk*$y);
$Xi1=$departx+$l*$x;
$Yi1=$StartY-($j*$y);
$Xi2=$departx+$l*$x;
$Yi2=$StartY-($kk*$y);
// Draw the square in a valid colour
if ($value > 0 && $value < $ncolours ) {
$colour=$this->hour_colour[$value];
ImageFilledPolygon($this->image,array($Xi1,$Yi1,$Xi2,$Yi2,$Xs2,$Ys2,$Xs1,$Ys1),4,$colour);
};
}; // end for $j=0;$j<$this->nbday;$j++
} // end for $h=($this->nbhour-1);$h>0;$h--
}
function create() {
Imagegif($this->image, $this->filename);
}
function destroy() {
ImageDestroy($this->image);
}
}
$graph=new graph(600,500); //width and height of the graph
srand(time());
if ($argv)
for ($i=1;$ifilename=$im.".gif";
$graph->datay=array();
$graph->nbcurve=24; //number of lines
$graph->nbday=31;
$graph->nbx=31; // nnmber of x on axe
$graph->nby=24; // number of y on axe
$graph->labelX=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32);
$graph->labelY=array("00","01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24");
$graph->title="Daily Temperature - ".$MONTH;
$graph->title_labelY="Hour";
$graph->title_labelX="Day";
// Generate a hue colour sequence from light blue through red to yellow
$index=0;
$red=0; $green=255; $blue=255;
for ($green=255;$green>=0;$green--) { $graph->colourtemp[$index][0]=$red; $graph->colourtemp[$index][1]=$green; $graph->colourtemp[$index][2]=$blue; $index++; }
$green=0;
for ($red=0;$red<256;$red++) { $graph->colourtemp[$index][0]=$red; $graph->colourtemp[$index][1]=$green; $graph->colourtemp[$index][2]=$blue; $index++; }
$red=255;
for ($blue=255;$blue>=0;$blue--) { $graph->colourtemp[$index][0]=$red; $graph->colourtemp[$index][1]=$green; $graph->colourtemp[$index][2]=$blue; $index++; }
$blue=0;
for ($green=0;$green<256;$green++) { $graph->colourtemp[$index][0]=$red; $graph->colourtemp[$index][1]=$green; $graph->colourtemp[$index][2]=$blue; $index++; }
// load a subset into the colour palette for the graph
$offset=0;
$offscale=1;
for ($ne=0;$ne<$index;$ne++) {
$ne2=($ne*$offscale)+$offset;
if ($ne2<$index) {
$graph->colouris($ne,$graph->colourtemp[$ne2][0],$graph->colourtemp[$ne2][1],$graph->colourtemp[$ne2][2]);
}
}
for ($ne=0;$ne<32;$ne++) { for ($nh=0;$nh<25;$nh++) { $graph->datay[$nh][$ne]=-100; } }
for ($element=0;$element<800;$element++) { $xmldata[$element] = -100; }
# read in the RRD XML file
$MONTH=$_GET['month'];
$XMLFILE="$MONTH.vals";
if (! $fh=fopen($XMLFILE,"r") ) {
echo "No such XML file $XMLFILE\n";
} else {
$element=0;
while (!feof($fh))
{
$string = chop(fgets($fh,32));
if ( $DEBUG ) echo "$string ";
if ( strcmp($string,"NaN") ) {
$xmldata[$element] = (float) $string;
if ( $xmldata[$element] == 0 ) $xmldata[$element] = -1000;
}
if ( $DEBUG ) echo "$xmldata[$element]
";
$element++;
}
$element=0;
for ($ne=0;$ne<31;$ne++) {
for ($nh=0;$nh<24;$nh++) {
//echo "$nh, $ne
";
$graph->datay[$nh][$ne] = round($xmldata[$element]);
$element++;
}
}
fclose ($fh);
$graph->create_graph();
$graph->create(); // create image
$graph->destroy(); // destroy image
}
?>