PHP Graph [ Vertical Bar Pair ]

Downloads:4295 Bookmark this code Bookmark and Share
Rank : 4/5
Rated by : 5 user(s)
Developer:Andrew R  |   13 submission(s)
Date Uploaded:July 13,2009
Level:Intermediate
Size: 0 Bytes
Category:PHP -> Images/Graphics
Developer Says:

The code can be used to produce graph-pairs to reflect page hits redirecting to two possible locations (ie: webpage & tee-time) for each external site. This code is modified from the original code found at <a href="http://www.qualitycodes.com/tutorial.php?articleid=20">http://www.qualitycodes.com/tutorial.php?articleid=20</a>

<?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);
 
 ?>


User Reviews
Susan
[20-Jul-2009]
#1

Andrew, this is great. Have you been able to turn it into a stacked bar graph (one bar above the other) or a line graph?

ashraf
[30-Sep-2009]
#2

the code give the following error: Warning: Wrong parameter count for imagepng() in D:test workxampphtdocsgraphpair_graph.php on line 163 can you please send me the complete code? thank you

Bhupinder
[20-Apr-2010]
#3

Excellent ! I copy/paste and it worked wonders. Amazing ..

Kriti Raj
[29-Nov-2010]
#4

Simple usefull....

Leave a Comment
Age (Required, will not be shown)
Name
Email (Required, will not be shown)
Website (Optional, starting with http://)
 
Rate this code
Poor   1 2 3 4 5   Outstanding
Are you human ?

Enter the code shown above