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 相关文章推荐
使用Limit参数优化MySQL查询的方法
Nov 12 PHP
深入解析php中的foreach函数
Aug 31 PHP
php与flash as3 socket通信传送文件实现代码
Aug 16 PHP
PHP连接和操作MySQL数据库基础教程
Sep 29 PHP
php生成图片验证码-附五种验证码
Aug 19 PHP
Yii调试查看执行SQL语句的方法
Jul 15 PHP
laravel5创建service provider和facade的方法详解
Jul 26 PHP
PHP实现QQ快速登录的方法
Sep 28 PHP
Laravel中基于Artisan View扩展包创建及删除应用视图文件的方法
Oct 08 PHP
2017年最新PHP经典面试题目汇总(上篇)
Mar 17 PHP
PHP实现一个限制实例化次数的类示例
Sep 16 PHP
php高性能日志系统 seaslog 的安装与使用方法分析
Feb 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实现的DateDiff和DateAdd时间函数代码分享
2014/08/16 PHP
PHP实现二叉树的深度优先与广度优先遍历方法
2015/09/28 PHP
php验证手机号码
2015/11/11 PHP
如何使用PHP给图片加水印
2016/10/12 PHP
PHP扩展安装方法步骤解析
2020/11/24 PHP
JS方法调用括号的问题探讨
2014/01/24 Javascript
js中for in语句的用法讲解
2015/04/24 Javascript
简单解析JavaScript中的__proto__属性
2016/05/10 Javascript
Bootstrap开发实战之第一次接触Bootstrap
2016/06/02 Javascript
vue.js+Element实现表格里的增删改查
2017/01/18 Javascript
一篇看懂vuejs的状态管理神器 vuex状态管理模式
2017/04/20 Javascript
windows下vue-cli及webpack搭建安装环境
2017/04/25 Javascript
js实现带进度条提示的多视频上传功能
2020/12/13 Javascript
vuex学习之Actions的用法详解
2017/08/29 Javascript
JavaScript自执行函数和jQuery扩展方法详解
2017/10/27 jQuery
js如何找出字符串中的最长回文串
2018/06/04 Javascript
node.js遍历目录的方法示例
2018/08/01 Javascript
Vue中的vue-resource示例详解
2018/11/02 Javascript
[02:28]DOTA2英雄基础教程 灰烬之灵
2013/12/19 DOTA
[01:13:46]iG vs Winstrike 2018国际邀请赛小组赛BO2 第一场 8.16
2018/08/17 DOTA
python2.7无法使用pip的解决方法(安装easy_install)
2018/04/03 Python
用于业余项目的8个优秀Python库
2018/09/21 Python
python 接收处理外带的参数方法
2018/12/03 Python
Django 多表关联 存储 使用方法详解 ManyToManyField save
2019/08/09 Python
Python如何实现大型数组运算(使用NumPy)
2020/07/24 Python
东方电视购物:东方CJ
2016/10/12 全球购物
欧舒丹澳洲版:L’OCCITANE
2017/07/17 全球购物
Vivo俄罗斯官方在线商店:中国智能手机品牌
2019/10/04 全球购物
巴西购物网站:Submarino
2020/01/19 全球购物
客服服务心得体会
2013/12/30 职场文书
办公室人员先进事迹
2014/01/27 职场文书
家具促销活动方案
2014/02/16 职场文书
人事经理岗位职责
2014/04/28 职场文书
领导干部作风整顿个人剖析材料
2014/10/11 职场文书
2014年财务经理工作总结
2014/12/08 职场文书
2015年生活老师工作总结
2015/05/27 职场文书