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
php下通过POST还是GET来传值
Jun 05 PHP
PHP实现PDO的mysql数据库操作类
Dec 12 PHP
php如何实现只替换一次或N次
Oct 29 PHP
使用PHP similar text计算两个字符串相似度
Nov 06 PHP
PHP中FTP相关函数小结
Jul 15 PHP
浅谈PHP的排列组合(如输入a,b,c 输出他们的全部组合)
Mar 14 PHP
php 数据结构之链表队列
Oct 17 PHP
Ubuntu 16.04中Laravel5.4升级到5.6的步骤
Dec 07 PHP
解决php写入数据库乱码的问题
Sep 17 PHP
php模式设计之观察者模式应用实例分析
Sep 25 PHP
laravel Task Scheduling(任务调度)在windows下的使用详解
Oct 22 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 字符串操作入门教程
2006/12/06 PHP
php验证是否是md5编码的简单代码
2014/04/01 PHP
php+mysqli使用面向对象方式更新数据库实例
2015/01/29 PHP
PHP+Ajax异步带进度条上传文件实例
2016/11/01 PHP
PHP基于自定义函数实现的汉字转拼音功能实例
2017/09/30 PHP
tp5(thinkPHP5框架)captcha验证码配置及验证操作示例
2019/05/28 PHP
详细讲解JS节点知识
2010/01/31 Javascript
js导航菜单(自写)简单大方
2013/03/28 Javascript
用nodejs写的一个简单项目打包工具
2013/05/11 NodeJs
JS简单限制textarea内输入字符数量的方法
2015/10/14 Javascript
谈谈对offsetleft兼容性的理解
2015/11/11 Javascript
AngularJS 实现弹性盒子布局的方法
2016/08/30 Javascript
vue-cli的webpack模板项目配置文件分析
2017/04/01 Javascript
垃圾回收器的相关知识点总结
2018/05/13 Javascript
深入剖析Node.js cluster模块
2018/05/23 Javascript
node.js到底要不要加分号浅析
2018/07/11 Javascript
解决vue 子组件修改父组件传来的props值报错问题
2019/11/09 Javascript
three.js 如何制作魔方
2020/07/31 Javascript
python安装PIL模块时Unable to find vcvarsall.bat错误的解决方法
2016/09/19 Python
tensorflow获取变量维度信息
2018/03/10 Python
Python使用progressbar模块实现的显示进度条功能
2018/05/31 Python
python重要函数eval多种用法解析
2020/01/14 Python
jupyter notebook 使用过程中python莫名崩溃的原因及解决方式
2020/04/10 Python
使用python批量修改XML文件中图像的depth值
2020/07/22 Python
python 下划线的不同用法
2020/10/24 Python
Ego Shoes官网:英国时髦鞋类品牌
2020/10/19 全球购物
房屋转让协议书
2014/10/18 职场文书
2014年学校教学工作总结
2014/12/06 职场文书
大学生简历自我评价2015
2015/03/03 职场文书
经营目标责任书
2015/05/08 职场文书
舌尖上的中国观后感
2015/06/02 职场文书
大学生暑期社会实践的个人总结!
2019/07/17 职场文书
python字符串常规操作大全
2021/05/02 Python
浅谈Python数学建模之数据导入
2021/06/23 Python
了解Kubernetes中的Service和Endpoint
2022/04/01 Servers
vue @ ~ 相对路径 路径别名设置方式
2022/06/05 Vue.js