linea21-externals
[ class tree: linea21-externals ] [ index: linea21-externals ] [ all elements ]

Source for file Label.class.php

Documentation is available at Label.class.php

  1. <?php
  2. /*
  3.  * This work is hereby released into the Public Domain.
  4.  * To view a copy of the public domain dedication,
  5.  * visit http://creativecommons.org/licenses/publicdomain/ or send a letter to
  6.  * Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
  7.  *
  8.  */
  9.  
  10. require_once dirname(__FILE__)."/../Graph.class.php";
  11.  
  12.  
  13. /* <php4> */
  14.  
  15. define("LABEL_LEFT"1);
  16. define("LABEL_RIGHT"2);
  17. define("LABEL_CENTER"3);
  18. define("LABEL_TOP"4);
  19. define("LABEL_BOTTOM"5);
  20. define("LABEL_MIDDLE"6);
  21.  
  22. /* </php4> */
  23.  
  24. /**
  25.  * Draw labels
  26.  *
  27.  * @package linea21.externals
  28.  * @subpackage artichow
  29.  */
  30. class awLabel implements awPositionable {
  31.  
  32.     /**
  33.      * Label border
  34.      *
  35.      * @var int 
  36.      */
  37.     public $border;
  38.  
  39.     /**
  40.      * Label texts
  41.      *
  42.      * @var array 
  43.      */
  44.     protected $texts;
  45.  
  46.     /**
  47.      * Text font
  48.      *
  49.      * @var int 
  50.      */
  51.     protected $font;
  52.  
  53.     /**
  54.      * Text angle
  55.      *
  56.      * @var int 
  57.      */
  58.     protected $angle = 0;
  59.  
  60.     /**
  61.      * Text color
  62.      *
  63.      * @var Color 
  64.      */
  65.     protected $color;
  66.  
  67.     /**
  68.      * Text background
  69.      *
  70.      * @var Color, Gradient
  71.      */
  72.     private $background;
  73.  
  74.     /**
  75.      * Callback function
  76.      *
  77.      * @var string 
  78.      */
  79.     private $function;
  80.  
  81.     /**
  82.      * Padding
  83.      *
  84.      * @var int 
  85.      */
  86.     private $padding;
  87.  
  88.     /**
  89.      * Move position from this vector
  90.      *
  91.      * @var Point 
  92.      */
  93.     protected $move;
  94.  
  95.     /**
  96.      * Label interval
  97.      *
  98.      * @var int 
  99.      */
  100.     protected $interval = 1;
  101.  
  102.     /**
  103.      * Horizontal align
  104.      *
  105.      * @var int 
  106.      */
  107.     protected $hAlign = awLabel::CENTER;
  108.  
  109.     /**
  110.      * Vertical align
  111.      *
  112.      * @var int 
  113.      */
  114.     protected $vAlign = awLabel::MIDDLE;
  115.     
  116.     /**
  117.      * Hide all labels ?
  118.      *
  119.      * @var bool 
  120.      */
  121.     protected $hide = FALSE;
  122.     
  123.     /**
  124.      * Keys to hide
  125.      *
  126.      * @var array 
  127.      */
  128.     protected $hideKey = array();
  129.     
  130.     /**
  131.      * Values to hide
  132.      *
  133.      * @var array 
  134.      */
  135.     protected $hideValue = array();
  136.     
  137.     /**
  138.      * Hide first label
  139.      *
  140.      * @var bool 
  141.      */
  142.     protected $hideFirst = FALSE;
  143.     
  144.     /**
  145.      * Hide last label
  146.      *
  147.      * @var bool 
  148.      */
  149.     protected $hideLast = FALSE;
  150.     
  151.     /**
  152.      * Build the label
  153.      *
  154.      * @param string $label First label
  155.      */
  156.     public function __construct($label NULL$font NULL$color NULL$angle 0{
  157.     
  158.         if(is_array($label)) {
  159.             $this->set($label);
  160.         else if(is_string($label)) {
  161.             $this->set(array($label));
  162.         }
  163.         
  164.         if($font === NULL{
  165.             $font new awFont2;
  166.         }
  167.         
  168.         $this->setFont($font);
  169.         $this->setAngle($angle);
  170.         
  171.         if($color instanceof awColor{
  172.             $this->setColor($color);
  173.         else {
  174.             $this->setColor(new awColor(000));
  175.         }
  176.         
  177.         $this->move = new awPoint(00);
  178.         
  179.         $this->border = new awBorder;
  180.         $this->border->hide();
  181.         
  182.     }
  183.     
  184.     /**
  185.      * Get an element of the label from its key
  186.      *
  187.      * @param int $key Element key
  188.      * @return string A value
  189.      */
  190.     public function get($key{
  191.         return array_key_exists($key$this->texts$this->texts[$keyNULL;
  192.     }
  193.     
  194.     /**
  195.      * Get all labels
  196.      *
  197.      * @return array 
  198.      */
  199.     public function all({
  200.         return $this->texts;
  201.     }
  202.     
  203.     /**
  204.      * Set one or several labels
  205.      *
  206.      * @param array $labels Array of string or a string
  207.      */
  208.     public function set($labels{
  209.     
  210.         if(is_array($labels)) {
  211.             $this->texts = $labels;
  212.         else {
  213.             $this->texts = array((string)$labels);
  214.         }
  215.         
  216.     }
  217.     
  218.     /**
  219.      * Count number of texts in the label
  220.      *
  221.      * @return int 
  222.      */
  223.     public function count({
  224.         return is_array($this->textscount($this->texts0;
  225.     }
  226.     
  227.     /**
  228.      * Set a callback function for labels
  229.      *
  230.      * @param string $function 
  231.      */
  232.     public function setCallbackFunction($function{
  233.         $this->function is_null($function$function : (string)$function;
  234.     }
  235.     
  236.     /**
  237.      * Return the callback function for labels
  238.      *
  239.      * @return string 
  240.      */
  241.     public function getCallbackFunction({
  242.         return $this->function;
  243.     }
  244.     
  245.     /**
  246.      * Change labels format
  247.      *
  248.      * @param string $format New format (printf style: %.2f for example)
  249.      */
  250.     public function setFormat($format{
  251.         $function 'label'.time().'_'.(microtime(1000000);
  252.         eval('function '.$function.'($value) {
  253.             return sprintf("'.addcslashes($format'"').'", $value);
  254.         }');
  255.         $this->setCallbackFunction($function);
  256.     }
  257.     
  258.     /**
  259.      * Change font for label
  260.      *
  261.      * @param awFont $font New font
  262.      * @param awColor $color Font color (can be NULL)
  263.      */
  264.     public function setFont(awFont $font$color NULL{
  265.         $this->font = $font;
  266.         if($color instanceof awColor{
  267.             $this->setColor($color);
  268.         }
  269.     }
  270.     
  271.     /**
  272.      * Change font angle
  273.      *
  274.      * @param int $angle New angle
  275.      */
  276.     public function setAngle($angle{
  277.         $this->angle = (int)$angle;
  278.     }
  279.     
  280.     /**
  281.      * Change font color
  282.      *
  283.      * @param awColor $color 
  284.      */
  285.     public function setColor(awColor $color{
  286.         $this->color = $color;
  287.     }
  288.     
  289.     /**
  290.      * Change text background
  291.      *
  292.      * @param mixed $background 
  293.      */
  294.     public function setBackground($background{
  295.         $this->background $background;
  296.     }
  297.     
  298.     /**
  299.      * Change text background color
  300.      *
  301.      * @param Color 
  302.      */
  303.     public function setBackgroundColor(awColor $color{
  304.         $this->background $color;
  305.     }
  306.     
  307.     /**
  308.      * Change text background gradient
  309.      *
  310.      * @param Gradient 
  311.      */
  312.     public function setBackgroundGradient(awGradient $gradient{
  313.         $this->background $gradient;
  314.     }
  315.  
  316.     /**
  317.      * Change padding
  318.      *
  319.      * @param int $left Left padding
  320.      * @param int $right Right padding
  321.      * @param int $top Top padding
  322.      * @param int $bottom Bottom padding
  323.      */
  324.     public function setPadding($left$right$top$bottom{
  325.         $this->padding array((int)$left(int)$right(int)$top(int)$bottom);
  326.     }
  327.     
  328.     /**
  329.      * Hide all labels ?
  330.      *
  331.      * @param bool $hide 
  332.      */
  333.     public function hide($hide TRUE{
  334.         $this->hide = (bool)$hide;
  335.     }
  336.     
  337.     /**
  338.      * Show all labels ?
  339.      *
  340.      * @param bool $show 
  341.      */
  342.     public function show($show TRUE{
  343.         $this->hide = (bool)!$show;
  344.     }
  345.     
  346.     /**
  347.      * Hide a key
  348.      *
  349.      * @param int $key The key to hide
  350.      */
  351.     public function hideKey($key{
  352.         $this->hideKey[$keyTRUE;
  353.     }
  354.     
  355.     /**
  356.      * Hide a value
  357.      *
  358.      * @param int $value The value to hide
  359.      */
  360.     public function hideValue($value{
  361.         $this->hideValue[$value;
  362.     }
  363.     
  364.     /**
  365.      * Hide first label
  366.      *
  367.      * @param bool $hide 
  368.      */
  369.     public function hideFirst($hide{
  370.         $this->hideFirst = (bool)$hide;
  371.     }
  372.     
  373.     /**
  374.      * Hide last label
  375.      *
  376.      * @param bool $hide 
  377.      */
  378.     public function hideLast($hide{
  379.         $this->hideLast = (bool)$hide;
  380.     }
  381.     
  382.     /**
  383.      * Set label interval
  384.      *
  385.      * @param int 
  386.      */
  387.     public function setInterval($interval{
  388.     
  389.         $this->interval = (int)$interval;
  390.         
  391.     }
  392.     
  393.     /**
  394.      * Change label position
  395.      *
  396.      * @param int $x Add this interval to X coord
  397.      * @param int $y Add this interval to Y coord
  398.      */
  399.     public function move($x$y{
  400.     
  401.         $this->move = $this->move->move($x$y);
  402.     
  403.     }
  404.     
  405.     /**
  406.      * Change alignment
  407.      *
  408.      * @param int $h Horizontal alignment
  409.      * @param int $v Vertical alignment
  410.      */
  411.     public function setAlign($h NULL$v NULL{
  412.         if($h !== NULL{
  413.             $this->hAlign = (int)$h;
  414.         }
  415.         if($v !== NULL{
  416.             $this->vAlign = (int)$v;
  417.         }
  418.     }
  419.     
  420.     /**
  421.      * Get a text from the labele
  422.      *
  423.      * @param mixed $key Key in the array text
  424.      * @return Text 
  425.      */
  426.     public function getText($key{
  427.     
  428.         if(is_array($this->textsand array_key_exists($key$this->texts)) {
  429.         
  430.             $value $this->texts[$key];
  431.             
  432.             if(is_string($this->function)) {
  433.                 $value call_user_func($this->function$value);
  434.             }
  435.         
  436.             $text new awText($value);
  437.             $text->setFont($this->font);
  438.             $text->setAngle($this->angle);
  439.             $text->setColor($this->color);
  440.             
  441.             if($this->background instanceof awColor{
  442.                 $text->setBackgroundColor($this->background);
  443.             else if($this->background instanceof awGradient{
  444.                 $text->setBackgroundGradient($this->background);
  445.             }
  446.             
  447.             $text->border $this->border;
  448.             
  449.             if($this->padding !== NULL{
  450.                 call_user_func_array(array($text'setPadding')$this->padding);
  451.             }
  452.             
  453.             return $text;
  454.             
  455.         else {
  456.             return NULL;
  457.         }
  458.     
  459.     }
  460.     
  461.     /**
  462.      * Get max width of all texts
  463.      *
  464.      * @param awDriver $driver A driver
  465.      * @return int 
  466.      */
  467.     public function getMaxWidth(awDriver $driver{
  468.     
  469.         return $this->getMax($driver'getTextWidth');
  470.     
  471.     }
  472.     
  473.     /**
  474.      * Get max height of all texts
  475.      *
  476.      * @param awDriver $driver A driver
  477.      * @return int 
  478.      */
  479.     public function getMaxHeight(awDriver $driver{
  480.     
  481.         return $this->getMax($driver'getTextHeight');
  482.         
  483.     }
  484.     
  485.     /**
  486.      * Draw the label
  487.      *
  488.      * @param awDriver $driver 
  489.      * @param awPoint $p Label center
  490.      * @param int $key Text position in the array of texts (default to zero)
  491.      */
  492.     public function draw(awDriver $driverawPoint $p$key 0{
  493.     
  494.         if(($key $this->interval!== 0{
  495.             return;
  496.         }
  497.     
  498.         // Hide all labels
  499.         if($this->hide{
  500.             return;
  501.         }
  502.         
  503.         // Key is hidden
  504.         if(array_key_exists($key$this->hideKey)) {
  505.             return;
  506.         }
  507.         
  508.         // Hide first label
  509.         if($key === and $this->hideFirst{
  510.             return;
  511.         }
  512.         
  513.         // Hide last label
  514.         if($key === count($this->textsand $this->hideLast{
  515.             return;
  516.         }
  517.     
  518.         $text $this->getText($key);
  519.         
  520.         if($text !== NULL{
  521.         
  522.             // Value must be hidden
  523.             if(in_array($text->getText()$this->hideValue)) {
  524.                 return;
  525.             }
  526.         
  527.             $x $p->x;
  528.             $y $p->y;
  529.             
  530.             // Get padding
  531.             list($left$right$top$bottom$text->getPadding();
  532.             
  533. //            $font = $text->getFont();
  534.             $width $driver->getTextWidth($text);
  535.             $height $driver->getTextHeight($text);
  536.             
  537.             switch($this->hAlign{
  538.             
  539.                 case awLabel::RIGHT :
  540.                     $x -= ($width $right);
  541.                     break;
  542.             
  543.                 case awLabel::CENTER :
  544.                     $x -= ($width $left $right2;
  545.                     break;
  546.             
  547.                 case awLabel::LEFT :
  548.                     $x += $left;
  549.                     break;
  550.             
  551.             }
  552.             
  553.             switch($this->vAlign{
  554.             
  555.                 case awLabel::TOP :
  556.                     $y -= ($height $bottom);
  557.                     break;
  558.             
  559.                 case awLabel::MIDDLE :
  560.                     $y -= ($height $top $bottom2;
  561.                     break;
  562.             
  563.                 case awLabel::BOTTOM :
  564.                     $y += $top;
  565.                     break;
  566.             
  567.             }
  568.         
  569.             $driver->string($text$this->move->move($x$y));
  570.             
  571.         }
  572.         
  573.     }
  574.     
  575.     protected function getMax(awDriver $driver$function{
  576.     
  577.         $max NULL;
  578.     
  579.         foreach($this->texts as $key => $text{
  580.         
  581.             $text $this->getText($key);
  582.             $font $text->getFont();
  583.         
  584.             if(is_null($max)) {
  585.                 $max $font->{$function}($text);
  586.             else {
  587.                 $max max($max$font->{$function}($text));
  588.             }
  589.         
  590.         }
  591.         
  592.         return $max;
  593.         
  594.     }
  595.  
  596. }
  597.  
  598. registerClass('Label');
  599. ?>

Documentation generated on Fri, 16 Oct 2009 09:34:24 +0200 by phpDocumentor 1.4.1