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语法(5)
Oct 09 PHP
PHP 中英文混合排版中处理字符串常用的函数
Apr 12 PHP
php urlencode()与urldecode()函数字符编码原理详解
Dec 06 PHP
使用PHP实现蜘蛛访问日志统计
Jul 05 PHP
php多层数组与对象的转换实例代码
Aug 05 PHP
PHP中4种常用的抓取网络数据方法
Jun 04 PHP
php实现有趣的人品测试程序实例
Jun 08 PHP
php 利用socket发送HTTP请求(GET,POST)
Aug 24 PHP
php 参数过滤、数据过滤详解
Oct 26 PHP
微信公众号开发之语音消息识别php代码
Aug 08 PHP
PHP数组生成XML格式数据的封装类实例
Nov 10 PHP
PHP实现双链表删除与插入节点的方法示例
Nov 11 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 __autoload()方法真的影响性能吗?
2012/03/30 PHP
基于php伪静态的实现详细介绍
2013/04/28 PHP
PHP与Java进行通信的实现方法
2013/10/21 PHP
php跨域cookie共享使用方法
2014/02/20 PHP
codeigniter发送邮件并打印调试信息的方法
2015/03/21 PHP
PHP+Ajax实现无刷新分页实例详解(附demo源码下载)
2016/04/07 PHP
PHP5.3连接Oracle客户端及PDO_OCI模块的安装方法
2016/05/13 PHP
关于 byval 与 byref 的区别分析总结
2007/10/08 Javascript
TreeView 用法(有代码)(asp.net)
2011/07/15 Javascript
基于OL2实现百度地图ABCD marker的效果
2015/10/01 Javascript
JS实现的论坛Ajax打分效果完整实例
2015/10/31 Javascript
关于验证码在IE中不刷新的快速解决方法
2016/09/23 Javascript
js实现加载更多功能实例
2016/10/27 Javascript
ES6中的箭头函数实例详解
2017/04/06 Javascript
微信小程序开发之toast提示插件使用示例
2017/06/08 Javascript
使用vue官方提供的模板vue-cli搭建一个helloWorld案例分析
2018/01/16 Javascript
vue实现短信验证码登录功能(流程详解)
2019/12/10 Javascript
ES6 Symbol在对象中的作用实例分析
2020/06/06 Javascript
javascript实现倒计时提示框
2021/03/02 Javascript
Django学习笔记之Class-Based-View
2017/02/15 Python
详解Python文本操作相关模块
2017/06/22 Python
windows下python安装paramiko模块和pycrypto模块(简单三步)
2017/07/06 Python
Python迭代器定义与简单用法分析
2018/04/30 Python
Python爬虫之UserAgent的使用实例
2019/02/21 Python
python flask中动态URL规则详解
2019/11/22 Python
Python爬取酷狗MP3音频的步骤
2021/02/26 Python
HTML高亮关键字的实现代码
2018/10/22 HTML / CSS
Joe Fresh官网:加拿大时尚品牌和零售连锁店
2016/11/30 全球购物
大一期末自我鉴定
2013/12/13 职场文书
岗位职责风险防控
2014/02/18 职场文书
销售经理竞聘书
2014/03/31 职场文书
股权转让意向书
2014/04/01 职场文书
开学典礼演讲稿
2014/05/23 职场文书
支部书记四风对照材料
2014/08/28 职场文书
给医院的感谢信
2015/01/21 职场文书
分析设计模式之模板方法Java实现
2021/06/23 Java/Android