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

相关文章

Python MySQL如何通过Binlog获取变更记录恢复数据

《PythonMySQL如何通过Binlog获取变更记录恢复数据》本文介绍了如何使用Python和pymysqlreplication库通过MySQL的二进制日志(Binlog)获取数据库的变更记录... 目录python mysql通过Binlog获取变更记录恢复数据1.安装pymysqlreplicat

Linux使用dd命令来复制和转换数据的操作方法

《Linux使用dd命令来复制和转换数据的操作方法》Linux中的dd命令是一个功能强大的数据复制和转换实用程序,它以较低级别运行,通常用于创建可启动的USB驱动器、克隆磁盘和生成随机数据等任务,本文... 目录简介功能和能力语法常用选项示例用法基础用法创建可启动www.chinasem.cn的 USB 驱动

Oracle数据库使用 listagg去重删除重复数据的方法汇总

《Oracle数据库使用listagg去重删除重复数据的方法汇总》文章介绍了在Oracle数据库中使用LISTAGG和XMLAGG函数进行字符串聚合并去重的方法,包括去重聚合、使用XML解析和CLO... 目录案例表第一种:使用wm_concat() + distinct去重聚合第二种:使用listagg,

Python实现将实体类列表数据导出到Excel文件

《Python实现将实体类列表数据导出到Excel文件》在数据处理和报告生成中,将实体类的列表数据导出到Excel文件是一项常见任务,Python提供了多种库来实现这一目标,下面就来跟随小编一起学习一... 目录一、环境准备二、定义实体类三、创建实体类列表四、将实体类列表转换为DataFrame五、导出Da

Python实现数据清洗的18种方法

《Python实现数据清洗的18种方法》本文主要介绍了Python实现数据清洗的18种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1. 去除字符串两边空格2. 转换数据类型3. 大小写转换4. 移除列表中的重复元素5. 快速统

Python数据处理之导入导出Excel数据方式

《Python数据处理之导入导出Excel数据方式》Python是Excel数据处理的绝佳工具,通过Pandas和Openpyxl等库可以实现数据的导入、导出和自动化处理,从基础的数据读取和清洗到复杂... 目录python导入导出Excel数据开启数据之旅:为什么Python是Excel数据处理的最佳拍档

在Pandas中进行数据重命名的方法示例

《在Pandas中进行数据重命名的方法示例》Pandas作为Python中最流行的数据处理库,提供了强大的数据操作功能,其中数据重命名是常见且基础的操作之一,本文将通过简洁明了的讲解和丰富的代码示例,... 目录一、引言二、Pandas rename方法简介三、列名重命名3.1 使用字典进行列名重命名3.编

Python使用Pandas库将Excel数据叠加生成新DataFrame的操作指南

《Python使用Pandas库将Excel数据叠加生成新DataFrame的操作指南》在日常数据处理工作中,我们经常需要将不同Excel文档中的数据整合到一个新的DataFrame中,以便进行进一步... 目录一、准备工作二、读取Excel文件三、数据叠加四、处理重复数据(可选)五、保存新DataFram

使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)

《使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)》在现代软件开发中,处理JSON数据是一项非常常见的任务,无论是从API接口获取数据,还是将数据存储为JSON格式,解析... 目录1. 背景介绍1.1 jsON简介1.2 实际案例2. 准备工作2.1 环境搭建2.1.1 添加

MySQL中删除重复数据SQL的三种写法

《MySQL中删除重复数据SQL的三种写法》:本文主要介绍MySQL中删除重复数据SQL的三种写法,文中通过代码示例讲解的非常详细,对大家的学习或工作有一定的帮助,需要的朋友可以参考下... 目录方法一:使用 left join + 子查询删除重复数据(推荐)方法二:创建临时表(需分多步执行,逻辑清晰,但会