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 / Plot / Bar.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  10.8 KB  |  307 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * Image_Graph - PEAR PHP OO Graph Rendering Utility.
  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 Plot
  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: Bar.php,v 1.14 2005/11/27 22:21:16 nosey Exp $
  28.  * @link       http://pear.php.net/package/Image_Graph
  29.  */
  30.  
  31. /**
  32.  * Include file Image/Graph/Plot.php
  33.  */
  34. require_once 'Image/Graph/Plot.php';
  35.  
  36. /**
  37.  * A bar chart.
  38.  *
  39.  * @category   Images
  40.  * @package    Image_Graph
  41.  * @subpackage Plot
  42.  * @author     Jesper Veggerby <pear.nosey@veggerby.dk>
  43.  * @copyright  Copyright (C) 2003, 2004 Jesper Veggerby Hansen
  44.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  45.  * @version    Release: 0.7.2
  46.  * @link       http://pear.php.net/package/Image_Graph
  47.  */
  48. class Image_Graph_Plot_Bar extends Image_Graph_Plot
  49. {
  50.  
  51.     /**
  52.      * The space between 2 bars (should be a multipla of 2)
  53.      * @var int
  54.      * @access private
  55.      */
  56.     var $_space = 4;
  57.  
  58.     /**
  59.      * The width of the bars
  60.      * @var array
  61.      * @access private
  62.      */
  63.     var $_width = 'auto';
  64.  
  65.     /**
  66.      * Perform the actual drawing on the legend.
  67.      *
  68.      * @param int $x0 The top-left x-coordinate
  69.      * @param int $y0 The top-left y-coordinate
  70.      * @param int $x1 The bottom-right x-coordinate
  71.      * @param int $y1 The bottom-right y-coordinate
  72.      * @access private
  73.      */
  74.     function _drawLegendSample($x0, $y0, $x1, $y1)
  75.     {
  76.         $dx = abs($x1 - $x0) / 7;
  77.         $this->_canvas->rectangle(array('x0' => $x0 + $dx, 'y0' => $y0, 'x1' => $x1 - $dx, 'y1' => $y1));
  78.     }
  79.  
  80.     /**
  81.      * Set the spacing between 2 neighbouring bars
  82.      *
  83.      * @param int $space The number of pixels between 2 bars, should be a
  84.      *   multipla of 2 (ie an even number)
  85.      */
  86.     function setSpacing($space)
  87.     {
  88.         $this->_space = (int) ($space / 2);
  89.     }
  90.  
  91.     /**
  92.      * Set the width of a bars.
  93.      *
  94.      * Specify 'auto' to auto calculate the width based on the positions on the
  95.      * x-axis.
  96.      *
  97.      * Supported units are:
  98.      *
  99.      * '%' The width is specified in percentage of the total plot width
  100.      *
  101.      * 'px' The width specified in pixels
  102.      *
  103.      * @param string $width The width of any bar
  104.      * @param string $unit The unit of the width
  105.      */
  106.     function setBarWidth($width, $unit = false)
  107.     {
  108.         if ($width == 'auto') {
  109.             $this->_width = $width;
  110.         } else {
  111.             $this->_width = array(
  112.                 'width' => $width,
  113.                 'unit' => $unit
  114.             );
  115.         }
  116.     }
  117.  
  118.     /**
  119.      * Output the plot
  120.      *
  121.      * @return bool Was the output 'good' (true) or 'bad' (false).
  122.      * @access private
  123.      */
  124.     function _done()
  125.     {
  126.         if (parent::_done() === false) {
  127.             return false;
  128.         }
  129.  
  130.         if (!is_array($this->_dataset)) {
  131.             return false;
  132.         }
  133.  
  134.         $this->_canvas->startGroup(get_class($this) . '_' . $this->_title);
  135.  
  136.         $this->_clip(true);        
  137.  
  138.         if ($this->_width == 'auto') {
  139.             $width = $this->_parent->_labelDistance(IMAGE_GRAPH_AXIS_X) / 2;            
  140.         } elseif ($this->_width['unit'] == '%') {
  141.             $width = $this->_width['width'] * $this->width() / 200;
  142.         } elseif ($this->_width['unit'] == 'px') {
  143.             $width = $this->_width['width'] / 2;
  144.         }
  145.        
  146.         if ($this->_multiType == 'stacked100pct') {
  147.             $total = $this->_getTotals();
  148.         }
  149.  
  150.         $minYaxis = $this->_parent->_getMinimum($this->_axisY);
  151.         $maxYaxis = $this->_parent->_getMaximum($this->_axisY);
  152.  
  153.         $number = 0;
  154.         $keys = array_keys($this->_dataset);
  155.         foreach ($keys as $key) {
  156.             $dataset =& $this->_dataset[$key];
  157.             $dataset->_reset();
  158.             while ($point = $dataset->_next()) {
  159.                 
  160.                 if ($this->_parent->_horizontal) {
  161.                     $y1 = $this->_pointY($point) - $width;
  162.                     $y2 = $this->_pointY($point) + $width;
  163.                     
  164.                     if ($y2 - $this->_space > $y1 + $this->_space) {
  165.                         /*
  166.                          * Take bar spacing into account _only_ if the space doesn't
  167.                          * turn the bar "inside-out", i.e. if the actual bar width
  168.                          * is smaller than the space between the bars
  169.                          */
  170.                         $y2 -= $this->_space;
  171.                         $y1 += $this->_space;
  172.                     }
  173.                 }
  174.                 else {               
  175.                     $x1 = $this->_pointX($point) - $width;
  176.                     $x2 = $this->_pointX($point) + $width;
  177.                     
  178.                     if ($x2 - $this->_space > $x1 + $this->_space) {
  179.                         /*
  180.                          * Take bar spacing into account _only_ if the space doesn't
  181.                          * turn the bar "inside-out", i.e. if the actual bar width
  182.                          * is smaller than the space between the bars
  183.                          */
  184.                         $x2 -= $this->_space;
  185.                         $x1 += $this->_space;
  186.                     }
  187.                 }                   
  188.                     
  189.  
  190.                 if (($this->_multiType == 'stacked') ||
  191.                     ($this->_multiType == 'stacked100pct'))
  192.                 {
  193.                     $x = $point['X'];
  194.  
  195.                     if ($point['Y'] >= 0) {
  196.                         if (!isset($current[$x])) {
  197.                             $current[$x] = 0;
  198.                         }
  199.  
  200.                         if ($this->_multiType == 'stacked') {
  201.                             $p0 = array(
  202.                                 'X' => $point['X'],
  203.                                 'Y' => $current[$x]
  204.                             );
  205.                             $p1 = array(
  206.                                 'X' => $point['X'],
  207.                                 'Y' => $current[$x] + $point['Y']
  208.                             );
  209.                         } else {
  210.                             $p0 = array(
  211.                                 'X' => $point['X'],
  212.                                 'Y' => 100 * $current[$x] / $total['TOTAL_Y'][$x]
  213.                             );
  214.                             $p1 = array(
  215.                                 'X' => $point['X'],
  216.                                 'Y' => 100 * ($current[$x] + $point['Y']) / $total['TOTAL_Y'][$x]
  217.                             );
  218.                         }
  219.                         $current[$x] += $point['Y'];
  220.                     } else {
  221.                         if (!isset($currentNegative[$x])) {
  222.                             $currentNegative[$x] = 0;
  223.                         }
  224.  
  225.                         $p0 = array(
  226.                                 'X' => $point['X'],
  227.                                 'Y' => $currentNegative[$x]
  228.                             );
  229.                         $p1 = array(
  230.                                 'X' => $point['X'],
  231.                                 'Y' => $currentNegative[$x] + $point['Y']
  232.                             );
  233.                         $currentNegative[$x] += $point['Y'];
  234.                     }
  235.                 } else {
  236.                     if (count($this->_dataset) > 1) {
  237.                         $w = 2 * ($width - $this->_space) / count($this->_dataset);                        
  238.                         if ($this->_parent->_horizontal) {
  239.                             $y2 = ($y1 = ($y1 + $y2) / 2  - ($width - $this->_space) + $number * $w) + $w;
  240.                         }
  241.                         else {
  242.                             $x2 = ($x1 = ($x1 + $x2) / 2  - ($width - $this->_space) + $number * $w) + $w;
  243.                         }
  244.                     }
  245.                     $p0 = array('X' => $point['X'], 'Y' => 0);
  246.                     $p1 = $point;
  247.                 }
  248.  
  249.                 if ((($minY = min($p0['Y'], $p1['Y'])) < $maxYaxis) &&
  250.                     (($maxY = max($p0['Y'], $p1['Y'])) > $minYaxis)
  251.                 ) {
  252.                     $p0['Y'] = $minY;
  253.                     $p1['Y'] = $maxY;
  254.  
  255.                     if ($p0['Y'] < $minYaxis) {
  256.                         $p0['Y'] = '#min_pos#';
  257.                     }
  258.                     if ($p1['Y'] > $maxYaxis) {
  259.                         $p1['Y'] = '#max_neg#';
  260.                     }
  261.  
  262.                     if ($this->_parent->_horizontal) {
  263.                         $x1 = $this->_pointX($p0);
  264.                         $x2 = $this->_pointX($p1);
  265.                     }
  266.                     else {                       
  267.                         $y1 = $this->_pointY($p0);
  268.                         $y2 = $this->_pointY($p1);
  269.                     }
  270.  
  271.                     $ID = $point['ID'];
  272.                     if (($ID === false) && (count($this->_dataset) > 1)) {
  273.                         $ID = $key;
  274.                     }
  275.                     $this->_getFillStyle($ID);
  276.                     $this->_getLineStyle($ID);
  277.  
  278.                     if (($y1 != $y2) && ($x1 != $x2)) {
  279.                         $this->_canvas->rectangle(
  280.                             $this->_mergeData(
  281.                                 $point,
  282.                                 array(
  283.                                     'x0' => min($x1, $x2),
  284.                                     'y0' => min($y1, $y2),
  285.                                     'x1' => max($x1, $x2),
  286.                                     'y1' => max($y1, $y2)
  287.                                 )
  288.                             )
  289.                         );
  290.                     }
  291.                 }
  292.             }
  293.             $number ++;
  294.         }
  295.         unset($keys);
  296.  
  297.         $this->_drawMarker();
  298.  
  299.         $this->_clip(false);        
  300.         
  301.         $this->_canvas->endGroup();        
  302.  
  303.         return true;
  304.     }
  305. }
  306.  
  307. ?>