Location: PHPKode > scripts > SimpleGraph > simplegraph/classChart.php
<?
# SimpleGraph v0.1, July 22 2007
# Requires PHP >= 4, distributable under the GNU GPL v2
# Requires GD library installed;
# PHP Class created by Black Monkey Media (http://www.blackmonkeymedia.com)
# Any questions with this script should be sent to cadenza39[at]hotmail{dot}com or development[at]blackmonkeymedia[dot]com
# Updates will be available at http://www.blackmonkeymedia.com/code/
# construct takes array with bar height,bar name, maxHeight ( if 0 maxHeight always set at 500 point above the highest bar) 
# bar width is constant for now and image width will stretch according to # of bars
# bar color will change on every refresh

///////////////// 
//CLASS CALL
  //THIS SETS VALUE OF EACH BAR
# $bars = array(500,657,1209,2000,1110,569);
  //THIS SETS NAME OF EACH BAR
# $barNames = array('JAN','FEB','MAR','APR','MAY','JUN');
# $myChart = new Chart();
# $myChart->construct($bars,$barNames,0,'SimpleGraph by BMM');
# $myChart->displayChart();

	class Chart {
	var $image;
	var $imageWidth;
	var $imageHeight;
	var $chartMaxIndicator;
	var $chartName;
	var $numberOfBars;
	var $barBetweenGap;
	var $barColor;
	var $barShadeColor;
	var $barHeight = array();
	var $barName = array();
	var $barWidth;
	var $chartDisplayArea;
	var $chartInfoArea;
	var $increment;
	
	function construct($barHeight,$barName,$maxHeight,$chartName){
		$this->chartName = $chartName;
		$this->chartMaxIndicator=$this->setMaxIndicator($barHeight,$maxHeight);
		//DISTANCE BETWEEN BARS
		$this->barBetweenGap=15;
		//# OF BARS WILL AFFECT IMAGE WIDTH;
		$this->numberOfBars = count($barHeight);
		//IMAGE HEIGHT IS CONSTANT VALUE;
		$this->imageHeight=200;
		//SIZE OF INFO AREA;
		$this->chartInfoArea=50;
		//BAR WIDTH IS CONSTANT VALUE;
		$this->barWidth=50;
		//SET IMAGE WIDTH;
		$this->imageWidth = (($this->barBetweenGap) + $this->barWidth) * $this->numberOfBars + $this->barBetweenGap + $this->chartInfoArea;
		//SET CHART DISPLAY AREA
		$this->chartDisplayArea=$this->imageWidth-$this->chartInfoArea;
		//SET BAR HEIGHT;
		for($a=0;$a<$this->numberOfBars;$a++){
			$this->barHeight[$a]=$barHeight[$a];
			$this->barName[$a]=$barName[$a];
		}
		$this->buildImage();
		$this->drawHorisontalLines();
		$this->drawVerticalLines();
		$this->displayBars();
		$this->displayImageFrame();
	}
	//CALCULATES MAX INDICATOR
	function setMaxIndicator($barHeight,$maxHeight){
		if($maxHeight < 1){
			$size = count($barHeight);
			$testValue=0;
			for($a=0;$a<$size;$a++){
				if($barHeight[$a]>$testValue)
				{
					$testValue=$barHeight[$a];
				}
			}
			$returnValue = ceil($testValue*1.3);
		}else{
			$returnValue = $maxHeight;
		}
		return $returnValue;
	}
			
	//FUNCTION CREATES IMAGE WITH BACKGOUND
	function buildImage(){
		$this->image = imagecreate($this->imageWidth,$this->imageHeight);
		//SET BG COLOR
		$bgcolor = imagecolorallocate($this->image,240,240,250);
		$framecolor = imagecolorallocate($this->image,0,0,0);
		ImageFill($this->image,0,0,$bgcolor);
		ImageRectangle($this->image,0,0,$this->imageWidth-1,$this->imageHeight-1,$framecolor);
	}
	//DRAW BACKGROUND GRID 
	//FUNCTION DRAWS HORISONTAL LINES
	function drawHorisontalLines(){
		$this->increment = ($this->imageHeight/100)*10; //
		$startingHeight = $this->imageHeight;
		//SET COLOR FOR 5 HORISONTAL LINES
		$lightblue = imageColorAllocate($this->image,211,211,211);
		while($startingHeight>0){
			
			$y=$startingHeight;
			imageline($this->image,0,$y,$this->chartDisplayArea,$y,$lightblue);
			$startingHeight-=$this->increment;
		}
		$this->drawHorisontalIndicatingLines();
	
	}
	//DRAW 5 HORISONTAL INDICATOR LINES
	function drawHorisontalIndicatingLines(){
		$increment = ($this->imageHeight/100)*20; //
		$infoIncrement = intval(($this->chartMaxIndicator/100)*20);
		$infoIndicator=0;
		$startingHeight = $this->imageHeight;
		//SET COLOR FOR 5 HORISONTAL LINES AND NUMBERS
		$gridColor = imageColorAllocate($this->image,119,136,153);
		$fontColor=imageColorAllocate($this->image,119,136,153);
		while($startingHeight>0){
			
			$y=$startingHeight;
			imageline($this->image,0,$y,$this->chartDisplayArea,$y,$gridColor);
			imagestring($this->image,1,$this->chartDisplayArea+10,$y-2,$infoIndicator,$fontColor);
			$startingHeight-=$increment;
			$infoIndicator+=$infoIncrement;
		}
		imagestring($this->image,1,$this->chartDisplayArea+10,5,$this->chartMaxIndicator,$fontColor);
	}
	function drawVerticalLines(){
		$startingWidth = $this->chartDisplayArea;
		//SET COLOR FOR HORISONTAL LINES
		$lightblue = imageColorAllocate($this->image,211,211,211);
		while($startingWidth>0){
			
			$x=$startingWidth;
			imageline($this->image,$x,0,$x,$this->imageHeight,$lightblue);
			$startingWidth-=$this->increment;
		}
	
	}
	
	function displayBars(){
		//SET COORDINATES FOR FIRST BAR
		$xTop = $this->barBetweenGap;
		$yBottom = $this->imageHeight;
		$xBottom=$xTop+$this->barWidth;
		$fontColor = imageColorAllocate($this->image,0,0,0);
		//SET X TOP COORDINATES FOR NUMBER WITHIN BAR
		

		//CREATE BARS
		for($a=0;$a<$this->numberOfBars;$a++){
			
			$yTop = $this->imageHeight-(ceil(($this->imageHeight/100)*ceil(($this->barHeight[$a]/$this->chartMaxIndicator)*100)));
			$barColor = &$this->generateBarColor();
			//3D EFFECT
			for($b=1;$b<5;$b++){
				ImageFilledRectangle($this->image,$xTop,($yTop+$b),($xBottom+$b),$yBottom,$barColor[1]);
			}
			$strXtop=$this->calculatePosition($this->barWidth,$this->barHeight[$a]);
			ImageFilledRectangle($this->image,$xTop,$yTop,$xBottom,$yBottom,$barColor[0]);
			imagestring($this->image,2,$xTop+$strXtop,$yTop+5,$this->barHeight[$a],$fontColor);
			imagestring($this->image,2,$xTop,$yTop-17,$this->barName[$a],$fontColor);
			//SET COORDINATES FOR NEXT BAR
			$xTop += $this->barWidth+$this->barBetweenGap;
			$xBottom=$xTop+$this->barWidth;
		}
	}
	function generateBarColor(){
		/*$color1=(rand()%254);
		$color2=(rand()%254);
		$color3=(rand()%254);*/
		$color1=(rand(150,240));
		$color2=(rand(150,240));
		$color3=(rand(150,240));
		//LIGHT COLORS FOR 3D EFFECT
		$lightcolor1=$color1+10;
	    $lightcolor2=$color2+5;
		$lightcolor3=$color3+5;
		$maincolor = imagecolorallocate($this->image,$color1,$color2,$color3);
		$lightcolor = imagecolorallocate($this->image,$lightcolor1,$lightcolor2,$lightcolor3);
		return array(&$maincolor,&$lightcolor);
	
	}
	 function displayImageFrame(){
	 	$framecolor = imagecolorallocate($this->image,0,0,0);
		$fontColor = imageColorAllocate($this->image,119,136,153);
		$xTop = $this->calculatePosition($this->chartDisplayArea,$this->chartName);
	 	ImageRectangle($this->image,0,0,$this->imageWidth-1,$this->imageHeight-1,$framecolor);
		imagestring($this->image,2,$xTop,5,$this->chartName,$fontColor);
	}
	
	function displayChart(){
		header("Content-type: image/png");
		ImagePng($this->image);
		ImageDestroy($this->image);
	}
	
	function calculatePosition($sectionWidth,$string){
		$chars = strlen($string);
		$start = ($sectionWidth/2)-(strlen($string)+5);
		return $start;
	}
	
}
?>
Return current item: SimpleGraph