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

Source for file Legend.class.php

Documentation is available at Legend.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__)."/../Graph.class.php";
  11.  
  12. /* <php4> */
  13.  
  14. define("LEGEND_LINE"1);
  15. define("LEGEND_BACKGROUND"2);
  16. define("LEGEND_MARK"3);
  17. define("LEGEND_MARKONLY"4);
  18.  
  19. define("LEGEND_MODEL_RIGHT"1);
  20. define("LEGEND_MODEL_BOTTOM"2);
  21.  
  22. define("LEGEND_LEFT"1);
  23. define("LEGEND_RIGHT"2);
  24. define("LEGEND_CENTER"3);
  25. define("LEGEND_TOP"4);
  26. define("LEGEND_BOTTOM"5);
  27. define("LEGEND_MIDDLE"6);
  28.  
  29. /* </php4> */
  30.  
  31. /**
  32.  * Some legends
  33.  *
  34.  * @package linea21.externals
  35.  * @subpackage artichow
  36.  */
  37. class awLegend implements awPositionable {
  38.  
  39.     /**
  40.      * Legends added
  41.      *
  42.      * @var array 
  43.      */
  44.     protected $legends = array();
  45.  
  46.     /**
  47.      * The current component
  48.      *
  49.      * @var Component 
  50.      */
  51.     protected $component;
  52.     
  53.     /**
  54.      * Background color or gradient
  55.      *
  56.      * @var Color, Gradient
  57.      */
  58.     protected $background;
  59.     
  60.     /**
  61.      * Text color
  62.      *
  63.      * @var Color 
  64.      */
  65.     protected $textColor;
  66.     
  67.     /**
  68.      * Text font
  69.      *
  70.      * @var Font 
  71.      */
  72.     protected $textFont;
  73.     
  74.     /**
  75.      * Text margin
  76.      *
  77.      * @var Side 
  78.      */
  79.     protected $textMargin;
  80.     
  81.     /**
  82.      * Number of columns
  83.      *
  84.      * @var int 
  85.      */
  86.     protected $columns = NULL;
  87.     
  88.     /**
  89.      * Number of rows
  90.      *
  91.      * @var int 
  92.      */
  93.     protected $rows = NULL;
  94.     
  95.     /**
  96.      * Legend position
  97.      *
  98.      * @var Point 
  99.      */
  100.     protected $position;
  101.     
  102.     /**
  103.      * Hide legend ?
  104.      *
  105.      * @var bool 
  106.      */
  107.     protected $hide = FALSE;
  108.     
  109.     /**
  110.      * Space between each legend
  111.      *
  112.      * @var int 
  113.      */
  114.     protected $space = 4;
  115.     
  116.     /**
  117.      * Horizontal alignment
  118.      *
  119.      * @var int 
  120.      */
  121.     protected $hAlign;
  122.     
  123.     /**
  124.      * Vertical alignment
  125.      *
  126.      * @var int 
  127.      */
  128.     protected $vAlign;
  129.  
  130.     /**
  131.      * Margin
  132.      *
  133.      * @var array Array for left, right, top and bottom margins
  134.      */
  135.     private $margin;
  136.     
  137.     /**
  138.      * Legend shadow
  139.      *
  140.      * @var Shadow 
  141.      */
  142.     public $shadow;
  143.     
  144.     /**
  145.      * Legend border
  146.      *
  147.      * @var Border 
  148.      */
  149.     public $border;
  150.     
  151.     /**
  152.      * Line legend
  153.      *
  154.      * @var int 
  155.      */
  156.     const LINE 1;
  157.     
  158.     /**
  159.      * Color/Gradient background legend
  160.      *
  161.      * @var int 
  162.      */
  163.     const BACKGROUND 2;
  164.     
  165.     /**
  166.      * Use marks and line as legend
  167.      *
  168.      * @var int 
  169.      */
  170.     const MARK 3;
  171.     
  172.     /**
  173.      * Use marks as legend
  174.      *
  175.      * @var int 
  176.      */
  177.     const MARKONLY 4;
  178.     
  179.     /**
  180.      * Right side model
  181.      *
  182.      * @var int 
  183.      */
  184.     const MODEL_RIGHT 1;
  185.     
  186.     /**
  187.      * Bottom side model
  188.      *
  189.      * @var int 
  190.      */
  191.     const MODEL_BOTTOM 2;
  192.  
  193.     /**
  194.      * Build the legend
  195.      *
  196.      * @param int $model Legend model
  197.      */
  198.     public function __construct($model awLegend::MODEL_RIGHT{
  199.     
  200.         $this->shadow = new awShadow(awShadow::LEFT_BOTTOM);
  201.         $this->border = new awBorder;
  202.         
  203.         $this->textMargin = new awSide(4);
  204.         $this->setModel($model);
  205.         
  206.     }
  207.     
  208.     /**
  209.      * Set a predefined model for the legend
  210.      *
  211.      * @param int $model 
  212.      */
  213.     public function setModel($model{
  214.         
  215.         $this->setBackgroundColor(new awColor(25525525515));
  216.         $this->setPadding(8888);
  217.         $this->setTextFont(new awFont2);
  218.         $this->shadow->setSize(3);
  219.     
  220.         switch($model{
  221.         
  222.             case awLegend::MODEL_RIGHT :
  223.             
  224.                 $this->setColumns(1);
  225.                 $this->setAlign(awLegend::RIGHTawLegend::MIDDLE);
  226.                 $this->setPosition(0.960.50);
  227.             
  228.                 break;
  229.         
  230.             case awLegend::MODEL_BOTTOM :
  231.             
  232.                 $this->setRows(1);
  233.                 $this->setAlign(awLegend::CENTERawLegend::TOP);
  234.                 $this->setPosition(0.500.92);
  235.             
  236.                 break;
  237.                 
  238.             default :
  239.             
  240.                 $this->setPosition(0.50.5);
  241.                 
  242.                 break;
  243.         
  244.         }
  245.     
  246.     }
  247.     
  248.     /**
  249.      * Hide legend ?
  250.      *
  251.      * @param bool $hide TRUE to hide legend, FALSE otherwise
  252.      */
  253.     public function hide($hide TRUE{
  254.         $this->hide = (bool)$hide;
  255.     }
  256.     
  257.     /**
  258.      * Show legend ?
  259.      *
  260.      * @param bool $show 
  261.      */
  262.     public function show($show TRUE{
  263.         $this->hide = (bool)!$show;
  264.     }
  265.     
  266.     
  267.     /**
  268.      * Add a Legendable object to the legend
  269.      *
  270.      * @param awLegendable $legendable 
  271.      * @param string $title Legend title
  272.      * @param int $type Legend type (default to awLegend::LINE)
  273.      */
  274.     public function add(awLegendable $legendable$title$type awLegend::LINE{
  275.     
  276.         $legend array($legendable$title$type);
  277.     
  278.         $this->legends[$legend;
  279.         
  280.     }
  281.     
  282.     /**
  283.      * Change legend padding
  284.      *
  285.      * @param int $left 
  286.      * @param int $right 
  287.      * @param int $top 
  288.      * @param int $bottom 
  289.      */
  290.     public function setPadding($left$right$top$bottom{
  291.         $this->padding array((int)$left(int)$right(int)$top(int)$bottom);
  292.     }
  293.     
  294.     /**
  295.      * Change space between each legend
  296.      *
  297.      * @param int $space 
  298.      */
  299.     public function setSpace($space{
  300.         $this->space = (int)$space;
  301.     }
  302.     
  303.     /**
  304.      * Change alignment
  305.      *
  306.      * @param int $h Horizontal alignment
  307.      * @param int $v Vertical alignment
  308.      */
  309.     public function setAlign($h NULL$v NULL{
  310.         if($h !== NULL{
  311.             $this->hAlign = (int)$h;
  312.         }
  313.         if($v !== NULL{
  314.             $this->vAlign = (int)$v;
  315.         }
  316.     }
  317.     
  318.     /**
  319.      * Change number of columns
  320.      *
  321.      * @param int $columns 
  322.      */
  323.     public function setColumns($columns{
  324.         $this->rows = NULL;
  325.         $this->columns = (int)$columns;
  326.     }
  327.     
  328.     /**
  329.      * Change number of rows
  330.      *
  331.      * @param int $rows 
  332.      */
  333.     public function setRows($rows{
  334.         $this->columns = NULL;
  335.         $this->rows = (int)$rows;
  336.     }
  337.     
  338.     /**
  339.      * Change legend position
  340.      * X and Y positions must be between 0 and 1.
  341.      *
  342.      * @param float $x 
  343.      * @param float $y 
  344.      */
  345.     public function setPosition($x NULL$y NULL{
  346.         $x (is_null($xand !is_null($this->position)) $this->position->$x;
  347.         $y (is_null($yand !is_null($this->position)) $this->position->$y;
  348.         
  349.         $this->position = new awPoint($x$y);
  350.     }
  351.     
  352.     /**
  353.      * Get legend position
  354.      *
  355.      * @return Point 
  356.      */
  357.     public function getPosition({
  358.         return $this->position;
  359.     }
  360.     
  361.     /**
  362.      * Change text font
  363.      *
  364.      * @param awFont $font 
  365.      */
  366.     public function setTextFont(awFont $font{
  367.         $this->textFont = $font;
  368.     }
  369.     
  370.     /**
  371.      * Change text margin
  372.      *
  373.      * @param int $left 
  374.      * @param int $right 
  375.      */
  376.     public function setTextMargin($left$right{
  377.         $this->textMargin->set($left$right);
  378.     }
  379.     
  380.     /**
  381.      * Change text color
  382.      *
  383.      * @param awColor $color 
  384.      */
  385.     public function setTextColor(awColor $color{
  386.         $this->textColor = $color;
  387.     }
  388.     
  389.     /**
  390.      * Change background
  391.      *
  392.      * @param mixed $background 
  393.      */
  394.     public function setBackground($background{
  395.         $this->background = $background;
  396.     }
  397.     
  398.     /**
  399.      * Change background color
  400.      *
  401.      * @param awColor $color 
  402.      */
  403.     public function setBackgroundColor(awColor $color{
  404.         $this->background = $color;
  405.     }
  406.     
  407.     /**
  408.      * Change background gradient
  409.      *
  410.      * @param awGradient $gradient 
  411.      */
  412.     public function setBackgroundGradient(awGradient $gradient{
  413.         $this->background = $gradient;
  414.     }
  415.     
  416.     /**
  417.      * Count the number of Legendable objects in the legend
  418.      *
  419.      * @return int 
  420.      */
  421.     public function count({
  422.         return count($this->legends);
  423.     }
  424.     
  425.     public function draw(awDriver $driver{
  426.         
  427.         if($this->hide{
  428.             return;
  429.         }
  430.     
  431.         $count $this->count();
  432.         
  433.         // No legend to print
  434.         if($count === 0{
  435.             return;
  436.         }
  437.         
  438.         // Get text widths and heights of each element of the legend
  439.         $widths array();
  440.         $heights array();
  441.         $texts array();
  442.         for($i 0$i $count$i++{
  443.             list($title$this->legends[$i];
  444.             $text new awText(
  445.                 $title,
  446.                 $this->textFont,
  447.                 $this->textColor,
  448.                 0
  449.             );
  450. //            $font = $text->getFont();
  451.             $widths[$i$driver->getTextWidth($text$this->textMargin->left $this->textMargin->right;
  452.             $heights[$i$driver->getTextHeight($text);
  453.             $texts[$i$text;
  454.         }
  455.         
  456.         // Maximum height of the font used
  457.         $heightMax array_max($heights);
  458.         
  459.         // Get number of columns
  460.         if($this->columns !== NULL{
  461.             $columns $this->columns;
  462.         else if($this->rows !== NULL{
  463.             $columns ceil($count $this->rows);
  464.         else {
  465.             $columns $count;
  466.         }
  467.         
  468.         // Number of  rows
  469.         $rows = (int)ceil($count $columns);
  470.         
  471.         // Get maximum with of each column
  472.         $widthMax array();
  473.         for($i 0$i $count$i++{
  474.             // Get column width
  475.             $column $i $columns;
  476.             if(array_key_exists($column$widthMax=== FALSE{
  477.                 $widthMax[$column$widths[$i];
  478.             else {
  479.                 $widthMax[$columnmax($widthMax[$column]$widths[$i]);
  480.             }
  481.         }
  482.         
  483.         $width $this->padding[0$this->padding[1$this->space;
  484.         for($i 0$i $columns$i++{
  485.             $width += $this->space + 10 $widthMax[$i];
  486.         }
  487.         
  488.         $height ($heightMax $this->space$rows $this->space + $this->padding[2$this->padding[3];
  489.         
  490.         // Look for legends position
  491.         list($x$y$driver->getSize();
  492.         
  493.         $p new awPoint(
  494.             $this->position->$x,
  495.             $this->position->$y
  496.         );
  497.         
  498.         switch($this->hAlign{
  499.         
  500.             case awLegend::CENTER :
  501.                 $p->-= $width 2;
  502.                 break;
  503.         
  504.             case awLegend::RIGHT :
  505.                 $p->-= $width;
  506.                 break;
  507.         
  508.         }
  509.         
  510.         switch($this->vAlign{
  511.         
  512.             case awLegend::MIDDLE :
  513.                 $p->-= $height 2;
  514.                 break;
  515.         
  516.             case awLegend::BOTTOM :
  517.                 $p->-= $height;
  518.                 break;
  519.         
  520.         }
  521.         
  522.         // Draw legend shadow
  523.         $this->shadow->draw(
  524.             $driver,
  525.             $p,
  526.             $p->move($width$height),
  527.             awShadow::OUT
  528.         );
  529.         
  530.         // Draw legends base
  531.         $this->drawBase($driver$p$width$height);
  532.         
  533.         // Draw each legend
  534.         for($i 0$i $count$i++{
  535.         
  536.             list($component$title$type$this->legends[$i];
  537.         
  538.             $column $i $columns;
  539.             $row = (int)floor($i $columns);
  540.             
  541.             // Get width of all previous columns
  542.             $previousColumns 0;
  543.             for($j 0$j $column$j++{
  544.                 $previousColumns += $this->space + 10 $widthMax[$j];
  545.             }
  546.             
  547.             // Draw legend text
  548.             $driver->string(
  549.                 $texts[$i],
  550.                 $p->move(
  551.                     $this->padding[0$previousColumns 10 $this->textMargin->left,
  552.                     $this->padding[2$row ($heightMax $this->space$heightMax $heights[$i2
  553.                 )
  554.             );
  555.             
  556.             // Draw legend icon
  557.             switch($type{
  558.             
  559.                 case awLegend::LINE :
  560.                 case awLegend::MARK :
  561.                 case awLegend::MARKONLY :
  562.                 
  563.                     // Get vertical position
  564.                     $x $this->padding[0$previousColumns;
  565.                     $y $this->padding[2$row ($heightMax $this->space$heightMax $component->getLegendLineThickness();
  566.                     
  567.                     // Draw two lines
  568.                     if($component->getLegendLineColor(!== NULL{
  569.                     
  570.                         $color $component->getLegendLineColor();
  571.                 
  572.                         if($color instanceof awColor and $type !== awLegend::MARKONLY{
  573.                         
  574.                             $driver->line(
  575.                                 $color,
  576.                                 new awLine(
  577.                                     $p->move(
  578.                                         $x// YaPB ??
  579.                                         $y $component->getLegendLineThickness(2
  580.                                     ),
  581.                                     $p->move(
  582.                                         $x 10,
  583.                                         $y $component->getLegendLineThickness(2
  584.                                     ),
  585.                                     $component->getLegendLineStyle(),
  586.                                     $component->getLegendLineThickness()
  587.                                 )
  588.                             );
  589.                         
  590.                             unset($color);
  591.                             
  592.                         }
  593.                         
  594.                     }
  595.                     
  596.                     if($type === awLegend::MARK or $type === awLegend::MARKONLY)  {
  597.                     
  598.                         $mark $component->getLegendMark();
  599.                     
  600.                         if($mark !== NULL{
  601.                             $mark->draw(
  602.                                 $driver,
  603.                                 $p->move(
  604.                                     $x 5.5,
  605.                                     $y $component->getLegendLineThickness(2
  606.                                 )
  607.                             );
  608.                         }
  609.                         
  610.                         unset($mark);
  611.                     
  612.                     }
  613.                     
  614.                     break;
  615.                     
  616.                 case awLegend::BACKGROUND :
  617.                 
  618.                     // Get vertical position
  619.                     $x $this->padding[0$previousColumns;
  620.                     $y $this->padding[2$row ($heightMax $this->space$heightMax 5;
  621.                     
  622.                     $from $p->move(
  623.                         $x,
  624.                         $y
  625.                     );
  626.                     
  627.                     $to $p->move(
  628.                         $x 10,
  629.                         $y 10
  630.                     );
  631.                     
  632.                     $background $component->getLegendBackground();
  633.                     
  634.                     if($background !== NULL{
  635.                 
  636.                         $driver->filledRectangle(
  637.                             $component->getLegendBackground(),
  638.                             new awLine($from$to)
  639.                         );
  640.             
  641.                         // Draw rectangle border
  642.                         $this->border->rectangle(
  643.                             $driver,
  644.                             $from->move(00),
  645.                             $to->move(00)
  646.                         );
  647.                         
  648.                     }
  649.                     
  650.                     unset($background$from$to);
  651.                 
  652.                     break;
  653.             
  654.             }
  655.         
  656.         }
  657.     
  658.     }
  659.     
  660.     private function drawBase(awDriver $driverawPoint $p$width$height{
  661.  
  662.         $this->border->rectangle(
  663.             $driver,
  664.             $p,
  665.             $p->move($width$height)
  666.         );
  667.         
  668.         $size $this->border->visible(0;
  669.         
  670.         $driver->filledRectangle(
  671.             $this->background,
  672.             new awLine(
  673.                 $p->move($size$size),
  674.                 $p->move($width $size$height $size)
  675.             )
  676.         );
  677.         
  678.     }
  679.  
  680. }
  681.  
  682. registerClass('Legend');
  683.  
  684. /**
  685.  * You can add a legend to components which implements this interface
  686.  *
  687.  * @package linea21.externals
  688.  * @subpackage artichow
  689.  */
  690. interface awLegendable {
  691.  
  692.     /**
  693.      * Get the line type
  694.      *
  695.      * @return int 
  696.      */
  697.     public function getLegendLineStyle();
  698.  
  699.     /**
  700.      * Get the line thickness
  701.      *
  702.      * @return int 
  703.      */
  704.     public function getLegendLineThickness();
  705.  
  706.     /**
  707.      * Get the color of line
  708.      *
  709.      * @return Color 
  710.      */
  711.     public function getLegendLineColor();
  712.  
  713.     /**
  714.      * Get the background color or gradient of an element of the component
  715.      *
  716.      * @return Color, Gradient
  717.      */
  718.     public function getLegendBackground();
  719.  
  720.     /**
  721.      * Get a Mark object
  722.      *
  723.      * @return Mark 
  724.      */
  725.     public function getLegendMark();
  726.  
  727. }
  728.  
  729. registerInterface('Legendable');
  730. ?>

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