<?php
/*
 @Autor    : Dyego Fernandes de Sousa
 @Date     : 2005-07-12 (16/06/2005)
 @Title    : OO in PHP (Programao Orientada  Objetos em PHP)
 @SubTitle : Bd Connect and Reports (Conexo  Banco de Dados e Relatrios)
 @License  : GNU (Public License)
 @email    : dyegofernandes@centauronet.com.br, dyegofern@hotmail.com
*/
class classeGrafico {
/*
Propriedades {properties}
*/
var $servHST;		// Nome do Host do Banco de Dados       {Data Base Host Name}      
var $servUSR;		// Nome do Usurio do Banco de Dados    {Data Base User Name}
var $servPWD;		// Senha do Usuario do Banco de Dados   {Data Base User Password}
var $servBNK;		// Nome do Banco de Dados               {Data Base Name}
var $query;			// String de Consulta ao Banco de Dados {Data Base Query String}
var $titulo;		// String de Ttulo do Grfico          {Charts Title}
var $fundo;			// Cor de fundo                         {Background Color}
var $barra;			// Cor da barra                         {Bars color}
var $letra_barra;	// Cor do texto sobre a barra           {Text color, color over the bar}
var $letra_norma;	// Cor padro do texto                  {Standard text color}
var $letra_titul;	// Cor do ttulo                        {Titles color}
var $legenda;		// Cor do fundo da legenda              {Legends Background color}
/*
 Mtodo para conectar no banco de dados
 {Method for to connect in the DB}
*/
function conectar(){
	$conn = mssql_connect($this->servHST,$this->servUSR,$this->servPWD) or die("Erro ao conectar ao Host do Banco de dados".mssql_get_last_message());
	mssql_select_db($this->servBNK,$conn)or die("Erro ao conectar  Base de dados".mssql_get_last_message());
	return $conn;
}
/*
 Mtodo para consultar no banco de dados
 {Method for querying in the DB}
*/
function consultar(){
	$query  = mssql_query($this->query,$this->conectar()) or die("Erro na Query");
	$result = mssql_fetch_array($query);
	$linhas = mssql_num_rows($query);
	$campos = mssql_num_fields($resultado);
    return array('id_result'=>$query,'linhas'=>$linhas,'campos'=>$campos);
}
/*
 Mtodo para construir a legenda
 {Method for to make the chart legend}
*/
function legenda($image,$yi,$largura,$altura,$legenda,$barra,$letra_barra,$fonte,$nome_campo){
	imagefilledrectangle($image,5,$yi,$largura-5,$altura-5,$legenda);
	imagerectangle($image,5,$yi,$largura-5,$altura-5,$barra);
	imagefilledrectangle($image,8,$yi+40,19,$yi+49,$barra);
	imagerectangle($image,8,$yi+40,19,$yi+49,$letra_barra);
	imagestring($image,$fonte,9,$yi+6,'Legenda',$letra_barra);
	imagestring($image,$fonte,7,$yi+5,'Legenda',$barra);
	imagestring($image,$fonte,22,$yi+38,$nome_campo,$barra);

}
/*
 Mtodo para construir o grfico
 {Method for to make the chart}
*/
function novoGrafico(){
    $r = $this->consultar();
	$tamanho     = 500;
	$altura      = $r['linhas']*20+90;
	$largura     = 640;
	$image       = imagecreate($largura,$altura);
	$cor         = imagecolorallocate($image,$this->fundo[0],$this->fundo[1],$this->fundo[2]);
	$barra       = imagecolorallocate($image,$this->barra[0],$this->barra[1],$this->barra[2]);
	$letra_barra = imagecolorallocate($image,$this->letra_barra[0],$this->letra_barra[1],$this->letra_barra[2]);
	$letra_norma = imagecolorallocate($image,$this->letra_norma[0],$this->letra_norma[1],$this->letra_norma[2]);
	$letra_titul = imagecolorallocate($image,$this->letra_titul[0],$this->letra_titul[1],$this->letra_titul[2]);
	$legenda     = imagecolorallocate($image,$this->legenda[0],$this->legenda[1],$this->legenda[2]);
	$fonte = 3;
	$i  = 0;
	$yi = 45;
	$q  = 0;
	/*
	 Enquanto existir resultado seu valor  gravado em um array, tambm  gravado o valor encontrado no campo numrico,
	 esse segundo valor servir de base para a posio inicial da barra.
	 {While exists a result, its value to be pushed into a array, also is pushed in another array tho numeric field. This
	  numeric field will be dish to resolve the inicial position for the bar}
	*/
	while($f = mssql_fetch_array($r['id_result'])){
		$array_nome[$i] = mssql_result($r['id_result'],$i,mssql_field_name($r['id_result'],0));
		$array_ini[$i]  = (strlen(mssql_result($r['id_result'],$i,mssql_field_name($r['id_result'],0))))*9.2;
		if(($array_cor[$i]-1)%2){
		 #$array_cor[$i]  = imagecolorallocate($image,rand(0,rand(0,254)),rand(0,254),rand(0,254));
		 $array_cor[$i]   = $barra;
		 } else {
				  #$array_cor[$i]  = imagecolorallocate($image,rand(0,rand(0,254)),rand(rand(0,10),rand(0,254)),rand(0,254));
				  $array_cor[$i]   = $barra;
				}
		$i++;
	}
	//Ttulo do grfico   {Charts title}
	$tit         = $this->titulo;
	imagerectangle($image,0,0,$largura-1,$altura-1,$barra);
	imagestring($image,$fonte+10,($largura/2 - strlen($tit)*5),10,$tit,$letra_titul);
	$i=0;
	foreach($array_nome as $nome){
	 $str_nome = strtoupper($array_nome[$i]);
	 imagestring($image,$fonte,4,$yi,$str_nome,$letra_norma);
	 imagefilledrectangle($image,max($array_ini),$yi,max($array_ini)+(mssql_result($r['id_result'],$i,mssql_field_name($r['id_result'],1))/1.3),$yi+15,$array_cor[$i]);
	 imagerectangle($image,max($array_ini),$yi,max($array_ini)+(mssql_result($r['id_result'],$i,mssql_field_name($r['id_result'],1))/1.3),$yi+15,$letra_barra);
	 imagestring($image,$fonte-3,max($array_ini)+2,$yi+4,mssql_result($r['id_result'],$i,mssql_field_name($r['id_result'],1)),$letra_barra);
	 $array_nome[$i];
	 $i++;
	 $yi+=20;
	}
	//Chama o mtodo legenda {Call the legend method}
    $this->legenda($image,$yi,$largura,$altura,$legenda,$barra,$letra_barra,$fonte,mssql_field_name($r['id_result'],1));
		
	header("Content-Type: image/gif");
	imagegif($image);
	imagedestroy($image);
}
/*
 Construtor {Constructor}
*/
function classeGrafico($conf,$servHST,$servUSR,$servPWD,$servBNK,$query,$titulo){
 $this->servHST     = $servHST;					// Inicializando
 $this->servUSR     = $servUSR;					//       as
 $this->servPWD     = $servPWD;					//  propriedades
 $this->servBNK     = $servBNK;					//       da
 $this->query       = $query;					//  nova instncia
 $this->titulo      = $titulo;					//       da
 $this->fundo       = $conf['fundo'];			//     classe
 $this->barra       = $conf['barra'];			//{ Initializing
 $this->letra_barra = $conf['letra_barra'];		//      the
 $this->letra_norma = $conf['letra_norma'];		// properties of the
 $this->letra_titul = $conf['letra_titul'];		//   new instance
 $this->legenda     = $conf['legenda'];			//   of this class }
 /*
  Desenhando o Grfico {Drawing the chart}
 */
 $this->novoGrafico();
} 
}// Fim da classe {End of class}

?>
