<?

/* You have a database with a table with three columns: ssn (primary key), name, and votes (for the number of votes
the candidate got). The following program reads the data and then draws a chart for the votes. Each candidate will 
have a different random color that is not used for any other candidate and it is not black nor white (these two 
colors are reserved for other things).
*/

ob_start();

// First get the data
@$d = mysql_connect('localhost','abu-jeib','iyad911') or  die("<p>We can't process your request at this time.</p>");

@mysql_select_db('exams') OR die("<p>We can't process your request at this time.</p>");


@$q = "select count(ssn) from a9"; 
@$r = mysql_query($q) OR die("<p>We can't process your request at this time.</p>");
@$row = mysql_fetch_row($r);
$number_of_candidates = $row[0];
mysql_free_result($r);

@$q = "select sum(votes) from a9"; 
@$r = mysql_query($q) OR die("<p>We can't process your request at this time.</p>");
@$row = mysql_fetch_row($r);
$total_votes = $row[0];
mysql_free_result($r);


@$q = "select name, votes from a9"; 
@$r = mysql_query($q) OR die("<p>We can't process your request at this time.</p>");


while (@$row = mysql_fetch_array($r))
{
	$percentage[$row['name']] = $row['votes'] / $total_votes;
}

// Now draw
$width_of_column = 50;
$length_of_column = 100;
$width = 50 + 2 * $number_of_candidates * $width_of_column;
$height = 200 + $number_of_candidates * 50;
 	
$im = ImageCreateTrueColor($width, $height);

$color_array = array();
$color_array[] = imagecolorallocate($im, 255, 255, 255);
$color_array[] = imagecolorallocate($im, 0, 0, 0);

imagefill($im,0,0,$color_array[0]);
imagestring($im,5,0,0,"Click refresh if the colors are not clear",$color_array[1]);
imagesetthickness($im,3);

// Display the vertical bar with numbers (out of 100)
imageline($im,40,30,40,130,$color_array[1]);
imagestring($im,3,10,130,"0",$color_array[1]);
imageline($im,35,130,40,130,$color_array[1]);
imagestring($im,3,10,110,"20",$color_array[1]);
imageline($im,35,110,40,110,$color_array[1]);
imagestring($im,3,10,90,"40",$color_array[1]);
imageline($im,35,90,40,90,$color_array[1]);
imagestring($im,3,10,70,"60",$color_array[1]);
imageline($im,35,70,40,70,$color_array[1]);
imagestring($im,3,10,50,"80",$color_array[1]);
imageline($im,35,50,40,50,$color_array[1]);
imagestring($im,3,10,30,"100",$color_array[1]);
imageline($im,35,30,40,30,$color_array[1]);

// Display the horizontal bar
imageline($im,40,130,$number_of_candidates*(30+$width_of_column),130,$color_array[1]);
$n = 0;
foreach ($percentage as $name => $votes)
{
	$n++;
	// Create an array of random distinct colors
	do
	{
		$random1 = mt_rand(0,250);
		$random2 = mt_rand(0,250);
		$random3 = mt_rand(0,250);
		$random_color = imagecolorallocate($im, $random1, $random2, $random3);
	}
	while (in_array($random_color,$color_array));
	$color_array[] = $random_color;		
	$end_degree = $begin_degree + $votes * 360;
	
	imagerectangle($im,20+$width_of_column * $n,30,$width_of_column * $n + $width_of_column,130, 
			$color_array[1]);
	imagefilledrectangle($im,20+$width_of_column * $n,30+100-$votes*100,$width_of_column * $n + 
				$width_of_column,130, $random_color);
	imagesetthickness($im,2);
	
	imagestring($im,5,$x,$y,$n,$color_array[1]);
	$begin_degree = $end_degree;
}

$x = 0;
$y = 130;
$n = 0;
foreach ($percentage as $name => $votes)
{
	$n++;
	$perc = round($votes * 100,2);
	imagefilledrectangle($im,$x,$y+30*$n+4,$x+5,$y+30*$n+9,$color_array[$n+1]);
	imagestring($im,3,$x+10,$y + 30 * $n," $name: $perc%",$color_array[1]); 
}

Header('Content-type: image/png');
Imagepng($im);

// Save image to a file
#ImagePng($im,"/home/abu-jeib/proposals/abc.png");
  
ImageDestroy($im);
ob_end_flush();
?>

