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

相关文章

Redis的数据过期策略和数据淘汰策略

《Redis的数据过期策略和数据淘汰策略》本文主要介绍了Redis的数据过期策略和数据淘汰策略,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录一、数据过期策略1、惰性删除2、定期删除二、数据淘汰策略1、数据淘汰策略概念2、8种数据淘汰策略

轻松上手MYSQL之JSON函数实现高效数据查询与操作

《轻松上手MYSQL之JSON函数实现高效数据查询与操作》:本文主要介绍轻松上手MYSQL之JSON函数实现高效数据查询与操作的相关资料,MySQL提供了多个JSON函数,用于处理和查询JSON数... 目录一、jsON_EXTRACT 提取指定数据二、JSON_UNQUOTE 取消双引号三、JSON_KE

Python给Excel写入数据的四种方法小结

《Python给Excel写入数据的四种方法小结》本文主要介绍了Python给Excel写入数据的四种方法小结,包含openpyxl库、xlsxwriter库、pandas库和win32com库,具有... 目录1. 使用 openpyxl 库2. 使用 xlsxwriter 库3. 使用 pandas 库

SpringBoot定制JSON响应数据的实现

《SpringBoot定制JSON响应数据的实现》本文主要介绍了SpringBoot定制JSON响应数据的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们... 目录前言一、如何使用@jsonView这个注解?二、应用场景三、实战案例注解方式编程方式总结 前言

使用Python在Excel中创建和取消数据分组

《使用Python在Excel中创建和取消数据分组》Excel中的分组是一种通过添加层级结构将相邻行或列组织在一起的功能,当分组完成后,用户可以通过折叠或展开数据组来简化数据视图,这篇博客将介绍如何使... 目录引言使用工具python在Excel中创建行和列分组Python在Excel中创建嵌套分组Pyt

在Rust中要用Struct和Enum组织数据的原因解析

《在Rust中要用Struct和Enum组织数据的原因解析》在Rust中,Struct和Enum是组织数据的核心工具,Struct用于将相关字段封装为单一实体,便于管理和扩展,Enum用于明确定义所有... 目录为什么在Rust中要用Struct和Enum组织数据?一、使用struct组织数据:将相关字段绑

在Mysql环境下对数据进行增删改查的操作方法

《在Mysql环境下对数据进行增删改查的操作方法》本文介绍了在MySQL环境下对数据进行增删改查的基本操作,包括插入数据、修改数据、删除数据、数据查询(基本查询、连接查询、聚合函数查询、子查询)等,并... 目录一、插入数据:二、修改数据:三、删除数据:1、delete from 表名;2、truncate

Java实现Elasticsearch查询当前索引全部数据的完整代码

《Java实现Elasticsearch查询当前索引全部数据的完整代码》:本文主要介绍如何在Java中实现查询Elasticsearch索引中指定条件下的全部数据,通过设置滚动查询参数(scrol... 目录需求背景通常情况Java 实现查询 Elasticsearch 全部数据写在最后需求背景通常情况下

Java中注解与元数据示例详解

《Java中注解与元数据示例详解》Java注解和元数据是编程中重要的概念,用于描述程序元素的属性和用途,:本文主要介绍Java中注解与元数据的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参... 目录一、引言二、元数据的概念2.1 定义2.2 作用三、Java 注解的基础3.1 注解的定义3.2 内

将sqlserver数据迁移到mysql的详细步骤记录

《将sqlserver数据迁移到mysql的详细步骤记录》:本文主要介绍将SQLServer数据迁移到MySQL的步骤,包括导出数据、转换数据格式和导入数据,通过示例和工具说明,帮助大家顺利完成... 目录前言一、导出SQL Server 数据二、转换数据格式为mysql兼容格式三、导入数据到MySQL数据