Heim >Backend-Entwicklung >PHP-Tutorial > php绘图,请圣人修改。
php绘图,请高人修改。。
<?<br /> $kuan=30;//色柱宽<br /> $jiange=20;//色柱间间隔<br /> $zuo=20;//左侧留空<br /> $you=20;//右侧留空<br /> $shang=20;//上留空<br /> $xia=10;//下留空<br /> $zuidashujuzhi=1;//初始化纵轴最大数据值<br /> if ($_GET["a"]=="") die("error id:0");<br /> $shuju=split(",",$_GET["a"]);<br /> //得到最大值<br /> for($i=0;$i<count($shuju);$i++){<br /> if(!is_numeric($shuju[$i])) die("error id:1");<br /> if($shuju[$i]>$zuidashujuzhi) $zuidashujuzhi=$shuju[$i];<br /> }<br /> //计算图像宽度 <br /> $img_kuan=$zuo+$you+$jiange+count($shuju)*($kuan+$jiange);<br /> //图像高 <br /> $img_gao=170;<br /> //存储色柱高度的数组<br /> $zhugaodu = array();<br /> $image = imagecreate($img_kuan,$img_gao);<br /> $white = imagecolorallocate($image, 0xEE, 0xEE, 0xEE);<br /> //色柱颜色<br /> $shuju_yanse =array(<br /> imagecolorallocate($image, 0x97, 0xbd, 0x00),<br /> imagecolorallocate($image, 0x00, 0x99, 0x00),<br /> imagecolorallocate($image, 0xcc, 0x33, 0x00),<br /> imagecolorallocate($image, 0xff, 0xcc, 0x00),<br /> imagecolorallocate($image, 0x33, 0x66, 0xcc),<br /> imagecolorallocate($image, 0x33, 0xcc, 0x33),<br /> imagecolorallocate($image, 0xff, 0x99, 0x33),<br /> imagecolorallocate($image, 0xcc, 0xcc, 0x99),<br /> imagecolorallocate($image, 0x99, 0xcc, 0x66),<br /> imagecolorallocate($image, 0x66, 0xff, 0x99)<br /> );<br /> <br /> <br /> <br /> //坐标轴颜色<br /> $zuobiao_yanse = imagecolorallocate($image, 0x00, 0x00, 0x00);<br /> //横轴<br /> imageline ( $image, $zuo, $img_gao-$xia, $img_kuan-$you/2, $img_gao-$xia, $zuobiao_yanse);<br /> //纵轴<br /> imageline ( $image, $zuo, $shang/2, $zuo, $img_gao-$xia, $zuobiao_yanse);<br /> <br /> <br /> <br /> //纵轴刻度,纵轴上共标注4个点,所以这里分别计算即可<br /> imageline ( $image, $zuo, $shang, $zuo+6, $shang, $zuobiao_yanse);<br /> imagestring ( $image, 3, $zuo/4, $shang,round($zuidashujuzhi), $zuobiao_yanse);<br /> imageline ( $image, $zuo, $shang+($img_gao-$shang-$xia)*1/4, $zuo+6, round($shang+($img_gao-$shang-$xia)*1/4), $zuobiao_yanse);<br /> imagestring ( $image, 3, $zuo/4, $shang+($img_gao-$shang-$xia)*1/4,round($zuidashujuzhi*3/4), $zuobiao_yanse);<br /> imageline ( $image, $zuo, $shang+($img_gao-$shang-$xia)*2/4, $zuo+6, $shang+($img_gao-$shang-$xia)*2/4, $zuobiao_yanse);<br /> imagestring ( $image, 3, $zuo/4, $shang+($img_gao-$shang-$xia)*2/4,round($zuidashujuzhi*2/4), $zuobiao_yanse);<br /> imageline ( $image, $zuo, $shang+($img_gao-$shang-$xia)*3/4, $zuo+6, $shang+($img_gao-$shang-$xia)*3/4, $zuobiao_yanse);<br /> imagestring ( $image, 3, $zuo/4, $shang+($img_gao-$shang-$xia)*3/4,round($zuidashujuzhi*1/4), $zuobiao_yanse);<br /> <br /> <br /> <br /> //得到每个柱的高度<br /> for($i=0;$i<count($shuju);$i++){<br /> array_push ($zhugaodu, round(($img_gao-$shang-$xia)*$shuju[$i]/$zuidashujuzhi));<br /> }<br /> //画数据柱<br /> $shuju_yanse_int=0;<br /> for($i=0;$i<count($shuju);$i++){<br /> imagefilledrectangle( $image,$zuo+$jiange+$i*($kuan+$jiange),$shang+($img_gao-$shang-$xia)-$zhugaodu[$i],$zuo+$jiange+$i*($kuan+$jiange)+$kuan,($img_gao-$xia)-1 ,$shuju_yanse[$shuju_yanse_int]);<br /> //因为只定义了10种颜色,所以这里做一个循环 <br /> if($shuju_yanse_int==9){<br /> $shuju_yanse_int=0;<br /> }else{<br /> $shuju_yanse_int++;<br /> }<br /> }<br /> //标注数据柱上方数据值<br /> for($i=0;$i<count($shuju);$i++){<br /> imagestring ( $image, 1, $zuo+$jiange+$i*($kuan+$jiange)+2,$shang+($img_gao-$shang-$xia)-$zhugaodu[$i]-10,$shuju[$i], $zuobiao_yanse);<br /> }<br /> header('Content-type: image/png');<br /> imagepng($image);<br /> imagedestroy($image);<br /> ?>