home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / Image / Graph / Legend.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  12.2 KB  |  385 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * Image_Graph - Main class for the graph creation.
  7.  *
  8.  * PHP versions 4 and 5
  9.  *
  10.  * LICENSE: This library is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU Lesser General Public License as published by
  12.  * the Free Software Foundation; either version 2.1 of the License, or (at your
  13.  * option) any later version. This library is distributed in the hope that it
  14.  * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  15.  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
  16.  * General Public License for more details. You should have received a copy of
  17.  * the GNU Lesser General Public License along with this library; if not, write
  18.  * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19.  * 02111-1307 USA
  20.  *
  21.  * @category   Images
  22.  * @package    Image_Graph
  23.  * @subpackage Legend
  24.  * @author     Jesper Veggerby <pear.nosey@veggerby.dk>
  25.  * @copyright  Copyright (C) 2003, 2004 Jesper Veggerby Hansen
  26.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  27.  * @version    CVS: $Id: Legend.php,v 1.16 2006/02/28 22:48:07 nosey Exp $
  28.  * @link       http://pear.php.net/package/Image_Graph
  29.  */
  30.  
  31. /**
  32.  * Include file Image/Graph/Layout.php
  33.  */
  34. require_once 'Image/Graph/Layout.php';
  35.  
  36. /**
  37.  * Displays a legend for a plotarea.
  38.  *
  39.  * A legend can be displayed in two ways:
  40.  *
  41.  * 1 As an overlayed box within the plotarea
  42.  *
  43.  * 2 Layout'ed on the canvas smewhere next to the plotarea.
  44.  *
  45.  * @category   Images
  46.  * @package    Image_Graph
  47.  * @subpackage Legend
  48.  * @author     Jesper Veggerby <pear.nosey@veggerby.dk>
  49.  * @copyright  Copyright (C) 2003, 2004 Jesper Veggerby Hansen
  50.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  51.  * @version    Release: 0.7.2
  52.  * @link       http://pear.php.net/package/Image_Graph
  53.  */
  54. class Image_Graph_Legend extends Image_Graph_Layout
  55. {
  56.     
  57.     /**
  58.      * Alignment of the text
  59.      * @var int
  60.      * @access private
  61.      */
  62.     var $_alignment = false;
  63.  
  64.     /**
  65.      * The plotarea(s) to show the legend for
  66.      * @var array
  67.      * @access private
  68.      */
  69.     var $_plotareas = array();
  70.  
  71.     /**
  72.      * Should markers be shown or not on this legend
  73.      * @var bool
  74.      * @access private
  75.      */
  76.     var $_showMarker = false;
  77.  
  78.     /**
  79.      * Image_Graph_Legend [Constructor]
  80.      */
  81.     function Image_Graph_Legend()
  82.     {
  83.         parent::Image_Graph_Layout();
  84.         $this->_padding = array('left' => 5, 'top' => 5, 'right' => 5, 'bottom' => 5);
  85.     }
  86.  
  87.     /**
  88.      * The number of actual plots in the plot area
  89.      *
  90.      * @return int The number of plotes
  91.      * @access private
  92.      */
  93.     function _plotCount()
  94.     {
  95.         $count = 0;
  96.         $keys = array_keys($this->_plotareas);
  97.         foreach($keys as $key) {
  98.             $plotarea =& $this->_plotareas[$key];
  99.             if (is_a($plotarea, 'Image_Graph_Plotarea')) {
  100.                 $keys2 = array_keys($plotarea->_elements);
  101.                 foreach ($keys2 as $key) {
  102.                     $element =& $plotarea->_elements[$key];
  103.                     if (is_a($element, 'Image_Graph_Plot')) {
  104.                         $count ++;
  105.                     }
  106.                 }
  107.                 unset($keys2);
  108.             }
  109.         }
  110.         unset($keys);
  111.         return $count;
  112.     }
  113.     
  114.     /**
  115.      * Get a default parameter array for legendSamples
  116.      * @param bool $simulate Whether the array should be used for simulation or
  117.      * not
  118.      * @return array Default parameter array
  119.      * @access private
  120.      */
  121.     function _parameterArray($simulate = false)
  122.     {
  123.         $param['left'] = $this->_left + $this->_padding['left'];
  124.         $param['top'] = $this->_top + $this->_padding['top'];
  125.         $param['right'] = $this->_right - $this->_padding['right'];
  126.         $param['bottom'] = $this->_bottom - $this->_padding['bottom'];
  127.         $param['align'] = $this->_alignment;
  128.         $param['x'] = $this->_left + $this->_padding['left'];
  129.         $param['y'] = $this->_top + $this->_padding['top'];
  130.         $param['width'] = 16;
  131.         $param['height'] = 16;
  132.         $param['show_marker'] = $this->_showMarker;
  133.         $param['maxwidth'] = 0;
  134.         $param['font'] = $this->_getFont();
  135.         if ($simulate) {
  136.             $param['simulate'] = true;
  137.         }
  138.             
  139.         return $param;
  140.     }
  141.  
  142.     /**
  143.      * The height of the element on the canvas
  144.      *
  145.      * @return int Number of pixels representing the height of the element
  146.      * @access private
  147.      */
  148.     function _height()
  149.     {
  150.         $parent = (is_object($this->_parent) ? get_class($this->_parent) : $this->_parent);
  151.  
  152.         if (strtolower($parent) == 'image_graph_plotarea') {
  153.             $param = $this->_parameterArray(true);
  154.             $param['align'] = IMAGE_GRAPH_ALIGN_VERTICAL;
  155.             $param0 = $param;
  156.             $keys = array_keys($this->_plotareas);
  157.             foreach($keys as $key) {
  158.                 $plotarea =& $this->_plotareas[$key];
  159.                 $keys2 = array_keys($plotarea->_elements);
  160.                 foreach($keys2 as $key) {
  161.                     $element =& $plotarea->_elements[$key];
  162.                     if (is_a($element, 'Image_Graph_Plot')) {
  163.                         $element->_legendSample($param);
  164.                     }
  165.                 }
  166.                 unset($keys2);
  167.             }
  168.             unset($keys);
  169.             return abs($param['y'] - $param0['y']) + $this->_padding['top'] + $this->_padding['bottom'];
  170.         } else {
  171.             return parent::height();
  172.         }
  173.     }
  174.  
  175.     /**
  176.      * The width of the element on the canvas
  177.      *
  178.      * @return int Number of pixels representing the width of the element
  179.      * @access private
  180.      */
  181.     function _width()
  182.     {
  183.         $parent = (is_object($this->_parent) ? get_class($this->_parent) : $this->_parent);
  184.  
  185.         if (strtolower($parent) == 'image_graph_plotarea') {
  186.             $param = $this->_parameterArray(true);
  187.             $param['align'] = IMAGE_GRAPH_ALIGN_VERTICAL;
  188.             $keys = array_keys($this->_plotareas);
  189.             foreach($keys as $key) {
  190.                 $plotarea =& $this->_plotareas[$key];
  191.                 $keys2 = array_keys($plotarea->_elements);
  192.                 foreach($keys2 as $key) {
  193.                     $element =& $plotarea->_elements[$key];
  194.                     if (is_a($element, 'Image_Graph_Plot')) {
  195.                         $element->_legendSample($param);
  196.                     }
  197.                 }
  198.                 unset($keys2);
  199.             }
  200.             unset($keys);
  201.             return $param['maxwidth'];
  202.         } else {
  203.             return parent::width();
  204.         }
  205.     }
  206.  
  207.     /**
  208.      * Set the alignment of the legend
  209.      *
  210.      * @param int $alignment The alignment
  211.      */
  212.     function setAlignment($alignment)
  213.     {
  214.         $this->_alignment = $alignment;
  215.     }
  216.  
  217.     /**
  218.      * Update coordinates
  219.      *
  220.      * @access private
  221.      */
  222.     function _updateCoords()
  223.     {
  224.         parent::_updateCoords();
  225.  
  226.         $parent = (is_object($this->_parent) ? get_class($this->_parent) : $this->_parent);
  227.  
  228.         if (strtolower($parent) == 'image_graph_plotarea') {
  229.             $w = $this->_width();
  230.             $h = $this->_height();
  231.             
  232.             if ($this->_alignment === false) {
  233.                 $this->_alignment = IMAGE_GRAPH_ALIGN_TOP + IMAGE_GRAPH_ALIGN_RIGHT;
  234.             }
  235.  
  236.             if (($this->_alignment & IMAGE_GRAPH_ALIGN_BOTTOM) != 0) {
  237.                 $y = $this->_parent->_fillBottom() - $h - $this->_padding['bottom'];
  238.             } else {
  239.                 $y = $this->_parent->_fillTop() + $this->_padding['top'];
  240.             }
  241.  
  242.             if (($this->_alignment & IMAGE_GRAPH_ALIGN_LEFT) != 0) {
  243.                 $x = $this->_parent->_fillLeft() + $this->_padding['left'];
  244.             } else {
  245.                 $x = $this->_parent->_fillRight() - $w - $this->_padding['right'];
  246.             }
  247.  
  248.             $this->_setCoords($x, $y, $x + $w, $y + $h);
  249.         }
  250.     }
  251.  
  252.     /**
  253.      * Sets Plotarea
  254.      *
  255.      * @param Image_Graph_Plotarea $plotarea The plotarea
  256.      */
  257.     function setPlotarea(& $plotarea)
  258.     {
  259.         if (is_a($plotarea, 'Image_Graph_Plotarea')) {
  260.             $this->_plotareas[] =& $plotarea;
  261.         }
  262.     }
  263.  
  264.     /**
  265.      * Sets the parent. The parent chain should ultimately be a GraPHP object
  266.      *
  267.      * @see Image_Graph
  268.      * @param Image_Graph_Common $parent The parent
  269.      * @access private
  270.      */
  271.     function _setParent(& $parent)
  272.     {
  273.         parent::_setParent($parent);
  274.         if (count($this->_plotareas) == 0) {
  275.             $this->setPlotarea($parent);
  276.         }
  277.     }
  278.  
  279.     /**
  280.      * Set if this legends should show markers
  281.      *
  282.      * @param bool $showMarker True if markers are to be shown, false is not
  283.      */
  284.     function setShowMarker($showMarker)
  285.     {
  286.         $this->_showMarker = $showMarker;
  287.     }
  288.  
  289.  
  290.     /**
  291.      * Output the plot
  292.      *
  293.      * @return bool Was the output 'good' (true) or 'bad' (false).
  294.      * @access private
  295.      */
  296.     function _done()
  297.     {
  298.  
  299.         if (Image_Graph_Element::_done() === false) {
  300.             return false;
  301.         }
  302.         
  303.         $this->_canvas->startGroup(get_class($this));
  304.  
  305.         $param = $this->_parameterArray();
  306.  
  307.         $parent = (is_object($this->_parent) ?
  308.             get_class($this->_parent) :
  309.             $this->_parent
  310.         );
  311.  
  312.         if (strtolower($parent) == 'image_graph_plotarea') {                    
  313.             $this->_getFillStyle();
  314.             $this->_getLineStyle();
  315.             $this->_canvas->rectangle(
  316.                 array(
  317.                     'x0' => $this->_left,
  318.                     'y0' => $this->_top,
  319.                     'x1' => $this->_right,
  320.                     'y1' => $this->_bottom
  321.                 )
  322.             );
  323.  
  324.             $param = $this->_parameterArray();
  325.             
  326.             $keys = array_keys($this->_plotareas);
  327.             foreach($keys as $key) {
  328.                 $plotarea =& $this->_plotareas[$key];
  329.                 $keys2 = array_keys($plotarea->_elements);
  330.                 foreach($keys2 as $key) {
  331.                     $element =& $plotarea->_elements[$key];
  332.                     if (is_a($element, 'Image_Graph_Plot')) {
  333.                         $element->_legendSample($param);
  334.                     }
  335.                 }
  336.                 unset($keys2);
  337.             }
  338.             unset($keys);
  339.         } else {
  340.             $param0 = $param;
  341.             $param0['simulate'] = true;
  342.             $keys = array_keys($this->_plotareas);
  343.             foreach($keys as $key) {
  344.                 $plotarea =& $this->_plotareas[$key];
  345.                 $keys2 = array_keys($plotarea->_elements);
  346.                 foreach($keys2 as $key) {
  347.                     $element =& $plotarea->_elements[$key];
  348.                     if (is_a($element, 'Image_Graph_Plot')) {
  349.                         $element->_legendSample($param0);
  350.                     }
  351.                 }
  352.                 unset($keys2);
  353.             }
  354.             unset($keys);
  355.             if (($this->_alignment & IMAGE_GRAPH_ALIGN_VERTICAL) != 0) {
  356.                 if ($param0['x'] == $param['x']) {
  357.                     $param['y'] = $param['y'] + ($this->_height() - ($param0['y'] - $param['y']))/2;
  358.                 }
  359.             } else {
  360.                 if ($param0['y'] == $param['y']) {
  361.                     $param['x'] = $param['x'] + ($this->_width() - ($param0['x'] - $param['x']))/2;
  362.                 }
  363.             }
  364.  
  365.             $keys = array_keys($this->_plotareas);
  366.             foreach($keys as $key) {
  367.                 $plotarea =& $this->_plotareas[$key];
  368.                 $keys2 = array_keys($plotarea->_elements);
  369.                 foreach($keys2 as $key) {
  370.                     $element =& $plotarea->_elements[$key];
  371.                     if (is_a($element, 'Image_Graph_Plot')) {
  372.                         $element->_legendSample($param);
  373.                     }
  374.                 }
  375.                 unset($keys2);
  376.             }
  377.             unset($keys);
  378.         }
  379.         
  380.         $this->_canvas->endGroup();
  381.         
  382.         return true;
  383.     }
  384. }
  385. ?>