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 相关文章推荐
一个改进的UBB类
Oct 09 PHP
php中看实例学正则表达式
Dec 25 PHP
PHP curl 并发最佳实践代码分享
Sep 05 PHP
PHP中操作ini配置文件的方法
Apr 25 PHP
linux下为php添加iconv模块的方法
Feb 28 PHP
php版微信发红包接口用法示例
Sep 23 PHP
PHP中大括号'{}'用法实例总结
Feb 08 PHP
PHP jQuery+Ajax结合写批量删除功能
May 19 PHP
php实现自定义中奖项数和概率的抽奖函数示例
May 26 PHP
laravel框架如何设置公共头和公共尾
Oct 22 PHP
php模拟post提交请求调用接口示例解析
Aug 07 PHP
ThinkPHP 5 AJAX跨域请求头设置实现过程解析
Oct 28 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求正负数数组中连续元素最大值示例
2014/04/11 PHP
PHP实现PDO的mysql数据库操作类
2014/12/12 PHP
php实现的美国50个州选择列表实例
2015/04/20 PHP
CI框架整合smarty步骤详解
2016/05/19 PHP
thinkPHP中钩子的两种配置调用方法详解
2016/11/11 PHP
PHP实现统计所有字符在字符串中出现次数的方法
2017/10/17 PHP
php测试kafka项目示例
2020/02/06 PHP
Javascript 入门基础学习
2010/03/10 Javascript
js图片自动切换效果处理代码
2013/05/07 Javascript
Js中获取frames中的元素示例代码
2013/07/30 Javascript
javascript作用域链(Scope Chain)用法实例解析
2015/11/30 Javascript
Bootstrap和Angularjs配合自制弹框的实例代码
2016/08/24 Javascript
JavaScript实现离开页面前提示功能【附jQuery实现方法】
2017/09/26 jQuery
解析Vue 2.5的Diff算法
2017/11/28 Javascript
详解angularjs实现echart图表效果最简洁教程
2017/11/29 Javascript
vue如何实现自定义底部菜单栏
2019/07/01 Javascript
angular中的post请求处理示例详解
2020/06/30 Javascript
基于javascript实现放大镜特效
2020/12/03 Javascript
python中MySQLdb模块用法实例
2014/11/10 Python
Python中正则表达式详解
2017/05/17 Python
解决python3中自定义wsgi函数,make_server函数报错的问题
2017/11/21 Python
Python使用pyodbc访问数据库操作方法详解
2018/07/05 Python
详解Python中的type和object
2018/08/15 Python
Python3自动签到 定时任务 判断节假日的实例
2018/11/13 Python
Python中一般处理中文的几种方法
2019/03/06 Python
瑞典度假品牌:OAS
2019/05/28 全球购物
德国药房apodiscounter中文官网:德国排名前三的网上药店
2019/06/03 全球购物
自动化专业毕业生自荐信
2013/11/01 职场文书
电子商务助理求职自荐信
2014/04/10 职场文书
党员群众路线承诺书
2014/05/20 职场文书
基石观后感
2015/06/12 职场文书
2019年度开业庆典祝福语大全!
2019/07/05 职场文书
话题作文之成长
2019/12/09 职场文书
Java 超详细讲解设计模式之中的抽象工厂模式
2022/03/25 Java/Android
python使用BeautifulSoup 解析HTML
2022/04/24 Python
Android开发手册自定义Switch开关按钮控件
2022/06/10 Java/Android