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 读取 Excel 数据

《如何使用Python读取Excel数据》:本文主要介绍使用Python读取Excel数据的详细教程,通过pandas和openpyxl,你可以轻松读取Excel文件,并进行各种数据处理操... 目录使用 python 读取 Excel 数据的详细教程1. 安装必要的依赖2. 读取 Excel 文件3. 读

Spring 请求之传递 JSON 数据的操作方法

《Spring请求之传递JSON数据的操作方法》JSON就是一种数据格式,有自己的格式和语法,使用文本表示一个对象或数组的信息,因此JSON本质是字符串,主要负责在不同的语言中数据传递和交换,这... 目录jsON 概念JSON 语法JSON 的语法JSON 的两种结构JSON 字符串和 Java 对象互转

C++如何通过Qt反射机制实现数据类序列化

《C++如何通过Qt反射机制实现数据类序列化》在C++工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作,所以本文就来聊聊C++如何通过Qt反射机制实现数据类序列化吧... 目录设计预期设计思路代码实现使用方法在 C++ 工程中经常需要使用数据类,并对数据类进行存储、打印、调试等操作。由于数据类

SpringBoot使用GZIP压缩反回数据问题

《SpringBoot使用GZIP压缩反回数据问题》:本文主要介绍SpringBoot使用GZIP压缩反回数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录SpringBoot使用GZIP压缩反回数据1、初识gzip2、gzip是什么,可以干什么?3、Spr

SpringBoot集成Milvus实现数据增删改查功能

《SpringBoot集成Milvus实现数据增删改查功能》milvus支持的语言比较多,支持python,Java,Go,node等开发语言,本文主要介绍如何使用Java语言,采用springboo... 目录1、Milvus基本概念2、添加maven依赖3、配置yml文件4、创建MilvusClient

SpringValidation数据校验之约束注解与分组校验方式

《SpringValidation数据校验之约束注解与分组校验方式》本文将深入探讨SpringValidation的核心功能,帮助开发者掌握约束注解的使用技巧和分组校验的高级应用,从而构建更加健壮和可... 目录引言一、Spring Validation基础架构1.1 jsR-380标准与Spring整合1

MySQL 中查询 VARCHAR 类型 JSON 数据的问题记录

《MySQL中查询VARCHAR类型JSON数据的问题记录》在数据库设计中,有时我们会将JSON数据存储在VARCHAR或TEXT类型字段中,本文将详细介绍如何在MySQL中有效查询存储为V... 目录一、问题背景二、mysql jsON 函数2.1 常用 JSON 函数三、查询示例3.1 基本查询3.2

SpringBatch数据写入实现

《SpringBatch数据写入实现》SpringBatch通过ItemWriter接口及其丰富的实现,提供了强大的数据写入能力,本文主要介绍了SpringBatch数据写入实现,具有一定的参考价值,... 目录python引言一、ItemWriter核心概念二、数据库写入实现三、文件写入实现四、多目标写入

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

Mysql如何将数据按照年月分组的统计

《Mysql如何将数据按照年月分组的统计》:本文主要介绍Mysql如何将数据按照年月分组的统计方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql将数据按照年月分组的统计要的效果方案总结Mysql将数据按照年月分组的统计要的效果方案① 使用 DA