#native_company# #native_desc#
#native_cta#

Graph IT

By Kobi Tyrkel
on December 18, 2006

Version: 1.0

Type: Full Script

Category: Graphics

License: GNU General Public License

Description: Graph IT – Display dynamic graph on your website using this free code – input can be by sending the data or sending a link to a file with the data
See example of script usage on <a href=http://www.3dpictor.com>3D Pictor</a>
I would appreciate a link back to http://www.3dpictor.com from the pages displaying graphs using this code

<?php
/************************************************************************
*	GraphIT - Dynamic graph display using PHP GD
*	By Kobi Tyrkel - (c) 2006
*	Free for use under the limitation of having this remark left in the 
*	code and a link to http://www.3dpictor.com with the 
*	title "GraphIT - Dynamic graph display from www.3dpictor.com"
*	from each page that contains the graph
************************************************************************/

// Graph display parameters
$SIZE = ((isset($_GET['size'])) ? $_GET['size'] : 25);
$THICK = ((isset($_GET['thick'])) ? $_GET['thick'] : 1);
$GRAPHTYPE = ((isset($_GET['graphtype'])) ? $_GET['graphtype'] : 4);
$XOFFSET = $SIZE;
$YOFFSET = 1.3 * $SIZE;
$STARTFROMZERO = ((isset($_GET['startfromzero'])) ? $_GET['startfromzero'] : 1);

function imagethreedframe ($img, $x0, $y0, $x1, $y1, $colour, $colourtop, $colourside, $backgroundcolor, $dx, $dy)
{
	if ($y0 > $y1)
		imagefilledpolygon($img, array ($x0 - $dx, $y1 + $dy, $x0, $y1, $x1, $y1, $x1 - $dx, $y1 + $dy), 4, $colourtop);
	else
		imagefilledpolygon($img, array ($x0 - $dx, $y0 + $dy, $x0, $y0, $x1, $y0, $x1 - $dx, $y0 + $dy), 4, $colourtop);
	imagefilledpolygon($img, array ($x1 - $dx, $y0 + $dy, $x1- $dx, $y1 + $dy, $x1, $y1, $x1, $y0), 4, $colourside);
	imagefilledrectangle($img, $x0 - $dx, $y0 + $dy, $x1 - $dx, $y1 + $dy, $colour);
	if ($y0 > $y1)
		imagepolygon($img, array ($x0 - $dx, $y1 + $dy, $x0, $y1, $x1, $y1, $x1 - $dx, $y1 + $dy), 4, $backgroundcolor);
	else
		imagepolygon($img, array ($x0 - $dx, $y0 + $dy, $x0, $y0, $x1, $y0, $x1 - $dx, $y0 + $dy), 4, $backgroundcolor);
	imagepolygon($img, array ($x1 - $dx, $y0 + $dy, $x1- $dx, $y1 + $dy, $x1, $y1, $x1, $y0), 4, $backgroundcolor);
	imagerectangle($img, $x0 - $dx, $y0 + $dy, $x1 - $dx, $y1 + $dy, $backgroundcolor);
}

function imageframe ($img, $x0, $y0, $x1, $y1, $colour, $colourtop, $colourside, $backgroundcolor)
{
	imagefilledrectangle($img, $x0, $y0 , $x1, $y1 , $colour);
	imagerectangle($img, $x0, $y0, $x1, $y1, $backgroundcolor);
}

// Location of the data file
$datafilename = ((isset($_GET['data'])) ? $_GET['data'] : '');


// Get a data file into an array of series
// the data source in a file at a URL
if ($datafilename<>'') 
	$series = file($datafilename);
else
	$series = Array($_GET['headers'], $_GET['values']);

$count_of_array_elements = 0;
$min_value = 0;
$max_value = 0;

$xvalue = explode(",", $series[0]);
$yvalue = explode(",", $series[1]);
$count_of_array_elements = max (count($xvalue),count($yvalue));
$min_value = min(0, min($yvalue));
$max_value = max(0, max($yvalue));

$WIDTH = ($count_of_array_elements + 2) * $SIZE;
$HEIGHT = ($max_value - $min_value+ 3) * $SIZE;
header ("Content-type: image/jpeg");

$img = ImageCreate ($WIDTH, $HEIGHT) or die("Cannot Initialize new GD image stream ");
$backgroundcolor = ImageColorAllocate ($img, 0, 0, 0);
$gridcolor = ImageColorAllocate ($img, 255, 255, 255);

//	X GRID
for ($x = 0; $x < count($xvalue); $x++) {
	imageline($img, $XOFFSET + $x * $SIZE, $HEIGHT - $YOFFSET ,$XOFFSET + $x * $SIZE - $SIZE/4, $HEIGHT - $YOFFSET + $SIZE/4, $gridcolor);
	imagestring($img, 5, $XOFFSET + $x * $SIZE - $SIZE/4, $HEIGHT - 0.6 * $SIZE * (2- $x%2), $xvalue[$x], $gridcolor);
}

// Y GRID
for ($y = $min_value; $y < $max_value + 1; $y++) {
	imageline($img, $XOFFSET, $HEIGHT - $YOFFSET - $y * $SIZE + $min_value * $SIZE, $XOFFSET - $SIZE/4, $HEIGHT - $YOFFSET - $y * $SIZE + $min_value * $SIZE, $gridcolor);
	imagestring($img, 5, 0, $HEIGHT - $SIZE - $y * $SIZE - $SIZE/2  + $min_value * $SIZE, $y, $gridcolor);
	imageline($img, $XOFFSET, $HEIGHT - $YOFFSET - $y * $SIZE  + $min_value * $SIZE, $XOFFSET + $count_of_array_elements * $SIZE, $HEIGHT - $YOFFSET - $y * $SIZE  + $min_value * $SIZE, $gridcolor);
}

imageline($img, $XOFFSET, $HEIGHT - $YOFFSET + $min_value * $SIZE, $XOFFSET + $count_of_array_elements * $SIZE, $HEIGHT - $YOFFSET  + $min_value * $SIZE, $gridcolor);
imageline($img, $XOFFSET, $HEIGHT - $YOFFSET , $XOFFSET, $HEIGHT - $YOFFSET- ($max_value + 1) * $SIZE  + $min_value * $SIZE, $gridcolor);

imagesetthickness ($img, $THICK);
$serie = 1;
$start=1;
$x1=0;
$y1=  $min_value * $SIZE;
if (is_array($yvalue)) {
	$colour[$serie] = ImageColorAllocate($img, 22 * $serie, 100 * $serie, 254 * $serie);
	$colourtop[$serie] = ImageColorAllocate($img, 2 * $serie, 2 * $serie, 214 * $serie);
	$colourside[$serie] = ImageColorAllocate($img, 2 * $serie, 2 * $serie, 224 * $serie);
	foreach ($yvalue as $line_num => $value) {
	$x0=$x1;
	$y0=$y1;
	$x1=$line_num * $SIZE + $XOFFSET;
	$y1=$HEIGHT - $value * $SIZE - $YOFFSET  + $min_value * $SIZE;
	switch ($GRAPHTYPE)	{
		case 1:	imagefilledellipse($img, $x1, $y1, $THICK*2, $THICK*2, $colour[$serie]);
						break;
		case 2:	if ($start == 1) {
							$start = 0;
						} else {
							imageline($img, $x0, $y0, $x1, $y1, $colour[$serie]);
						}
						break;
		case 3:	imageframe ($img, $x1 - ($serie - 1) * $SIZE, $HEIGHT - $YOFFSET + ($serie - 1) * $SIZE  + $min_value * $SIZE, $x1+$SIZE/1.1  - ($serie - 1) * $SIZE , $y1  + ($serie - 1) * $SIZE, $colour[$serie], $colourtop[$serie], $colourside[$serie], $backgroundcolor);
						break;
		case 4:	imagethreedframe ($img, $x1 - ($serie - 1) * $SIZE, $HEIGHT - $YOFFSET + ($serie - 1) * $SIZE  + $min_value * $SIZE, $x1+$SIZE/1.1  - ($serie - 1) * $SIZE , $y1  + ($serie - 1) * $SIZE, $colour[$serie], $colourtop[$serie], $colourside[$serie], $backgroundcolor,$SIZE/4, $SIZE/4);
						break;
		default :	imagefilledellipse($img, $x1, $y1, $THICK*2, $THICK*2, $colour[$serie]);
						if ($start == 1) {
							$start = 0;
						} else {
							imageline($img, $x0, $y0, $x1, $y1,  $colour[$serie]);
						}
						break;
		}
	}
}
imagegif($img);  
ImageDestroy($img);
?>