Home >Backend Development >PHP Tutorial >Bar chart PHP bar chart implementation code

Bar chart PHP bar chart implementation code

WBOY
WBOYOriginal
2016-07-29 08:41:191393browse

If you still have questions, you can add me on QQ: 460634320 and let’s discuss together.
Rendering:
 PHP 柱状图实现代码

Copy code The code is as follows:


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;//Get the width of the image
$im = imagecreate($width,$height);//Create the image
$bgcolor = imagecolorallocate($im,255,255,255);//Background color
$jcolor = imagecolorallocate($im,255,255,0);//The background color of the rectangle
$acolor = imagecolorallocate($im,0,0,0);//The color of the line
imageline($im,25,$height-20, $width-5,$height -20,$acolor);//X-axis
imageline($im,25,$height-20,25,2,$acolor);//Y-axis
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 the value on the column
imagestring($im,3,$i*($tspace+$twidth)+40+$twidth/2,$height-15,$dataname[ $i],$acolor);//Write the value below the column
$i ++;
}
while($j < 400/10){
imageline($im,25,($height-20) -$j*8,28,($height-20)-$j*8,$acolor);//Draw the scale
imagestring($im,2,5,($height-30)-$j*8 ,$j*10,$acolor);//Mark the scale value
$j = $j +10;
}
imagejpeg($im);
}
$data =array("1"=>25, "2"=>30,"3" =>21 );
createImage($data,40,40,300);
?>

The above introduces the bar chart PHP bar chart implementation code, including the content of the bar chart. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn