Specifications

to draw a black outline around the edge of the canvas. This function draws an outlined rectan-
gle instead of a filled one. The parameters are the same. Notice that we have drawn the rectan-
gle to $width-1 and $height-1a canvas of width by height goes from (0, 0) to these values.
If we drew it to $width and $height, the rectangle would be outside the canvas area.
We use the same logic and functions as we did in our last script to center and write the title on
the graph.
Finally, we draw the baseline for the bars with
ImageLine($im, $x, $y-5, $x, $height-15, $line_color);
The ImageLine() function draws a line on the image we specify ($im) from one set of coordi-
nates (
$x, $y-5) to another ($x, $height-15), in the color specified by $line_color.
In this case, we draw the baseline from a little above where we want to draw the first bar, to a
little above the bottom of the canvas.
We are now ready to fill in the data on the graph. Part 4 is shown in Listing 19.5.4.
LISTING 19.5.4 showpoll.php Part 4 Draws the Actual Data on to the Graph and
Finishes Up
/*******************************************
Draw data into graph
*******************************************/
// Get each line of db data and draw corresponding bars
while ($row = mysql_fetch_object ($result))
{
if ($total_votes > 0)
$percent = intval(round(($row->num_votes/$total_votes)*100));
else
$percent = 0;
// display percent for this value
ImageTTFText($im, $main_size, 0, $width-30, $y+($bar_height/2),
$percent_color, $font, $percent.”%”);
if ($total_votes > 0)
$right_value = intval(round(($row->num_votes/$total_votes)*100));
else
$right_value = 0;
// length of bar for this value
$bar_length = $x + ($right_value * $bar_unit);
// draw bar for this value
Advanced PHP Techniques
P
ART IV
426
24 7842 CH19 3/6/01 3:42 PM Page 426