-
/**
- * PHP histogram imagefilledrectugle function use case
- * by bbs.it-home.org
- */
- function CreateImage ($data, $twidth, $tspace, $height) {
- Header ("Content-type:image/jpeg");
- $dataname = Array ();
- $datavalue = Array (); The value inside the//data
- $i = 0;
- $j = 0;
- $k = 0;
- $num = sizeof ($DATA);
-
foreach ($data as $key = = $val) {
- $dataname [] = $key;
- $datavalue [] = $val;
- }
-
$width = $num * ($twidth + $tspace) + 20;//Get the width of the image
- $im = Imagecreate ($width, $height);//Create Image
-
$bgcolor = Imagecolorallocate ($im, 255,255,255);//Background color
- $jcolor = Imagecolorallocate ($im, 255,255,0);//Background color of Rectangle
- $acolor = Imagecolorallocate ($im, 0,0,0);//Line Color
-
Imageline ($im, $height -20, $width -5, $height -20, $acolor);//x axis
- Imageline ($im, $height -20,25,2, $acolor);//y Shaft
- while ($i < $num) {
- Imagefilledrectangle ($im, $i * ($tspace + $twidth) +40, $height-$datavalue [$i]-20, $i * ($twidth + $tspace) + $tspace +40,$ HEIGHT-20, $jcolor);//Draw a rectangle
- Imagestring ($im, 3, $i * ($tspace + $twidth) +40+ $twidth/2, $height-$datavalue [$i]-35, $datavalue [$i], $acolor);// Write a value on a pillar
- Imagestring ($im, 3, $i * ($tspace + $twidth) +40+ $twidth/2, $height -15, $dataname [$i], $acolor);//write a value under a pillar
- $i + +;
-
}
- while ($j < 400/10) {
- Imageline ($im, $height -20)-$j *8,28, ($height -20)-$j *8, $acolor);//Draw a scale
- Imagestring ($im, 2,5, ($height -30)-$j *8, $j *10, $acolor);//Mark scale value
- $j = $j +10;
- }
- Imagejpeg ($im);
- }
- $data =array ("1" =>25, "2" =>30, "3" =>21);
- CreateImage ($data, 40,40,300);
- ?>
Copy Code |