• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP生成条形图的方法_php技巧

php 搞代码 4年前 (2022-01-26) 30次浏览 已收录 0个评论

本文实例讲述了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程序设计有所帮助。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP生成条形图的方法_php技巧
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址