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 / Dataset.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  12.0 KB  |  483 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 Dataset
  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: Dataset.php,v 1.10 2005/08/24 20:35:55 nosey Exp $
  28.  * @link       http://pear.php.net/package/Image_Graph
  29.  */
  30.  
  31.  
  32. /**
  33.  * Data set used to represent a data collection to plot in a chart
  34.  *
  35.  * @category   Images
  36.  * @package    Image_Graph
  37.  * @subpackage Dataset
  38.  * @author     Jesper Veggerby <pear.nosey@veggerby.dk>
  39.  * @copyright  Copyright (C) 2003, 2004 Jesper Veggerby Hansen
  40.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  41.  * @version    Release: 0.7.2
  42.  * @link       http://pear.php.net/package/Image_Graph
  43.  * @abstract
  44.  */
  45. class Image_Graph_Dataset
  46. {
  47.  
  48.     /**
  49.      * The pointer of the data set
  50.      * @var int
  51.      * @access private
  52.      */
  53.     var $_posX = 0;
  54.  
  55.     /**
  56.      * The minimum X value of the dataset
  57.      * @var int
  58.      * @access private
  59.      */
  60.     var $_minimumX = 0;
  61.  
  62.     /**
  63.      * The maximum X value of the dataset
  64.      * @var int
  65.      * @access private
  66.      */
  67.     var $_maximumX = 0;
  68.  
  69.     /**
  70.      * The minimum Y value of the dataset
  71.      * @var int
  72.      * @access private
  73.      */
  74.     var $_minimumY = 0;
  75.  
  76.     /**
  77.      * The maximum Y value of the dataset
  78.      * @var int
  79.      * @access private
  80.      */
  81.     var $_maximumY = 0;
  82.  
  83.     /**
  84.      * The number of points in the dataset
  85.      * @var int
  86.      * @access private
  87.      */
  88.     var $_count = 0;
  89.  
  90.     /**
  91.      * The name of the dataset, used for legending
  92.      * @var string
  93.      * @access private
  94.      */
  95.     var $_name = '';
  96.  
  97.     /**
  98.      * Image_Graph_Dataset [Constructor]
  99.      */
  100.     function Image_Graph_Dataset()
  101.     {
  102.     }
  103.  
  104.     /**
  105.      * Sets the name of the data set, used for legending
  106.      *
  107.      * @param string $name The name of the dataset
  108.      */
  109.     function setName($name)
  110.     {
  111.         $this->_name = $name;
  112.     }
  113.  
  114.     /**
  115.      * Add a point to the dataset
  116.      *
  117.      * $ID can contain either the ID of the point, i.e. 'DK', 123, 'George', etc. or it can contain
  118.      * values used for creation of the HTML image map. This is achieved using is an an associated array
  119.      * with the following values:
  120.      * 
  121.      * 'url' The URL to create the link to
  122.      * 
  123.      * 'alt' [optional] The alt text on the link
  124.      * 
  125.      * 'target' [optional] The target of the link
  126.      * 
  127.      * 'htmltags' [optional] An associated array with html tags (tag as key), fx. 'onMouseOver' => 'history.go(-1);', 'id' => 'thelink'
  128.      *
  129.      * @param int $x The X value to add
  130.      * @param int $y The Y value to add, can be omited
  131.      * @param var $ID The ID of the point
  132.      */
  133.     function addPoint($x, $y = false, $ID = false)
  134.     {
  135.         if ($y !== null) {
  136.             if (is_array($y)) {
  137.                 $maxY = max($y);
  138.                 $minY = min($y);
  139.             } else {
  140.                 $maxY = $y;
  141.                 $minY = $y;
  142.             }
  143.         }
  144.  
  145.         if ($this->_count) {
  146.             $this->_minimumX = min($x, $this->_minimumX);
  147.             $this->_maximumX = max($x, $this->_maximumX);
  148.             if ($y !== null) {
  149.                 $this->_minimumY = min($minY, $this->_minimumY);
  150.                 $this->_maximumY = max($maxY, $this->_maximumY);
  151.             }
  152.         } else {
  153.             $this->_minimumX = $x;
  154.             $this->_maximumX = $x;
  155.             if ($y !== null) {
  156.                 $this->_minimumY = $minY;
  157.                 $this->_maximumY = $maxY;
  158.             }
  159.         }
  160.  
  161.         $this->_count++;
  162.     }
  163.  
  164.     /**
  165.      * The number of values in the dataset
  166.      *
  167.      * @return int The number of values in the dataset
  168.      */
  169.     function count()
  170.     {
  171.         return $this->_count;
  172.     }
  173.  
  174.     /**
  175.      * Gets a X point from the dataset
  176.      *
  177.      * @param var $x The variable to return an X value from, fx in a vector
  178.      *   function data set
  179.      * @return var The X value of the variable
  180.      * @access private
  181.      */
  182.     function _getPointX($x)
  183.     {
  184.         return $x;
  185.     }
  186.  
  187.     /**
  188.      * Gets a Y point from the dataset
  189.      *
  190.      * @param var $x The variable to return an Y value from, fx in a vector
  191.      *   function data set
  192.      * @return var The Y value of the variable
  193.      * @access private
  194.      */
  195.     function _getPointY($x)
  196.     {
  197.         return $x;
  198.     }
  199.  
  200.     /**
  201.      * Gets a ID from the dataset
  202.      *
  203.      * @param var $x The variable to return an Y value from, fx in a vector
  204.      *   function data set
  205.      * @return var The ID value of the variable
  206.      * @access private
  207.      */
  208.     function _getPointID($x)
  209.     {
  210.         return false;
  211.     }
  212.     
  213.     /**
  214.      * Gets point data from the dataset
  215.      *
  216.      * @param var $x The variable to return an Y value from, fx in a vector
  217.      *   function data set
  218.      * @return array The data for the point
  219.      * @access private
  220.      */
  221.     function _getPointData($x)
  222.     {
  223.         return false;
  224.     }
  225.  
  226.     /**
  227.      * The minimum X value
  228.      *
  229.      * @return var The minimum X value
  230.      */
  231.     function minimumX()
  232.     {
  233.         return $this->_minimumX;
  234.     }
  235.  
  236.     /**
  237.      * The maximum X value
  238.      *
  239.      * @return var The maximum X value
  240.      */
  241.     function maximumX()
  242.     {
  243.         return $this->_maximumX;
  244.     }
  245.  
  246.     /**
  247.      * The minimum Y value
  248.      *
  249.      * @return var The minimum Y value
  250.      */
  251.     function minimumY()
  252.     {
  253.         return $this->_minimumY;
  254.     }
  255.  
  256.     /**
  257.      * The maximum Y value
  258.      *
  259.      * @return var The maximum Y value
  260.      */
  261.     function maximumY()
  262.     {
  263.         return $this->_maximumY;
  264.     }
  265.  
  266.     /**
  267.      * The first point
  268.      *
  269.      * @return array The last point
  270.      */
  271.     function first()
  272.     {
  273.         return array('X' => $this->minimumX(), 'Y' => $this->minimumY());
  274.     }
  275.  
  276.     /**
  277.      * The last point
  278.      *
  279.      * @return array The first point
  280.      */
  281.     function last()
  282.     {
  283.         return array('X' => $this->maximumX(), 'Y' => $this->maximumY());
  284.     }
  285.     
  286.     /**
  287.      * The minimum X value
  288.      *
  289.      * @param var $value The minimum X value
  290.      * @access private
  291.      */
  292.     function _setMinimumX($value)
  293.     {
  294.         $this->_minimumX = $value;
  295.     }
  296.  
  297.     /**
  298.      * The maximum X value
  299.      *
  300.      * @param var $value The maximum X value
  301.      * @access private
  302.      */
  303.     function _setMaximumX($value)
  304.     {
  305.         $this->_maximumX = $value;
  306.     }
  307.  
  308.     /**
  309.      * The minimum Y value
  310.      *
  311.      * @param var $value The minimum X value
  312.      * @access private
  313.      */
  314.     function _setMinimumY($value)
  315.     {
  316.         $this->_minimumY = $value;
  317.     }
  318.  
  319.     /**
  320.      * The maximum Y value
  321.      *
  322.      * @param var $value The maximum X value
  323.      * @access private
  324.      */
  325.     function _setMaximumY($value)
  326.     {
  327.         $this->_maximumY = $value;
  328.     }
  329.  
  330.     /**
  331.      * The interval between 2 adjacent X values
  332.      *
  333.      * @return var The interval
  334.      * @access private
  335.      */
  336.     function _stepX()
  337.     {
  338.         return 1;
  339.     }
  340.  
  341.     /**
  342.      * The interval between 2 adjacent Y values
  343.      *
  344.      * @return var The interval
  345.      * @access private
  346.      */
  347.     function _stepY()
  348.     {
  349.         return 1;
  350.     }
  351.  
  352.     /**
  353.      * Reset the intertal dataset pointer
  354.      *
  355.      * @return var The first X value
  356.      * @access private
  357.      */
  358.     function _reset()
  359.     {
  360.         $this->_posX = $this->_minimumX;
  361.         return $this->_posX;
  362.     }
  363.  
  364.     /**
  365.      * Get a point close to the internal pointer
  366.      *
  367.      * @param int Step Number of points next to the internal pointer, negative
  368.      *   Step is towards lower X values, positive towards higher X values
  369.      * @return array The point
  370.      * @access private
  371.      */
  372.     function _nearby($step = 0)
  373.     {
  374.         $x = $this->_getPointX($this->_posX + $this->_stepX() * $step);
  375.         $y = $this->_getPointY($this->_posX + $this->_stepX() * $step);
  376.         $ID = $this->_getPointID($this->_posX + $this->_stepX() * $step);
  377.         $data = $this->_getPointData($this->_posX + $this->_stepX() * $step);
  378.         if (($x === false) || ($y === false)) {
  379.             return false;
  380.         } else {
  381.             return array ('X' => $x, 'Y' => $y, 'ID' => $ID, 'data' => $data);
  382.         }
  383.     }
  384.  
  385.     /**
  386.      * Get the next point the internal pointer refers to and advance the pointer
  387.      *
  388.      * @return array The next point
  389.      * @access private
  390.      */
  391.     function _next()
  392.     {
  393.         if ($this->_posX > $this->_maximumX) {
  394.             return false;
  395.         }
  396.  
  397.         $x = $this->_getPointX($this->_posX);
  398.         $y = $this->_getPointY($this->_posX);
  399.         $ID = $this->_getPointID($this->_posX);
  400.         $data = $this->_getPointData($this->_posX);
  401.         $this->_posX += $this->_stepX();
  402.  
  403.         return array ('X' => $x, 'Y' => $y, 'ID' => $ID, 'data' => $data);
  404.     }
  405.  
  406.     /**
  407.      * Get the average of the dataset's Y points
  408.      *
  409.      * @return var The Y-average across the dataset
  410.      * @access private
  411.      */
  412.     function _averageY()
  413.     {
  414.         $posX = $this->_minimumX;
  415.         $count = 0;
  416.         $total = 0;
  417.         while ($posX < $this->_maximumX) {
  418.             $count ++;
  419.             $total += $this->_getPointY($posX);
  420.             $posX += $this->_stepX();
  421.         }
  422.  
  423.         if ($count != 0) {
  424.             return $total / $count;
  425.         } else {
  426.             return false;
  427.         }
  428.     }
  429.  
  430.     /**
  431.      * Get the median of the array passed Y points
  432.      *
  433.      * @param array $data The data-array to get the median from
  434.      * @param int $quartile The quartile to return the median from
  435.      * @return var The Y-median across the dataset from the specified quartile
  436.      * @access private
  437.      */
  438.     function _median($data, $quartile = 'second')
  439.     {
  440.         sort($data);
  441.         $point = (count($data) - 1) / 2;
  442.  
  443.         if ($quartile == 'first') {
  444.             $lowPoint = 0;
  445.             $highPoint = floor($point);
  446.         } elseif ($quartile == 'third') {
  447.             $lowPoint = round($point);
  448.             $highPoint = count($data) - 1;
  449.         } else {
  450.             $lowPoint = 0;
  451.             $highPoint = count($data) - 1;
  452.         }
  453.  
  454.         $point = ($lowPoint + $highPoint) / 2;
  455.  
  456.         if (floor($point) != $point) {
  457.             $point = floor($point);
  458.             return ($data[$point] + $data[($point + 1)]) / 2;
  459.         } else {
  460.             return $data[$point];
  461.         }
  462.     }
  463.  
  464.     /**
  465.      * Get the median of the datasets Y points
  466.      *
  467.      * @param int $quartile The quartile to return the median from
  468.      * @return var The Y-median across the dataset from the specified quartile
  469.      * @access private
  470.      */
  471.     function _medianY($quartile = 'second')
  472.     {
  473.         $pointsY = array();
  474.         $posX = $this->_minimumX;
  475.         while ($posX <= $this->_maximumX) {
  476.             $pointsY[] = $this->_getPointY($posX);
  477.             $posX += $this->_stepX();
  478.         }
  479.         return $this->_median($pointsY, $quartile);
  480.     }
  481.  
  482. }
  483. ?>