<?php
header("content-type: image/jpeg");
class GraficarConPhp
{
var $Blanco,$Negro;
var $Azul,$Verde,$Cyan,$Rosa,$Rojo,$Amarillo;
var $Color;
var $Alto,$Ancho;
var $MargenIzquierdo, $MargenDerecho, $MargenInferior, $MargenSuperior;
var $EjeX = array(0,0,0,0);
var $EjeY = array(0,0,0,0);
var $Base,$Altura;
var $Frecuencia;
var $Calendario;
var $Maximo;
function __construct()
{
$this->Ancho=600;// defines ancho y largo, lo demas se ajusta en forma automatica
$this->Alto=300;
$Imagen=imagecreate($this->Ancho, $this->Alto );
imagecolorallocate ($Imagen , 255 , 255 , 0 ); // fondo amarillo, cambialo al que quieras
$this->CalcularDimensiones($Imagen);
$this->DefineColores($Imagen);
$this->CrearEjesXY($Imagen);
$this->MsSql($Imagen);// aqui es donde se grafican los datos
imagejpeg($Imagen);
imagedestroy($Imagen);
}
function CrearEjesXY($Imagen)
{
imagesetthickness($Imagen, 2);
imageline($Imagen,$this->EjeX[0],$this->EjeX[1],$this->EjeX[2],$this->EjeX[3],$this->Negro);
imageline($Imagen,$this->EjeY[0],$this->EjeY[1],$this->EjeY[2],$this->EjeY[3],$this->Negro);
}
function DefineColores($Imagen)
{
$im = imagecreate(100, 100);
$this->Negro = imagecolorallocate($Imagen, 0, 0, 0);
$this->Azul = imagecolorallocate($Imagen, 0, 0, 255);
$this->Verde = imagecolorallocate($Imagen, 0, 255, 0);
$this->Cyan = imagecolorallocate($Imagen, 0, 255, 255);
$this->Rojo = imagecolorallocate($Imagen, 255, 0, 0);
$this->Rosa = imagecolorallocate($Imagen, 255, 0, 255);
$this->Amarillo = imagecolorallocate($Imagen, 255, 128, 64);
$this->Blanco = imagecolorallocate($Imagen, 255, 255, 255);
}
function CalcularDimensiones($Imagen)
{//ajusta el area donde se va a imprimir el grafico
$this->Maximo=50;
$this->MargenIzquierdo=30;
$this->MargenDerecho=30;
$this->MargenInferior=30;
$this->MargenSuperior=30;
$this->Base=$this->Ancho-$this->MargenIzquierdo-$this->MargenDerecho;
$this->Altura=$this->Alto-$this->MargenSuperior-$this->MargenInferior;
$this->Frecuencia=floor($this->Base/10);
$this->EjeY[0]=$this->MargenIzquierdo;
$this->EjeY[1]=$this->MargenSuperior;
$this->EjeY[2]=$this->MargenIzquierdo;
$this->EjeY[3]=$this->Alto-$this->MargenInferior+20;
$this->EjeX[0]=$this->MargenIzquierdo-20;
$this->EjeX[1]=$this->Alto-$this->MargenInferior;
$this->EjeX[2]=$this->Ancho-$this->MargenDerecho;
$this->EjeX[3]=$this->Alto-$this->MargenInferior;
}
function MsSql($Imagen)
{
$Fecha=mktime(0,0,0,date("m"),date("d"),date("Y"));// el dia de hoy
$Fecha-=(6*24*60*60);// los ultimo 6 dias, pero mas abajo lo cancelo a solo los primeros 10 registros
$FechaInicia=date("Ymd",$Fecha);
$FechaTermina=date("Ymd");
$SqlCommand=
"Select top 10".
" convert(varchar(30),fecha,102) as Fecha,".
" convert(varchar(30),fecha,114) as hora, ".
" temp,".
" humedad,".
" temp2 ".
" from Tabla".
" where Fecha between '$FechaInicia' and '$FechaTermina 23:59:59'".
" order by convert(varchar(10),fecha,102),convert(varchar(30),fecha,114)";
//die($SqlCommand);
$Sql=mssql_connect("111.222.333.444","Login","Password") or die("Connection error.");
mssql_select_db("Tabla",$Sql) or die("Com Selection Error.");
$MsSqlQuery=mssql_query($SqlCommand,$Sql) or die (mssql_error());
$x=0;
//inician los graficos
while($Rs=mssql_fetch_array($MsSqlQuery,MSSQL_ASSOC))
{
if($x>0)
{
$this->Graficar($Imagen,$temp,$Rs["temp"],$x,$this->Rojo);
$this->Graficar($Imagen,$humedad,$Rs["humedad"],$x,$this->Azul);
$this->Graficar($Imagen,$temp2,$Rs["temp2"],$x,$this->Verde);
}
$temp=$Rs["temp"];
$humedad=$Rs["humedad"];
$temp2=$Rs["temp2"];
$x++;
}
}
function Graficar($Imagen,$v1,$v2,$x,$Color)
{
$x1=$this->MargenIzquierdo+($this->Frecuencia*($x-1));
$y1=$this->Altura-floor(($v1*$this->Altura)/$this->Maximo)+$this->MargenSuperior;
$x2=$this->MargenIzquierdo+($this->Frecuencia*$x);
$y2=$this->Altura-floor(($v2*$this->Altura)/$this->Maximo)+$this->MargenSuperior;
//die("x=$x, $x1,$y1,$x2,$y2");
imageline($Imagen,$x1,$y1,$x2,$y2,$Color);// esta orden es la que genera la linea
}
}
$SA=new GraficarConPhp();// y aqui inicia todo.
?>