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 / BoxWhisker.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  10.4 KB  |  298 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: BoxWhisker.php,v 1.14 2005/11/27 22:21:17 nosey Exp $
  28.  * @link       http://pear.php.net/package/Image_Graph
  29.  * @since      File available since Release 0.3.0dev2
  30.  */
  31.  
  32. /**
  33.  * Include file Image/Graph/Plot.php
  34.  */
  35. require_once 'Image/Graph/Plot.php';
  36.  
  37. /**
  38.  * Box & Whisker chart.
  39.  *
  40.  * @category   Images
  41.  * @package    Image_Graph
  42.  * @subpackage Plot
  43.  * @author     Jesper Veggerby <pear.nosey@veggerby.dk>
  44.  * @copyright  Copyright (C) 2003, 2004 Jesper Veggerby Hansen
  45.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  46.  * @version    Release: 0.7.2
  47.  * @link       http://pear.php.net/package/Image_Graph
  48.  * @since      Class available since Release 0.3.0dev2
  49.  */
  50. class Image_Graph_Plot_BoxWhisker extends Image_Graph_Plot
  51. {
  52.     /**
  53.      * Whisker circle size
  54.      * @var int
  55.      * @access private
  56.      */
  57.     var $_whiskerSize = false;
  58.  
  59.     /**
  60.      * Draws a box & whisker
  61.      *
  62.      * @param int $x The x position
  63.      * @param int $w The width of the box
  64.      * @param int $r The radius of the circle markers
  65.      * @param int $y_min The Y position of the minimum value
  66.      * @param int $y_q1 The Y position of the median of the first quartile
  67.      * @param int $y_med The Y position of the median
  68.      * @param int $y_q3 The Y position of the median of the third quartile
  69.      * @param int $y_max The Y position of the maximum value
  70.      * @param int $key The ID tag
  71.      * @access private
  72.      */
  73.     function _drawBoxWhiskerV($x, $w, $r, $y_min, $y_q1, $y_med, $y_q3, $y_max, $key = false)
  74.     {
  75.         // draw circles
  76.         $this->_getLineStyle();
  77.         $this->_getFillStyle('min');
  78.         $this->_canvas->ellipse(array('x' => $x, 'y' => $y_min, 'rx' => $r, 'ry' => $r));
  79.  
  80.         $this->_getLineStyle();
  81.         $this->_getFillStyle('quartile1');
  82.         $this->_canvas->ellipse(array('x' => $x, 'y' => $y_q1, 'rx' => $r, 'ry' => $r));
  83.  
  84.         $this->_getLineStyle();
  85.         $this->_getFillStyle('median');
  86.         $this->_canvas->ellipse(array('x' => $x, 'y' => $y_med, 'rx' => $r, 'ry' => $r));
  87.  
  88.         $this->_getLineStyle();
  89.         $this->_getFillStyle('quartile3');
  90.         $this->_canvas->ellipse(array('x' => $x, 'y' => $y_q3, $r, 'rx' => $r, 'ry' => $r));
  91.  
  92.         $this->_getLineStyle();
  93.         $this->_getFillStyle('max');
  94.         $this->_canvas->ellipse(array('x' => $x, 'y' => $y_max, $r, 'rx' => $r, 'ry' => $r));
  95.  
  96.         // draw box and lines
  97.  
  98.         $this->_getLineStyle();
  99.         $this->_canvas->line(array('x0' => $x, 'y0' => $y_min, 'x1' => $x, 'y1' => $y_q1));
  100.         $this->_getLineStyle();
  101.         $this->_canvas->line(array('x0' => $x, 'y0' => $y_q3, 'x1' => $x, 'y1' => $y_max));
  102.  
  103.         $this->_getLineStyle();
  104.         $this->_getFillStyle('box');
  105.         $this->_canvas->rectangle(array('x0' => $x - $w, 'y0' => $y_q1, 'x1' => $x + $w, 'y1' => $y_q3));
  106.  
  107.         $this->_getLineStyle();
  108.         $this->_canvas->line(array('x0' => $x - $w, 'y0' => $y_med, 'x1' => $x + $w, 'y1' => $y_med));
  109.     }
  110.  
  111.     /**
  112.      * Draws a box & whisker
  113.      *
  114.      * @param int $y The x position
  115.      * @param int $h The width of the box
  116.      * @param int $r The radius of the circle markers
  117.      * @param int $x_min The Y position of the minimum value
  118.      * @param int $x_q1 The Y position of the median of the first quartile
  119.      * @param int $x_med The Y position of the median
  120.      * @param int $x_q3 The Y position of the median of the third quartile
  121.      * @param int $x_max The Y position of the maximum value
  122.      * @param int $key The ID tag
  123.      * @access private
  124.      */
  125.     function _drawBoxWhiskerH($y, $h, $r, $x_min, $x_q1, $x_med, $x_q3, $x_max, $key = false)
  126.     {
  127.         // draw circles
  128.         $this->_getLineStyle();
  129.         $this->_getFillStyle('min');
  130.         $this->_canvas->ellipse(array('x' => $x_min, 'y' => $y, 'rx' => $r, 'ry' => $r));
  131.  
  132.         $this->_getLineStyle();
  133.         $this->_getFillStyle('quartile1');
  134.         $this->_canvas->ellipse(array('x' => $x_q1, 'y' => $y, 'rx' => $r, 'ry' => $r));
  135.  
  136.         $this->_getLineStyle();
  137.         $this->_getFillStyle('median');
  138.         $this->_canvas->ellipse(array('x' => $x_med, 'y' => $y, 'rx' => $r, 'ry' => $r));
  139.  
  140.         $this->_getLineStyle();
  141.         $this->_getFillStyle('quartile3');
  142.         $this->_canvas->ellipse(array('x' => $x_q3, 'y' => $y, $r, 'rx' => $r, 'ry' => $r));
  143.  
  144.         $this->_getLineStyle();
  145.         $this->_getFillStyle('max');
  146.         $this->_canvas->ellipse(array('x' => $x_max, 'y' => $y, $r, 'rx' => $r, 'ry' => $r));
  147.  
  148.         // draw box and lines
  149.  
  150.         $this->_getLineStyle();
  151.         $this->_canvas->line(array('x0' => $x_min, 'y0' => $y, 'x1' => $x_q1, 'y1' => $y));
  152.         $this->_getLineStyle();
  153.         $this->_canvas->line(array('x0' => $x_q3, 'y0' => $y, 'x1' => $x_max, 'y1' => $y));
  154.  
  155.         $this->_getLineStyle();
  156.         $this->_getFillStyle('box');
  157.         $this->_canvas->rectangle(array('x0' => $x_q1, 'y0' => $y - $h, 'x1' => $x_q3, 'y1' => $y + $h));
  158.  
  159.         $this->_getLineStyle();
  160.         $this->_canvas->line(array('x0' => $x_med, 'y0' => $y - $h, 'x1' => $x_med, 'y1' => $y + $h));
  161.     }
  162.  
  163.     /**
  164.      * Perform the actual drawing on the legend.
  165.      *
  166.      * @param int $x0 The top-left x-coordinate
  167.      * @param int $y0 The top-left y-coordinate
  168.      * @param int $x1 The bottom-right x-coordinate
  169.      * @param int $y1 The bottom-right y-coordinate
  170.      * @access private
  171.      */
  172.     function _drawLegendSample($x0, $y0, $x1, $y1)
  173.     {
  174.         $x = round(($x0 + $x1) / 2);
  175.         $h = abs($y1 - $y0) / 9;
  176.         $w = round(abs($x1 - $x0) / 5);
  177.         $r = 2;//round(abs($x1 - $x0) / 13);
  178.         $this->_drawBoxWhiskerV($x, $w, $r, $y1, $y1 - 2 * $h, $y1 - 4 * $h, $y0 + 3 * $h, $y0);
  179.     }
  180.     
  181.     /**
  182.      * Sets the whisker circle size
  183.      *
  184.      * @param int $size Size (radius) of the whisker circle/dot
  185.      */
  186.     function setWhiskerSize($size = false)
  187.     {
  188.         $this->_whiskerSize = $size;
  189.     }
  190.  
  191.     /**
  192.      * Output the plot
  193.      *
  194.      * @return bool Was the output 'good' (true) or 'bad' (false).
  195.      * @access private
  196.      */
  197.     function _done()
  198.     {
  199.         if (parent::_done() === false) {
  200.             return false;
  201.         }
  202.  
  203.         if (!is_array($this->_dataset)) {
  204.             return false;
  205.         }
  206.  
  207.         $this->_canvas->startGroup(get_class($this) . '_' . $this->_title);
  208.  
  209.         $this->_clip(true);        
  210.  
  211.         if ($this->_multiType == 'stacked100pct') {
  212.             $total = $this->_getTotals();
  213.         }
  214.         $current = array();
  215.         $number = 0;
  216.         $width = floor(0.5 * $this->_parent->_labelDistance(IMAGE_GRAPH_AXIS_X) / 2);
  217.  
  218.         if ($this->_whiskerSize !== false) {
  219.             $r = $this->_whiskerSize;
  220.         } else {            
  221.             $r = min(5, $width / 10);
  222.         }
  223.  
  224.         $keys = array_keys($this->_dataset);
  225.         foreach ($keys as $key) {
  226.             $dataset =& $this->_dataset[$key];
  227.             $dataset->_reset();
  228.             while ($data = $dataset->_next()) {
  229.                 if ($this->_parent->_horizontal) {
  230.                     $point['X'] = $data['X'];
  231.                     $y = $data['Y'];
  232.     
  233.                     $min = min($y);
  234.                     $max = max($y);
  235.                     $q1 = $dataset->_median($y, 'first');
  236.                     $med = $dataset->_median($y, 'second');
  237.                     $q3 = $dataset->_median($y, 'third');
  238.     
  239.                     $point['Y'] = $min;
  240.                     $y = $this->_pointY($point);
  241.                     $x_min = $this->_pointX($point);
  242.     
  243.                     $point['Y'] = $max;
  244.                     $x_max = $this->_pointX($point);
  245.     
  246.                     $point['Y'] = $q1;
  247.                     $x_q1 = $this->_pointX($point);
  248.     
  249.                     $point['Y'] = $med;
  250.                     $x_med = $this->_pointX($point);
  251.     
  252.                     $point['Y'] = $q3;
  253.                     $x_q3 = $this->_pointX($point);
  254.     
  255.                     $this->_drawBoxWhiskerH($y, $width, $r, $x_min, $x_q1, $x_med, $x_q3, $x_max, $key);
  256.                 }
  257.                 else {
  258.                     $point['X'] = $data['X'];
  259.                     $y = $data['Y'];
  260.     
  261.                     $min = min($y);
  262.                     $max = max($y);
  263.                     $q1 = $dataset->_median($y, 'first');
  264.                     $med = $dataset->_median($y, 'second');
  265.                     $q3 = $dataset->_median($y, 'third');
  266.     
  267.                     $point['Y'] = $min;
  268.                     $x = $this->_pointX($point);
  269.                     $y_min = $this->_pointY($point);
  270.     
  271.                     $point['Y'] = $max;
  272.                     $y_max = $this->_pointY($point);
  273.     
  274.                     $point['Y'] = $q1;
  275.                     $y_q1 = $this->_pointY($point);
  276.     
  277.                     $point['Y'] = $med;
  278.                     $y_med = $this->_pointY($point);
  279.     
  280.                     $point['Y'] = $q3;
  281.                     $y_q3 = $this->_pointY($point);
  282.     
  283.                     $this->_drawBoxWhiskerV($x, $width, $r, $y_min, $y_q1, $y_med, $y_q3, $y_max, $key);
  284.                 }
  285.             }
  286.         }
  287.         unset($keys);
  288.         $this->_drawMarker();
  289.  
  290.         $this->_clip(false);        
  291.  
  292.         $this->_canvas->endGroup();
  293.         return true;
  294.     }
  295.  
  296. }
  297.  
  298. ?>