note 60691 deleted from ref.image by tularis
| From: | tularis@php.net | Date: | Sun, 29 Jun 2008 21:03:26 +0000 |
| Subject: | note 60691 deleted from ref.image by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-140930@lists.php.net to get a copy of this message | ||
Note Submitter: matrixhasu at gmail dot com
----
I've take the example of Peter Hulstaert and modified so:
1. grid width is scaled basing on the number of data points to print;
2. images are scaled by min and max values:
even if min is 1000, that point will be very close to the image bottom
Hope this will helpfull to someone.
<?
//Original from peter dot hulstaert at gmail dot com
//Available at http://it.php.net/manual/en/ref.image.php#60410
header("Content-type: image/png");
// Define variables
$Values=array(120,190,130,155,150,140,320,150,140,186,240,128,650);
$imgWidth=500;
$imgHeight=200;
$grid=25;
$graphspacing=0.07;
//Scales number of vertical grid lines for the number of data to print
$gridW=$imgWidth/(count($Values)-1);
//Get min and max values to scale image
for ($i=0; $i<count($Values); $i++){
if ($Values[$i]>$max){$max=$Values[$i];}
}
//Min values are less the maximum...
$min = $max;
for ($i=0; $i<count($Values); $i++){
if ($Values[$i]<$min){$min=$Values[$i];}
}
for ($i=0; $i<count($Values); $i++){
$graphValues[$i] =
($Values[$i] - $min*(1-2*$graphspacing)) *
(($imgHeight*(1-$graphspacing))/($max-$min));
}
// Create image and define colors
$image=imagecreate($imgWidth, $imgHeight);
$colorWhite=imagecolorallocate($image, 255, 255, 255);
$colorGrey=imagecolorallocate($image, 192, 192, 192);
$colorBlue=imagecolorallocate($image, 0, 0, 255);
// Create border around image
imageline($image, 0, 0, 0, $imgHeight, $colorGrey);
imageline($image, 0, 0, $imgWidth, 0, $colorGrey);
imageline($image, $imgWidth-1, 0, $imgWidth-1, $imgHeight-1, $colorGrey);
imageline($image, 0, $imgHeight-1, $imgWidth-1, $imgHeight-1, $colorGrey);
// Create grid
for ($i=1; $i<($imgWidth/$gridW); $i++)
{imageline($image, $i*$gridW, 0, $i*$gridW, $imgHeight, $colorGrey);}
for ($i=1; $i<($imgHeight/$grid); $i++)
{imageline($image, 0, $i*$grid, $imgWidth, $i*$grid, $colorGrey);}
//Create the lines connecting points
for ($i=0; $i<count($graphValues)-1; $i++)
{imageline($image, $i*$gridW, ($imgHeight-$graphValues[$i]),
($i+1)*$gridW, ($imgHeight-$graphValues[$i+1]), $colorBlue);}
// Output graph and clear image from memory
imagepng($image);
imagedestroy($image);
?>