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 相关文章推荐
php输出表格的实现代码(修正版)
Dec 29 PHP
新浪微博API开发简介之用户授权(PHP基础篇)
Sep 25 PHP
php对mongodb的扩展(初出茅庐)
Nov 11 PHP
php获取用户IPv4或IPv6地址的代码
Nov 15 PHP
七款最流行的PHP本地服务器分享
Feb 19 PHP
基于PHP CURL获取邮箱地址的详解
Jun 03 PHP
ThinkPHP中处理表单中的注意事项
Nov 22 PHP
php array_key_exists() 与 isset() 的区别
Oct 24 PHP
php set_include_path函数设置 include_path 配置选项
Oct 30 PHP
php事件驱动化设计详解
Nov 10 PHP
PHP bin2hex()函数基础实例讲解
Feb 11 PHP
Yii框架 session 数据库存储操作方法示例
Nov 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
随机广告显示(PHP函数)
2006/10/09 PHP
PHPMailer使用教程(PHPMailer发送邮件实例分析)
2012/12/06 PHP
PHP文件上传类实例详解
2016/04/08 PHP
DOM 脚本编程中的兄弟节点
2009/10/31 Javascript
浅析JavaScript原型继承的陷阱
2013/12/03 Javascript
简述JavaScript中正则表达式的使用方法
2015/06/15 Javascript
js中DOM三级列表(代码分享)
2017/03/20 Javascript
AngularJS的脏检查深入分析
2017/04/22 Javascript
基于Bootstrap的标签页组件及bootstrap-tab使用说明
2017/07/25 Javascript
websocket4.0+typescript 实现热更新的方法
2019/08/14 Javascript
前端vue+elementUI如何实现记住密码功能
2020/09/20 Javascript
Javascript执行上下文顺序的深入讲解
2020/11/04 Javascript
详解uniapp的全局变量实现方式
2021/01/11 Javascript
[01:33]一分钟玩转DOTA2第三弹:DOTA2&DotA快捷操作大对比
2014/06/04 DOTA
python数据结构之二叉树的统计与转换实例
2014/04/29 Python
Python标准库之循环器(itertools)介绍
2014/11/25 Python
在Python的setuptools框架下生成egg的教程
2015/04/13 Python
Python实现简单字典树的方法
2016/04/29 Python
python机器学习理论与实战(二)决策树
2018/01/19 Python
windows下添加Python环境变量的方法汇总
2018/05/14 Python
我用Python抓取了7000 多本电子书案例详解
2019/03/25 Python
PyQt4实时显示文本内容GUI的示例
2019/06/14 Python
使用python-opencv读取视频,计算视频总帧数及FPS的实现
2019/12/10 Python
python将数据插入数据库的代码分享
2020/08/16 Python
LocalStorage记住用户和密码功能
2017/07/24 HTML / CSS
墨尔本复古时尚品牌:Dangerfield
2018/12/12 全球购物
一套英文Java笔试题面试题
2016/04/21 面试题
打架检讨书100字
2014/01/08 职场文书
护理专科自荐书范文
2014/02/18 职场文书
小学生通知书评语
2014/12/31 职场文书
会计求职自荐信
2015/03/26 职场文书
2015年幼儿教师个人工作总结
2015/05/20 职场文书
2016入党培训心得体会范文
2016/01/08 职场文书
个人业务学习心得体会
2016/01/25 职场文书
Django实现聊天机器人
2021/05/31 Python
Python保存并浏览用户的历史记录
2022/04/29 Python