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 / Plotarea / Map.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  9.1 KB  |  304 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 Plotarea
  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: Map.php,v 1.10 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/Plotarea.php
  33.  */
  34. require_once 'Image/Graph/Plotarea.php';
  35.  
  36. /**
  37.  * Plot area used for map plots.
  38.  *
  39.  * A map plot is a chart that displays a map (fx. a world map) in the form of  .
  40.  * png file. The maps must be located in the /Images/Maps folder and a
  41.  * corresponding .txt files mush also exist in this location where named
  42.  * locations are mapped to an (x, y) coordinate of the map picture (this text
  43.  * file is tab separated with 'Name' 'X' 'Y' values, fx 'Denmark 378 223'). The
  44.  * x-values in the dataset are then the named locations (fx 'Denmark') and the
  45.  * y-values are then the data to plot. Currently the best (if not only) use is
  46.  * to combine a map plot area with a {@link Image_Graph_Plot_Dot} using {@link
  47.  * Image_Graph_Marker_PercentageCircle} as marker.
  48.  *
  49.  * @category   Images
  50.  * @package    Image_Graph
  51.  * @subpackage Plotarea
  52.  * @author     Jesper Veggerby <pear.nosey@veggerby.dk>
  53.  * @copyright  Copyright (C) 2003, 2004 Jesper Veggerby Hansen
  54.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  55.  * @version    Release: 0.7.2
  56.  * @link       http://pear.php.net/package/Image_Graph
  57.  */
  58. class Image_Graph_Plotarea_Map extends Image_Graph_Plotarea
  59. {
  60.  
  61.     /**
  62.      * The GD image for the map
  63.      * @var string
  64.      * @access private
  65.      */
  66.     var $_imageMap;
  67.  
  68.     /**
  69.      * The value for scaling the width and height to fit into the layout boundaries
  70.      * @var int
  71.      * @access private
  72.      */
  73.     var $_scale;
  74.  
  75.     /**
  76.      * The (x,y)-points for the named point
  77.      * @var array
  78.      * @access private
  79.      */
  80.     var $_mapPoints;
  81.  
  82.     /**
  83.      * The original size of the image map
  84.      * @var array
  85.      * @access private
  86.      */
  87.     var $_mapSize;
  88.  
  89.     /**
  90.      * PlotareaMap [Constructor]
  91.      *
  92.      * @param string $map The name of the map, i.e. the [name].png and  [name].
  93.      *   txt files located in the Images/maps folder
  94.      */
  95.     function Image_Graph_Plotarea_Map($map)
  96.     {
  97.         parent::Image_Graph_Plotarea();
  98.  
  99.         $this->_imageMap = dirname(__FILE__)."/../Images/Maps/$map.png";
  100.         $points = file(dirname(__FILE__)."/../Images/Maps/$map.txt");
  101.         list($width, $height) = getimagesize($this->_imageMap);
  102.         $this->_mapSize['X'] = $width;
  103.         $this->_mapSize['Y'] = $height;
  104.  
  105.         if (is_array($points)) {
  106.             unset($this->_mapPoints);
  107.             foreach ($points as $line) {
  108.                 list($country, $x, $y) = explode("\t", $line);
  109.                 $this->_mapPoints[$country] = array('X' => $x, 'Y' => $y);
  110.             }
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * Left boundary of the background fill area
  116.      *
  117.      * @return int Leftmost position on the canvas
  118.      * @access private
  119.      */
  120.     function _fillLeft()
  121.     {
  122.         return $this->_left + $this->_padding['left'];
  123.     }
  124.  
  125.     /**
  126.      * Top boundary of the background fill area
  127.      *
  128.      * @return int Topmost position on the canvas
  129.      * @access private
  130.      */
  131.     function _fillTop()
  132.     {
  133.         return $this->_top + $this->_padding['top'];
  134.     }
  135.  
  136.     /**
  137.      * Right boundary of the background fill area
  138.      *
  139.      * @return int Rightmost position on the canvas
  140.      * @access private
  141.      */
  142.     function _fillRight()
  143.     {
  144.         return $this->_right - $this->_padding['right'];
  145.     }
  146.  
  147.     /**
  148.      * Bottom boundary of the background fill area
  149.      *
  150.      * @return int Bottommost position on the canvas
  151.      * @access private
  152.      */
  153.     function _fillBottom()
  154.     {
  155.         return $this->_bottom - $this->_padding['bottom'];
  156.     }
  157.  
  158.     /**
  159.      * Set the extrema of the axis
  160.      *
  161.      * @param Image_Graph_Plot $plot The plot that 'hold' the values
  162.      * @access private
  163.      */
  164.     function _setExtrema(& $plot)
  165.     {
  166.     }
  167.  
  168.     /**
  169.      * Get the X pixel position represented by a value
  170.      *
  171.      * @param double $value The value to get the pixel-point for
  172.      * @return double The pixel position along the axis
  173.      * @access private
  174.      */
  175.     function _pointX($value)
  176.     {
  177.         $country = $value['X'];
  178.         return $this->_plotLeft+$this->_mapPoints[$country]['X']*$this->_scale;
  179.     }
  180.  
  181.     /**
  182.      * Get the Y pixel position represented by a value
  183.      *
  184.      * @param double $value The value to get the pixel-point for
  185.      * @return double The pixel position along the axis
  186.      * @access private
  187.      */
  188.     function _pointY($value)
  189.     {
  190.         $country = $value['X'];
  191.         return $this->_plotTop+$this->_mapPoints[$country]['Y']*$this->_scale;
  192.     }
  193.  
  194.     /**
  195.      * Hides the axis
  196.      */
  197.     function hideAxis()
  198.     {
  199.     }
  200.  
  201.     /**
  202.      * Add a point to the maps
  203.      *
  204.      * @param int $latitude The latitude of the point
  205.      * @param int $longiude The longitude of the point
  206.      * @param string $name The name of the plot
  207.      */
  208.     function addMappoint($latitude, $longitude, $name)
  209.     {
  210.         $x = (($longitude + 180) * ($this->_mapSize['X'] / 360));
  211.         $y = ((($latitude * -1) + 90) * ($this->_mapSize['Y'] / 180));
  212.         $this->_mapPoints[$name] = array('X' => $x, 'Y' => $y);
  213.     }
  214.  
  215.     /**
  216.      * Add a point to the maps
  217.      *
  218.      * @param int $x The latitude of the point
  219.      * @param int $y The longitude of the point
  220.      * @param string $name The name of the plot
  221.      */
  222.     function addPoint($x, $y, $name)
  223.     {
  224.         $this->_mapPoints[$name] = array('X' => $x, 'Y' => $y);
  225.     }
  226.  
  227.     /**
  228.      * Update coordinates
  229.      *
  230.      * @access private
  231.      */
  232.     function _updateCoords()
  233.     {
  234.         parent::_updateCoords();
  235.  
  236.         $mapAspectRatio = $this->_mapSize['X']/$this->_mapSize['Y'];
  237.         $plotAspectRatio = ($width = $this->_fillWidth())/($height = $this->_fillHeight());
  238.  
  239.         $scaleFactorX = ($mapAspectRatio > $plotAspectRatio);
  240.  
  241.         if ((($this->_mapSize['X'] <= $width) && ($this->_mapSize['Y'] <= $height)) ||
  242.             (($this->_mapSize['X'] >= $width) && ($this->_mapSize['Y'] >= $height)))
  243.         {
  244.             if ($scaleFactorX) {
  245.                 $this->_scale = $width / $this->_mapSize['X'];
  246.             } else {
  247.                 $this->_scale = $height / $this->_mapSize['Y'];
  248.             }
  249.         } elseif ($this->_mapSize['X'] < $width) {
  250.             $this->_scale = $height / $this->_mapSize['Y'];
  251.         } elseif ($this->_mapSize['Y'] < $height) {
  252.             $this->_scale = $width / $this->_mapSize['X'];
  253.         }
  254.  
  255.         $this->_plotLeft = ($this->_fillLeft() + $this->_fillRight() -
  256.             $this->_mapSize['X']*$this->_scale)/2;
  257.  
  258.         $this->_plotTop = ($this->_fillTop() + $this->_fillBottom() -
  259.             $this->_mapSize['Y']*$this->_scale)/2;
  260.  
  261.         $this->_plotRight = ($this->_fillLeft() + $this->_fillRight() +
  262.             $this->_mapSize['X']*$this->_scale)/2;
  263.  
  264.         $this->_plotBottom = ($this->_fillTop() + $this->_fillBottom() +
  265.             $this->_mapSize['Y']*$this->_scale)/2;
  266.     }
  267.  
  268.     /**
  269.      * Output the plotarea to the canvas
  270.      *
  271.      * @return bool Was the output 'good' (true) or 'bad' (false).
  272.      * @access private
  273.      */
  274.     function _done()
  275.     {
  276.         $this->_getFillStyle();
  277.         $this->_canvas->rectangle(
  278.             array(
  279.                 'x0' => $this->_fillLeft(),
  280.                 'y0' => $this->_fillTop(),
  281.                 'x1' => $this->_fillRight(),
  282.                 'y1' => $this->_fillBottom()
  283.             )
  284.         );
  285.  
  286.         $scaledWidth = $this->_mapSize['X']*$this->_scale;
  287.         $scaledHeight = $this->_mapSize['Y']*$this->_scale;
  288.  
  289.         $this->_canvas->image(
  290.             array(
  291.                 'x' => $this->_plotLeft,
  292.                 'y' => $this->_plotTop,
  293.                 'filename' => $this->_imageMap,
  294.                 'width' => $scaledWidth,
  295.                 'height' => $scaledHeight
  296.             )
  297.         );
  298.  
  299.         return Image_Graph_Layout::_done();
  300.     }
  301.  
  302. }
  303.  
  304. ?>