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 相关文章推荐
phpexcel导入excel数据使用方法实例
Dec 24 PHP
php下foreach提示Warning:Invalid argument supplied for foreach()的解决方法
Nov 11 PHP
smarty内置函数{loteral}、{ldelim}和{rdelim}用法实例
Jan 22 PHP
PHP调用.NET的WebService 简单实例
Mar 27 PHP
php文件读取方法实例分析
Jun 20 PHP
php多线程实现方法及用法实例详解
Oct 26 PHP
PHP数学运算与数据处理实例分析
Apr 01 PHP
浅谈PHP Cookie处理函数
Jun 10 PHP
PHP会话控制实例分析
Dec 24 PHP
php实现与python进行socket通信的方法示例
Aug 30 PHP
完美的php分页类
Oct 24 PHP
PHP简单实现解析xml为数组的方法
May 02 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制作简单的内容采集器的代码
2007/11/28 PHP
Server.HTMLEncode让代码在页面里显示为源代码
2013/12/08 PHP
php 创建以UNIX时间戳命名的文件夹(示例代码)
2014/03/08 PHP
Script的加载方法小结
2011/01/12 Javascript
基于jquery完美拖拽,可返回拖动轨迹
2012/03/29 Javascript
解析URI与URL之间的区别与联系
2013/11/22 Javascript
JQuery插件Marquee.js实现无缝滚动效果
2016/04/26 Javascript
fullpage.js全屏滚动插件使用实例
2016/09/06 Javascript
原生js编写焦点图效果
2016/12/08 Javascript
jQuery实现页面顶部下拉广告
2016/12/30 Javascript
Node.js的Mongodb使用实例
2016/12/30 Javascript
解决微信二次分享不显示摘要和图片的问题
2017/08/18 Javascript
从对象列表中获取一个对象的方法,依据关键字和值
2017/09/20 Javascript
使用vue 国际化i18n 实现多实现语言切换功能
2018/10/11 Javascript
Node.js + express实现上传大文件的方法分析【图片、文本文件】
2019/03/14 Javascript
微信小程序云开发之模拟后台增删改查
2019/05/16 Javascript
vue + typescript + video.js实现 流媒体播放 视频监控功能
2019/07/07 Javascript
vue 数据操作相关总结
2020/12/17 Vue.js
Django中的CACHE_BACKEND参数和站点级Cache设置
2015/07/23 Python
Python搜索引擎实现原理和方法
2017/11/27 Python
tensorflow TFRecords文件的生成和读取的方法
2018/02/06 Python
对Python3 解析html的几种操作方式小结
2019/02/16 Python
基于Python实现拆分和合并GIF动态图
2019/10/22 Python
Python enumerate() 函数如何实现索引功能
2020/06/29 Python
洛杉矶生活休闲而精致的基础品牌:Mika Jaymes
2018/01/07 全球购物
Spartoo荷兰:鞋子、包包和服装
2018/07/12 全球购物
英国奢侈品概念店:Base Blu
2019/05/16 全球购物
现代绅士日常奢侈品:Todd Snyder
2019/12/13 全球购物
爱我中华教学反思
2014/04/28 职场文书
机械工程及自动化专业求职信
2014/09/03 职场文书
2015年毕业生实习评语
2015/03/25 职场文书
小学班主任工作总结2015
2015/04/07 职场文书
2015教师个人师德工作总结
2015/10/23 职场文书
2016春季田径运动会广播稿
2015/12/21 职场文书
小学数学教学反思范文
2016/02/16 职场文书
教你使用Ubuntu搭建DNS服务器
2022/09/23 Servers