PHP histogram implementation code

  • 2020-03-31 17:03:48
  • OfStack

Still have the friend of doubt to be able to add my QQ: 460634320, everybody discusses together.
Effect:
< img Alt = "" border = 0 SRC =" http://files.jb51.net/upload/2009-12/20091204130131526.jpg ">
 
<?php 
function createImage($data,$twidth,$tspace,$height){ 
header("Content-Type:image/jpeg"); 
$dataname = array(); 
$datavalue = array();//The value in data
$i = 0; 
$j = 0; 
$k = 0; 
$num = sizeof($data); 

foreach($data as $key => $val){ 
$dataname[] = $key; 
$datavalue[] = $val; 
} 

$width = $num * ($twidth + $tspace) + 20 ;//Gets the width of the image
$im = imagecreate($width,$height);//Create an image

$bgcolor = imagecolorallocate($im,255,255,255);//The background color
$jcolor = imagecolorallocate($im,255,255,0);// rectangular The background color
$acolor = imagecolorallocate($im,0,0,0);//The color of the line

imageline($im,25,$height-20,$width-5,$height -20,$acolor);//The X axis
imageline($im,25,$height-20,25,2,$acolor);//Y
while($i< $num){ 
imagefilledrectangle($im,$i*($tspace+$twidth)+40,$height-$datavalue[$i]-20,$i*($twidth+$tspace)+$tspace+40,$height-20,$jcolor);//Draw a rectangular
imagestring($im,3,$i*($tspace+$twidth)+40+$twidth/2,$height-$datavalue[$i]-35,$datavalue[$i],$acolor);//Write the value on the column
imagestring($im,3,$i*($tspace+$twidth)+40+$twidth/2,$height-15,$dataname[$i],$acolor);//Write the value under the column
$i ++; 

} 
while($j < 400/10){ 
imageline($im,25,($height-20)-$j*8,28,($height-20)-$j*8,$acolor);//Draw the calibration
imagestring($im,2,5,($height-30)-$j*8,$j*10,$acolor);//Mark the scale
$j = $j +10; 
} 
imagejpeg($im); 
} 
$data =array("1"=>25,"2"=>30,"3" =>21 ); 
createImage($data,40,40,300); 

?> 

Related articles: