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 13 PHP
开启CURL扩展,让服务器支持PHP curl函数(远程采集)
Mar 19 PHP
php解压文件代码实现php在线解压
Feb 13 PHP
ThinkPHP使用心得分享-上传类UploadFile的使用
May 15 PHP
php实现的支持imagemagick及gd库两种处理的缩略图生成类
Sep 23 PHP
PHP处理Oracle的CLOB实例
Nov 03 PHP
PHP、Java des加密解密实例
Apr 27 PHP
php使用pdo连接sqlite3的配置示例
May 27 PHP
php封装的单文件(图片)上传类完整实例
Oct 18 PHP
php array_values 返回数组的值实例详解
Nov 17 PHP
Discuz不使用插件实现简单的打赏功能
Mar 21 PHP
Thinkphp 框架配置操作之动态配置、扩展配置及批量配置实例分析
May 15 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 方便水印和缩略图的图形类
2009/05/21 PHP
PHP中mysql_field_type()函数用法
2014/11/24 PHP
php简单实现MVC
2015/02/05 PHP
基于thinkPHP3.2实现微信接入及查询token值的方法
2017/04/18 PHP
php 算法之实现相对路径的实例
2017/10/17 PHP
PHP操作Redis常用技巧总结
2018/04/24 PHP
PHP获取远程http或ftp文件的md5值的方法
2019/04/15 PHP
thinkphp5框架前后端分离项目实现分页功能的方法分析
2019/10/08 PHP
Docker 安装 PHP并与Nginx的部署实例讲解
2021/02/27 PHP
js 图片缩放(按比例)控制代码
2009/05/27 Javascript
JavaScript性能陷阱小结(附实例说明)
2010/12/28 Javascript
jquery动态改变form属性提交表单
2014/06/03 Javascript
jQuery实现倒计时按钮功能代码分享
2014/09/03 Javascript
node.js中的fs.truncateSync方法使用说明
2014/12/15 Javascript
js拖拽的原型声明和用法总结
2016/04/04 Javascript
50 个 jQuery 插件可将你的网站带到另外一个高度
2016/04/26 Javascript
axios拦截设置和错误处理方法
2018/03/05 Javascript
浅析Vue中method与computed的区别
2018/03/06 Javascript
node.js部署之启动后台运行forever的方法
2018/05/23 Javascript
JS+HTML5本地存储Localstorage实现注册登录及验证功能示例
2020/02/10 Javascript
django批量导入xml数据
2016/10/16 Python
在Django同1个页面中的多表单处理详解
2017/01/25 Python
python中matplotlib实现最小二乘法拟合的过程详解
2017/07/11 Python
浅谈python中对于json写入txt文件的编码问题
2018/06/07 Python
实例讲解python中的协程
2018/10/08 Python
Python常见读写文件操作实例总结【文本、json、csv、pdf等】
2019/04/15 Python
python多线程与多进程及其区别详解
2019/08/08 Python
解决pandas展示数据输出时列名不能对齐的问题
2019/11/18 Python
Python tensorflow实现mnist手写数字识别示例【非卷积与卷积实现】
2019/12/19 Python
TUMI澳大利亚网站:美国旅行箱包品牌
2017/03/27 全球购物
英国最大最好的无人机商店:Drones Direct
2019/07/12 全球购物
全球工业:Global Industrial
2020/02/01 全球购物
数据库测试通常都包括哪些方面
2015/11/30 面试题
离婚代理词范文
2015/05/23 职场文书
旷工检讨书大全
2015/08/15 职场文书
高中16字霸气押韵班级口号集锦!
2019/06/27 职场文书