des = array(0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('file', '/tmp/err.txt', 'a'));
$this->debug=1;
}
function addHeader($header) {
$this->headers[]=$header;
}
function dumpHeaders() {
foreach($this->headers as $header) {
header($header);
}
}
function cleanHeaders() {
$this->headers=array();
}
/**
* Define o valor M�nimo para o eixo X
*
* @param int valor m�nimo para o eixo X
*/
function setMinX($xm) {
$this->noRanges=false;
$this->ranges['x']['min']=$xm;
}
/**
* Define o valor M�ximo para o eixo Y
*
* @param int valor m�ximo para o eixo X
*/
function setMaxX($xm) {
$this->noRanges=false;
$this->ranges['x']['max']=$xm;
}
/**
* Define o valor M�nimo para o eixo Y
*
* @param int valor m�nimo para o eixo Y
*/
function setMinY($ym) {
$this->noRanges=false;
$this->ranges['y']['min']=$ym;
}
/**
* Define o valor m�ximo para o eixo Y
*
* @param int valor m�nimo par ao eixo Y
*/
function setMaxY($ym) {
$this->noRanges=false;
$this->ranges['y']['max']=$ym;
}
/**
* Define o t�tulo do gr�fico
*
* @param string String contendo o t�tulo do gr�fico
*/
function setTitle($tstr) {
$this->noTitle=false;
$this->title=$tstr;
}
/**
* Define o r�tulo do eixo X
*
* @param string String contendo o r�tulo do eixo X
*/
function setXLabel($xstr){
$this->noXLabel=false;
$this->xlabel=$xstr;
}
/**
* Define o r�tulo do eixo Y
*
* @param string String contendo o r�tulo do eixo Y
*/
function setYLabel($ystr){
$this->noYLabel=false;
$this->ylabel=$ystr;
}
/**
* Define uma curva do gr�fico
*
* Este m�todo permite definir uma curva a ser tra�ada no gr�fico,
* especificando os pontos X, Y, e as caracter�sticas da curva,
* como largura do tra�o, cor, se � uma curva de dispers�o, etc.
* @param array vetor de dados para o eixo X
* @param array vetor de dados para o eixo Y
* @param int cor da curva, [-1,255] -1 = preto, 1 = azul, 2 = vermelho
* @param int [1,10] largura do tra�o da curva
* @param int [1,10] usado em curvas de dispers�o, define a forma do cursor utilizado
* @param int [1,10] usado em curvas de dispers�o, define o tamanho do cursor utilizado
*/
function setPlot($x, $y, $color = -1 , $width = 3 , $dType=null, $dSize=null){
if (count($x)==count($y)) {
$this->xd[]=$x;
$this->yd[]=$y;
$this->dataSet[]=true;
/**
* define the plot properties
*/
//line color
if( !empty( $color ) && ($color >= -1 && $color <= 255) )
$this->color[] = $color;
else
$this->color[] = -1;
//line width
if( !empty( $width ) && ($width >= 1 && $width <= 10) )
$this->width[] = $width;
else
$this->width[] = 3;
if( ! is_null($dType) && ($dType >= 0 && $dType <= 10) )
{
$this->noDispersion[] = false;
$this->dType[] = $dType;
if( !empty( $dSize ) && ( $dSize >= 1 && $dSize <= 10) )
$this->dSize[] = $dSize;
else
$this->dSize[] = 1;
}
else
{
$this->noDispersion[] = true;
}
}
}
/**
* Define o Aspect Ratio do gr�fico
*
* Define que o aspecto deve ser o mesmo tanto para o eixo X, quanto para
* o eixo Y
*/
function setSameAxes()
{
$this->noSameAxes = false;
}
/**
* DEPRECATED
* Fun��o mantida somente para fins de compatibilidade com programas
* antigos
*/
function setData1($x,$y)
{
if( count($x) == count($y) )
{
$this->setPlot($x,$y,-1, 1);
return 0;
}
}
/*
* DEPRECATED
*
* Fun��o mantida somente para fins de compatibilidade com programas
* antigos
*/
function setData2($x,$y)
{
if( count($x) == count($y) )
{
$this->setPlot($x,$y,-1, 1);
return 0;
}
}
/*
* DEPRECATED
*/
function setColor1( $color )
{
if( $color >= -1 && $color <= 255 )
$this->color[0] = $color;
}
/*
* DEPRECATED
*/
function setColor2( $color )
{
if( $color >= -1 && $color <= 255 )
$this->color[1] = $color;
}
/**
* M�todo utilizado para desenhar os gr�ficos
*
* Este m�todo � respons�vel por executar o programa gnuplot e
* executar as fun��es para desenhar o gr�fico
*/
function Plot($format='png')
{
switch($format) {
case 'png':
$this->format='png';
$this->addHeader('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
$this->addHeader('Last-Modified: '.gmdate("D, d m Y H:i:s").' GMT');
$this->addHeader('Content-type: image/png');
$this->addHeader('Cache-Control: no-cache');
$this->addHeader('Cache-Control: post-check=0, pre-check=0');
$this->addHeader('Pragma: no-cache');
break;
}
$p = proc_open($this->gnuplot, $this->des, $pipes);
/**
* define x,y labels and aspect ratio
*/
$lstr = ($this->noXLabel) ? '' : sprintf("set xlabel '%s' \n", $this->xlabel);
$lstr .= ($this->noYLabel) ? '' : sprintf("set ylabel '%s' \n", $this->ylabel);
$lstr .= ($this->noSameAxes)? '' : "set size ratio -1 \n";
/**
* Values used in the first plot
*/
$rstr=($this->noRanges)?'':sprintf('[%s:%s][%s:%s]', $this->ranges['x']['min'], $this->ranges['x']['max'], $this->ranges['y']['min'], $this->ranges['y']['max']);
$tstr=($this->noTitle)?'notitle':sprintf("title '%s'",$this->title);
$plotstr = '';
$nd = count($this->dataSet);
for( $i=0; $i<$nd; $i++ )
{
$dstr = ($this->noDispersion[$i]) ? "lines": sprintf("points pt %s ps %s", $this->dType[$i], $this->dSize[$i]);
$color = sprintf("lt %s", $this->color[$i] );
$width = sprintf("lw %s", $this->width[$i] );
//caracteristicas dos pontos
// "pt 1 ps 1";
/**
* verify if this is the last dataSet
*/
$next = ($i < ($nd-1)) ? " , \\\n" : "\n";
if( $i == 0 )
$plotstr .= sprintf("plot %s '-' %s with %s %s %s %s", $rstr, $tstr, $dstr, $color, $width, $next);
else
$plotstr .= sprintf(" '-' notitle with %s %s %s %s", $dstr, $color, $width, $next);
}
/**
* Write Graph title, label, size, etc... to gnuplot
*/
fwrite($pipes[0], "set term ".$this->format."\n");
fwrite($pipes[0], "set key off \n set size {0.7,0.7}\n");
fwrite($pipes[0], $lstr);
if( $this->debug )
error_log($plotstr);
fwrite($pipes[0], $plotstr);
/**
* Send plot data to gnuplot
*/
for( $i=0; $i<$nd; $i++ )
{
if( $this->debug )
error_log( $this->xd[$i] );
for ($c=0; $cxd[$i]); $c++)
{
fwrite($pipes[0], $this->xd[$i][$c].' '.$this->yd[$i][$c]."\n");
}
fwrite($pipes[0], "e\n");
}
fclose($pipes[0]);
$this->dumpHeaders();
while(!feof($pipes[1])) {
print fgets($pipes[1], 1024);
}
fclose($pipes[1]);
proc_close($p);
}
}
/**
* Sub-Classe de GNUPlot2D, especializada em desenhar curvas de n�vel.
*
* Esta subclasse simplesmente faz o overloading do m�todo setPlot(), Plot()
* especializando-os para trabalhar com curvas tridimensionais
*/
class GNUPlot3D extends GNUPlot2D {
var $zd = array();
function GNUPlot3D()
{
}
/**
* Define uma curva tridimensional a ser tra�ada no gr�fico
*
* @param array vetor com os pontos que definem o eixo x do grid
* @param array vetor com os pontos que definem o eixo y do grid
* @param array matriz, no formato do MATLAB, com os valores de Z
**/
function setPlot($x, $y, $z, $color = -1 , $width = 3 , $dType=null, $dSize=null){
$this->des = array(0 => array('pipe', 'r'),
1 => array('pipe', 'w'),
2 => array('file', '/tmp/err.txt', 'a'));
$this->debug=1;
if (true) {
$this->xd[]=$x;
$this->yd[]=$y;
$this->zd[]=$z;
$this->dataSet[]=true;
/**
* define the plot properties
*/
//line color
if( !empty( $color ) && ($color >= -1 && $color <= 255) )
$this->color[] = $color;
else
$this->color[] = -1;
//line width
if( !empty( $width ) && ($width >= 1 && $width <= 10) )
$this->width[] = $width;
else
$this->width[] = 3;
if( ! is_null($dType) && ($dType >= 0 && $dType <= 10) )
{
$this->noDispersion[] = false;
$this->dType[] = $dType;
if( !empty( $dSize ) && ( $dSize >= 1 && $dSize <= 10) )
$this->dSize[] = $dSize;
else
$this->dSize[] = 1;
}
else
{
$this->noDispersion[] = true;
}
}
else
return 1;
}
/**
* Tra�a o gr�fico
*
* Tra�a o gr�fico em forma de curvas de n�vel.
*/
function Plot($format='png')
{
if( $this->debug )
$fd = fopen('/tmp/saida.txt', 'a+');
switch($format) {
case 'png':
$this->format='png';
$this->addHeader('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
$this->addHeader('Last-Modified: '.gmdate("D, d m Y H:i:s").' GMT');
$this->addHeader('Content-type: image/png');
$this->addHeader('Cache-Control: no-cache');
$this->addHeader('Cache-Control: post-check=0, pre-check=0');
$this->addHeader('Pragma: no-cache');
break;
}
$p = proc_open($this->gnuplot, $this->des, $pipes);
//exit;
/**
* define x,y labels and aspect ratio
*/
$lstr = ($this->noXLabel) ? '' : sprintf("set xlabel '%s' \n", $this->xlabel);
$lstr .= ($this->noYLabel) ? '' : sprintf("set ylabel '%s' \n", $this->ylabel);
$lstr .= ($this->noSameAxes)? '' : "set size ratio -1 \n";
/**
* Values used in the first plot
*/
$rstr=($this->noRanges)?'':sprintf('[%s:%s][%s:%s]', $this->ranges['x']['min'], $this->ranges['x']['max'], $this->ranges['y']['min'], $this->ranges['y']['max']);
$tstr=($this->noTitle)?'notitle':sprintf("title '%s'",$this->title);
$plotstr = '';
$nd = count($this->dataSet);
for( $i=0; $i<$nd; $i++ )
{
$dstr = ($this->noDispersion[$i]) ? "lines": sprintf("points pt %s ps %s", $this->dType[$i], $this->dSize[$i]);
$color = sprintf("lt %s", $this->color[$i] );
$width = sprintf("lw %s", $this->width[$i] );
//caracteristicas dos pontos
// "pt 1 ps 1";
/**
* verify if this is the last dataSet
*/
$next = ($i < ($nd-1)) ? " , \\\n" : " \n";
if( $i == 0 )
$plotstr .= sprintf("splot %s '-' %s", $rstr, $tstr, $next);
else
$plotstr .= sprintf(" '-' notitle with %s %s %s %s", $dstr, $color, $width, $next);
}
/**
* Write Graph title, label, size, etc... to gnuplot
*/
fwrite($pipes[0], "set term ".$this->format."\n");
fwrite($pipes[0], " set palette defined ( 0 \"#00008f\", 1 \"blue\", 2 \"#0fffff\", 3 \"green\", 4 \"yellow\", 5 \"red\", 6 \"#8f0000\" )\n"); //seta cores para saida 3D
fwrite($pipes[0], "set pm3d map \n"); //seta saida 3D
//fwrite($pipes[0], "set contour base\n unset surface\n set view 0,0 \n"); //seta saida 3D
fwrite($pipes[0], "set key off \n set size {1,1}\n");
//fwrite($pipes[0], "set key off \n");
fwrite($pipes[0], $lstr);
if( $this->debug )
error_log($plotstr);
if( $this->debug )
fwrite($fd, $plotstr);
fwrite($pipes[0], $plotstr);
/**
* Send plot data to gnuplot
*/
for( $d=0; $d<$nd; $d++ )
{
if( $this->debug )
error_log( $this->xd[$d] );
for ($i=0; $ixd[$d]); $i++)
{
for( $j = 0; $jyd[$d]); $j++)
{
fwrite($pipes[0], $this->xd[$d][$i].' '.$this->yd[$d][$j].' '.$this->zd[$d][$i][$j]."\n");
if( $this->debug )
fwrite($fd, $this->xd[$d][$i].' '.$this->yd[$d][$j].' '.$this->zd[$d][$i][$j]."\n");
}
fwrite($pipes[0], "\n");
if( $this->debug )
fwrite($fd, "\n");
}
fwrite($pipes[0], "e\n");
if( $this->debug )
fwrite($fd, "e\n");
}
fclose($pipes[0]);
if( $this->debug )
fclose($fd);
$this->dumpHeaders();
while(!feof($pipes[1])) {
print fgets($pipes[1], 1024);
}
fclose($pipes[1]);
proc_close($p);
}
}
/* TEste da classe gnuplot2d
$x = array();
for( $i=0; $i < 300; $i++ )
{
$x[] = $i/50;
$y1[] = sin($i/50);
$y2[] = cos($i/50);
}
$g = new GNUPlot2D();
$g->SetPlot($x,$y1);
$g->SetPlot($x,$y2, 2, 5);
$g->Plot();
*/
/* Teste da classe GNUplot3D
for( $i=0; $i<=100; $i++ )
{
$x[$i] = 0.06 * ($i) - 3.0;
$y[$i] = $x[$i];
}
//print_r($x);
//print_r($y);
for( $i=0; $i <= 100; $i++ )
for( $j=0; $j <= 100; $j++ ){
$z[$i][$j] = sin($x[$i])*cos($y[$j]);
}
$a = new GNUPlot3D();
$a->setPlot($x,$y,$z);
$a->Plot();
*/
?>