PHP生成条形图的方法


Posted in PHP onDecember 10, 2014

本文实例讲述了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 相关文章推荐
WordPress中&quot;无法将上传的文件移动至&quot;错误的解决方法
Jul 01 PHP
在Win2003(64位)中配置IIS6+PHP5.2.17+MySQL5.5的运行环境
Apr 04 PHP
php实现当前页面点击下载文件的简单方法
Sep 22 PHP
php自定义函数转换html标签示例
Sep 29 PHP
PHP 根据key 给二维数组分组
Dec 09 PHP
PHP使用finfo_file()函数检测上传图片类型的实现方法
Apr 18 PHP
Laravel中获取路由参数Route Parameters的五种方法示例
Sep 29 PHP
详解php语言最牛掰的Laravel框架
Nov 20 PHP
PHP微信企业号开发之回调模式开启与用法示例
Nov 25 PHP
Yii框架参数配置文件params用法实例分析
Sep 11 PHP
laravel实现上传图片的两种方式小结
Oct 12 PHP
php判断IP地址是否在多个IP段内
Aug 18 PHP
php自定文件保存session的方法
Dec 10 #PHP
php通过session防url攻击方法
Dec 10 #PHP
php利用cookies实现购物车的方法
Dec 10 #PHP
php针对cookie操作的队列操作类实例
Dec 10 #PHP
php利用cookie实现自动登录的方法
Dec 10 #PHP
PHP使用header()输出图片缓存实例
Dec 09 #PHP
PHP实现服务器状态监控的方法
Dec 09 #PHP
You might like
Zend公司全球首推PHP认证
2006/10/09 PHP
smarty实现多级分类的方法
2014/12/05 PHP
值得分享的php+ajax实时聊天室
2016/07/20 PHP
PHP实现小偷程序实例
2016/10/31 PHP
laravel model模型处理之修改查询或修改字段时的类型格式案例
2019/10/17 PHP
Laravel框架中队列和工作(Queues、Jobs)操作实例详解
2020/04/06 PHP
Add Formatted Data to a Spreadsheet
2007/06/12 Javascript
关于js中window.location.href,location.href,parent.location.href,top.location.href的用法与区别
2010/10/18 Javascript
超级有用的13个基于jQuery的内容滚动插件和教程
2011/07/31 Javascript
在javascript中执行任意html代码的方法示例解读
2013/12/25 Javascript
AngularJs Dependency Injection(DI,依赖注入)
2016/09/02 Javascript
JavaScript查看代码运行效率console.time()与console.timeEnd()用法
2019/01/18 Javascript
基于Express框架使用POST传递Form数据
2019/08/10 Javascript
详解vue 中 scoped 样式作用域的规则
2020/09/14 Javascript
JavaScript仿京东轮播图效果
2021/02/25 Javascript
横向对比分析Python解析XML的四种方式
2016/03/30 Python
python做量化投资系列之比特币初始配置
2018/01/23 Python
python中正则表达式的使用方法
2018/02/25 Python
python中logging包的使用总结
2018/02/28 Python
对Python闭包与延迟绑定的方法详解
2019/01/07 Python
pytorch实现MNIST手写体识别
2020/02/14 Python
python GUI库图形界面开发之PyQt5下拉列表框控件QComboBox详细使用方法与实例
2020/02/27 Python
Pycharm在指定目录下生成文件和删除文件的实现
2020/12/28 Python
python中的unittest框架实例详解
2021/02/05 Python
css3实现波纹特效、H5实现动态波浪效果
2018/01/31 HTML / CSS
css3实现背景颜色渐变让图片不再是唯一的实现方式
2012/12/18 HTML / CSS
viagogo法国票务平台:演唱会、体育比赛、戏剧门票
2017/03/27 全球购物
Urban Outfitters德国官网:美国跨国生活方式零售公司
2018/05/21 全球购物
遗体告别仪式答谢词
2014/01/23 职场文书
高三生物教学反思
2014/01/25 职场文书
市场营销管理毕业生自荐信
2014/03/03 职场文书
选秀节目策划方案
2014/06/06 职场文书
大学生学习计划书
2014/09/15 职场文书
群众路线教育实践活动个人对照检查材料思想汇报(社区班子)
2014/10/06 职场文书
《合作意向书》怎么写?
2019/08/20 职场文书
用Python写一个简易版弹球游戏
2021/04/13 Python