Location: PHPKode > projects > DATA system > datasystem-0.8.7/php/libs/gnuplot.class.php
<?php
/*
 *   gnuplot.class.php
 *   gnuplot class
 *
 */

class gplot
{
    private $_plotId;            // unique id from one
    private $_dim;               // dimention 2: 2-dim, 3: 3-dim

    private $_script="";         // gnuplot script
    private $_scriptName = "";        // script name
    private $_data = "";
    private $_terminal = "png";   // terminal type. default is PNG format
    private $_ext = "png";        // file format extention.

    var $outputName ;

    var $plot = array();
    var $axis;

    private $_lineNum = 1;


    function __construct() {
        $this->axis = array();
        $this->axis['x'] = new axis();
        $this->axis['y'] = new axis();
        $this->functions = array();
    }


    function addPlot($type,$data) {
        $this->plot[$this->_plotId] = new plot($type, $data);
        $this->_plotID++;
    }

    function setOutput($terminal = "png", $fname="test") {

        $this->_terminal = $terminal;
        $this->outputName = $fname;
        $this->_ext = "png";

        $this->_script .= "set terminal ".$this->_terminal."\n";
        $this->_script .= "set output \"/tmp/datasystem/".$this->outputName.".".$this->_ext."\"\n";
    }

    /*
     *  $type :
     *  $data : actual data or formula
     */

    function getExtension() {
        return $this->_ext;
    }

    function getScriptName() {
        return $this->_scriptName;
    }


    function draw() {
        // output script file path
        $fpath = tempnam(DS_TEMP_PATH, "gp");

        $this->_scriptName = basename($fpath);

        //  Set terminal
        $this->_script .= "set terminal ".$this->_terminal."\n";
        $this->_script .= "set output \"".$fpath.".".$this->_ext."\"\n";

        // Set size
        $this->_script .= "set size 0.7,0.7\n";

        // Set axis
        foreach ($this->axis as $key=>$axis) {
            //error_log(print_r($axis,true));
            $this->_script .= "set ".$key."label \"".$axis->label."\"\n";
            if (!$axis->autorange and $axis->min != $axis->max)
                $this->_script .= "set ".$key."range [ ".$axis->min." : ".$axis->max." ]\n";
            if ($axis->log)
                $this->_script .= "set logscale ".$key." 10\n";
        }

        // Plotting
        $this->_script .= "plot ";
        foreach ($this->plot as $id=>$plot)
            $this->_script .= $plot->data."\n";


        // Write down script
        if (!file_put_contents($fpath, $this->_script))
            errorlog("Gnuplot script was not written to the file.");

        error_log($this->_script);

        exec("gnuplot ".$fpath);

    }

    function showCommand() {
        return htmlspecialchars($this->_script);


    }
}  // end gplot class

class plot
{
    var $marker;
    var $line;
    var $type;   // function, data
    var $data;   // formula or data set

    function __construct($type, $data) {
        $this->marker = new marker();
        $this->line = new line();
        $this->type = $type;
        $this->data = $data;
    }


}



/*
 *  axis class
 */
class axis
{
    var $name;
    var $label;
    var $min;
    var $max;
    var $autorange;
    var $mag;
    var $param;  // parameter (T,t,x, etc.)
    var $log;  // log scaling
    var $inverse;   // axis direction inversion

    function __construct() {

        // Set default value
        $this->label = "";
        $this->min = 0;
        $this->max = 0;
        $this->autorange = TRUE;
        $this->mag = 1;
        $this->param = "";
        $this->log = FALSE;
        $this->inverse = FALSE;

    }

} // end axis class

class marker
{
    var $size;
    var $color;
    var $shape;

    function __construct($size=1, $color=1, $shape=1){
        $this->size = $size;
        $this->color = $color;
        $this->shape = $shape;
    }
} // end marker class


class line
{
    var $num;
    var $width;
    var $color;
    var $type;
    var $legend;

    function __construct($legend="", $width=1, $color=1, $type=1) {
        $this->legend = $legend;
        $this->width = $width;
        $this->color = $color;
        $this->type = $type;
    }
}
?>
Return current item: DATA system