首页 / PHP / PHP生成条形图的方法_php技巧
PHP生成条形图的方法_php技巧
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP生成条形图的方法_php技巧,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1826字,纯文字阅读大概需要3分钟。
内容图文
本文实例讲述了PHP生成条形图的方法。分享给大家供大家参考。具体实现方法如下: 代码如下:
<?php
// create an array of values for the chart. These values
// could come from anywhere, POST, GET, database etc.
$values = array(23,32,35,57,12,3,36,54,32,15,43,24,30);
// 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,0xcc);
$gray_lite = imagecolorallocate ($im,0xee,0xee,0xee);
$gray_dark = imagecolorallocate ($im,0x7f,0x7f,0x7f);
$white = imagecolorallocate ($im,0xff,0xff,0xff);
// set the background color of the graph
imagefilledrectangle($im,0,0,$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,$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程序设计有所帮助。
内容总结
以上是互联网集市为您收集整理的PHP生成条形图的方法_php技巧全部内容,希望文章能够帮你解决PHP生成条形图的方法_php技巧所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。