<?

/* 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 the votes each candidate got as a part of the
circle representing 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

$height = 600;
$width = 400;

$im = ImageCreateTrueColor($width, $height);

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

$diameter = 0.75 * $width;
$radius = $diameter/2;
$center_x = $width/2;
$center_y = $width/2;
imagefill($im,0,0,$color_array[0]);
imagestring($im,5,0,0,"Click refresh if the colors are not clear",$color_array[1]);
$begin_degree = 0;
$n = 0;
foreach ($percentage as $name => $votes)
{
	$n++;
	// Create a random color that is not used so far
	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;
	
	imageFilledArc($im,$center_x,$center_y,$diameter,$diameter,$begin_degree,$end_degree,$random_color,IMG_ARC_PIE);
	imagesetthickness($im,2);
	imageFilledArc($im,$center_x,$center_y,$diameter,$diameter,$begin_degree,$end_degree,$color_array[1],IMG_ARC_NOFILL+IMG_ARC_EDGED);
	// Convert degree to radian because trig functions use radian arguments
	$rad_degree = ($begin_degree + ($end_degree - $begin_degree)/2) * M_PI / 180;

	$x = $center_x + ($radius+20) * cos($rad_degree);
	#($x > $center_x? $x = $x + 10: $x = $x - 10);
	$y = $center_y + ($radius+20) *sin($rad_degree);
	#($y > $center_y? $y = $y + 10: $y = $y - 10);

	// Display numbers on each part of the circle corresponding to a candidate
	imagestring($im,5,$x,$y,$n,$color_array[1]);
	$begin_degree = $end_degree;
}

$x = 50;
$y = $width;
$n = 0;
// Display names corresponding to numbers above
foreach ($percentage as $name => $votes)
{
	$n++;
	$perc = round($votes * 100,2);
	imagestring($im,5,$x,$y + 30 * $n,$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();
?>

