Page 1 of 1

Trouble creating chart image

Posted: Tue Nov 09, 2010 5:40 am
by InGale
Hi!
I'm trying to create a line chart for a sports team place progress throughout the season. I'm storing the progress in a MySQL DB, where there's a table with 2 columns: matchday (1 to 38) and place. In the chart I'm creating "Match day" text and vertical bars on the X axis and places and "Places" and horizontal bars on the Y axis. Now I'm trying to pull the data from the DB, create red circles and place them exactly on the junction of related matchday and place.
This is the code I'm using for the X axis:

Code: Select all

//Creating matchdays
for($matchday = 1; $matchday <= 38; $matchday++)
{
	$x_matchday_position = 35 + $matchday * (imagefontwidth($text_number)+15);
	imagestring($image, $text_number, $x_matchday_position, $y_position + 20, $matchday, $header_color);
	imageline($image, ($x_matchday_position + (imagefontwidth($text_number)/2))+1, 30, ($x_matchday_position + (imagefontwidth($text_number)/2))+1, 355, $line_color);
}

//Axis X text
$x_axis_text_position = ($image_width - (strlen($x_axis_text) * imagefontwidth($text_number))) / 2;
imagestring($image, $text_number, $x_axis_text_position, $y_position + 40, $x_axis_text, $header_color);
This is the code of the Y axis:

Code: Select all

//Creting places
for($place = 1; $place <= 20; $place++)
{
	$y_position = 20 + $place * (imagefontheight($text_number)+3);
	imagestring($image, $text_number, 30, $y_position, $place, $header_color);
	imageline($image, 35+strlen($place)+10, ($y_position + (imagefontheight($text_number)/2))+1, 850, ($y_position + (imagefontheight($text_number)/2))+1, $line_color);
}

//Axis Y text
$y_axis_text_position = ($image_height - (strlen($y_axis_text) * imagefontwidth($text_number))) / 2;
imagestringup($image, $text_number, 5, $y_axis_text_position, $y_axis_text, $header_color);
And this is the code I'm trying to place the red circles:

Code: Select all

$connection = mysql_connect("127.0.0.1:3306", "root", "**********")
	or die ("Couldn't connect to server.");
	
$db = mysql_select_db("team_stats", $connection)
	or die ("Couldn't select database.");
	
$query = "SELECT * FROM 10_11_chart WHERE place != 0 AND place != ''";
$result = mysql_query($query)
	or die ("Query failed: " . mysql_error());

while ($row = mysql_fetch_array($result))
{
	for($place_circle = 1; $place_circle <= $row['matchday']; $place_circle++)
	{
		//$x_circle_position = 35 + $place_circle * (imagefontwidth($text_number)+15);
		$x_circle_position = 35 + ((10 * $row['matchday'])+10);
		$y_circle_position = (35+strlen($place))+(10*$row['place']);
		imagefilledellipse($image, $x_circle_position + (imagefontwidth($text_number)/2)+1, $y_circle_position, 8, 8, $chart_color);
	}
}	

mysql_close($connection);
Here's the result I'm getting:
Image
So the circles are placed as they're supposed to be (the general structure), but they're not aligned with the gray X and Y bars and not aligned to the matching days and places. I'm trying to solve this for 2 days now, tried several things and no success. Please help!

Thank you!