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

Source for file Rectangle.php

Documentation is available at Rectangle.php

  1. <?php
  2.  
  3. /*
  4. **************************************************
  5. Class: Rectangle.php
  6. **************************************************
  7. Author: Tsiavos Chris <jaames@freemail.gr>
  8. Date: October 2004
  9. **************************************************/
  10.  
  11. /**
  12. *Includes the Shape abstract class
  13. */
  14. require_once("Shape.php");
  15.  
  16. /**
  17. *Wrapper around gd's Image(Filled)Rectangle functions
  18. *Class used to draw rectangles in the chart image
  19. *This class is utilized by:
  20. *<ul>
  21. *<li>Chart class for drawing the legend rectangles</li>
  22. *<li>BarChart class for drawing the chart bars</li>
  23. *</ul>
  24. *@author Tsiavos Chris <jaames@freemail.gr>
  25. *@license http://opensource.org/licenses/gpl-license.php GNU Public License
  26. */
  27.  
  28. class Rectangle extends Shape {
  29. /**
  30. *x value of the upper left coordinate of the rectangle
  31. *@access private
  32. *@var integer
  33. */
  34. private $X_Start;
  35. /**
  36. *x value of the bottom right coordinate of the rectangle
  37. *@access private
  38. *@var integer
  39. */
  40. private $X_Finish;
  41. /**
  42. *y value of the upper left coordinate of the rectangle
  43. *@access private
  44. *@var integer
  45. */
  46. private $Y_Start;
  47. /**
  48. *y value of the bottom right coordinate of the rectangle
  49. *@access private
  50. *@var integer
  51. */
  52. private $Y_Finish;
  53. /**
  54. *the caption of the rectangle. (This property is mostly used for the status-indication
  55. *property of the chart)
  56. *@access private
  57. *@var string
  58. */
  59. private $Caption;
  60. /**
  61. *the color of the rectangle's caption
  62. *@access private
  63. *@var string
  64. */
  65. private $CaptionColor;
  66. /**
  67. *the font instance used for drawing the Rectangle's caption
  68. *@access private
  69. *@var Font
  70. */
  71. private $Font;
  72. /**
  73. *Constructor
  74. *@param mixed $Canvas reference to the image identifier the rectangle will be drawn in
  75. *@param ColorAllocator $ColorAllocator reference to the ColorAllocator the class will use for allocating the rectangle color
  76. *@param string $UseAntialias whether antialias functions should be used or not when drawing the rectangle
  77. *@param integer $X_Start x value of the upper left coordinate of the rectangle
  78. *@param integer $Y_Start y value of the upper left coordinate of the rectangle
  79. *@param integer $X_Finish x value of the bottom right coordinate of the rectangle
  80. *@param integer $Y_Finish y value of the bottom right coordinate of the rectangle
  81. *@param string $Caption the caption of the rectangle
  82. */
  83. function __construct(&$Canvas,ColorAllocator &$ColorAllocator,$UseAntialias,$X_Start,$Y_Start,$X_Finish,$Y_Finish) {
  84. Shape::__construct($Canvas,$ColorAllocator,$UseAntialias);
  85. $this->X_Start=$X_Start;
  86. $this->Y_Start=$Y_Start;
  87. $this->X_Finish=$X_Finish;
  88. $this->Y_Finish=$Y_Finish;
  89. }
  90. /**
  91. *Specifies the caption properties of the rectangle
  92. *@access public
  93. *@return void
  94. *@param Font &$Font
  95. *@param string $Caption
  96. *@param string $CaptionColor
  97. */
  98. public function set_Caption(Font &$Font,$Caption,$CaptionColor) {
  99. $this->Font=$Font;
  100. $this->Caption=$Caption;
  101. $this->CaptionColor=$CaptionColor;
  102. }
  103. /**
  104. *Draws the border of the rectangle
  105. *@param string $BorderColor
  106. *@param integer $Alpha the alpha value of the border color
  107. *@return void
  108. *@access public
  109. */
  110. public function draw($BorderColor,$Alpha) {
  111. if ($this->UseAntialias=="Yes")
  112. ImageAntialias($this->Canvas,1);
  113. $this->draw_Caption();
  114. $ColorRange=$this->Y_Finish-$this->Y_Start;
  115. $ColorHandler=$this->ColorAllocator->Allocate($this->Canvas,$BorderColor,$BorderColor,$Alpha,$ColorRange);
  116. ImageRectangle($this->Canvas,$this->X_Start,$this->Y_Start,$this->X_Finish,$this->Y_Finish,$ColorHandler);
  117. if ($this->UseAntialias=="Yes")
  118. ImageAntialias($this->Canvas,0);
  119. }
  120.  
  121. /**
  122. *Draws a filled rectangle
  123. *@param string $StartColor specifies the starting color of the filled rectangle
  124. *@param string $FinishColor specifies the finish color of the filled rectangle
  125. *IIf the $StartColor and $FinishColor are different then the rectangle will be filled
  126. *wwith gradient color
  127. *@param integer $Alpha the alpha value of the rectangle's color
  128. *@return void
  129. *@access public
  130. */
  131. public function draw_Filled($StartColor,$FinishColor,$Alpha) {
  132. $this->draw_Caption();
  133. $ColorRange=abs($this->Y_Finish-$this->Y_Start);
  134. $ColorHandler=$this->ColorAllocator->Allocate($this->Canvas,$StartColor,$FinishColor,$Alpha,$ColorRange);
  135. if (is_array($ColorHandler))
  136. $this->draw_GradientRectangle($ColorHandler);
  137. else
  138. $this->draw_NormalRectangle($ColorHandler);
  139. }
  140. /**
  141. *Draws a uniform color filled rectangle
  142. *@param mixed $ColorHandler the color indentifier which will be used to fill the rectangle
  143. *@return void
  144. *@access private
  145. */
  146. private function draw_NormalRectangle($ColorHandler) {
  147. ImageFilledRectangle($this->Canvas,$this->X_Start,$this->Y_Start,$this->X_Finish,$this->Y_Finish,$ColorHandler);
  148. }
  149. /**
  150. *Draws a gradient color filled rectangle
  151. *@param mixed $ColorHandler the color indentifier which will be used to fill the rectangle
  152. *@return void
  153. *@access private
  154. */
  155. private function draw_GradientRectangle($ColorHandler) {
  156. for ($i=0;$i<abs($this->Y_Finish-$this->Y_Start);$i++)
  157. ImageLine($this->Canvas,$this->X_Start,$this->Y_Start+$i,$this->X_Finish,$this->Y_Start+$i,$ColorHandler[$i]);
  158. }
  159. /**
  160. *Draws the rectangle's caption
  161. *@access private
  162. *@return void
  163. */
  164. private function draw_Caption() {
  165. if (!($this->Font instanceof Font)) return;
  166. $FontMetrics=$this->Font->get_FontMetrics($this->Caption);
  167. $CenterPos=(int)(abs($this->X_Finish-$this->X_Start)-$FontMetrics["FontWidth"])/2;
  168. $Caption_XPos=$this->X_Start+$CenterPos;
  169. $Caption_YPos=$this->Y_Start-$FontMetrics["FontHeight"]-5;
  170. $this->Font->draw_String($Caption_XPos,$Caption_YPos,$this->Caption,$this->CaptionColor);
  171. }
  172. }
  173.  
  174. ?>

Documentation generated on Sun, 3 Oct 2004 14:59:32 +0300 by phpDocumentor 1.3.0RC3