NBA投票系统(4):投票数据条形图显示

2024-06-12 01:38

本文主要是介绍NBA投票系统(4):投票数据条形图显示,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这一部分主要是用到了一些img库函数进行绘图,这里只给出代码,相关函数使用方法可以参考PHP帮助文档:

<?php/*************************************************************************1.Database query to get vote info**************************************************************************/$team   = $_REQUEST['team'];$player = $_POST['player'];
/*  echo "You choose champion team is $team.</br>";echo "You choose best play is $player.</br>";*/@$db_conn_team = new mysqli('localhost','NBA','NBA','NBATeam');@$db_conn_play = new mysqli('localhost','NBA','NBA','NBAPlayer'); if(!$db_conn_team){echo "Could not connect to db_team <br />";exit;}if(!$db_conn_play){echo "Could not connect to db_play <br />";}if(!empty($team) && !empty($player)){$team   = addslashes($team);$player = addslashes($player);$query_team = "update NBAChampionTeam set teamVotes = teamVotes + 1where teamName = '$team'";$query_play = "update NBABestPlayerset playerVotes = playerVotes + 1where playerName = '$player'";$result_team = @$db_conn_team->query($query_team);$result_play = @$db_conn_play->query($query_play);if(!$result_team){echo 'Could not connect to db_team<br />';exit;}if(!$result_play){echo 'Could not connect to db_play<br />';exit;}//echo "Insert Record Succeed!</br>";}$query_team = 'select * from NBAChampionTeam';$query_play = 'select * from NBABestPlayer';$result_team = @$db_conn_team->query($query_team);$result_play = @$db_conn_play->query($query_play);if(!$result_team || !$result_play){echo 'Could not connect to db</br >';exit;}$total_teams = $result_team->num_rows;$total_plays = $result_play->num_rows;$total_team_votes = 0;$total_player_votes = 0;while($row_team = $result_team->fetch_object()){$total_team_votes += $row_team->teamVotes;}while($row_play = $result_play->fetch_object()){$total_play_votes += $row_play->playerVotes;}/*echo "Total Teams is $total_teams<br/>";echo "Total Plays is $total_plays<br/>";echo "Total Team Votes is $total_team_votes<br/>";echo "Total Player Votes is $total_play_votes<br/>";*/$result_team->data_seek(0);$result_play->data_seek(0); /*************************************************************************2.Initial calculations for graph**************************************************************************///set up contents$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];//echo $DOCUMENT_ROOT."<br>";$width = 500;$left_margin = 100;$right_margin = 50;$bar_height = 40;$bar_spacing = $bar_height/2;$font = $DOCUMENT_ROOT.'/vote/ARIAL.TTF';$title_size = 16;$main_size = 12;$small_size = 12;$text_indet = 10;$x = $left_margin + 60;$y = 50;$bar_unit = ($width-$x-$right_margin)/100;$height = ($bar_height + $bar_spacing) * ($total_teams + $total_plays) + 50; /*************************************************************************3.Set Up Base Image**************************************************************************///create a blank canvas$im = imagecreatetruecolor($width,$height);$white = imagecolorallocate($im,255,255,255);$blue  = imagecolorallocate($im,0,64,128);$black = imagecolorallocate($im,0,0,0);$pink  = imagecolorallocate($im,255,78,243);$red   = imagecolorallocate($im,255,0,0);$text_color = $black;$percent_color = $black;$bg_color = $white;$line_color = $black;$team_color = $blue;$number_color = $pink;$play_color = $red;imagefilledrectangle($im,0,0,$width,$height,$bg_color);imagerectangle($im,0,0,$width-1,$height-1,$line_color);$title = "NBA Chamption & MVP Vote Results";$title_dimensions = imagettfbbox($title_size,0,$font,$title);/*for($i=0;$i<=7;$i++){echo "$title_dimensions[$i] ";}*/$title_length = $title_dimensions[2] - $title_dimensions[0];$title_height = abs($title_dimensions[7] - $title_dimensions[1]);$title_above_line = abs($title_dimensions[7]);$title_x = ($width - $title_length)/2;$title_y = ($y - $title_height)/2 + $title_above_line;/*echo "title_above_line = $title_above_line<br>";echo "title_x = $title_x<br>";echo "title_y = $title_y<br>";*/imagettftext($im,$title_size,0,$title_x,$title_y,$text_color,$font,$title);imageline($im,$x,$y-5,$x,$height-15,$line_color);/*************************************************************************4.Draw data into graph**************************************************************************///Get each line of db data and draw corresponding barswhile($row_team = $result_team->fetch_object()){if($total_team_votes > 0){$percent = intval(($row_team->teamVotes/$total_team_votes)*100);}else{$percent = 0;}$percent_dimensions = imagettfbbox($main_size,0,$font,$percent.'%');$percent_length = $percent_dimensions[2] - $percent_dimensions[0];imagettftext($im,$main_size,0,$width-$percent_length-$text_indent,$y+($bar_height/2),$percent_color,$font,$percent.'%');$bar_length = $x + ($percent * $bar_unit);imagefilledrectangle($im,$x,$y-2,$bar_length,$y+$bar_height,$team_color);imagettftext($im,$small_size,0,$text_indent,$y+($bar_height/2),$text_color,$font,"$row_team->teamName");imagerectangle($im,$bar_length+1,$y-2,($x+(100*$bar_unit)),$y+$bar_height,$line_color);imagettftext($im,$small_size,0,$x+(100*$bar_unit)-50,$y+($bar_height/2),$number_color,$font,$row_team->teamVotes.'/'.$total_team_votes);$y += $bar_height + $bar_spacing;}while($row_play = $result_play->fetch_object()){if($total_play_votes > 0){$percent = intval(($row_play->playerVotes/$total_play_votes)*100);}else{$percent = 0;}$percent_dimensions = imagettfbbox($main_size,0,$font,$percent.'%');$percent_length = $percent_dimensions[2] - $percent_dimensions[0];imagettftext($im,$main_size,0,$width-$percent_length-$text_indent,$y+($bar_height/2),$percent_color,$font,$percent.'%');$bar_length = $x + ($percent * $bar_unit);imagefilledrectangle($im,$x,$y-2,$bar_length,$y+$bar_height,$play_color);imagettftext($im,$small_size,0,$text_indent+10,$y+($bar_height/2),$text_color,$font,"$row_play->playerName");imagerectangle($im,$bar_length+1,$y-2,($x+(100*$bar_unit)),$y+$bar_height,$line_color);imagettftext($im,$small_size,0,$x+(100*$bar_unit)-50,$y+($bar_height/2),$number_color,$font,$row_play->playerVotes.'/'.$total_play_votes);$y += $bar_height + $bar_spacing;}Header('Content-type:image/png');imagepng($im);imagedestroy($im);
?>

这篇关于NBA投票系统(4):投票数据条形图显示的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1052834

相关文章

JAVA读取MongoDB中的二进制图片并显示在页面上

1:Jsp页面: <td><img src="${ctx}/mongoImg/show"></td> 2:xml配置: <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001

【服务器运维】MySQL数据存储至数据盘

查看磁盘及分区 [root@MySQL tmp]# fdisk -lDisk /dev/sda: 21.5 GB, 21474836480 bytes255 heads, 63 sectors/track, 2610 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical)

据阿谱尔APO Research调研显示,2023年全球髓内钉市场销售额约为4.7亿美元

根据阿谱尔 (APO Research)的统计及预测,2023年全球髓内钉市场销售额约为4.7亿美元,预计在2024-2030年预测期内将以超过3.82%的CAGR(年复合增长率)增长。 髓内钉市场是指涉及髓内钉制造、分销和销售的行业。髓内钉是一种用于整形外科手术的医疗器械,用于稳定长骨骨折,特别是股骨、胫骨和肱骨。髓内钉通常由不銹钢或钛等材料制成,并插入骨的髓管中,以在愈合过程中提供结构支

vue+elementUI下拉框联动显示

<el-row><el-col :span="12"><el-form-item label="主账号:" prop="partyAccountId" :rules="[ { required: true, message: '主账号不能为空'}]"><el-select v-model="detailForm.partyAccountId" filterable placeholder="

SQL Server中,查询数据库中有多少个表,以及数据库其余类型数据统计查询

sqlserver查询数据库中有多少个表 sql server 数表:select count(1) from sysobjects where xtype='U'数视图:select count(1) from sysobjects where xtype='V'数存储过程select count(1) from sysobjects where xtype='P' SE

数据时代的数字企业

1.写在前面 讨论数据治理在数字企业中的影响和必要性,并介绍数据治理的核心内容和实践方法。作者强调了数据质量、数据安全、数据隐私和数据合规等方面是数据治理的核心内容,并介绍了具体的实践措施和案例分析。企业需要重视这些方面以实现数字化转型和业务增长。 数字化转型行业小伙伴可以加入我的星球,初衷成为各位数字化转型参考库,星球内容每周更新 个人工作经验资料全部放在这里,包含数据治理、数据要

如何在Java中处理JSON数据?

如何在Java中处理JSON数据? 大家好,我是免费搭建查券返利机器人省钱赚佣金就用微赚淘客系统3.0的小编,也是冬天不穿秋裤,天冷也要风度的程序猿!今天我们将探讨在Java中如何处理JSON数据。JSON(JavaScript Object Notation)作为一种轻量级的数据交换格式,在现代应用程序中被广泛使用。Java通过多种库和API提供了处理JSON的能力,我们将深入了解其用法和最佳

两个基因相关性CPTAC蛋白组数据

目录 蛋白数据下载 ①蛋白数据下载 1,TCGA-选择泛癌数据  2,TCGA-TCPA 3,CPTAC(非TCGA) ②蛋白相关性分析 1,数据整理 2,蛋白相关性分析 PCAS在线分析 蛋白数据下载 CPTAC蛋白组学数据库介绍及数据下载分析 – 王进的个人网站 (jingege.wang) ①蛋白数据下载 可以下载泛癌蛋白数据:UCSC Xena (xena

中国341城市生态系统服务价值数据集(2000-2020年)

生态系统服务反映了人类直接或者间接从自然生态系统中获得的各种惠益,对支撑和维持人类生存和福祉起着重要基础作用。目前针对全国城市尺度的生态系统服务价值的长期评估还相对较少。我们在Xie等(2017)的静态生态系统服务当量因子表基础上,选取净初级生产力,降水量,生物迁移阻力,土壤侵蚀度和道路密度五个变量,对生态系统供给服务、调节服务、支持服务和文化服务共4大类和11小类的当量因子进行了时空调整,计算了

温湿度采集及OLED显示

目录 软件I2C和硬件I2C每隔2秒钟采集一次温湿度数据,显示到OLED上,同时通过串口发送到上位机的“串口助手”软件 软件I2C和硬件I2C "I2C"代表Inter-Integrated Circuit,是一种用于在数字电路之间进行通信的串行通信协议。软件I2C和硬件I2C是两种实现这种协议的方式。 软件I2C是通过软件来模拟I2C通信协议的实现方式。在这种情况下,微控制