加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP生成条形图的方法

发布时间:2020-12-12 20:33:27 所属栏目:PHP教程 来源:网络整理
导读:本篇章节讲解PHP生成条形图的方法。供大家参考研究。具体实现方法如下: 代码如下: // now we get the number of values in the array. this will // tell us how many columns to plot $columns = count($values); // set the height and width of

本篇章节讲解PHP生成条形图的方法。分享给大家供大家参考。具体实现方法如下:

代码如下:
// now we get the number of values in the array. this will
// tell us how many columns to plot
$columns = count($values);

// set the height and width of the graph image

$width = 300;
$height = 200;

// Set the amount of space between each column
$padding = 5;

// Get the width of 1 column
$column_width = $width / $columns ;

// set the graph color variables
$im = imagecreate($width,$height);
$gray = imagecolorallocate ($im,0xcc,0xcc);
$gray_lite = imagecolorallocate ($im,0xee,0xee);
$gray_dark = imagecolorallocate ($im,0x7f,0x7f);
$white = imagecolorallocate ($im,0xff,0xff);

// set the background color of the graph
imagefilledrectangle($im,$width,$height,$white);

// Calculate the maximum value we are going to plot
$max_value = max($values);

// loop over the array of columns
for($i=0;$i<$columns;$i++)
{
// set the column hieght for each value
$column_height = ($height / 100) * (( $values[$i] / $max_value)

100);
// now the coords
$x1 = $i
$column_width;
$y1 = $height-$column_height;
$x2 = (($i+1)*$column_width)-$padding;
$y2 = $height;

// write the columns over the background
imagefilledrectangle($im,$x1,$y1,$x2,$y2,$gray);

// This gives the columns a little 3d effect
imageline($im,$gray_lite);
imageline($im,$gray_dark);
}

// set the correct png headers
header ("Content-type: image/png");
// spit the image out the other end
imagepng($im);
?>

运行效果如下图所示:

希望本文所述对大家的PHP程序设计有所帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读