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 array_flip() 删除数组重复元素
Jan 14 PHP
PHP UTF8编码内的繁简转换类
Jul 20 PHP
基于php实现长连接的方法与注意事项的问题
May 10 PHP
几个实用的PHP内置函数使用指南
Nov 27 PHP
PHP+Jquery与ajax相结合实现下拉淡出瀑布流效果【无需插件】
May 06 PHP
AES加解密在php接口请求过程中的应用示例
Oct 26 PHP
php实现PDO中捕获SQL语句错误的方法
Feb 16 PHP
php使用imagecopymerge()函数创建半透明水印
Jan 25 PHP
PHP实现微信退款的方法示例
Mar 26 PHP
php+jQuery ajax实现的实时刷新显示数据功能示例
Sep 12 PHP
浅谈laravel orm 中的一对多关系 hasMany
Oct 21 PHP
TP5(thinkPHP5)框架使用ajax实现与后台数据交互的方法小结
Feb 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
PHP迅雷、快车、旋风下载专用链转换代码
2010/06/15 PHP
php获取汉字拼音首字母的方法
2015/10/21 PHP
[原创]js与自动伸缩图片 自动缩小图片的多浏览器兼容的方法总结
2007/03/12 Javascript
浅析JavaScript中的常用算法与函数
2013/11/21 Javascript
jquery 图片缩放拖动的简单实例
2014/01/08 Javascript
jQuery setTimeout传递字符串参数报错的解决方法
2014/06/09 Javascript
JS运动基础框架实例分析
2015/03/03 Javascript
JavaScript控制table某列不显示的方法
2015/03/16 Javascript
JSON遍历方式实例总结
2015/12/07 Javascript
CSS中position属性之fixed实现div居中
2015/12/14 Javascript
Nodejs 搭建简单的Web服务器详解及实例
2016/11/30 NodeJs
BootStrap Validator对于隐藏域验证和程序赋值即时验证的问题浅析
2016/12/01 Javascript
Bootstrap源码解读导航条(7)
2016/12/23 Javascript
javascript实现延时显示提示框效果
2017/06/01 Javascript
jQuery Ajax向服务端传递数组参数值的实例代码
2017/09/03 jQuery
移动web开发之touch事件实例详解
2018/01/17 Javascript
优雅的elementUI table单元格可编辑实现方法详解
2018/12/23 Javascript
echarts大屏字体自适应的方法步骤
2019/07/12 Javascript
JavaScript 函数用法详解【函数定义、参数、绑定、作用域、闭包等】
2020/05/12 Javascript
[00:37]DOTA2上海特级锦标赛 Secert 战队宣传片
2016/03/03 DOTA
[01:42]DOTA2 – 虚无之灵
2019/08/25 DOTA
Python实现合并字典的方法
2015/07/07 Python
python3写爬取B站视频弹幕功能
2017/12/22 Python
Python编程把二叉树打印成多行代码
2018/01/04 Python
Python冲顶大会 快来答题!
2018/01/17 Python
python如何发布自已pip项目的方法步骤
2018/10/09 Python
PyQt5使用QTimer实现电子时钟
2019/07/29 Python
Python Pandas数据中对时间的操作
2019/07/30 Python
Python3从零开始搭建一个语音对话机器人的实现
2019/08/23 Python
python生成13位或16位时间戳以及反向解析时间戳的实例
2020/03/03 Python
HTML5 3D书本翻页动画的实现示例
2019/08/28 HTML / CSS
学生就业推荐信
2013/11/13 职场文书
普师专业个人自荐信范文
2013/11/26 职场文书
无故旷工检讨书
2014/01/26 职场文书
新年联欢会主持词
2014/03/27 职场文书
竞聘演讲稿怎么写
2014/08/28 职场文书