Location: PHPKode > scripts > Graidle > graidle/graidle.php
<?php
#Graidle Versione 0.2.2

class Graidle
{
	var $font="./vera.ttf";		#Riferimento al file TTF
	var $font_big=18;			#Grandezza carattere 'Grande'
	var $font_small=8;			#Grandezza carattere 'Piccolo'
	###
	var $legend=0;				#flag per visualizzare la legenda
	var $dim_quad=12;			#dimensione quadrato di riferimento legenda
	var $spacing=6;				#spaziatura voci legenda
	###
	var $font_color="#000000";	#Colore del Carattere
	var $bg_color="#FFFFFF";	#Colore dello Sfondo
	var $axis_color="#666666";	#Colore degli Assi
	###
	var $value=array();
	var $type=array();

	var $cvl=0;					#variabile Current Value
	var $mx=0;					#variabile del massimo
	var $mn=0;					#variabile del minimo
	var $cn=0;					#variabile del nubero massimo di valori in una serie

	function Graidle($title=NULL,$mass=NULL,$mnvs=NULL)
	{
		$this->title=$title;
		$this->width=NULL;
		$this->height=NULL;
		$this->xAxis=NULL;
		$this->yAxis=NULL;
		$this->vlx=NULL;
		$this->legend=NULL;
		$this->sx=0;
		$this->sy=0;
		$this->larg=20;
		$this->mass=$mass;
		$this->mnvs=$mnvs;
		$this->LegendAlign="right";
		include_once("graidle_color.ext.php");
		Color::color();
	}
	function create()
	{
		if(!in_array("p",$this->type))
		{
			for($bar=$i=0;$i < count($this->type);$i++)
				if($this->type[$i]=='b')	$bar+=1;

			$this->disbar=$this->larg*$bar;

			if(in_array("l",$this->type))
				if($this->disbar==0)
					$this->disbar=2*$this->larg;

			if(!isset($this->mass))	$this->mass=$this->mx;
			if(!isset($this->mnvs))	$this->mnvs=$this->mn;

			$this->dvx=round($this->mass/10);

			if($this->mx>0)
			{
				if($this->mass==$this->mx)		$this->scarmax=0;						#considerare se mettere zero o un valore o rimettere dvx
				else							$this->scarmax=$this->mass-$this->mx;
			}
			if($this->mn<0)
			{
				if($this->mnvs>0)										$this->scarmin=0;
				else if($this->mnvs>$this->mn||$this->mnvs<$this->mn)	$this->scarmin=$this->mnvs-$this->mn;
				else 													$this->scarmin=-1;
			}

			$this->ld=$this->larg+$this->disbar;	# variabile di comodo #

			if(strlen($this->mn)>strlen($this->mx))	$this->y_flag=strlen($this->mn);
			else									$this->y_flag=strlen($this->mx);

			$this->s+=$this->font_small*($this->y_flag+2);

			if(!isset($this->w))
			{
				$this->w=($this->ld*$this->cnt)+$this->s+$this->d;
	
				while($this->w>=800)
				{
					$this->larg-=1;
					$this->disbar=($this->larg)*$bar;
					$this->ld=$this->larg+$this->disbar;
					$this->w=($this->ld*$this->cnt)+$this->s+$this->d;
				}
			}
			
			else
			{
				while( (($this->ld*$this->cnt)+$this->s+$this->d) <= $this->w)
				{
					$this->larg+=1;
					$this->disbar=($this->larg)*$bar;
					$this->ld=$this->larg+$this->disbar;				
				}
				
				while( (($this->ld*$this->cnt)+$this->s+$this->d) >= $this->w)
				{
					$this->larg-=1;
					$this->disbar=($this->larg)*$bar;
					$this->ld=$this->larg+$this->disbar;				
				}
			
			}

			if(!isset($this->h))	$this->h=round((3/4)*$this->w);
			
			$this->b=5*$this->font_small;
			
			if(!isset($this->title))	$this->a=0;
			else						$this->a=2*$this->font_big;

			if($this->mnvs>0&&$this->mass>0)	$this->mul=($this->h-$this->a-$this->b)/($this->mx+$this->scarmax-$this->mnvs);
			else								$this->mul=($this->h-$this->a-$this->b)/(($this->mx+$this->scarmax)+(abs($this->mn)-$this->scarmin));

			$this->div=$this->dvx*$this->mul;
			$this->im=imagecreatetruecolor($this->w,$this->h);							#<----CREO L'IMMAGINE PER IL GRAFICO ISTO O LINEE
			
			$rgb=Color::hex2rgb($this->axis_color);
			$this->axis_color=imagecolorallocate($this->im,$rgb[0],$rgb[1],$rgb[2]);

			$rgb=Color::hex2rgb($this->font_color);
			$this->font_color=imagecolorallocate($this->im,$rgb[0],$rgb[1],$rgb[2]);

			$rgb=Color::hex2rgb($this->bg_color);
			$this->bg_color=imagecolorallocate($this->im,$rgb[0],$rgb[1],$rgb[2]);
			imagefilltoborder($this->im,1,1,1,$this->bg_color);							#<---- Creo lo sfondo

			if(isset($this->legend) || isset($this->name))	graidle::legend();
						
			graidle::title($this->title,$this->xAxis,$this->yAxis);
			graidle::gradAxis($this->sx,$this->sy);
			
			if(in_array("b",$this->type))
			{
				include("graidle_histo.ext.php");
				histogram::drawHisto();
			}
			
			if(in_array("l",$this->type))
			{
				include("graidle_line.ext.php");
				line::drawLine();
			}
			graidle::drawAxis();
		}
		else
		{
			include("graidle_pie.ext.php");

			for($this->pie=$i=0;$i < count($this->type);$i++)
				if($this->type[$i]=='p')	$this->pie+=1;
				
			if(!isset($this->incl))	$this->incl=55;
			
			if(!isset($this->AA))	$this->AA=4;
			
			if(!isset($this->w))	$this->w=500;
			
			if(!isset($this->h))	$this->h=500;
			
			$this->tre_d=0;
			if($this->incl!=90)		$this->tre_d=round(($this->incl*10)/60);

			$this->radius=$this->w;
			
			$this->a=$this->font_big+6;
			$this->b=5;
			$e=sin(deg2rad($this->incl));
			$rapp=pow($e,2);
			$a=$this->radius;
			$b=$a*$rapp;
			
			while( $a >= ($this->w-$this->s-$this->d))
			{
				$a-=1;

				$this->radius=$a;
				
				$b=$a*$rapp;			
			}
			
			while( ($b*$this->pie) > $this->h-($this->a)-($this->pie*$this->b)-($this->pie*$this->tre_d))
			{
				$b-=1;

				$a=$b/$rapp;

				$this->radius=$a;
			}

			$this->im=imagecreatetruecolor($this->w,$this->h);	#<----CREO L'IMMAGINE PER IL GRAFICO A TORTA

			$rgb=Color::hex2rgb($this->bg_color);
			$this->bg_color=imagecolorallocate($this->im,$rgb[0],$rgb[1],$rgb[2]);
			imagefilltoborder($this->im,1,1,1,$this->bg_color);	#<---- Creo lo sfondo

			$rgb=Color::hex2rgb($this->font_color);
			$this->font_color=imagecolorallocate($this->im,$rgb[0],$rgb[1],$rgb[2]);
			
			graidle::legend();
			graidle::title($this->title);
			pie::drawPie($a,$b);
		}
	}
	function carry()
	{
		header("Content-type: image/png");
		header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
		header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
		header("Cache-Control: post-check=0, pre-check=0", false);
		header("Pragma: no-cache");
		imagepng($this->im);
		imagedestroy($this->im);
	}
	function carry2file()
	{
		$fname=rand(0,9999);
		imagepng($this->im,"./tmp/$fname.png");
		echo"<img src=\"./tmp/$fname.png\">";
		imagedestroy($this->im);
	}
	function title($title=NULL,$xAxis=NULL,$yAxis=NULL)
	{
		if($xAxis!="" || $xAxis!=NULL)	imagefttext($this->im , $this->font_small , 0 , ($this->w-$this->d)-((imagefontwidth($this->font_small))*strlen($xAxis)) , $this->h-$this->font_small , $this->font_color , $this->font , $xAxis);
		if($yAxis!="" || $yAxis!=NULL)	imagefttext($this->im , $this->font_small , 90 , ($this->s)-($this->font_small*strlen($this->mx)+4) , $this->a+((imagefontwidth($this->font_small))*strlen($yAxis)) , $this->font_color , $this->font , $yAxis);
		if($title!="" || $title!=NULL)	imagefttext($this->im , $this->font_big , 0 , ($this->w/2)-((strlen($title)*imagefontwidth($this->font_big))/2) , ($this->a/2)+($this->font_big/2) , $this->font_color , $this->font , $title);
	}
	function drawAxis()
	{
		imageline($this->im , $this->s , $this->a , $this->s , $this->h-$this->b , $this->axis_color);

		$n=1;
		if(!isset($this->vlx))
			for($i=1;$i<=$this->cnt;$i++)
				$this->vlx[$i]=$i;

		else	$n=0;

		for($i=$this->s ; $i<=($this->w-$this->d) ; $i+=$this->disbar+$this->larg , $n++ )
		{
			imageline($this->im , $i , $this->h-$this->b+2 , $i , $this->h-$this->b , $this->axis_color);
			imagefttext($this->im , $this->font_small , 0 , $i+(($this->larg+$this->disbar)/2)-(($this->font_small*strlen($this->vlx[$n]))/2) , $this->h-($this->b/2)-($this->font_small/2) , $this->font_color , $this->font , $this->vlx[$n]);
		}

		if($this->mnvs<=0)
		{
			imageline($this->im , $this->s , $lu=$this->h-$this->b-(abs($this->mn+$this->scarmin)*$this->mul) , $this->w-$this->d,$lu,$this->axis_color);
		}
	}
	function gradAxis($sy=NULL,$sx=NULL)
	{
		$c=imagecolorallocatealpha($this->im,255,255,255,127);
		
		$style=array($c,$this->axis_color);
		imagesetstyle ($this->im, $style);

		if(strlen($this->mn)>strlen($this->mx))	$y_flag=strlen($this->mn);
		else									$y_flag=strlen($this->mx);

		#Asse x griglia secondaria e tacche
		if($sx)
			for($i=$this->s;$i<=($this->w-$this->d);$i+=$this->disbar+$this->larg)
				imageline($this->im, $i , $this->a , $i , $this->h-$this->b , IMG_COLOR_STYLED);
		#Asse Y
		$zero=(($this->scarmax+$this->mx)*$this->mul)+$this->a;
		#Upper zero
		for($n=($this->dvx*$this->mul) ; ($zero-$n) >= $this->a ; $n+=($this->dvx*$this->mul))
		{
			imageline($this->im, $this->s-2 , $zero-$n , $this->s , $zero-$n ,$this->axis_color);
			if($sy)	imageline($this->im, $this->s , $zero-$n , $this->w-$this->d , $zero-$n , IMG_COLOR_STYLED);
			$v=($n/$this->mul);
			imagefttext($this->im,$this->font_small,0,$this->s-(imagefontwidth($this->font_small)*strlen($v)),($zero-$n)+($this->font_small/2),$this->font_color,$this->font,$v);
		}
		#Under zero
		for($n=($this->dvx*$this->mul) ; ($zero+$n) <= ($this->h-$this->b) ; $n+=($this->dvx*$this->mul))
		{
			imageline($this->im, $this->s-2 , $zero+$n , $this->s , $zero+$n ,$this->axis_color);
			if($sy)	imageline($this->im, $this->s , $zero+$n , $this->w-$this->d , $zero+$n , IMG_COLOR_STYLED);
			$v=($n/$this->mul);
			imagefttext($this->im,$this->font_small,0,$this->s-(imagefontwidth($this->font_small)*(strlen($v)+1)),($zero+$n)+($this->font_small/2),$this->font_color,$this->font,-$v);
		}
	}
	function legend()
	{
		if(!isset($this->legend))
			$this->legend=array_merge($this->name,$this->legend);

		$cla=imagecolorallocatealpha($this->im,127,127,127,110);

		$sp_mez=$this->spacing/2;

		if($this->LegendAlign=="right")
		{
			$x1=($this->w-$this->d)+($this->spacing);
			$x2=$this->w-1;
			$y1=$this->a;
			$y2=($this->a)+($this->dim_quad+$this->spacing)*(count($this->legend));
		}
		
		else if($this->LegendAlign=="left")
		{
			$x1=0;
			$x2=$this->s-($this->font_small*(strlen($this->mx)+2)+4);
			$y1=$this->a;
			$y2=($this->a)+($this->dim_quad+$this->spacing)*(count($this->legend));
		}

		imagefilledrectangle($this->im, $x1 , $y1 , $x2 , $y2 , $cla);
		imagerectangle($this->im, $x1 , $y1 , $x2 , $y2 , $cla);

		for($x1+=$sp_mez,$y1+=$sp_mez,$s=1,$i=0;$i < count($this->legend);$i++,$s++,$y1+=$this->spacing)
		{
			$c=$this->color[$i];
			list($name,$red,$green,$blue)=explode(',',$c);
			$rgb=imagecolorallocatealpha($this->im,$red,$green,$blue,12);
			imagefilledrectangle($this->im , $x1 , $y1 , $x1+$this->dim_quad , $y1+=$this->dim_quad , $rgb);
			$rgb=imagecolorallocatealpha($this->im,$red/2,$green/2,$blue/2,90);
			imagerectangle($this->im , $x1 , $y1-$this->dim_quad , $x1+$this->dim_quad , $y1 , $rgb);
			imagefttext($this->im , $this->font_small , 0 , $x1+$this->dim_quad+$sp_mez , $y1-($this->dim_quad/2)+($this->font_small/2), $this->font_color , $this->font , $this->legend[$i]);
			imageline($this->im,$x1-($sp_mez),$y1+$sp_mez,$x2,$y1+$sp_mez,$cla);
		}
	}
	function setValue($value,$type,$name=NULL)
	{
		array_push($this->value,$value);
		array_push($this->type,$type);

		if(isset($name))
		{
			if($type=="p")
			{
				if(!isset($this->PieTitle))	$this->PieTitle=array();				
				array_push($this->PieTitle,$name);
			}
			else
			{
				if(!isset($this->name))	$this->name=array();				
				array_push($this->name,$name);
					
				for($maxlen=$i=0;$i < count($this->name);$i++)
					if($maxlen < strlen($this->name[$i]))
						$maxlen=strlen($this->name[$i])+1;
	
				$this->d=($this->spacing*3)+($this->dim_quad)+($this->font_small*$maxlen);
			}
		}

		if(max($this->value[$this->cvl])>$this->mx)		$this->mx=max($this->value[$this->cvl]);
		if(min($this->value[$this->cvl])<$this->mn)		$this->mn=min($this->value[$this->cvl]);
		if(count($this->value[$this->cvl])>$this->cnt)	$this->cnt=count($this->value[$this->cvl]);

		$this->cvl+=1;
	}	
	function setHeight($height){
		if(is_numeric($height))	$this->h=$height;
	}
	function setWidth($width){
		if(is_numeric($width))	$this->w=$width;
	}
	function setFont($font){
		$this->font=$font;
	}
	function setBgCl($HEXcolor){
		$this->bg_color=$HEXcolor;
	}
	function setFontCl($HEXcolor){
		$this->font_color=$HEXcolor;
	}
	function setAxisCl($HEXcolor){
		$this->axis_color=$HEXcolor;
	}
	function setSecondaryAxis($sx,$sy)
	{
		if($sx)	$this->sx=1;
		if($sy)	$this->sy=1;
	}
	function setXtitle($xAxis){
		$this->xAxis=$xAxis;
	}
	function setYtitle($yAxis){
		$this->yAxis=$yAxis;
	}
	function setXValue($vlx){
		$this->vlx=$vlx;
	}
	function setInclination($incl){
		$this->incl=$incl;
	}
	function setAA($AA){
		$this->AA=$AA;
	}
	function setLegend($legend,$align=NULL)
	{
		$this->legend=$legend;
		
		for($maxlen=$i=0;$i < count($this->legend);$i++)
				if($maxlen < strlen($this->legend[$i]))
					$maxlen=strlen($this->legend[$i]);

		$this->LegendAlign=strtolower($align);
		
		switch($this->LegendAlign)
		{
			case "left":	$this->s+=($this->spacing*3)+($this->dim_quad)+($this->font_small*$maxlen);break;
			default:		$this->LegendAlign="right";$this->d=($this->spacing*3)+($this->dim_quad)+($this->font_small*$maxlen);break;
		}
	}
	function setPieLegendExt($type=0)
	{
		switch($type)
		{
			case 1 :	$this->PieExtLeg=1;break;	#Only Percent;
			case 2 :	$this->PieExtLeg=2;break;	#Both Value and Percent;
			default:	$this->PieExtLeg=0;break;	#Only Value;
		}
	}
}
?>
Return current item: Graidle