Heim  >  Artikel  >  Backend-Entwicklung  >  柱状图 PHP 柱状图实现代码

柱状图 PHP 柱状图实现代码

WBOY
WBOYOriginal
2016-07-29 08:41:191333Durchsuche

还有疑问的朋友可以加我QQ:460634320,大家一起讨论。
效果图:
 PHP 柱状图实现代码

复制代码 代码如下:


function createImage($data,$twidth,$tspace,$height){
header("Content-Type:image/jpeg");
$dataname = array();
$datavalue = array();//data里面的值
$i = 0;
$j = 0;
$k = 0;
$num = sizeof($data);
foreach($data as $key => $val){
$dataname[] = $key;
$datavalue[] = $val;
}
$width = $num * ($twidth + $tspace) + 20 ;//获取图像的宽度
$im = imagecreate($width,$height);//创建图像
$bgcolor = imagecolorallocate($im,255,255,255);//背景色
$jcolor = imagecolorallocate($im,255,255,0);//矩形的背景色
$acolor = imagecolorallocate($im,0,0,0);//线的颜色
imageline($im,25,$height-20,$width-5,$height -20,$acolor);//X轴
imageline($im,25,$height-20,25,2,$acolor);//Y轴
while($iimagefilledrectangle($im,$i*($tspace+$twidth)+40,$height-$datavalue[$i]-20,$i*($twidth+$tspace)+$tspace+40,$height-20,$jcolor);//画矩形
imagestring($im,3,$i*($tspace+$twidth)+40+$twidth/2,$height-$datavalue[$i]-35,$datavalue[$i],$acolor);//在柱子上面写出值
imagestring($im,3,$i*($tspace+$twidth)+40+$twidth/2,$height-15,$dataname[$i],$acolor);//在柱子下面写出值
$i ++;
}
while($j imageline($im,25,($height-20)-$j*8,28,($height-20)-$j*8,$acolor);//画出刻度
imagestring($im,2,5,($height-30)-$j*8,$j*10,$acolor);//标出刻度值
$j = $j +10;
}
imagejpeg($im);
}
$data =array("1"=>25,"2"=>30,"3" =>21 );
createImage($data,40,40,300);
?>

以上就介绍了柱状图 PHP 柱状图实现代码,包括了柱状图方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn