Image_Graph
[ class tree: Image_Graph ] [ index: Image_Graph ] [ all elements ]

Source for file Graph.php

Documentation is available at Graph.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * Image_Graph - Main class for the graph creation.
  7.  *
  8.  * PHP version 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.  * @author    Jesper Veggerby <pear.nosey@veggerby.dk>
  24.  * @author    Stefan Neufeind <pear.neufeind@speedpartner.de>
  25.  * @copyright 2003-2009 The PHP Group
  26.  * @license   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  27.  * @version   SVN: $Id: Graph.php 291756 2009-12-06 02:55:46Z neufeind $
  28.  * @link      http://pear.php.net/package/Image_Graph
  29.  */
  30.  
  31.  
  32. /**
  33.  * Include PEAR.php
  34.  */
  35. require_once 'PEAR.php';
  36.  
  37. /**
  38.  * Include file Image/Graph/Element.php
  39.  */
  40. require_once 'Image/Graph/Element.php';
  41.  
  42. /**
  43.  * Include file Image/Graph/Constants.php
  44.  */
  45. require_once 'Image/Graph/Constants.php';
  46.  
  47. /**
  48.  * Main class for the graph creation.
  49.  *
  50.  * This is the main class, it manages the canvas and performs the final output
  51.  * by sequentialy making the elements output their results. The final output is
  52.  * handled using the {@link Image_Canvas} classes which makes it possible
  53.  * to use different engines (fx GD, PDFlib, libswf, etc) for output to several
  54.  * formats with a non-intersecting API.
  55.  *
  56.  * This class also handles coordinates and the correct managment of setting the
  57.  * correct coordinates on child elements.
  58.  *
  59.  * @category  Images
  60.  * @package   Image_Graph
  61.  * @author    Jesper Veggerby <pear.nosey@veggerby.dk>
  62.  * @author    Stefan Neufeind <pear.neufeind@speedpartner.de>
  63.  * @copyright 2003-2009 The PHP Group
  64.  * @license   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  65.  * @version   Release: 0.8.0
  66.  * @link      http://pear.php.net/package/Image_Graph
  67.  */
  68. {
  69.  
  70.     /**
  71.      * Show generation time on graph
  72.      * @var bool 
  73.      * @access private
  74.      */
  75.     var $_showTime = false;
  76.  
  77.     /**
  78.      * Display errors on the canvas
  79.      * @var boolean 
  80.      * @access private
  81.      */
  82.     var $_displayErrors = false;
  83.  
  84.     /**
  85.      * Image_Graph [Constructor].
  86.      *
  87.      * If passing the 3 parameters they are defined as follows:'
  88.      * 
  89.      * Fx.:
  90.      * 
  91.      * $Graph = new Image_Graph(400, 300);
  92.      * 
  93.      * or using the factory method:
  94.      * 
  95.      * $Graph =& Image_Graph::factory('graph', array(400, 300));
  96.      * 
  97.      * This causes a 'png' canvas to be created by default.
  98.      * 
  99.      * Otherwise use a single parameter either as an associated array or passing
  100.      * the canvas along to the constructor:
  101.      *
  102.      * 1) Create a new canvas with the following parameters:
  103.      *
  104.      * 'canvas' - The canvas type, can be any of 'gd', 'jpg', 'png' or 'svg'
  105.      * (more to come) - if omitted the default is 'gd'
  106.      *
  107.      * 'width' - The width of the graph
  108.      *
  109.      * 'height' - The height of the graph
  110.      * 
  111.      * An example of this usage:
  112.      * 
  113.      * $Graph =& Image_Graph::factory('graph', array(array('width' => 400,
  114.      * 'height' => 300, 'canvas' => 'jpg')));
  115.      * 
  116.      * NB! In th�s case remember the "double" array (see {@link Image_Graph::}
  117.      * factory()})
  118.      * 
  119.      * 2) Use the canvas specified, pass a valid Image_Canvas as
  120.      * parameter. Remember to pass by reference, i. e. &amp;$canvas, fx.:
  121.      *
  122.      * $Graph = new Image_Graph($Canvas);
  123.      *
  124.      * or using the factory method:
  125.      *
  126.      * $Graph =& Image_Graph::factory('graph', $Canvas));
  127.      *
  128.      * @param mixed $params            The width of the graph, an indexed array
  129.      *    describing a new canvas or a valid {@link Image_Canvas} object
  130.      * @param int   $height            The height of the graph in pixels
  131.      * @param bool  $createTransparent Specifies whether the graph should be
  132.      *    created with a transparent background (fx for PNG's - note: transparent
  133.      *    PNG's is not supported by Internet Explorer!)
  134.      */
  135.     function Image_Graph($params$height = false$createTransparent = false)
  136.     {
  137.         parent::__construct();
  138.  
  139.         $this->setFont(Image_Graph::factory('Image_Graph_Font'));
  140.  
  141.         if (defined('IMAGE_GRAPH_DEFAULT_CANVAS_TYPE')) {
  142.             $canvasType = IMAGE_GRAPH_DEFAULT_CANVAS_TYPE;
  143.         else {
  144.             $canvasType 'png'// use GD as default, if nothing else is specified
  145.         }
  146.  
  147.         if (is_array($params)) {
  148.             if (isset($params['canvas'])) {
  149.                 $canvasType $params['canvas'];
  150.             }
  151.  
  152.             $width = 0;
  153.             $height = 0;
  154.  
  155.             if (isset($params['width'])) {
  156.                 $width $params['width'];
  157.             }
  158.  
  159.             if (isset($params['height'])) {
  160.                 $height $params['height'];
  161.             }
  162.         elseif (is_a($params'Image_Canvas')) {
  163.             $this->_canvas =$params;
  164.             $width $this->_canvas->getWidth();
  165.             $height $this->_canvas->getHeight();
  166.         elseif (is_numeric($params)) {
  167.             $width $params;
  168.         }
  169.  
  170.         if ($this->_canvas == null{
  171.             include_once 'Image/Canvas.php';
  172.             $this->_canvas =&
  173.                 Image_Canvas::factory(
  174.                     $canvasType,
  175.                     array('width' => $width'height' => $height)
  176.                 );
  177.         }
  178.  
  179.         $this->_setCoords(00$width - 1$height - 1);
  180.     }
  181.  
  182.     /**
  183.      * Gets the canvas for this graph.
  184.      *
  185.      * The canvas is set by either passing it to the constructor {@link }
  186.      * Image_Graph::ImageGraph()} or using the {@link Image_Graph::setCanvas()}
  187.      * method.
  188.      *
  189.      * @return Image_Canvas The canvas used by this graph
  190.      * @access private
  191.      * @since 0.3.0dev2
  192.      */
  193.     function &_getCanvas()
  194.     {
  195.         return $this->_canvas;
  196.     }
  197.  
  198.     /**
  199.      * Sets the canvas for this graph.
  200.      *
  201.      * Calling this method makes this graph use the newly specified canvas for
  202.      * handling output. This method should be called whenever multiple
  203.      * 'outputs' are required. Invoke this method after calls to {@link }
  204.      * Image_Graph:: done()} has been performed, to switch canvass.
  205.      *
  206.      * @param Image_Canvas &$canvas The new canvas
  207.      *
  208.      * @return Image_Canvas The new canvas
  209.      * @since 0.3.0dev2
  210.      */
  211.     function &setCanvas(&$canvas)
  212.     {
  213.         if (!is_a($this->_canvas'Image_Canvas')) {
  214.             return $this->_error('The canvas introduced is not an Image_Canvas object');
  215.         }
  216.         
  217.         $this->_canvas =$canvas;
  218.         $this->_setCoords(
  219.             0,
  220.             0,
  221.             $this->_canvas->getWidth(- 1,
  222.             $this->_canvas->getHeight(- 1
  223.         );
  224.         return $this->_canvas;
  225.     }
  226.  
  227.     /**
  228.      * Gets a very precise timestamp
  229.      *
  230.      * @return The number of seconds to a lot of decimals
  231.      * @access private
  232.      */
  233.     function _getMicroTime()
  234.     {
  235.         list($usec$secexplode(' 'microtime())
  236.         return ((float)$usec + (float)$sec)
  237.     }
  238.  
  239.     /**
  240.      * Gets the width of this graph.
  241.      *
  242.      * The width is returned as 'defined' by the canvas.
  243.      *
  244.      * @return int the width of this graph
  245.      */
  246.     function width()
  247.     {
  248.         return $this->_canvas->getWidth();
  249.     }
  250.  
  251.     /**
  252.      * Gets the height of this graph.
  253.      *
  254.      * The height is returned as 'defined' by the canvas.
  255.      *
  256.      * @return int the height of this graph
  257.      */
  258.     function height()
  259.     {
  260.         return $this->_canvas->getHeight();
  261.     }
  262.  
  263.     /**
  264.      * Enables displaying of errors on the output.
  265.      *
  266.      * Use this method to enforce errors to be displayed on the output. Calling
  267.      * this method makes PHP uses this graphs error handler as default {@link }
  268.      * Image_Graph::_default_error_handler()}.
  269.      *
  270.      * @return void 
  271.      */
  272.     function displayErrors()
  273.     {
  274.         $this->_displayErrors = true;
  275.         set_error_handler(array(&$this'_default_error_handler'));
  276.     }
  277.  
  278.     /**
  279.      * Sets the log method for this graph.
  280.      *
  281.      * Use this method to enable logging. This causes any errors caught
  282.      * by either the error handler {@see Image_Graph::displayErrors()}
  283.      * or explicitly by calling {@link Image_Graph_Common::_error()} be
  284.      * logged using the specified logging method.
  285.      *
  286.      * If a filename is specified as log method, a Log object is created (using
  287.      * the 'file' handler), with a handle of 'Image_Graph Error Log'.
  288.      *
  289.      * Logging requires {@link Log}.
  290.      *
  291.      * @param mixed $log The log method, either a Log object or filename to log to
  292.      *
  293.      * @since 0.3.0dev2
  294.      * @return void 
  295.      */
  296.     function setLog($log)
  297.     {
  298.     }
  299.  
  300.     /**
  301.      * Factory method to create Image_Graph objects.
  302.      *
  303.      * Used for 'lazy including', i.e. loading only what is necessary, when it
  304.      * is necessary. If only one parameter is required for the constructor of
  305.      * the class simply pass this parameter as the $params parameter, unless the
  306.      * parameter is an array or a reference to a value, in that case you must
  307.      * 'enclose' the parameter in an array. Similar if the constructor takes
  308.      * more than one parameter specify the parameters in an array, i.e
  309.      *
  310.      * Image_Graph::factory('MyClass', array($param1, $param2, &$param3));
  311.      *
  312.      * Variables that need to be passed by reference *must* have the &amp;
  313.      * before the variable, i.e:
  314.      *
  315.      * Image_Graph::factory('line', &$Dataset);
  316.      *
  317.      * or
  318.      *
  319.      * Image_graph::factory('bar', array(array(&$Dataset1, &$Dataset2),
  320.      * 'stacked'));
  321.      *
  322.      * Class name can be either of the following:
  323.      *
  324.      * 1 The 'real' Image_Graph class name, i.e. Image_Graph_Plotarea or
  325.      * Image_Graph_Plot_Line
  326.      *
  327.      * 2 Short class name (leave out Image_Graph) and retain case, i.e.
  328.      * Plotarea, Plot_Line *not* plot_line
  329.      *
  330.      * 3 Class name 'alias', the following are supported:
  331.      *
  332.      * 'graph' = Image_Graph
  333.      *
  334.      * 'plotarea' = Image_Graph_Plotarea
  335.      *
  336.      * 'line' = Image_Graph_Plot_Line
  337.      *
  338.      * 'area' = Image_Graph_Plot_Area
  339.      *
  340.      * 'bar' = Image_Graph_Plot_Bar
  341.      *
  342.      * 'pie' = Image_Graph_Plot_Pie
  343.      *
  344.      * 'radar' = Image_Graph_Plot_Radar
  345.      *
  346.      * 'step' = Image_Graph_Plot_Step
  347.      *
  348.      * 'impulse' = Image_Graph_Plot_Impulse
  349.      *
  350.      * 'dot' or 'scatter' = Image_Graph_Plot_Dot
  351.      *
  352.      * 'smooth_line' = Image_Graph_Plot_Smoothed_Line
  353.      *
  354.      * 'smooth_area' = Image_Graph_Plot_Smoothed_Area
  355.  
  356.      * 'dataset' = Image_Graph_Dataset_Trivial
  357.      *
  358.      * 'random' = Image_Graph_Dataset_Random
  359.      *
  360.      * 'function' = Image_Graph_Dataset_Function
  361.      *
  362.      * 'vector' = Image_Graph_Dataset_VectorFunction
  363.      *
  364.      * 'category' = Image_Graph_Axis_Category
  365.      *
  366.      * 'axis' = Image_Graph_Axis
  367.      *
  368.      * 'axis_log' = Image_Graph_Axis_Logarithmic
  369.      *
  370.      * 'title' = Image_Graph_Title
  371.      *
  372.      * 'line_grid' = Image_Graph_Grid_Lines
  373.      *
  374.      * 'bar_grid' = Image_Graph_Grid_Bars
  375.      *
  376.      * 'polar_grid' = Image_Graph_Grid_Polar
  377.      *
  378.      * 'legend' = Image_Graph_Legend
  379.      *
  380.      * 'font' = Image_Graph_Font
  381.      *
  382.      * 'ttf_font' = Image_Graph_Font
  383.      * 
  384.      * 'Image_Graph_Font_TTF' = Image_Graph_Font (to maintain BC with Image_Graph_Font_TTF)
  385.      *
  386.      * 'gradient' = Image_Graph_Fill_Gradient
  387.      *
  388.      * 'icon_marker' = Image_Graph_Marker_Icon
  389.      *
  390.      * 'value_marker' = Image_Graph_Marker_Value
  391.      *
  392.      * @param string $class  The class for the new object
  393.      * @param mixed  $params The paramaters to pass to the constructor
  394.      *
  395.      * @return object new object for the class
  396.      * @static
  397.      */
  398.     function &factory($class$params = null)
  399.     {
  400.         static $Image_Graph_classAliases = array(
  401.             'graph'          => 'Image_Graph',
  402.             'plotarea'       => 'Image_Graph_Plotarea',            
  403.  
  404.             'line'           => 'Image_Graph_Plot_Line',
  405.             'area'           => 'Image_Graph_Plot_Area',
  406.             'bar'            => 'Image_Graph_Plot_Bar',
  407.             'smooth_line'    => 'Image_Graph_Plot_Smoothed_Line',
  408.             'smooth_area'    => 'Image_Graph_Plot_Smoothed_Area',
  409.             'pie'            => 'Image_Graph_Plot_Pie',
  410.             'radar'          => 'Image_Graph_Plot_Radar',
  411.             'step'           => 'Image_Graph_Plot_Step',
  412.             'impulse'        => 'Image_Graph_Plot_Impulse',
  413.             'dot'            => 'Image_Graph_Plot_Dot',
  414.             'scatter'        => 'Image_Graph_Plot_Dot',
  415.  
  416.             'dataset'        => 'Image_Graph_Dataset_Trivial',
  417.             'random'         => 'Image_Graph_Dataset_Random',
  418.             'function'       => 'Image_Graph_Dataset_Function',
  419.             'vector'         => 'Image_Graph_Dataset_VectorFunction',
  420.  
  421.             'category'       => 'Image_Graph_Axis_Category',
  422.             'axis'           => 'Image_Graph_Axis',
  423.             'axis_log'       => 'Image_Graph_Axis_Logarithmic',
  424.  
  425.             'title'          => 'Image_Graph_Title',
  426.  
  427.             'line_grid'      => 'Image_Graph_Grid_Lines',
  428.             'bar_grid'       => 'Image_Graph_Grid_Bars',
  429.             'polar_grid'     => 'Image_Graph_Grid_Polar',
  430.  
  431.             'legend'         => 'Image_Graph_Legend',
  432.             'font'             => 'Image_Graph_Font',
  433.             'ttf_font'       => 'Image_Graph_Font',
  434.             'Image_Graph_Font_TTF' => 'Image_Graph_Font'// BC with Image_Graph_Font_TTF
  435.             'gradient'       => 'Image_Graph_Fill_Gradient',
  436.  
  437.             'icon_marker'    => 'Image_Graph_Marker_Icon',
  438.             'value_marker'   => 'Image_Graph_Marker_Value'
  439.         );
  440.  
  441.         if (substr($class011!= 'Image_Graph'{
  442.             if (isset($Image_Graph_classAliases[$class])) {
  443.                 $class $Image_Graph_classAliases[$class];
  444.             else {
  445.                 $class 'Image_Graph_' $class;
  446.             }
  447.         }
  448.  
  449.         include_once str_replace('_''/'$class'.php';
  450.  
  451.         $obj = null;
  452.  
  453.         if (is_array($params)) {
  454.             switch (count($params)) {
  455.             case 1:
  456.                 $obj = new $class(
  457.                     $params[0]
  458.                 );
  459.                 break;
  460.  
  461.             case 2:
  462.                 $obj = new $class(
  463.                     $params[0],
  464.                     $params[1]
  465.                 );
  466.                 break;
  467.  
  468.             case 3:
  469.                 $obj = new $class(
  470.                     $params[0],
  471.                     $params[1],
  472.                     $params[2]
  473.                 );
  474.                 break;
  475.  
  476.             case 4:
  477.                 $obj = new $class(
  478.                     $params[0],
  479.                     $params[1],
  480.                     $params[2],
  481.                     $params[3]
  482.                 );
  483.                 break;
  484.  
  485.             case 5:
  486.                 $obj = new $class(
  487.                     $params[0],
  488.                     $params[1],
  489.                     $params[2],
  490.                     $params[3],
  491.                     $params[4]
  492.                 );
  493.                 break;
  494.  
  495.             case 6:
  496.                 $obj = new $class(
  497.                     $params[0],
  498.                     $params[1],
  499.                     $params[2],
  500.                     $params[3],
  501.                     $params[4],
  502.                     $params[5]
  503.                 );
  504.                 break;
  505.  
  506.             case 7:
  507.                 $obj = new $class(
  508.                     $params[0],
  509.                     $params[1],
  510.                     $params[2],
  511.                     $params[3],
  512.                     $params[4],
  513.                     $params[5],
  514.                     $params[6]
  515.                 );
  516.                 break;
  517.  
  518.             case 8:
  519.                 $obj = new $class(
  520.                     $params[0],
  521.                     $params[1],
  522.                     $params[2],
  523.                     $params[3],
  524.                     $params[4],
  525.                     $params[5],
  526.                     $params[6],
  527.                     $params[7]
  528.                 );
  529.                 break;
  530.  
  531.             case 9:
  532.                 $obj = new $class(
  533.                     $params[0],
  534.                     $params[1],
  535.                     $params[2],
  536.                     $params[3],
  537.                     $params[4],
  538.                     $params[5],
  539.                     $params[6],
  540.                     $params[7],
  541.                     $params[8]
  542.                 );
  543.                 break;
  544.  
  545.             case 10:
  546.                 $obj = new $class(
  547.                     $params[0],
  548.                     $params[1],
  549.                     $params[2],
  550.                     $params[3],
  551.                     $params[4],
  552.                     $params[5],
  553.                     $params[6],
  554.                     $params[7],
  555.                     $params[8],
  556.                     $params[9]
  557.                 );
  558.                 break;
  559.  
  560.             default:
  561.                 $obj = new $class();
  562.                 break;
  563.  
  564.             }
  565.         else {
  566.             if ($params == null{
  567.                 $obj = new $class();
  568.             else {
  569.                 $obj = new $class($params);
  570.             }
  571.         }
  572.         return $obj;
  573.     }
  574.  
  575.     /**
  576.      * Factory method to create layouts.
  577.      *
  578.      * This method is used for easy creation, since using {@link Image_Graph::}
  579.      * factory()} does not work with passing newly created objects from
  580.      * Image_Graph::factory() as reference, this is something that is
  581.      * fortunately fixed in PHP5. Also used for 'lazy including', i.e. loading
  582.      * only what is necessary, when it is necessary.
  583.      *
  584.      * Use {@link Image_Graph::horizontal()} or {@link Image_Graph::vertical()}
  585.      * instead for easier access.
  586.      *
  587.      * @param mixed               $layout     The type of layout, can be either 'Vertical'
  588.      *    or 'Horizontal' (case sensitive)
  589.      * @param Image_Graph_Element &$part1     The 1st part of the layout
  590.      * @param Image_Graph_Element &$part2     The 2nd part of the layout
  591.      * @param int                 $percentage The percentage of the layout to split at
  592.      *
  593.      * @return Image_Graph_Layout The newly created layout object
  594.      * @static
  595.      */
  596.     function &layoutFactory($layout&$part1&$part2$percentage = 50)
  597.     {
  598.         if (($layout != 'Vertical'&& ($layout != 'Horizontal')) {
  599.             return $this->_error('Layouts must be either \'Horizontal\' or \'Vertical\'');
  600.         }
  601.         
  602.         if (!(is_a($part1'Image_Graph_Element'))) {
  603.             return $this->_error('Part 1 is not a valid Image_Graph element');
  604.         }
  605.         
  606.         if (!(is_a($part2'Image_Graph_Element'))) {
  607.             return $this->_error('Part 2 is not a valid Image_Graph element');
  608.         }
  609.         
  610.         if ((!is_numeric($percentage)) || ($percentage < 0|| ($percentage > 100)) {
  611.             return $this->_error('Percentage has to be a number between 0 and 100');
  612.         }
  613.         
  614.         include_once "Image/Graph/Layout/$layout.php";
  615.         $class = "Image_Graph_Layout_$layout";
  616.         $obj = new $class($part1$part2$percentage);
  617.         return $obj;
  618.     }
  619.  
  620.     /**
  621.      * Factory method to create horizontal layout.
  622.      *
  623.      * See {@link Image_Graph::layoutFactory()}
  624.      *
  625.      * @param Image_Graph_Element &$part1     The 1st (left) part of the layout
  626.      * @param Image_Graph_Element &$part2     The 2nd (right) part of the layout
  627.      * @param int                 $percentage The percentage of the layout to split at
  628.      *    (percentage of total height from the left side)
  629.      *
  630.      * @return Image_Graph_Layout The newly created layout object
  631.      * @static
  632.      */
  633.     function &horizontal(&$part1&$part2$percentage = 50)
  634.     {
  635.         $obj =Image_Graph::layoutFactory('Horizontal'$part1$part2$percentage);
  636.         return $obj;
  637.     }
  638.  
  639.     /**
  640.      * Factory method to create vertical layout.
  641.      *
  642.      * See {@link Image_Graph::layoutFactory()}
  643.      *
  644.      * @param Image_Graph_Element &$part1     The 1st (top) part of the layout
  645.      * @param Image_Graph_Element &$part2     The 2nd (bottom) part of the layout
  646.      * @param int                 $percentage The percentage of the layout to split at
  647.      *    (percentage of total width from the top edge)
  648.      *
  649.      * @return Image_Graph_Layout The newly created layout object
  650.      * @static
  651.      */
  652.     function &vertical(&$part1&$part2$percentage = 50)
  653.     {
  654.         $obj =Image_Graph::layoutFactory('Vertical'$part1$part2$percentage);
  655.         return $obj;
  656.     }
  657.  
  658.     /**
  659.      * The error handling routine set by set_error_handler().
  660.      *
  661.      * This method is used internaly by Image_Graph and PHP as a proxy for {@link }
  662.      * Image_Graph::_error()}.
  663.      *
  664.      * @param string  $error_type    The type of error being handled.
  665.      * @param string  $error_msg     The error message being handled.
  666.      * @param string  $error_file    The file in which the error occurred.
  667.      * @param integer $error_line    The line in which the error occurred.
  668.      * @param string  $error_context The context in which the error occurred.
  669.      *
  670.      * @access private
  671.      * @return void 
  672.      */
  673.     function _default_error_handler($error_type$error_msg$error_file$error_line$error_context)
  674.     {
  675.         switch$error_type {
  676.         case E_ERROR:
  677.             $level 'error';
  678.             break;
  679.  
  680.         case E_USER_ERROR:
  681.             $level 'user error';
  682.             break;
  683.  
  684.         case E_WARNING:
  685.             $level 'warning';
  686.             break;
  687.  
  688.         case E_USER_WARNING:
  689.             $level 'user warning';
  690.             break;
  691.  
  692.         case E_NOTICE:
  693.             $level 'notice';
  694.             break;
  695.  
  696.         case E_USER_NOTICE:
  697.             $level 'user notice';
  698.             break;
  699.  
  700.         default:
  701.             $level '(unknown)';
  702.             break;
  703.  
  704.         }
  705.  
  706.         $this->_error(
  707.             "PHP $level$error_msg",
  708.             array(
  709.                 'type' => $error_type,
  710.                 'file' => $error_file,
  711.                 'line' => $error_line,
  712.                 'context' => $error_context
  713.             )
  714.         );
  715.     }
  716.  
  717.     /**
  718.      * Displays the errors on the error stack.
  719.      *
  720.      * Invoking this method cause all errors on the error stack to be displayed
  721.      * on the graph-output, by calling the {@link Image_Graph::_displayError()}
  722.      * method.
  723.      *
  724.      * @access private
  725.      * @return boolean 
  726.      */
  727.     function _displayErrors()
  728.     {
  729.         return true;
  730.     }
  731.  
  732.     /**
  733.      * Display an error from the error stack.
  734.      *
  735.      * This method writes error messages caught from the {@link Image_Graph::}
  736.      * _default_error_handler()} if {@Image_Graph::displayErrors()} was invoked,
  737.      * and the error explicitly set by the system using {@link }
  738.      * Image_Graph_Common::_error()}.
  739.      *
  740.      * @param int   $x     The horizontal position of the error message
  741.      * @param int   $y     The vertical position of the error message
  742.      * @param array $error The error context
  743.      *
  744.      * @access private
  745.      * @return void 
  746.      */
  747.     function _displayError($x$y$error)
  748.     {
  749.     }
  750.  
  751.     /**
  752.      * Outputs this graph using the canvas.
  753.      *
  754.      * This causes the graph to make all elements perform their output. Their
  755.      * result is 'written' to the output using the canvas, which also performs
  756.      * the actual output, fx. it being to a file or directly to the browser
  757.      * (in the latter case, the canvas will also make sure the correct HTTP
  758.      * headers are sent, making the browser handle the output correctly, if
  759.      * supported by it).
  760.      * 
  761.      * Parameters are the ones supported by the canvas, common ones are:
  762.      * 
  763.      * 'filename' To output to a file instead of browser
  764.      * 
  765.      * 'tohtml' Return a HTML string that encompasses the current graph/canvas - this
  766.      * implies an implicit save using the following parameters: 'filename' The "temporary"
  767.      * filename of the graph, 'filepath' A path in the file system where Image_Graph can
  768.      * store the output (this file must be in DOCUMENT_ROOT scope), 'urlpath' The URL that the
  769.      * 'filepath' corresponds to (i.e. filepath + filename must be reachable from a browser using
  770.      * urlpath + filename)
  771.      * 
  772.      * 'output' = 'none' Forces the graph to be drawn, but no output is sent to
  773.      * the browser. This allows working with an existing canvas after graphs has
  774.      * been drawn
  775.      * 
  776.      * @param mixed $param The output parameters to pass to the canvas
  777.      *
  778.      * @return bool Was the output 'good' (true) or 'bad' (false).
  779.      */
  780.     function done($param = false)
  781.     {
  782.         $result $this->_reset();
  783.         if (PEAR::isError($result)) {
  784.             return $result;
  785.         }
  786.         return $this->_done($param);
  787.     }   
  788.  
  789.     /**
  790.      * Outputs this graph using the canvas.
  791.      *
  792.      * This causes the graph to make all elements perform their output. Their
  793.      * result is 'written' to the output using the canvas, which also performs
  794.      * the actual output, fx. it being to a file or directly to the browser
  795.      * (in the latter case, the canvas will also make sure the correct HTTP
  796.      * headers are sent, making the browser handle the output correctly, if
  797.      * supported by it).
  798.      *
  799.      * @param mixed $param The output parameters to pass to the canvas
  800.      *
  801.      * @return bool Was the output 'good' (true) or 'bad' (false).
  802.      * @access private
  803.      */
  804.     function _done($param = false)
  805.     {
  806.         $timeStart $this->_getMicroTime();
  807.  
  808.         if ($this->_shadow{
  809.             $this->setPadding(20);
  810.             $this->_setCoords(
  811.                 $this->_left,
  812.                 $this->_top,
  813.                 $this->_right - 10,
  814.                 $this->_bottom - 10
  815.             );
  816.         }
  817.  
  818.         $result $this->_updateCoords();        
  819.         if (PEAR::isError($result)) {
  820.             return $result;
  821.         }
  822.  
  823.         if ($this->_getBackground()) {
  824.             $this->_canvas->rectangle(
  825.                 array(
  826.                     'x0' => $this->_left,
  827.                     'y0' => $this->_top,
  828.                     'x1' => $this->_right,
  829.                     'y1' => $this->_bottom
  830.                 )
  831.             );
  832.         }
  833.  
  834.         $result = parent::_done();
  835.         if (PEAR::isError($result)) {
  836.             return $result;
  837.         }
  838.  
  839.         if ($this->_displayErrors{
  840.             $this->_displayErrors();
  841.         }
  842.  
  843.         $timeEnd $this->_getMicroTime();
  844.  
  845.         if (($this->_showTime)
  846.             || ((isset($param['showtime'])) && ($param['showtime'=== true))
  847.         {
  848.             $text 'Generated in ' .
  849.                 sprintf('%0.3f'$timeEnd $timeStart' sec';
  850.             $this->write(
  851.                 $this->_right,
  852.                 $this->_bottom,
  853.                 $text,
  854.                 IMAGE_GRAPH_ALIGN_RIGHT + IMAGE_GRAPH_ALIGN_BOTTOM,
  855.                 array('color' => 'red')
  856.             );
  857.         }
  858.  
  859.         if ((!isset($param['output'])) || ($param['output'!== 'none')) {               
  860.             if (isset($param['filename'])) {
  861.                 if ((isset($param['tohtml'])) && ($param['tohtml'])) {
  862.                     return $this->_canvas->toHtml($param);
  863.                 else {
  864.                     return $this->_canvas->save($param);
  865.                 }
  866.             else {
  867.                 return $this->_canvas->show($param);
  868.             }
  869.         }
  870.     }
  871. }
  872.  
  873. ?>

Documentation generated on Tue, 05 Oct 2010 16:00:35 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.