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开发大型项目的一点经验
Oct 09 PHP
mysql4.1以上版本连接时出现Client does not support authentication protocol问题解决办法
Mar 15 PHP
纯php打造的tab选项卡效果代码(不用js)
Dec 29 PHP
解析php二分法查找数组是否包含某一元素
May 23 PHP
php通过数组实现多条件查询实现方法(字符串分割)
May 06 PHP
php实现多维数组中每个单元值(数字)翻倍的方法
Feb 16 PHP
PHP的Socket网络编程入门指引
Aug 11 PHP
PHP 验证身份证是否合法的函数
Feb 09 PHP
php简单处理XML数据的方法示例
May 19 PHP
Laravel接收前端ajax传来的数据的实例代码
Jul 20 PHP
PHP实现自动发送邮件功能代码(qq 邮箱)
Aug 18 PHP
基于ThinkPHP5.0实现图片上传插件
Sep 25 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
使用 eAccelerator加速PHP代码的方法
2007/09/30 PHP
php 判断访客是否为搜索引擎蜘蛛的函数代码
2011/07/29 PHP
基于PHP读取csv文件内容的详解
2013/06/18 PHP
PHP实现的限制IP投票程序IP来源分析
2016/05/04 PHP
CI框架简单邮件发送类实例
2016/05/18 PHP
Laravel框架中Blade模板的用法示例
2017/08/30 PHP
php实现微信发红包功能
2018/07/13 PHP
前台js对象在后台转化java对象的问题探讨
2013/12/20 Javascript
屏蔽IE弹出&quot;您查看的网页正在试图关闭窗口,是否关闭此窗口&quot;的方法
2013/12/31 Javascript
通过$(this)使用jQuery包装后的方法或属性
2014/05/18 Javascript
javascript 操作符(~、&amp;、|、^、)使用案例
2014/12/31 Javascript
js控制网页前进和后退的方法
2015/06/08 Javascript
浅谈使用MVC模式进行JavaScript程序开发
2015/11/10 Javascript
JavaScript正则表达式匹配 div  style标签
2016/03/15 Javascript
BootStrap智能表单实战系列(三)分块表单配置详解
2016/06/13 Javascript
jquery 获取select数组与name数组长度的实现代码
2016/06/20 Javascript
Vue.js每天必学之Class与样式绑定
2016/09/05 Javascript
jQuery弹出div层过2秒自动消失
2016/11/29 Javascript
原生js实现可拖拽效果
2017/02/28 Javascript
Three.js基础学习教程
2017/11/16 Javascript
vue单页面实现当前页面刷新或跳转时提示保存
2018/11/02 Javascript
VSCode写vue项目一键生成.vue模版,修改定义其他模板的方法
2020/04/17 Javascript
如何在 Vue 中使用 JSX
2021/02/14 Vue.js
[03:54]DOTA2英雄梦之声_第06期_昆卡
2014/06/23 DOTA
python开发中range()函数用法实例分析
2015/11/12 Python
20招让你的Python飞起来!
2016/09/27 Python
Python实现脚本锁功能(同时只能执行一个脚本)
2017/05/10 Python
CSS3实现时间轴特效
2020/11/02 HTML / CSS
ALDI奥乐齐官方海外旗舰店:德国百年超市
2017/12/27 全球购物
索尼巴西商店:Sony巴西
2019/06/21 全球购物
数控专业毕业生求职信范文
2013/09/21 职场文书
英文请假条
2014/04/11 职场文书
工作求职自荐信
2014/06/13 职场文书
单身证明范本
2015/06/15 职场文书
详解Js模块化的作用原理和方案
2021/04/29 Javascript
OpenFeign实现远程调用
2022/08/14 Java/Android