热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

phpGD绘制24小时柱状图

h24这个函数就是生成柱状图的函数需要一个字符串作为参数这个参数的格式为:每小时的数量按照从0-23点的顺序加逗号连接在一起可以看例子里面的
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个数字 一个都不能少哦少了要出错 你可以修改函数判断一下

代码如下:


/*
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);
}
?>

推荐阅读
  • 本文介绍了Python高级网络编程及TCP/IP协议簇的OSI七层模型。首先简单介绍了七层模型的各层及其封装解封装过程。然后讨论了程序开发中涉及到的网络通信内容,主要包括TCP协议、UDP协议和IPV4协议。最后还介绍了socket编程、聊天socket实现、远程执行命令、上传文件、socketserver及其源码分析等相关内容。 ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文分享了一个关于在C#中使用异步代码的问题,作者在控制台中运行时代码正常工作,但在Windows窗体中却无法正常工作。作者尝试搜索局域网上的主机,但在窗体中计数器没有减少。文章提供了相关的代码和解决思路。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • PHP设置MySQL字符集的方法及使用mysqli_set_charset函数
    本文介绍了PHP设置MySQL字符集的方法,详细介绍了使用mysqli_set_charset函数来规定与数据库服务器进行数据传送时要使用的字符集。通过示例代码演示了如何设置默认客户端字符集。 ... [详细]
  • Java序列化对象传给PHP的方法及原理解析
    本文介绍了Java序列化对象传给PHP的方法及原理,包括Java对象传递的方式、序列化的方式、PHP中的序列化用法介绍、Java是否能反序列化PHP的数据、Java序列化的原理以及解决Java序列化中的问题。同时还解释了序列化的概念和作用,以及代码执行序列化所需要的权限。最后指出,序列化会将对象实例的所有字段都进行序列化,使得数据能够被表示为实例的序列化数据,但只有能够解释该格式的代码才能够确定数据的内容。 ... [详细]
  • 橱窗设计的表现手法及其应用
    本文介绍了橱窗设计的表现手法,包括直接展示、寓意与联想、夸张与幽默等。通过对商品的折、拉、叠、挂、堆等陈列技巧,橱窗设计能够充分展现商品的形态、质地、色彩、样式等特性。同时,寓意与联想可以通过象形形式或抽象几何道具来唤起消费者的联想与共鸣,创造出强烈的时代气息和视觉空间。合理的夸张和贴切的幽默能够明显夸大商品的美的因素,给人以新颖奇特的心理感受,引起人们的笑声和思考。通过这些表现手法,橱窗设计能够有效地传达商品的个性内涵,吸引消费者的注意力。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • faceu激萌变老特效的使用方法详解
    本文介绍了faceu激萌变老特效的使用方法,包括打开faceu激萌app、点击贴纸、选择热门贴纸中的变老特效,然后对准人脸进行拍摄,即可给照片添加变老特效。操作简单,适合新用户使用。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • 大连微软技术社区举办《.net core始于足下》活动,获得微软赛百味和易迪斯的赞助
    九月十五日,大连微软技术社区举办了《.net core始于足下》活动,共有51人报名参加,实际到场人数为43人,还有一位专程从北京赶来的同学。活动得到了微软赛百味和易迪斯的赞助,场地也由易迪斯提供。活动中大家积极交流,取得了非常成功的效果。 ... [详细]
  • 给定一个二叉树,要求随机选择树上的一个节点。解法:遍历树的过程中,随机选择一个节点即可。具体做法参看:从输入 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文介绍了在微店中如何修改分销产品的价格以及设置价格的方法。客户在拍下商品后,在1小时内可以进行修改价格的操作,通过进入订单管理,点击未付款子项,可以找到订单信息并进行改价操作。修改价格后,买家会收到改价后的短信通知,在微店订单中进行付款即可。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
author-avatar
lailin2025
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有