#native_company# #native_desc#
#native_cta#

Graph of the type cheese 3D

By Fernando Gomes
on February 20, 2005

Version: 1.0

Type: Function

Category: Graphics

License: GNU General Public License

Description: it allows to the creation of graficos 3D, the data can be gotten through one bd, it functions well, it needs the library php_gd2

<?php 					

	  class arc{
     			var $start, $end; 
     			function arc($s=0, $e=360){
     						$this->start = $s;
     						$this->end = $e; 	 
     			}
    } 		
    class graficoQueijo{
    			var $cx, $cy, $image, $array_dados, $array_nomes, $arcos;
					var $w, $h, $total, $width, $height, $num_barras;
    			var $cor_titulo, $cor_fundo, $cor_letras, $cores, $dark_cores, $preto;					
    			
    			function graficoQueijo($larg=220, $alt=300, $estilo){
    					$this->image = imagecreate($larg, $alt);		            	 					         
    					$this->cores = array("0" => imagecolorallocate($this->image, 0xFF, 0x00, 0x00), //red
            					 	 				   "1" => imagecolorallocate($this->image, 0x00, 0xFF, 0x00),  //green
                    						   "2" => imagecolorallocate($this->image, 0x00, 0x00, 0xFF),  //blue   
            										   "3" => imagecolorallocate($this->image, 0xFF, 0xFF, 0x00), //yellow
																	 "4" => imagecolorallocate($this->image, 0xFF, 0x00, 0xFF), //purple
																	 "5" => imagecolorallocate($this->image, 0x00, 0xFF, 0xFF)); //cyan
          	  $this->dark_cores = array("0" => imagecolorallocate($this->image, 0x90, 0x00, 0x00), 
                    					 	 				"1" => imagecolorallocate($this->image, 0x00, 0x90, 0x00),  
                            						"2" => imagecolorallocate($this->image, 0x00, 0x00, 0x90),  
                    										"3" => imagecolorallocate($this->image, 0x90, 0x90, 0x00),
																				"4" => imagecolorallocate($this->image, 0x90, 0x00, 0x90),
																				"5" => imagecolorallocate($this->image, 0x00, 0x90, 0x90)); 
							//$this->cor_fundo = imagecolorallocate($this->image, 0xDE, 0xE3, 0xE7);	
							$estilo = ($estilo == "") ? 'Classico' : $estilo;
      				switch($estilo){			 
						 			case 'Classico': $this->cor_fundo = imagecolorallocate($this->image, 0xC1, 0xC7, 0xCC);
											 break;
								  case 'Feminista': $this->cor_fundo = imagecolorallocate($this->image, 0xFF, 0xB3, 0xB3);
											 break; 
									case 'Moderno': //$this->cor_fundo = imagecolorallocate($this->image, 0xDE, 0xEB, 0xF7);
											 $this->cor_fundo = imagecolorallocate($this->image, 0x8C, 0xBA, 0xDE);
											 break; 
						  }		
							$this->preto = imagecolorallocate($this->image, 0, 0, 0); 																	 		
    					$this->array_dados = array();
							$this->array_nomes = array(); 		 										
    					$this->cx = 208;
    					$this->cy = 170;				
    					$this->w = 350; // 300
    					$this->h = 170; // 200
							$this->width = $larg;
							$this->height = $alt;																		 
    					imagefill($this->image, 0, 0, $this->cor_fundo);	
							imagesetpixel ($this->image, 100, 100, $this->cor_fundo);			
							imagerectangle ($this->image, 0, 0, $larg-1, $alt-1, $this->preto);
    			}
    			
    			function adicionarTitulo($titulo){	
    					$this->cor_titulo = imagecolorallocate($this->image, 255, 255, 255);
							$corRect = imagecolorallocate($this->image, 0, 0x3F, 0x7D); 
							$x2 = $this->width-2;
							$y2 = 20;
							imagefilledrectangle ($this->image, 1, 1, $x2, $y2, $corRect); 			  		
							imagestring($this->image, 5, 4, 3, ':: '.$titulo, $this->cor_titulo);		
							imageline ($this->image, 0, $y2+1, $x2, $y2+1, $this->preto);															
    		  }
    			
    			function addDados($dado){ array_push($this->array_dados, $dado); }
					
					function addNomes($dado){ array_push($this->array_nomes, $dado); }
					
					function destroy(){ imagedestroy($this->image); }												
					    							    			
    			function criaGrafico(){		
    					$this->total = array_sum($this->array_dados);
          		$num = count($this->array_dados);					
              $this->arcos = array($num); // guarda os angulos dos arcos		 							
          		$start=0; $soma=0;				
							if ($this->total == 0)
								 return;			
          		for ($a=0; $a<$num; $a++){
          					$end = $this->array_dados[$a]/$this->total*360;	
          					$soma += $end;
          					$this->arcos[$a] = new arc($start, $soma);
          					$start = $soma;
          		}																																																				
          		// -------- faz o efeito 3D --------
							$inc3D = $this->cy - 35;  // 20
							srand((double)microtime()*1000000);		
              for ($i=$this->cy; $i>$inc3D; $i--){
          			 for ($a=0; $a<$num; $a++){
								 	 if ($a+1 > count($this->cores)){									 
											$r = rand(100, 255);
											$g = rand(100, 255);
											$b = rand(100, 255);
											$cor = imagecolorallocate($this->image, $r, $g, $b);
											$dark_cor = imagecolorallocate($this->image, $r-100, $g-100, $b-100);														
											array_push($this->cores, $cor);
											array_push($this->dark_cores, $dark_cor);		
									 }imagefilledarc($this->image, $this->cx, $i, $this->w, $this->h, $this->arcos[$a]->start, $this->arcos[$a]->end, $this->dark_cores[$a], IMG_ARC_PIE);									 									 
								 }	        
							}		
							
							// --- parte de baixo ---
							imagearc($this->image, $this->cx, $this->cy, $this->w, $this->h, 0, 180, $this->preto); 
							$x1 = $this->cx+$this->w*cos(deg2rad(0))/2; 
							$x2 = $this->cx+$this->w*cos(deg2rad(180))/2;		
							imageline ($this->image, $x1, $inc3D, $x1, $this->cy, $this->preto);
							imageline ($this->image, $x2, $inc3D, $x2, $this->cy, $this->preto);		
											 							
							//--- desenha a parte de cima do grfico ---																												              																												
							for ($a=0; $a<$num; $a++){ 			
								 if ($this->array_dados[$a] == $this->total){										 		
								 		imagefilledarc($this->image, $this->cx, $inc3D, $this->w, $this->h, $this->arcos[$a]->start, $this->arcos[$a]->end, $this->cores[$a], IMG_ARC_PIE);
										imagearc($this->image, $this->cx, $inc3D, $this->w, $this->h, 0, 360, $this->preto);
								 }else{
								 		imagefilledarc($this->image, $this->cx, $inc3D, $this->w, $this->h, $this->arcos[$a]->start, $this->arcos[$a]->end, $this->cores[$a], IMG_ARC_PIE);	
  									imagefilledarc($this->image, $this->cx, $inc3D, $this->w, $this->h, $this->arcos[$a]->start, $this->arcos[$a]->end, $this->preto, IMG_ARC_NOFILL | IMG_ARC_EDGED);										 								 
								 }									
							}																														  
    			}						    		
					
    			function desenhaGrafico(){			 
						  imagepng($this->image);    				 						 
    				  imagedestroy($this->image);
     			}
					
					function guardaGrafico($img){										 
							imagepng($this->image, $img);
							imagedestroy($this->image);
  			  }		
										
					function criaLegenda(){			
							$cor_legenda = imagecolorallocate($this->image, 255, 255, 204);		 
  						$this->num_barras = count($this->array_dados);				      																			
  						$x1=10;	
							$y1=290;
  						$x2=$this->width-11; 
							$y2=$y1+(20*$this->num_barras); 
  					  $inc=10; 																						
  						imagestring ($this->image, 3, 10, $y1-15, "Legenda:", $this->dark_cores[2]);						
            	imagerectangle($this->image, $x1, $y1, $x2, $y2, $this->dark_cores[2]);  //caixa envolvente
							imagefilledrectangle($this->image, $x1+1, $y1+1, $x2-1, $y2-1, $cor_legenda);
  						$x1+=10; 								
  						$total = array_sum($this->array_dados);
  						$soma = 0;	
							if ($total == 0)
								 return;					
  						for ($a=0, $k=5; $a<$this->num_barras; $a++, $k+=20){
  							  imagefilledrectangle($this->image, $x1, $y1+$k, $x1+$inc, $y1+$k+$inc, $this->cores[$a]);					
      						imagerectangle($this->image, $x1-1, $y1+$k-1, $x1+$inc+1, $y1+$k+$inc+1, $this->preto);												      		
      						imagestring($this->image, 3, 38, $y1+$k-1, $this->array_nomes[$a], $this->preto);  								
									$perc = $this->array_dados[$a]*100/$total;
									$final = round($perc, 2);
									$soma += floatval($final);;								  								
									if ($a+1 != $this->num_barras){  									 
  									 imagestring($this->image, 3, 330, $y1+$k-1, $final." %", $this->preto);
  								}else{ 
  									 $val = (100 - $soma); 
  									 imagestring($this->image, 3, 330, $y1+$k-1, $final." %", $this->preto);
  								}
  						}								            		      						
    		  }
    }

					
					// ------------- criar o grafico -------------		
					$maior = 100;											
					$titulo = "Popularidade dos Clubes Portugueses"; 															 
    		  $gq = new graficoQueijo(420, 360, $estilo_s);
    		  $gq->adicionarTitulo($titulo);    		 		 
    		  
					
					$gq->addDados(100);					
    			$gq->addNomes("Benfica");
					$gq->addDados(75);					
    			$gq->addNomes("Porto MFC");
					$gq->addDados(50);					
    			$gq->addNomes("Juveo Gay");
								 		 
    		  $gq->criaGrafico();
    		  $gq->criaLegenda();	 
					$gq->desenhaGrafico();		
																																	
  ?>