<?php
// Modified from original code found here:
// http://www.qualitycodes.com/tutorial.php?articleid=20
//
// Utilized the code to produce graph-pairs to reflect
// + page hits redirecting to two possible locations
// + (ie: webpage & tee-time) for each external site
// set up the graph dimensions
//
$imgx = 550;
$imgy = 420;
$border = 2;
$margin = 20;
$barx = 50;
// set up the graph formula
//
$lines = 15;
$pattrn = array(1,2,5,10,20,25);
// establish the current working directory
//
$dir = dirname(__FILE__);
// establish the course names
//
$clubs = array('Club One','Club Two','Club Three','Club Four');
// establish the graphing data
//
$clicks = array(
array(
'web' => '21',
'tee' => '12'
),
array(
'web' => '13',
'tee' => '11'
),
array(
'web' => '24',
'tee' => '12'
),
array(
'web' => '9',
'tee' => '6'
)
);
// create the graph image
//
$graphx = $imgx - ($margin * 2);
$graphy = $imgy - ($margin * 2);
$graph = imagecreate($imgx, $imgy);
// set our graph colours
//
$colour_text = imagecolorallocate($graph,0,64,128);
$colour_webbar = imagecolorallocate($graph,0,64,128);
$colour_teebar = imagecolorallocate($graph,64,128,192);
$colour_background = imagecolorallocate($graph,240,240,255);
$colour_border = imagecolorallocate($graph,200,200,200);
$colour_lines = imagecolorallocate($graph,220,220,220);
// draw the borders and margins
//
imagefilledrectangle($graph, $border, $border, $imgx-($border*2), $imgy-($border*2), $colour_border);
imagefilledrectangle($graph, $margin, $margin, $imgx-$margin, $imgy-$margin, $colour_background);
// determine the maximum click count
//
$maxval = max(max($clicks));
$maxy = 0;
$step = 0;
$i = 0;
$j = 1;
// calculate the top rounded click value
//
while($maxy < $maxval)
{
$step = $pattrn[$i];
$maxy = $step * $lines;
$i++;
if($i == count($pattrn))
{
$j = ($j * 10);
for($k=0; $k<count($pattrn); $k++)
{
$pattrn[$k] = ($pattrn[$k] * $j);
}
$i=0;
}
}
// calculate the increment ratio
//
$ratio = $graphy / $maxy;
// calculate the spacing of the horizontals
//
$hgap = ($graphy - $margin) / $lines;
// draw the base horizontal
//
$y = $imgy - $margin;
$v = 0;
imagestring($graph, 0, 5, $y-5, $v, $colour_bars);
// draw the horizontals
//
if(!$maxval)
{
$y = $margin;
$v = ($maxval) ? $maxval : 1;
imagestring($graph, 0, 5, $y-5, $v, $colour_bars);
}
else
{
for($i=1; $i<=$lines; $i++)
{
$y = $graphy - ($hgap * $i);
$v = $i * $step;
imageline($graph, $margin, $y, $imgx-$margin, $y, $colour_lines);
imagestring($graph, 0, 5, $y-5, $v, $colour_text);
}
}
// calculate the whitespace between bars
//
$bars = count($clubs);
$gap = intval(($graphx - ($barx * 2) * $bars) / ($bars + 1));
// calculate and draw the vertical bars
//
for($i=0; $i<count($clicks); $i++)
{
$hits = $clicks[$i];
foreach($hits as $key => $value)
{
$x1 = (($gap * $i) + $gap) + (($barx * 2) * $i) + (($key == 'tee') ? $barx : 0);
$y1 = ($imgy - $margin) - intval($value * $ratio);
$x2 = $x1 + $barx;
$y2 = $imgy - $margin;
$bar = "colour_{$key}bar";
imagefilledrectangle($graph, $x1, $y1, $x2, $y2, ${"colour_{$key}bar"});
imagestring($graph, 0, $x1 + 17, $y1-10, $value, $colour_text);
}
imagestring($graph, 2, $x1-intval($barx/2), $imgy-17, $clubs[$i], $color_text);
}
// write the image to the filesystem
//
imagepng($graph,"{$dir}/statistics.png", 0, PNG_NO_FILTER);
echo "<img src=\"statistics.png\" />";
// remove the original from the filesystem
//
imagedestroy($graph);
?>