Source for file Curve.php

Documentation is available at Curve.php

  1. <?php
  2. /**
  3.  * $Source: /cvsroot/svggrapher/svggrapher/mains/Curve.php,v $
  4.  *
  5.  * @version $Id: Curve.php,v 1.7 2007/10/23 09:22:53 geelweb Exp $
  6.  * @author Guillaume L. <guillaume@geelweb.org>
  7.  * @copyright Copyright (c) 2006, Guillaume Luchet.
  8.  * @license http://opensource.org/licenses/bsd-license.php BSD License
  9.  * @package SvgGrapher
  10.  */
  11.  
  12. // consts {{{
  13.  
  14. /** 
  15.  * define the default curve style
  16.  */
  17. if(!defined('DEFAULT_CURVE_STYLE')) {
  18.     define('DEFAULT_CURVE_STYLE''stroke-width:2;visibility:visible;');
  19. }
  20.  
  21. // }}}
  22. // requirements {{{
  23.  
  24. /**
  25.  * include the plugin manager
  26.  */
  27. require_once SVGGRAPHER_CLASS_PATH '/mains/PluginManager.php';
  28.  
  29. /**
  30.  * include the svg style element
  31.  */
  32. require_once SVGGRAPHER_CLASS_PATH '/mains/SvgStyle.php';
  33.  
  34. // }}}
  35.  
  36. /**
  37.  * class Curve
  38.  *
  39.  * @author Guillaume L. <guillaume@geelweb.org>
  40.  * @link http://www.geelweb.org
  41.  * @license http://opensource.org/licenses/bsd-license.php BSD License
  42.  * @copyright Copyright (c) 2006, Guillaume Luchet.
  43.  * @version CVS: $Id: Curve.php,v 1.7 2007/10/23 09:22:53 geelweb Exp $
  44.  * @package SvgGrapher
  45.  */
  46. class Curve 
  47. {
  48.     // properties {{{
  49.  
  50.     /** 
  51.      * The Curve id.
  52.      * @var string 
  53.      * @access public
  54.      */
  55.     public $id;
  56.  
  57.     /**
  58.      * Abscisse data.
  59.      * @var array 
  60.      * @access public
  61.      */
  62.     public $labels;
  63.  
  64.     /**
  65.      * Ordinate data.
  66.      * @var array 
  67.      * @access public
  68.      */
  69.     public $values;
  70.  
  71.     /**
  72.      * Curve style
  73.      * @var string or SvgStyle
  74.      * @access public
  75.      */
  76.     protected $style = NULL;
  77.  
  78.     /**
  79.      * Curve color
  80.      *
  81.      * color of the curve, string or array of string if the curve have many
  82.      * colors.
  83.      *
  84.      * @var mixed 
  85.      * @access public
  86.      */
  87.     protected $color = NULL;
  88.  
  89.     /**
  90.      * Curve legend.
  91.      *
  92.      * legend of the curve, string or array of string if the curve have many
  93.      * legends.
  94.      *
  95.      * @var mixed 
  96.      * @access public
  97.      */
  98.     public $legend;
  99.  
  100.     /**
  101.      * Curve visual.
  102.      *
  103.      * name of the visual plugin to use to render the curve.
  104.      *
  105.      * @var string 
  106.      * @access public
  107.      */
  108.     public $visual = 'polyline';
  109.  
  110.     /**
  111.      * Curve filters
  112.      * @var mixed 
  113.      * @access public
  114.      */
  115.     private $_curveFilters = array();
  116.  
  117.     /**
  118.      * Curve marker
  119.      * @var array 
  120.      * @access public
  121.      */
  122.     private $_curveMarker = array();
  123.  
  124.     // }}}
  125.     // Curve::__construct() {{{
  126.     
  127.     /**
  128.      * __construct
  129.      *
  130.      * Initialize the object with the params.
  131.      *
  132.      * the key of the array params are:
  133.      * - labels : curve abscisse data (array)
  134.      * - values : curve ordinate data (array)
  135.      * - style : curve style (string)
  136.      * - color : curve color (string or array of string)
  137.      * - legend : curve legend (string or array of string)
  138.      * - visual : curve visual (string)
  139.      * - filter : plugin filter name (string or array of string)
  140.      * - marker : plugin marker params (array) see Curve::setMarker() to params
  141.      * keys
  142.      * 
  143.      * @param array $params key / value array params
  144.      * @param int $id curve id
  145.      * @access public
  146.      * @return void 
  147.      */
  148.     public function __construct($params=array()$id=0
  149.     {
  150.         $this->id = $id;
  151.  
  152.         if(isset($params['labels'])) {
  153.             $this->labels = $params['labels'];
  154.         }
  155.         if(isset($params['values'])) {
  156.             $this->values = $params['values'];
  157.         }
  158.         if(isset($params['legend'])) {
  159.             $this->legend = $params['legend'];
  160.         }
  161.         if(isset($params['visual'])) {
  162.             $this->visual = $params['visual'];
  163.         }
  164.         if(isset($params['filter'])) {
  165.             $this->setFilter($params['filter']);
  166.         }
  167.         if(isset($params['marker'])) {
  168.             $this->setMarker($params['marker']);
  169.         }
  170.         $this->style = isset($params['style']
  171.             $params['style'DEFAULT_CURVE_STYLE;
  172.         
  173.         // @TODO arange this
  174.         if(isset($params['color'])) {
  175.             $this->setColor($params['color']);
  176.         else {
  177.             if(is_array($this->legend)) {
  178.                 $this->color = array();
  179.                 foreach($this->legend as $key=>$text{
  180.                     if(!isset($this->color[$key])) {
  181.                         $this->color[$keysvgGrapher_internal_randomColor();
  182.                     }
  183.                 }
  184.             else {
  185.                 $this->setColor(svgGrapher_internal_randomColor());
  186.             }
  187.         }
  188.     }
  189.     
  190.     // }}}
  191.     // Curve::setFilter() {{{
  192.  
  193.     /**
  194.      * setFilter
  195.      *
  196.      * Add filters to the curve calling the according filter plugin.
  197.      * 
  198.      * @param mixed $filters filter plugin name, array of string to set many
  199.      *  filters
  200.      * @access public
  201.      * @return void 
  202.      */
  203.     public function setFilter($filters
  204.     {
  205.         $filters is_array($filters$filters array($filters);
  206.         foreach($filters as $filter{
  207.             PluginManager::execute('filter'$filter$this->id);
  208.             $this->style .= sprintf('filter:url(#%s);'$filter);
  209.             $this->_curveFilters[$filter;
  210.         }
  211.     }
  212.  
  213.     // }}}
  214.     // Curve::setMarker() {{{
  215.  
  216.     /**
  217.      * Set the curve Markers
  218.      * 
  219.      * Add a marker to the curve calling the according marker plugin.
  220.      *
  221.      * marker params:
  222.      * - plugin : plugin to use (string)
  223.      * - color : marker color
  224.      * - id : marker id
  225.      *
  226.      * @param mixed $marker marker plugin name, array of marker params array to
  227.      *  set many markers
  228.      * @access public
  229.      * @return void 
  230.      */
  231.     public function setMarker($marker
  232.     {
  233.         $marker is_array($marker$marker['plugin'$marker;
  234.         $color = isset($marker['color']$marker['color'$this->color;
  235.         
  236.         $markerId = isset($marker['id']$marker['id'NULL;
  237.         if(is_null($markerId)) {
  238.             $markerId 'markerCurve_' $markerId '_' $this->id;
  239.         }
  240.  
  241.         PluginManager::execute('marker'$marker$markerId$color);
  242.         $this->style .= sprintf('marker-end:url(#%s);'$markerId);
  243.         $this->_curveMarker = $marker;
  244.     }
  245.  
  246.     // }}}
  247.     // Curve::setColor() {{{
  248.  
  249.     /**
  250.      * setColor
  251.      *
  252.      * set the curve color, if $color is string (when the curve use only on
  253.      * color) the color is add to the style attributes (stroke and fill)
  254.      * 
  255.      * @param mixed $color 
  256.      * @access public
  257.      * @return void 
  258.      */
  259.     public function setColor($color
  260.     {
  261.         if(is_string($color)) {
  262.             $style new SvgStyle($this->style);
  263.             $style->setAttribute('stroke'$color);
  264.             $style->setAttribute('fill'$color);
  265.             $this->style = $style->toString();
  266.         }
  267.         $this->color = $color;
  268.     }
  269.  
  270.     // }}}
  271.     // Curve::__get() {{{
  272.  
  273.     /**
  274.      * __get
  275.      *
  276.      * magic method to the properties who needs specials actions (like filter,
  277.      * marker or color).
  278.      *
  279.      * @param string $property 
  280.      * @access public
  281.      * @return mixed 
  282.      */
  283.     public function __get($property
  284.     {
  285.         $getter 'get' $property;
  286.         if(method_exists($this$getter)) {
  287.             return $this->$getter();
  288.         }
  289.         return $this->$property;
  290.     }
  291.  
  292.     // }}}
  293.     // Curve::__set() {{{
  294.  
  295.     /**
  296.      * __set
  297.      *
  298.      * magic method to the properties who needs specials action (filters, color
  299.      * or marker)
  300.      *
  301.      * @param string $property 
  302.      * @param mixed $value 
  303.      * @access public
  304.      * @return mixed 
  305.      */
  306.     public function __set($property$value
  307.     {
  308.         $setter 'set' $property;
  309.         if(method_exists($this$setter)) {
  310.             return $this->$setter($value);
  311.         }
  312.         return $this->$property $value;
  313.     }
  314.  
  315.     // }}}
  316.     // Curve::render() {{{
  317.     
  318.     /**
  319.      * render
  320.      *
  321.      * Render the curve using the PluginManager
  322.      * 
  323.      * @access public
  324.      * @return void 
  325.      */
  326.     public function render(
  327.     {
  328.         PluginManager::execute('visual'$this->visual$this->id);
  329.     }
  330.     
  331.     // }}}
  332. }
  333.  
  334. ?>

Documentation generated on Tue, 23 Oct 2007 11:31:54 +0200 by phpDocumentor 1.4.0