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中调用JAVA
Oct 09 PHP
介绍几个array库的新函数 php
Dec 29 PHP
php数组转换js数组操作及json_encode的用法详解
Oct 26 PHP
curl不使用文件存取cookie php使用curl获取cookie示例
Jan 26 PHP
php反射应用示例
Feb 25 PHP
PHP_NETWORK_GETADDRESSES: GETADDRINFO FAILED问题解决办法
May 04 PHP
PHP的cURL库简介及使用示例
Feb 06 PHP
又十个超级有用的PHP代码片段
Sep 24 PHP
php基于CodeIgniter实现图片上传、剪切功能
May 14 PHP
php微信公众平台开发(一) 配置接口
Dec 06 PHP
PHP实现登录注册之BootStrap表单功能
Sep 03 PHP
PHP自定义函数实现assign()数组分配到模板及extract()变量分配到模板功能示例
May 23 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/03/10 PHP
PHP 工厂模式使用方法
2010/05/18 PHP
php生成略缩图代码
2012/07/16 PHP
深入PHP5中的魔术方法详解
2013/06/17 PHP
php中$_POST与php://input的区别实例分析
2015/01/07 PHP
微信企业转账之入口类分装php代码
2018/10/01 PHP
phpfpm的作用和用法
2019/10/10 PHP
yii框架结合charjs统计上一年与当前年数据的方法示例
2020/04/04 PHP
filemanage功能中用到的lib.js
2007/04/08 Javascript
对JavaScript的eval()中使用函数的进一步讨论
2008/07/26 Javascript
javascript改变position值实现菜单滚动至顶部后固定
2013/01/18 Javascript
css样式标签和js语法属性区别
2013/11/06 Javascript
详解JavaScript的表达式与运算符
2015/11/30 Javascript
JavaScript生成带有缩进的表格代码
2016/06/15 Javascript
Angular 中 select指令用法详解
2016/09/29 Javascript
javascript实现鼠标点击页面 移动DIV
2016/12/02 Javascript
数组Array的排序sort方法
2017/02/17 Javascript
jQuery实现点击下拉框中的值累加到文本框中的方法示例
2017/10/28 jQuery
jQuery实现动态添加和删除input框实例代码
2019/03/26 jQuery
layui复选框限制选择个数的方法
2019/09/18 Javascript
python实现代码行数统计示例分享
2014/02/10 Python
Python的“二维”字典 (two-dimension dictionary)定义与实现方法
2016/04/27 Python
django1.8使用表单上传文件的实现方法
2016/11/04 Python
python中实现延时回调普通函数示例代码
2017/09/08 Python
python实现顺序表的简单代码
2018/09/28 Python
Linux下通过python获取本机ip方法示例
2019/09/06 Python
Python如何实现强制数据类型转换
2019/11/22 Python
python如何使用socketserver模块实现并发聊天
2019/12/14 Python
Python使用Tkinter实现滚动抽奖器效果
2020/01/06 Python
HTML5 通信API 跨域门槛将不再高、数据推送也不再是梦
2013/04/25 HTML / CSS
海信商城:海信电视、科龙空调、容声冰箱官方专卖
2017/02/07 全球购物
毕业生求职自荐书范文
2014/03/27 职场文书
四川省传达学习贯彻党的群众路线教育实践活动总结大会精神新闻稿
2014/10/26 职场文书
个人房屋转让协议书范本
2014/10/26 职场文书
用Python将GIF动图分解成多张静态图片
2021/06/11 Python
python flappy bird小游戏分步实现流程
2022/02/15 Python