linea21-externals
[ class tree: linea21-externals ] [ index: linea21-externals ] [ all elements ]

Source for file MathPlot.class.php

Documentation is available at MathPlot.class.php

  1. <?php
  2. /*
  3.  * This work is hereby released into the Public Domain.
  4.  * To view a copy of the public domain dedication,
  5.  * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
  6.  * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  7.  *
  8.  */
  9.  
  10. require_once dirname(__FILE__)."/Component.class.php";
  11.  
  12. /**
  13.  * A mathematic function
  14.  *
  15.  * @package linea21.externals
  16.  * @subpackage artichow
  17.  */
  18. class awMathFunction implements awLegendable {
  19.  
  20.     /**
  21.      * Function line
  22.      *
  23.      * @var Line 
  24.      */
  25.     public $line;
  26.     
  27.     /**
  28.      * Marks for your plot
  29.      *
  30.      * @var Mark 
  31.      */
  32.     public $mark;
  33.     
  34.     /**
  35.      * Callback function
  36.      *
  37.      * @var string 
  38.      */
  39.     public $f;
  40.     
  41.     /**
  42.      * Start the drawing from this value
  43.      *
  44.      * @var float 
  45.      */
  46.     public $fromX;
  47.     
  48.     /**
  49.      * Stop the drawing at this value
  50.      *
  51.      * @var float 
  52.      */
  53.     public $toX;
  54.  
  55.     /**
  56.      * Line color
  57.      *
  58.      * @var Color 
  59.      */
  60.     protected $color;
  61.     
  62.     /**
  63.      * Construct the function
  64.      *
  65.      * @param string $f Callback function
  66.      * @param float $fromX 
  67.      * @param float $toX 
  68.      */
  69.     public function __construct($f$fromX NULL$toX NULL{
  70.     
  71.         $this->f = (string)$f;
  72.         $this->fromX = is_null($fromXNULL : (float)$fromX;
  73.         $this->toX = is_null($toXNULL : (float)$toX;
  74.         
  75.         $this->line = new awLine;
  76.         $this->mark = new awMark;
  77.         $this->color = new awBlack;
  78.     
  79.     }
  80.     
  81.     /**
  82.      * Change line color
  83.      *
  84.      * @param awColor $color A new awcolor
  85.      */
  86.     public function setColor(awColor $color{
  87.         $this->color = $color;
  88.     }
  89.     
  90.     /**
  91.      * Get line color
  92.      *
  93.      * @return Color 
  94.      */
  95.     public function getColor({
  96.         return $this->color;
  97.     }
  98.  
  99.     /**
  100.      * Get the background color or gradient of an element of the component
  101.      *
  102.      * @return Color, Gradient
  103.      */
  104.     public function getLegendBackground({
  105.     }
  106.  
  107.     /**
  108.      * Get the line thickness
  109.      *
  110.      * @return NULL 
  111.      */
  112.     public function getLegendLineThickness({
  113.         return $this->line->getThickness();
  114.     }
  115.  
  116.     /**
  117.      * Get the line type
  118.      *
  119.      * @return NULL 
  120.      */
  121.     public function getLegendLineStyle({
  122.         return $this->line->getStyle();
  123.     }
  124.  
  125.     /**
  126.      * Get the color of line
  127.      *
  128.      * @return NULL 
  129.      */
  130.     public function getLegendLineColor({
  131.         return $this->color;
  132.     }
  133.  
  134.     /**
  135.      * Get a mark object
  136.      *
  137.      * @return NULL 
  138.      */
  139.     public function getLegendMark({
  140.         return $this->mark;
  141.     }
  142.  
  143. }
  144.  
  145. registerClass('MathFunction');
  146.  
  147. /**
  148.  * For mathematics functions
  149.  *
  150.  * @package linea21.externals
  151.  * @subpackage artichow
  152.  */
  153. class awMathPlot extends awComponent {
  154.     
  155.     /**
  156.      * Functions
  157.      *
  158.      * @var array 
  159.      */
  160.     protected $functions = array();
  161.     
  162.     /**
  163.      * Grid properties
  164.      *
  165.      * @var Grid 
  166.      */
  167.     public $grid;
  168.     
  169.     /**
  170.      * X axis
  171.      *
  172.      * @var Axis 
  173.      */
  174.     public $xAxis;
  175.     
  176.     /**
  177.      * Y axis
  178.      *
  179.      * @var Axis 
  180.      */
  181.     public $yAxis;
  182.     
  183.     /**
  184.      * Extremum
  185.      *
  186.      * @var Side 
  187.      */
  188.     private $extremum NULL;
  189.     
  190.     /**
  191.      * Interval
  192.      *
  193.      * @var float 
  194.      */
  195.     private $interval 1;
  196.     
  197.     /**
  198.      * Build the plot
  199.      *
  200.      * @param int $xMin Minimum X value
  201.      * @param int $xMax Maximum X value
  202.      * @param int $yMax Maximum Y value
  203.      * @param int $yMin Minimum Y value
  204.      */
  205.     public function __construct($xMin$xMax$yMax$yMin{
  206.     
  207.         parent::__construct();
  208.         
  209.         $this->setPadding(8888);
  210.         
  211.         $this->grid = new awGrid;
  212.         
  213.         // Hide grid by default
  214.         $this->grid->hide(TRUE);
  215.         
  216.         // Set extremum
  217.         $this->extremum new awSide($xMin$xMax$yMax$yMin);
  218.         
  219.         // Create axis
  220.         $this->xAxis = new awAxis;
  221.         $this->xAxis->setTickStyle(awTick::IN);
  222.         $this->xAxis->label->hideValue(0);
  223.         $this->initAxis($this->xAxis);
  224.         
  225.         $this->yAxis = new awAxis;
  226.         $this->yAxis->setTickStyle(awTick::IN);
  227.         $this->yAxis->label->hideValue(0);
  228.         $this->initAxis($this->yAxis);
  229.         
  230.     }
  231.     
  232.     protected function initAxis(awAxis $axis{
  233.     
  234.         $axis->setLabelPrecision(1);
  235.         $axis->addTick('major'new awTick(05));
  236.         $axis->addTick('minor'new awTick(03));
  237.         $axis->addTick('micro'new awTick(01));
  238.         $axis->setNumberByTick('minor''major'1);
  239.         $axis->setNumberByTick('micro''minor'4);
  240.         $axis->label->setFont(new awTuffy(7));
  241.         
  242.     }
  243.     
  244.     /**
  245.      * Interval to calculate values
  246.      *
  247.      * @param float $interval 
  248.      */
  249.     public function setInterval($interval{
  250.         $this->interval = (float)$interval;
  251.     }
  252.     
  253.     /**
  254.      * Add a formula f(x)
  255.      *
  256.      * @param awMathFunction $function 
  257.      * @param string $name Name for the legend (can be NULL if you don't want to set a legend)
  258.      * @param int $type Type for the legend
  259.      */
  260.     public function add(awMathFunction $function$name NULL$type awLegend::LINE{
  261.     
  262.         $this->functions[$function;
  263.         
  264.         if($name !== NULL{
  265.             $this->legend->add($function$name$type);
  266.         }
  267.     
  268.     }
  269.     
  270.     public function init(awDriver $driver{
  271.         
  272.         list($x1$y1$x2$y2$this->getPosition();
  273.         
  274.         $this->xAxis->line->setX($x1$x2);
  275.         $this->xAxis->label->setAlign(NULLawLabel::BOTTOM);
  276.         $this->xAxis->label->move(03);
  277.         $this->xAxis->setRange($this->extremum->left$this->extremum->right);
  278.         
  279.         $this->yAxis->line->setY($y2$y1);
  280.         $this->yAxis->label->setAlign(awLabel::RIGHT);
  281.         $this->yAxis->label->move(-60);
  282.         $this->yAxis->reverseTickStyle();
  283.         $this->yAxis->setRange($this->extremum->bottom$this->extremum->top);
  284.         
  285.         
  286.         $this->xAxis->setYCenter($this->yAxis0);
  287.         $this->yAxis->setXCenter($this->xAxis0);
  288.         
  289.         if($this->yAxis->getLabelNumber(=== NULL{
  290.             $number $this->extremum->top $this->extremum->bottom 1;
  291.             $this->yAxis->setLabelNumber($number);
  292.         }
  293.         
  294.         if($this->xAxis->getLabelNumber(=== NULL{
  295.             $number $this->extremum->right $this->extremum->left 1;
  296.             $this->xAxis->setLabelNumber($number);
  297.         }
  298.         
  299.         // Set ticks
  300.         /* <php5> */
  301.         $this->xAxis->tick('major')->setNumber($this->xAxis->getLabelNumber());
  302.         $this->yAxis->tick('major')->setNumber($this->yAxis->getLabelNumber());
  303.         /* </php5> */
  304.         /* <php4> --
  305.         $this->xAxis->ticks['major']->setNumber($this->xAxis->getLabelNumber());
  306.         $this->yAxis->ticks['major']->setNumber($this->yAxis->getLabelNumber());
  307.         -- </php4> */
  308.         
  309.         // Set axis labels
  310.         $labels array();
  311.         for($i 0$count $this->xAxis->getLabelNumber()$i $count$i++{
  312.             $labels[$i;
  313.         }
  314.         $this->xAxis->label->set($labels);
  315.         
  316.         $labels array();
  317.         for($i 0$count $this->yAxis->getLabelNumber()$i $count$i++{
  318.             $labels[$i;
  319.         }
  320.         $this->yAxis->label->set($labels);
  321.     
  322.         parent::init($driver);
  323.         
  324.         // Create the grid
  325.         $this->createGrid();
  326.     
  327.         // Draw the grid
  328.         $this->grid->draw($driver$x1$y1$x2$y2);
  329.         
  330.     }
  331.     
  332.     public function drawEnvelope(awDriver $driver{
  333.         
  334.         // Draw axis
  335.         $this->xAxis->draw($driver);
  336.         $this->yAxis->draw($driver);
  337.     
  338.     }
  339.     
  340.     public function drawComponent(awDriver $driver$x1$y1$x2$y2$aliasing{
  341.     
  342.         foreach($this->functions as $function{
  343.         
  344.             $f $function->f;
  345.             $fromX is_null($function->fromX$this->extremum->left $function->fromX;
  346.             $toX is_null($function->toX$this->extremum->right $function->toX;
  347.             
  348.             $old NULL;
  349.             
  350.             for($i $fromX$i <= $toX$i += $this->interval{
  351.             
  352.                 $p awAxis::toPosition($this->xAxis$this->yAxisnew awPoint($i$f($i)));
  353.                 
  354.                 if($p->>= $y1 and $p-><= $y2{
  355.                     $function->mark->draw($driver$p);
  356.                 }
  357.             
  358.                 if($old !== NULL{
  359.                 
  360.                     $line $function->line;
  361.                     $line->setLocation($old$p);
  362.                 
  363.                     if(
  364.                         ($line->p1->>= $y1 and $line->p1-><= $y2or
  365.                         ($line->p2->>= $y1 and $line->p2-><= $y2)
  366.                     {
  367.                         $driver->line(
  368.                             $function->getColor(),
  369.                             $line
  370.                         );
  371.                     }
  372.                 
  373.                 }
  374.                 
  375.                 $old $p;
  376.             
  377.             }
  378.             
  379.             // Draw last point if needed
  380.             if($old !== NULL and $i $this->interval != $toX{
  381.             
  382.                 $p awAxis::toPosition($this->xAxis$this->yAxisnew awPoint($toX$f($toX)));
  383.                 
  384.                 if($p->>= $y1 and $p-><= $y2{
  385.                     $function->mark->draw($driver$p);
  386.                 }
  387.                 
  388.                 
  389.                 $line $function->line;
  390.                 $line->setLocation($old$p);
  391.                 
  392.                 if(
  393.                     ($line->p1->>= $y1 and $line->p1-><= $y2or
  394.                     ($line->p2->>= $y1 and $line->p2-><= $y2)
  395.                 {
  396.                     $driver->line(
  397.                         $function->getColor(),
  398.                         $line
  399.                     );
  400.                 }
  401.                 
  402.             }
  403.         
  404.         }
  405.     
  406.     }
  407.     
  408.     protected function createGrid({
  409.         
  410.         // Horizontal lines of the grid
  411.  
  412.         $major $this->yAxis->tick('major');
  413.         $interval $major->getInterval();
  414.         $number $this->yAxis->getLabelNumber(1;
  415.         
  416.         $h array();
  417.         if($number 0{
  418.             for($i 0$i <= $number$i++{
  419.                 $h[$i $number;
  420.             }
  421.         }
  422.         
  423.         // Vertical lines
  424.     
  425.         $major $this->xAxis->tick('major');
  426.         $interval $major->getInterval();
  427.         $number $this->xAxis->getLabelNumber(1;
  428.         
  429.         $w array();
  430.         if($number 0{
  431.             for($i 0$i <= $number$i++{
  432.                 if($i%$interval === 0{
  433.                     $w[$i $number;
  434.                 }
  435.             }
  436.         }
  437.     
  438.         $this->grid->setGrid($w$h);
  439.     
  440.     }
  441.  
  442. }
  443.  
  444. registerClass('MathPlot');
  445. ?>

Documentation generated on Fri, 16 Oct 2009 09:36:28 +0200 by phpDocumentor 1.4.1