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小马小结(方便查找后门的朋友)
May 05 PHP
深入掌握include_once与require_once的区别
Jun 17 PHP
php随机抽奖实例分析
Mar 04 PHP
PHP读取mssql json数据中文乱码的解决办法
Apr 11 PHP
php打乱数组二维数组多维数组的简单实例
Jun 17 PHP
php版微信公众平台入门教程之开发者认证的方法
Sep 26 PHP
PHP基于正则批量替换Img中src内容实现获取缩略图的功能示例
Jun 07 PHP
PHP实现的mysql主从数据库状态检测功能示例
Jul 20 PHP
PHP并发查询MySQL的实例代码
Aug 09 PHP
PHP强制转化的形式整理
May 22 PHP
基于php解决json_encode中文UNICODE转码问题
Nov 10 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
基于session_unset与session_destroy的区别详解
2013/06/03 PHP
php中get_cfg_var()和ini_get()的用法及区别
2015/03/04 PHP
php计算两个整数的最大公约数常用算法小结
2015/03/05 PHP
详解php用static方法的原因
2018/09/12 PHP
PHP实现简易图形计算器
2020/08/28 PHP
javascript操作文本框readOnly
2007/05/15 Javascript
javascript中使用正则计算中文长度的例子
2014/04/29 Javascript
Jquery实现的角色左右选择特效
2014/05/21 Javascript
javascript面向对象之对象的深入理解
2015/01/13 Javascript
JavaScript中的方法重载实例
2015/03/16 Javascript
JS实现弹出居中的模式窗口示例
2016/06/20 Javascript
分享jQuery封装好的一些常用操作
2016/07/28 Javascript
关于JSON与JSONP简单总结
2016/08/16 Javascript
手机浏览器 后退按钮强制刷新页面方法总结
2016/10/09 Javascript
jQuery Easyui datagrid editor为combobox时指定数据源实例
2016/12/19 Javascript
nodejs操作mongodb的增删改查功能实例
2017/11/09 NodeJs
jQuery实现的上传图片本地预览效果简单示例
2018/03/29 jQuery
layui框架table 数据表格的方法级渲染详解
2018/08/19 Javascript
jQuery Ajax async=&gt;false异步改为同步时,解决导致浏览器假死的问题
2019/07/22 jQuery
在layui框架中select下拉框监听更改事件的例子
2019/09/20 Javascript
[05:06]2017亚洲邀请赛DAC回顾片
2017/04/19 DOTA
Python Deque 模块使用详解
2014/07/04 Python
对于Python的Django框架部署的一些建议
2015/04/09 Python
浅述python中argsort()函数的实例用法
2017/03/30 Python
python 实现提取某个索引中某个时间段的数据方法
2019/02/01 Python
python网络爬虫 CrawlSpider使用详解
2019/09/27 Python
pytorch方法测试详解——归一化(BatchNorm2d)
2020/01/15 Python
Python如何存储数据到json文件
2020/03/09 Python
python计算auc的方法
2020/09/09 Python
python如何构建mock接口服务
2021/01/28 Python
香港礼品网站:GiftU eshop
2017/09/01 全球购物
学生违纪检讨书200字
2014/10/21 职场文书
倡议书范文大全
2015/04/28 职场文书
少年雷锋观后感
2015/06/10 职场文书
高效课堂教学反思
2016/02/24 职场文书
详解Python中__new__方法的作用
2022/03/31 Python