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 / CandleStick.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  7.7 KB  |  251 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: CandleStick.php,v 1.12 2005/11/27 22:21:16 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.  * Candlestick 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_CandleStick extends Image_Graph_Plot
  51. {
  52.  
  53.     /**
  54.      * (Add basic description here)
  55.      *
  56.      * @access private
  57.      */
  58.     function _drawCandleStickH($y, $h, $x_min, $x_open, $x_close, $x_max, $ID) 
  59.     {
  60.         $this->_getLineStyle($ID);
  61.         $this->_canvas->line(
  62.             array(
  63.                 'x0' => min($x_open, $x_close), 
  64.                 'y0' => $y, 
  65.                 'x1' => $x_min, 
  66.                 'y1' => $y
  67.             )
  68.         );
  69.         $this->_getLineStyle($ID);
  70.         $this->_canvas->line(
  71.             array(
  72.                 'x0' => max($x_open, $x_close), 
  73.                 'y0' => $y, 
  74.                 'x1' => $x_max, 
  75.                 'y1' => $y
  76.             )
  77.         );
  78.     
  79.         $this->_getLineStyle($ID);
  80.         $this->_getFillStyle($ID);
  81.         $this->_canvas->rectangle(
  82.             array(
  83.                 'x0' => min($x_open, $x_close), 
  84.                 'y0' => $y - $h, 
  85.                 'x1' => max($x_open, $x_close), 
  86.                 'y1' => $y + $h
  87.             )
  88.         );
  89.     }
  90.  
  91.     /**
  92.      * (Add basic description here)
  93.      *
  94.      * @access private
  95.      */
  96.     function _drawCandleStickV($x, $w, $y_min, $y_open, $y_close, $y_max, $ID) 
  97.     {
  98.         $this->_getLineStyle($ID);
  99.         $this->_canvas->line(
  100.             array(
  101.                 'x0' => $x, 
  102.                 'y0' => min($y_open, $y_close), 
  103.                 'x1' => $x, 
  104.                 'y1' => $y_max  
  105.             )
  106.         );
  107.         $this->_getLineStyle($ID);
  108.         $this->_canvas->line(
  109.             array(
  110.                 'x0' => $x, 
  111.                 'y0' => max($y_open, $y_close), 
  112.                 'x1' => $x, 
  113.                 'y1' => $y_min
  114.             )
  115.         );
  116.     
  117.         $this->_getLineStyle($ID);
  118.         $this->_getFillStyle($ID);
  119.         $this->_canvas->rectangle(
  120.             array(
  121.                 'x0' => $x - $w, 
  122.                 'y0' => min($y_open, $y_close), 
  123.                 'x1' => $x + $w, 
  124.                 'y1' => max($y_open, $y_close)
  125.             )
  126.         );
  127.     }
  128.  
  129.     /**
  130.      * Perform the actual drawing on the legend.
  131.      *
  132.      * @param int $x0 The top-left x-coordinate
  133.      * @param int $y0 The top-left y-coordinate
  134.      * @param int $x1 The bottom-right x-coordinate
  135.      * @param int $y1 The bottom-right y-coordinate
  136.      * @access private
  137.      */
  138.     function _drawLegendSample($x0, $y0, $x1, $y1)
  139.     {
  140.         $x = round(($x0 + $x1) / 2);
  141.         $h = abs($y1 - $y0) / 4;
  142.         $w = round(abs($x1 - $x0) / 5);
  143.         $this->_drawCandleStickV($x, $w, $y1, $y1 - $h, $y0 + $h, $y0, 'green');
  144.     }
  145.  
  146.     /**
  147.      * Output the plot
  148.      *
  149.      * @return bool Was the output 'good' (true) or 'bad' (false).
  150.      * @access private
  151.      */
  152.     function _done()
  153.     {
  154.         if (parent::_done() === false) {
  155.             return false;
  156.         }
  157.  
  158.         if (!is_array($this->_dataset)) {
  159.             return false;
  160.         }
  161.  
  162.         $this->_canvas->startGroup(get_class($this) . '_' . $this->_title);
  163.         
  164.         $this->_clip(true);
  165.  
  166.         if ($this->_multiType == 'stacked100pct') {
  167.             $total = $this->_getTotals();
  168.         }
  169.         $current = array();
  170.         $number = 0;
  171.         $width = floor(0.8 * $this->_parent->_labelDistance(IMAGE_GRAPH_AXIS_X) / 2);
  172.  
  173.         $lastClosed = false;
  174.         $keys = array_keys($this->_dataset);
  175.         foreach ($keys as $key) {
  176.             $dataset =& $this->_dataset[$key];
  177.             $dataset->_reset();
  178.             while ($data = $dataset->_next()) {
  179.                 if ($this->_parent->_horizontal) {
  180.                     $point['X'] = $data['X'];
  181.                     //$y = $data['Y'];
  182.  
  183.                     if (isset($data['Y']['open'])) {                       
  184.                         $point['Y'] = $data['Y']['open'];
  185.                     } else {
  186.                         $point['Y'] = $lastClosed;
  187.                     }
  188.                     $y = $this->_pointY($point);
  189.                     $x_open = $this->_pointX($point);
  190.     
  191.                     $lastClosed = $point['Y'] = $data['Y']['close'];
  192.                     $x_close = $this->_pointX($point);
  193.     
  194.                     $point['Y'] = $data['Y']['min'];
  195.                     $x_min = $this->_pointX($point);
  196.     
  197.                     $point['Y'] = $data['Y']['max'];
  198.                     $x_max = $this->_pointX($point);
  199.     
  200.                     if ($data['Y']['close'] < $data['Y']['open']) {
  201.                         $ID = 'red';
  202.                     } else {
  203.                         $ID = 'green';
  204.                     }
  205.     
  206.                     $this->_drawCandleStickH($y, $width, $x_min, $x_open, $x_close, $x_max, $ID);
  207.                 }
  208.                 else {
  209.                     $point['X'] = $data['X'];
  210.                     //$y = $data['Y'];
  211.     
  212.                     if (isset($data['Y']['open'])) {                       
  213.                         $point['Y'] = $data['Y']['open'];
  214.                     } else {
  215.                         $point['Y'] = $lastClosed;
  216.                     }
  217.                     $x = $this->_pointX($point);
  218.                     $y_open = $this->_pointY($point);
  219.     
  220.                     $lastClosed = $point['Y'] = $data['Y']['close'];
  221.                     $y_close = $this->_pointY($point);
  222.     
  223.                     $point['Y'] = $data['Y']['min'];
  224.                     $y_min = $this->_pointY($point);
  225.     
  226.                     $point['Y'] = $data['Y']['max'];
  227.                     $y_max = $this->_pointY($point);
  228.     
  229.                     if ($data['Y']['close'] < $data['Y']['open']) {
  230.                         $ID = 'red';
  231.                     } else {
  232.                         $ID = 'green';
  233.                     }
  234.     
  235.                     $this->_drawCandleStickV($x, $width, $y_min, $y_open, $y_close, $y_max, $ID);
  236.                 }
  237.             }
  238.         }
  239.         unset($keys);
  240.         $this->_drawMarker();
  241.         
  242.         $this->_clip(false);        
  243.         
  244.         $this->_canvas->endGroup($this->_title);
  245.         
  246.         return true;
  247.     }
  248.  
  249. }
  250.  
  251. ?>