php GD绘制24小时柱状图


Posted in PHP onJune 28, 2008

80,250,430,134,35,60,233,90,263,225,120,59,151,677,340,221,550,300,229,97,230,123,133,87  一共24个数字 一个都不能少哦少了要出错 你可以修改函数判断一下

<?PHP     
/*    
24小时柱状图    
作者:taokey    
QQ:29611705    
*/    function h24($str){     
        $hour = explode(",",$str);     
        $hmax = max($hour);     
        $ppix = 150/$hmax;     
        //计算柱状图高度     
        $h0 = 190-$hour[0]*$ppix;     
        $h1 = 190-$hour[1]*$ppix;     
        $h2 = 190-$hour[2]*$ppix;     
        $h3 = 190-$hour[3]*$ppix;     
        $h4 = 190-$hour[4]*$ppix;     
        $h5 = 190-$hour[5]*$ppix;     
        $h6 = 190-$hour[6]*$ppix;     
        $h7 = 190-$hour[7]*$ppix;     
        $h8 = 190-$hour[8]*$ppix;     
        $h9 = 190-$hour[9]*$ppix;     
        $h10 = 190-$hour[10]*$ppix;     
        $h11 = 190-$hour[11]*$ppix;     
        $h12 = 190-$hour[12]*$ppix;     
        $h13 = 190-$hour[13]*$ppix;     
        $h14 = 190-$hour[14]*$ppix;     
        $h15 = 190-$hour[15]*$ppix;     
        $h16 = 190-$hour[16]*$ppix;     
        $h17 = 190-$hour[17]*$ppix;     
        $h18 = 190-$hour[18]*$ppix;     
        $h19 = 190-$hour[19]*$ppix;     
        $h20 = 190-$hour[20]*$ppix;     
        $h21 = 190-$hour[21]*$ppix;     
        $h22 = 190-$hour[22]*$ppix;     
        $h23 = 190-$hour[23]*$ppix;     
        //创建一个img     
        $img = imagecreate(755,210);     
        //背景     
        $bgc = imagecolorallocate ($img, 245, 250, 254);     
        //黑色     
        $bc = imagecolorallocate($img,0,0,0);     
        //画竖轴     
        imageline($img,15,30,15,189, $bc);     
        //画横轴     
        imageline($img,15,190,750,190, $bc);     
        //画竖轴点     
        for($i=39,$j=10;$i<189;$i=$i+15,$j--){     
                imageline($img,13,$i,15,$i, $bc);     
                imagestring($img,1,1,$i-4,$j."x", $bc);     
        }     
        //画横轴点     
        $t = true;     
        for($i=31,$j=29;$i<750;$i=$j+1,$j=$j+15){     
                if($t){     
                        $x=$i;     
                        $t=false;     
                }else{     
                        $x=$i+1;     
                        $t=true;     
                }     
                imageline($img,$x,190,$x,192, $bc);     
        }     
        //竖轴标记     
        $x = ceil($hmax/10);     
        imagestring($img,2,10,15,"X=".$x,$bc);     
        //竖轴标记     
        //0点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,31,$h0,45,189,$color);     
        imagestring($img,1,31,$h0-10,$hour[0],$color);     
        imagechar($img,1,36,195,0,$bc);     
        //1点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,61,$h1,75,189,$color);     
        imagestring($img,1,61,$h1-10,$hour[1],$color);     
        imagechar($img,1,66,195,1,$bc);     
        //2点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,91,$h2,105,189,$color);     
        imagestring($img,1,91,$h2-10,$hour[2],$color);     
        imagechar($img,1,96,195,2,$bc);     
        //3点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,121,$h3,135,189,$color);     
        imagestring($img,1,121,$h3-10,$hour[3],$color);     
        imagechar($img,1,126,195,3,$bc);     
        //4点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,151,$h4,165,189,$color);     
        imagestring($img,1,151,$h4-10,$hour[4],$color);     
        imagechar($img,1,156,195,4,$bc);     
        //5点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,181,$h5,195,189,$color);     
        imagestring($img,1,181,$h5-10,$hour[5],$color);     
        imagechar($img,1,186,195,5,$bc);     
        //6点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,211,$h6,225,189,$color);     
        imagestring($img,1,211,$h6-10,$hour[6],$color);     
        imagechar($img,1,216,195,6,$bc);     
        //7点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,241,$h7,255,189,$color);     
        imagestring($img,1,241,$h7-10,$hour[7],$color);     
        imagechar($img,1,246,195,7,$bc);     
        //8点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,271,$h8,285,189,$color);     
        imagestring($img,1,271,$h8-10,$hour[8],$color);     
        imagechar($img,1,276,195,8,$bc);     
        //9点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,301,$h9,315,189,$color);     
        imagestring($img,1,301,$h9-10,$hour[9],$color);     
        imagechar($img,1,306,195,9,$bc);     
        //10点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,331,$h10,345,189,$color);     
        imagestring($img,1,331,$h10-10,$hour[10],$color);     
        imagestring($img,1,334,195,10,$bc);     
        //11点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,361,$h11,375,189,$color);     
        imagestring($img,1,361,$h11-10,$hour[11],$color);     
        imagestring($img,1,364,195,11,$bc);     
        //12点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,391,$h12,405,189,$color);     
        imagestring($img,1,391,$h12-10,$hour[12],$color);     
        imagestring($img,1,394,195,12,$bc);     
        //13点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,421,$h13,435,189,$color);     
        imagestring($img,1,421,$h13-10,$hour[13],$color);     
        imagestring($img,1,424,195,13,$bc);     
        //14点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,451,$h14,465,189,$color);     
        imagestring($img,1,451,$h14-10,$hour[14],$color);     
        imagestring($img,1,454,195,14,$bc);     
        //15点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,481,$h15,495,189,$color);     
        imagestring($img,1,481,$h15-10,$hour[15],$color);     
        imagestring($img,1,481,195,15,$bc);     
        //16点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,511,$h16,525,189,$color);     
        imagestring($img,1,511,$h16-10,$hour[16],$color);     
        imagestring($img,1,511,195,16,$bc);     
        //17点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,541,$h17,555,189,$color);     
        imagestring($img,1,541,$h17-10,$hour[17],$color);     
        imagestring($img,1,544,195,17,$bc);     
        //18点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,571,$h18,585,189,$color);     
        imagestring($img,1,571,$h18-10,$hour[18],$color);     
        imagestring($img,1,571,195,18,$bc);     
        //19点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,601,$h19,615,189,$color);     
        imagestring($img,1,601,$h19-10,$hour[19],$color);     
        imagestring($img,1,604,195,19,$bc);     
        //20点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,631,$h20,645,189,$color);     
        imagestring($img,1,631,$h20-10,$hour[20],$color);     
        imagestring($img,1,634,195,20,$bc);     
        //21点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,661,$h21,675,189,$color);     
        imagestring($img,1,661,$h21-10,$hour[21],$color);     
        imagestring($img,1,664,195,21,$bc);     
        //22点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,691,$h22,705,189,$color);     
        imagestring($img,1,691,$h22-10,$hour[22],$color);     
        imagestring($img,1,694,195,22,$bc);     
        //23点     
        $color = imagecolorallocate($img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));     
        imagefilledrectangle($img,721,$h23,735,189,$color);     
        imagestring($img,1,721,$h23-10,$hour[23],$color);     
        imagestring($img,1,724,195,23,$bc);     
        //加个边框 加了之后不好看     
        //imagerectangle($img, 0, 0, 754, 209, $bc);     
        imagepng($img);     
        imagedestroy($img);             
}     
$str = isset($_GET['str'])?$_GET['str']:"";     
if($str){     
        h24($str);     
}     
?>

PHP 相关文章推荐
编写PHP的安全策略
Oct 09 PHP
php时间不正确的解决方法
Apr 09 PHP
解析PHP跳出循环的方法以及continue、break、exit的区别介绍
Jul 01 PHP
解析数组非数字键名引号的必要性
Aug 09 PHP
PHP微框架Dispatch简介
Jun 12 PHP
php车辆违章查询数据示例
Oct 14 PHP
PHP递归获取目录内所有文件的实现方法
Nov 01 PHP
PHP未登录自动跳转到登录页面
Dec 21 PHP
PHP中Trait及其应用详解
Feb 14 PHP
在 PHP 和 Laravel 中使用 Traits的方法
Nov 13 PHP
PHP中类与对象功能、用法实例解读
Mar 27 PHP
详解Go与PHP的语法对比
May 29 PHP
php 在线打包_支持子目录
Jun 28 #PHP
php 前一天或后一天的日期
Jun 28 #PHP
js下函数般调用正则的方法附代码
Jun 22 #PHP
php的正则处理函数总结分析
Jun 20 #PHP
PHP伪静态页面函数附使用方法
Jun 20 #PHP
PHP伪静态写法附代码
Jun 20 #PHP
用PHP伪造referer突破网盘禁止外连的代码
Jun 15 #PHP
You might like
用Php编写注册后Email激活验证的实例代码
2013/03/11 PHP
PHP rsa加密解密使用方法
2015/04/27 PHP
thinkPHP模型初始化实例分析
2015/12/03 PHP
用正则xmlHttp实现的偷(转)
2007/01/22 Javascript
JavaScript DOM学习第六章 表单实例
2010/02/19 Javascript
jquery isEmptyObject判断是否为空对象的函数
2011/02/14 Javascript
使用js检测浏览器是否支持html5中的video标签的方法
2014/03/12 Javascript
基于jquery实现复选框全选,反选,全不选等功能
2015/10/16 Javascript
第四篇Bootstrap网格系统偏移列和嵌套列
2016/06/21 Javascript
微信小程序 scroll-view组件实现列表页实例代码
2016/12/14 Javascript
三种方式实现瀑布流布局
2017/02/10 Javascript
js以及jquery实现手风琴效果
2020/04/17 Javascript
Vue.js如何优雅的进行form validation
2017/04/07 Javascript
深入理解vue-router之keep-alive
2017/08/31 Javascript
纯JS实现的读取excel文件内容功能示例【支持所有浏览器】
2018/06/23 Javascript
Layui数据表格之单元格编辑方式
2019/10/26 Javascript
jQuery实现html可联动的百分比进度条
2020/03/26 jQuery
js验证账户名是否重复
2020/05/26 Javascript
python list使用示例 list中找连续的数字
2014/01/27 Python
Python实现的多线程端口扫描工具分享
2015/01/21 Python
在Python中处理XML的教程
2015/04/29 Python
详解Python的Django框架中inclusion_tag的使用
2015/07/21 Python
在Linux系统上通过uWSGI配置Nginx+Python环境的教程
2015/12/25 Python
解决phantomjs截图失败,phantom.exit位置的问题
2018/05/17 Python
Python实现将Excel转换成为image的方法
2018/10/23 Python
numpy中的ndarray方法和属性详解
2019/05/27 Python
Python实现不规则图形填充的思路
2020/02/02 Python
Python实现捕获异常发生的文件和具体行数
2020/04/25 Python
TensorFlow保存TensorBoard图像操作
2020/06/23 Python
关于Python3的import问题(pycharm可以运行命令行import错误)
2020/11/18 Python
世界上最大的在线学习和教学市场:Udemy
2017/11/08 全球购物
高三自我鉴定范文
2013/10/19 职场文书
技术支持岗位职责
2015/02/13 职场文书
创卫工作总结2015
2015/04/22 职场文书
学校社团活动总结
2015/05/07 职场文书
Python中快速掌握Data Frame的常用操作
2021/03/31 Python