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

Source for file LinePlot.class.php

Documentation is available at LinePlot.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__)."/Plot.class.php";
  11.  
  12. /* <php4> */
  13.  
  14. define("LINEPLOT_LINE"0);
  15. define("LINEPLOT_MIDDLE"1);
  16.  
  17. /* </php4> */
  18.  
  19. /**
  20.  * LinePlot
  21.  *
  22.  * @package linea21.externals
  23.  * @subpackage artichow
  24.  */
  25. class awLinePlot extends awPlot implements awLegendable {
  26.     
  27.     /**
  28.      * Add marks to your line plot
  29.      *
  30.      * @var Mark 
  31.      */
  32.     public $mark;
  33.     
  34.     /**
  35.      * Labels on your line plot
  36.      *
  37.      * @var Label 
  38.      */
  39.     public $label;
  40.     
  41.     /**
  42.      * Filled areas
  43.      *
  44.      * @var bool 
  45.      */
  46.     protected $areas = array();
  47.     
  48.     /**
  49.      * Is the line hidden
  50.      *
  51.      * @var bool 
  52.      */
  53.     protected $lineHide = FALSE;
  54.     
  55.     /**
  56.      * Line color
  57.      *
  58.      * @var Color 
  59.      */
  60.     protected $lineColor;
  61.     
  62.     /**
  63.      * Line mode
  64.      *
  65.      * @var int 
  66.      */
  67.     protected $lineMode = awLinePlot::LINE;
  68.     
  69.     /**
  70.      * Line type
  71.      *
  72.      * @var int 
  73.      */
  74.     protected $lineStyle = awLine::SOLID;
  75.     
  76.     /**
  77.      * Line thickness
  78.      *
  79.      * @var int 
  80.      */
  81.     protected $lineThickness = 1;
  82.     
  83.     /**
  84.      * Line background
  85.      *
  86.      * @var Color, Gradient
  87.      */
  88.     protected $lineBackground;
  89.     
  90.     /**
  91.      * Line mode
  92.      *
  93.      * @var int 
  94.      */
  95.     const LINE = 0;
  96.     
  97.     /**
  98.      * Line in the middle
  99.      *
  100.      * @var int 
  101.      */
  102.     const MIDDLE = 1;
  103.          
  104.     /**
  105.      * Construct a new awLinePlot
  106.      *
  107.      * @param array $values Some numeric values for Y axis
  108.      * @param int $mode 
  109.      */
  110.     public function __construct($values$mode awLinePlot::LINE{
  111.     
  112.         parent::__construct();
  113.         
  114.         $this->mark = new awMark;
  115.         $this->label = new awLabel;
  116.         
  117.         $this->lineMode = (int)$mode;
  118.         
  119.         $this->setValues($values);
  120.     
  121.     }
  122.     
  123.     /**
  124.      * Hide line
  125.      *
  126.      * @param bool $hide 
  127.      */
  128.     public function hideLine($hide{
  129.         $this->lineHide = (bool)$hide;
  130.     }
  131.     
  132.     /**
  133.      * Add a filled area
  134.      *
  135.      * @param int $start Begining of the area
  136.      * @param int $end End of the area
  137.      * @param mixed $background Background color or gradient of the area
  138.      */
  139.     public function setFilledArea($start$stop$background{
  140.     
  141.         if($stop <= $start{
  142.             awImage::drawError("Class LinePlot: End position can not be greater than begin position in setFilledArea().");
  143.         }
  144.     
  145.         $this->areas[array((int)$start(int)$stop$background);
  146.     
  147.     }
  148.     
  149.     /**
  150.      * Change line color
  151.      *
  152.      * @param awColor $color 
  153.      */
  154.     public function setColor(awColor $color{
  155.         $this->lineColor = $color;
  156.     }
  157.     
  158.     /**
  159.      * Change line style
  160.      *
  161.      * @param int $style 
  162.      */
  163.     public function setStyle($style{
  164.         $this->lineStyle = (int)$style;
  165.     }
  166.     
  167.     /**
  168.      * Change line tickness
  169.      *
  170.      * @param int $tickness 
  171.      */
  172.     public function setThickness($tickness{
  173.         $this->lineThickness = (int)$tickness;
  174.     }
  175.     
  176.     /**
  177.      * Change line background color
  178.      *
  179.      * @param awColor $color 
  180.      */
  181.     public function setFillColor(awColor $color{
  182.         $this->lineBackground = $color;
  183.     }
  184.     
  185.     /**
  186.      * Change line background gradient
  187.      *
  188.      * @param awGradient $gradient 
  189.      */
  190.     public function setFillGradient(awGradient $gradient{
  191.         $this->lineBackground = $gradient;
  192.     }
  193.  
  194.     /**
  195.      * Get the line thickness
  196.      *
  197.      * @return int 
  198.      */
  199.     public function getLegendLineThickness({
  200.         return $this->lineThickness;
  201.     }
  202.  
  203.     /**
  204.      * Get the line type
  205.      *
  206.      * @return int 
  207.      */
  208.     public function getLegendLineStyle({
  209.         return $this->lineStyle;
  210.     }
  211.  
  212.     /**
  213.      * Get the color of line
  214.      *
  215.      * @return Color 
  216.      */
  217.     public function getLegendLineColor({
  218.         return $this->lineColor;
  219.     }
  220.  
  221.     /**
  222.      * Get the background color or gradient of an element of the component
  223.      *
  224.      * @return Color, Gradient
  225.      */
  226.     public function getLegendBackground({
  227.         return $this->lineBackground;
  228.     }
  229.  
  230.     /**
  231.      * Get a mark object
  232.      *
  233.      * @return Mark 
  234.      */
  235.     public function getLegendMark({
  236.         return $this->mark;
  237.     }
  238.     
  239.     public function drawComponent(awDriver $driver$x1$y1$x2$y2$aliasing{
  240.         
  241.         $max $this->getRealYMax();
  242.         $min $this->getRealYMin();
  243.         
  244.         // Get start and stop values
  245.         list($start$stop$this->getLimit();
  246.         
  247.         if($this->lineMode === awLinePlot::MIDDLE{
  248.             $inc $this->xAxis->getDistance(012;
  249.         else {
  250.             $inc 0;
  251.         }
  252.         
  253.         // Build the polygon
  254.         $polygon new awPolygon;
  255.         
  256.         for($key $start$key <= $stop$key++{
  257.         
  258.             $value $this->datay[$key];
  259.             
  260.             if($value !== NULL{
  261.             
  262.                 $p awAxis::toPosition($this->xAxis$this->yAxisnew awPoint($key$value));
  263.                 $p $p->move($inc0);
  264.                 $polygon->set($key$p);
  265.                 
  266.             }
  267.         
  268.         }
  269.         
  270.         // Draw backgrounds
  271.         if($this->lineBackground instanceof awColor or $this->lineBackground instanceof awGradient{
  272.         
  273.             $backgroundPolygon new awPolygon;
  274.         
  275.             $p $this->xAxisPoint($start);
  276.             $p $p->move($inc0);
  277.             $backgroundPolygon->append($p);
  278.             
  279.             // Add others points
  280.             foreach($polygon->all(as $point{
  281.                 $backgroundPolygon->append(clone $point);
  282.             }
  283.             
  284.             $p $this->xAxisPoint($stop);
  285.             $p $p->move($inc0);
  286.             $backgroundPolygon->append($p);
  287.         
  288.             // Draw polygon background
  289.             $driver->filledPolygon($this->lineBackground$backgroundPolygon);
  290.         
  291.         }
  292.         
  293.         $this->drawArea($driver$polygon);
  294.         
  295.         // Draw line
  296.         $prev NULL;
  297.         
  298.         // Line color
  299.         if($this->lineHide === FALSE{
  300.         
  301.             if($this->lineColor === NULL{
  302.                 $this->lineColor = new awColor(000);
  303.             }
  304.             
  305.             foreach($polygon->all(as $point{
  306.             
  307.                 if($prev !== NULL{
  308.                     $driver->line(
  309.                         $this->lineColor,
  310.                         new awLine(
  311.                             $prev,
  312.                             $point,
  313.                             $this->lineStyle,
  314.                             $this->lineThickness
  315.                         )
  316.                     );
  317.                 }
  318.                 $prev $point;
  319.                 
  320.             }
  321.  
  322.         }
  323.         
  324.         // Draw marks and labels
  325.         foreach($polygon->all(as $key => $point{
  326.  
  327.             $this->mark->draw($driver$point);
  328.             $this->label->draw($driver$point$key);
  329.             
  330.         }
  331.         
  332.     }
  333.     
  334.     protected function drawArea(awDriver $driverawPolygon $polygon{
  335.     
  336.         $starts array();
  337.         foreach($this->areas as $area{
  338.             list($start$area;
  339.             $starts[$startTRUE;
  340.         }
  341.         
  342.         // Draw filled areas
  343.         foreach($this->areas as $area{
  344.         
  345.             list($start$stop$background$area;
  346.             
  347.             $polygonArea new awPolygon;
  348.             
  349.             $p $this->xAxisPoint($start);
  350.             $polygonArea->append($p);
  351.             
  352.             for($i $start$i <= $stop$i++{
  353.                 $p clone $polygon->get($i);
  354.                 if($i === $stop and array_key_exists($stop$starts)) {
  355.                     $p $p->move(-10);
  356.                 }
  357.                 $polygonArea->append($p);
  358.             }
  359.             
  360.             $p $this->xAxisPoint($stop);
  361.             if(array_key_exists($stop$starts)) {
  362.                 $p $p->move(-10);
  363.             }
  364.             $polygonArea->append($p);
  365.         
  366.             // Draw area
  367.             $driver->filledPolygon($background$polygonArea);
  368.         
  369.         }
  370.         
  371.     }
  372.     
  373.     public function getXAxisNumber({
  374.         if($this->lineMode === awLinePlot::MIDDLE{
  375.             return count($this->datay1;
  376.         else {
  377.             return count($this->datay);
  378.         }
  379.     }
  380.     
  381.     protected function xAxisPoint($position{
  382.         $y $this->xAxisZero ? $this->getRealYMin();
  383.         return awAxis::toPosition($this->xAxis$this->yAxisnew awPoint($position$y));
  384.     }
  385.     
  386.     public function getXCenter({
  387.         return ($this->lineMode === awLinePlot::MIDDLE);
  388.     }
  389.  
  390. }
  391.  
  392. registerClass('LinePlot');
  393.  
  394.  
  395. /**
  396.  * Simple LinePlot
  397.  * Useful to draw simple horizontal lines
  398.  *
  399.  * @package linea21.externals
  400.  * @subpackage artichow
  401.  */
  402. class awSimpleLinePlot extends awPlot implements awLegendable {
  403.     
  404.     /**
  405.      * Line color
  406.      *
  407.      * @var Color 
  408.      */
  409.     protected $lineColor;
  410.     
  411.     /**
  412.      * Line start
  413.      *
  414.      * @var int 
  415.      */
  416.     protected $lineStart;
  417.     
  418.     /**
  419.      * Line stop
  420.      *
  421.      * @var int 
  422.      */
  423.     protected $lineStop;
  424.     
  425.     /**
  426.      * Line value
  427.      *
  428.      * @var flaot 
  429.      */
  430.     protected $lineValue;
  431.     
  432.     /**
  433.      * Line mode
  434.      *
  435.      * @var int 
  436.      */
  437.     protected $lineMode = awLinePlot::LINE;
  438.     
  439.     /**
  440.      * Line type
  441.      *
  442.      * @var int 
  443.      */
  444.     protected $lineStyle = awLine::SOLID;
  445.     
  446.     /**
  447.      * Line thickness
  448.      *
  449.      * @var int 
  450.      */
  451.     protected $lineThickness = 1;
  452.     
  453.     /**
  454.      * Line mode
  455.      *
  456.      * @var int 
  457.      */
  458.     const LINE = 0;
  459.     
  460.     /**
  461.      * Line in the middle
  462.      *
  463.      * @var int 
  464.      */
  465.     const MIDDLE = 1;
  466.          
  467.     /**
  468.      * Construct a new awLinePlot
  469.      *
  470.      * @param float $value A Y value
  471.      * @param int $start Line start index
  472.      * @param int $stop Line stop index
  473.      * @param int $mode Line mode
  474.      */
  475.     public function __construct($value$start$stop$mode awLinePlot::LINE{
  476.     
  477.         parent::__construct();
  478.         
  479.         $this->lineMode = (int)$mode;
  480.         
  481.         $this->lineStart = (int)$start;
  482.         $this->lineStop = (int)$stop;
  483.         $this->lineValue = (float)$value;
  484.         
  485.         $this->lineColor = new awColor(000);
  486.     
  487.     }
  488.     
  489.     /**
  490.      * Change line color
  491.      *
  492.      * @param awColor $color 
  493.      */
  494.     public function setColor(awColor $color{
  495.         $this->lineColor = $color;
  496.     }
  497.     
  498.     /**
  499.      * Change line style
  500.      *
  501.      * @param int $style 
  502.      */
  503.     public function setStyle($style{
  504.         $this->lineStyle = (int)$style;
  505.     }
  506.     
  507.     /**
  508.      * Change line tickness
  509.      *
  510.      * @param int $tickness 
  511.      */
  512.     public function setThickness($tickness{
  513.         $this->lineThickness = (int)$tickness;
  514.     }
  515.  
  516.     /**
  517.      * Get the line thickness
  518.      *
  519.      * @return int 
  520.      */
  521.     public function getLegendLineThickness({
  522.         return $this->lineThickness;
  523.     }
  524.  
  525.     /**
  526.      * Get the line type
  527.      *
  528.      * @return int 
  529.      */
  530.     public function getLegendLineStyle({
  531.         return $this->lineStyle;
  532.     }
  533.  
  534.     /**
  535.      * Get the color of line
  536.      *
  537.      * @return Color 
  538.      */
  539.     public function getLegendLineColor({
  540.         return $this->lineColor;
  541.     }
  542.  
  543.     public function getLegendBackground({
  544.         return NULL;
  545.     }
  546.  
  547.     public function getLegendMark({
  548.         return NULL;
  549.     }
  550.     
  551.     public function drawComponent(awDriver $driver$x1$y1$x2$y2$aliasing{
  552.         
  553.         if($this->lineMode === awLinePlot::MIDDLE{
  554.             $inc $this->xAxis->getDistance(012;
  555.         else {
  556.             $inc 0;
  557.         }
  558.         
  559.         $p1 awAxis::toPosition($this->xAxis$this->yAxisnew awPoint($this->lineStart$this->lineValue));
  560.         $p2 awAxis::toPosition($this->xAxis$this->yAxisnew awPoint($this->lineStop$this->lineValue));
  561.         
  562.         $driver->line(
  563.             $this->lineColor,
  564.             new awLine(
  565.                 $p1->move($inc0),
  566.                 $p2->move($inc0),
  567.                 $this->lineStyle,
  568.                 $this->lineThickness
  569.             )
  570.         );
  571. }
  572.     
  573.     public function getXAxisNumber({
  574.         if($this->lineMode === awLinePlot::MIDDLE{
  575.             return count($this->datay1;
  576.         else {
  577.             return count($this->datay);
  578.         }
  579.     }
  580.     
  581.     protected function xAxisPoint($position{
  582.         $y $this->xAxisZero ? $this->getRealYMin();
  583.         return awAxis::toPosition($this->xAxis$this->yAxisnew awPoint($position$y));
  584.     }
  585.     
  586.     public function getXCenter({
  587.         return ($this->lineMode === awLinePlot::MIDDLE);
  588.     }
  589.  
  590. }
  591.  
  592. registerClass('SimpleLinePlot');
  593. ?>

Documentation generated on Fri, 16 Oct 2009 09:35:43 +0200 by phpDocumentor 1.4.1