本文实例讲述了PHP生成条形图的方法。分享给大家供大家参考。具体实现方法如下:
<br /><?php <br /> // create an array of values for the chart. These values <br /> // could come from anywhere, POST, GET, database etc. <br /> $values = array(23,32,35,57,12,3,36,54,32,15,43,24,30); <br /> <br /> // now we get the number of values in the array. this will <br /> // tell us how many columns to plot <br /> $columns = count($values); <br /> <br /> // set the height and width of the graph image <br /> <br /> $width = 300; <br /> $height = 200; <br /> <br /> // Set the amount of space between each column <br /> $padding = 5; <br /> <br /> // Get the width of 1 column <br /> $column_width = $width / $columns ; <br /> <br /> // set the graph color variables <br /> $im = imagecreate($width,$height); <br /> $gray = imagecolorallocate ($im,0xcc,0xcc,0xcc); <br /> $gray_lite = imagecolorallocate ($im,0xee,0xee,0xee); <br /> $gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f); <br /> $white = imagecolorallocate ($im,0xff,0xff,0xff); <br /> <br /> // set the background color of the graph <br /> imagefilledrectangle($im,0,0,$width,$height,$white); <br /> <br /> <br /> // Calculate the maximum value we are going to plot <br /> $max_value = max($values); <br /> <br /> // loop over the array of columns <br /> for($i=0;$i<$columns;$i++) <br /> { <br /> // set the column hieght for each value <br /> $column_height = ($height / 100) * (( $values[$i] / $max_value) <br /> <br />*100); <br /> // now the coords <br /> $x1 = $i*$column_width; <br /> $y1 = $height-$column_height; <br /> $x2 = (($i+1)*$column_width)-$padding; <br /> $y2 = $height; <br /> <br /> // write the columns over the background <br /> imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray); <br /> <br /> // This gives the co<b>6本文来源gao@dai!ma.com搞$代^码!网7</b><pre>搞gaodaima代码
lumns a little 3d effect
imageline($im,$x1,$y1,$x1,$y2,$gray_lite);
imageline($im,$x1,$y2,$x2,$y2,$gray_lite);
imageline($im,$x2,$y1,$x2,$y2,$gray_dark);
}
// set the correct png headers
header (“Content-type: image/png”);
// spit the image out the other end
imagepng($im);
?>
运行效果如下图所示:
希望本文所述对大家的PHP程序设计有所帮助。