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动态创建Flash动画
Oct 09 PHP
PHP base64+gzinflate压缩编码和解码代码
Oct 03 PHP
PHP中获取文件扩展名的N种方法小结
Feb 27 PHP
php selectradio和checkbox默认选择的实现方法详解
Jun 29 PHP
ThinkPHP验证码使用简明教程
Mar 05 PHP
浅谈php错误提示及查错方法
Jul 14 PHP
PHP中substr_count()函数获取子字符串出现次数的方法
Jan 07 PHP
利用PHP生成CSV文件简单示例
Dec 21 PHP
php实现查询功能(数据访问)
May 23 PHP
PHP微信企业号开发之回调模式开启与用法示例
Nov 25 PHP
PHP实现的二分查找算法实例分析
Dec 19 PHP
TP5(thinkPHP框架)实现后台清除缓存功能示例
May 29 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控制用户的浏览器--ob*函数的使用说明
2007/03/16 PHP
CodeIgniter图像处理类的深入解析
2013/06/17 PHP
PHP静态文件生成类实例
2014/11/29 PHP
PHP面向对象编程之深入理解方法重载与方法覆盖(多态)
2015/12/24 PHP
Yii2框架控制器、路由、Url生成操作示例
2019/05/27 PHP
改变javascript函数内部this指针指向的三种方法
2010/04/23 Javascript
JavaScript去掉空格的方法集合
2010/12/28 Javascript
jQuery方法简洁实现隔行换色及toggleClass的使用
2013/03/15 Javascript
jquery自定义类似$.ajax()的方法实现代码
2013/08/13 Javascript
Js实现双击鼠标自动滚动屏幕的示例代码
2013/12/14 Javascript
jQuery动态改变图片显示大小(修改版)的实现思路及代码
2013/12/24 Javascript
介绍一个简单的JavaScript类框架
2015/06/24 Javascript
jQuery左右滚动支持图片放大缩略图图片轮播代码分享
2015/08/26 Javascript
浅析Node.js:DNS模块的使用
2016/11/23 Javascript
JS排序之冒泡排序详解
2017/04/08 Javascript
node.js+jQuery实现用户登录注册AJAX交互
2017/04/28 jQuery
js实现把时间戳转换为yyyy-MM-dd hh:mm 格式(es6语法)
2017/12/28 Javascript
js canvas实现画图、滤镜效果
2018/11/27 Javascript
node.js中ws模块创建服务端和客户端,网页WebSocket客户端
2019/03/06 Javascript
微信小程序实现的绘制table表格功能示例
2019/04/26 Javascript
Python中的自省(反射)详解
2015/06/02 Python
python脚本作为Windows服务启动代码详解
2018/02/11 Python
Python使用numpy模块创建数组操作示例
2018/06/20 Python
学生信息管理系统python版
2018/10/17 Python
使用Python制作表情包实现换脸功能
2019/07/19 Python
Python matplotlib可视化实例解析
2020/06/01 Python
Python爬虫基于lxml解决数据编码乱码问题
2020/07/31 Python
python 实现网易邮箱邮件阅读和删除的辅助小脚本
2021/03/01 Python
Etam艾格英国官网:法国著名女装品牌
2019/04/15 全球购物
校园之声广播稿
2014/01/31 职场文书
英语求职信范文
2014/05/23 职场文书
月度优秀员工获奖感言
2014/08/16 职场文书
交通局领导班子群众路线教育实践活动对照检查材料思想汇报
2014/10/09 职场文书
关于长城的导游词
2015/01/30 职场文书
乡镇司法所2015年度工作总结
2015/10/14 职场文书
python基础之爬虫入门
2021/05/10 Python