Making Usage Statistics in PHP - Charting the Results
(Page 6 of 7 )
There is another interesting thing, that I've done it with $visitors array. It's keys are the hostnames of the visitors, values are the number of their visits. To draw them in our figure, I use $pixel_per_visit. That's relative to the Top 1 - the visitor whose column is the highest on the diagram.
imageline
($image, 20, 20, 20, 180, $pen);
// The vertical axe
imageline($image, 15, 25, 20, 20, $pen);
// The arrow
imageline($image, 25, 25, 20, 20, $pen);
imageline($image, 20, 180, 380, 180, $pen);
// The horizontal axe
imageline($image, 375, 175, 380, 180, $pen);
imageline($image, 375, 185, 380, 180, $pen);
$visitors=array();
while ($stat=mysql_fetch_array($stat_r))
$visitors[$stat['Host']]=$stat['Count_ID'];
// Loading up the array...
$max=current($visitors);
// How many pages did the Top 1 visit?
$pixel_per_visit=150/$max;
// This is because we get the data
// in number of visits, not in pixels
$i=0;
// This is for the loop, to know
// the number of the current visitor
foreach($visitors as $host => $visits) {
// now $visits is number of visits
imagefilledrectangle($image, 35+$i*30, 180-(visits*$pixel_per_visit),
45+$i*30, 180, $pen);
imagettftext($image, 8, 0, 40+$i*30, 175-($visits*$pixel_per_visit),
$pen, "\windows\fonts\arial.ttf", $host);
imagettftext($image, 15, 0, 32+(($i++)*30), 200,
$pen,"\windows\fonts\arial.ttf", $visits);
}
When you're ready with drawing the figure you have to send the output to the browser. Here are the functions and the code:
- ImageGIF (resource image) - Outputs the image in GIF format
- ImageJPEG (resource image) - Outputs the image in JPEG format
- ImagePNG (resource image) - Outputs the image in PNG format imagepng($image);
Note that these functions accept an optional second argument, the name of a new file to save the picture there. This can be useful if you'd like to make a picture saving option for your site.
And here's the result:

Next: Before Saying Goodbye >>
More PHP Articles
More By Adam Szanto