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

Source for file Polygon.php

Documentation is available at Polygon.php

  1. <?php
  2.  
  3. /*
  4. **************************************************
  5. Class: Polygon.php
  6. **************************************************
  7. Author: Tsiavos Chris <jaames@freemail.gr>
  8. Date: October 2004
  9. **************************************************/
  10.  
  11. /**
  12. *Includes the abstract Shape class
  13. */
  14. require_once("Shape.php");
  15.  
  16. /**
  17. *Wrapper around gd's Image(Filled)Polygon functions
  18. *Class used to draw polygons in the chart image
  19. *Utilized by AreaChart
  20. *@author Tsiavos Chris <jaames@freemail.gr>
  21. *@license http://opensource.org/licenses/gpl-license.php GNU Public License
  22. */
  23.  
  24. class Polygon extends Shape {
  25. /**
  26. *the polygon's vertices
  27. *@access private
  28. *@var integer
  29. */
  30. private $Vertices=array();
  31. /**
  32. *reference to the font instance used by Polygon::draw_Caption()
  33. *@access private
  34. *@var Font
  35. */
  36. private $Font;
  37. /**
  38. *Array holding the caption of each of the polygon's vertices
  39. *@access private
  40. *@var private
  41. */
  42. private $Caption=array();
  43. /**
  44. *the font color used to draw the caption of the polygon's vertices
  45. *@access private
  46. *@var private
  47. */
  48. private $CaptionColor;
  49. /**
  50. *Constructor
  51. *@param mixed &$Canvas Reference to the image handler the font will be used in
  52. *@param ColorAllocator Reference to the ColorAllocator the class will use for allocating
  53. *tthe font color
  54. *@param string ("Yes","No") Specifies if antialias functions should be used or not when drawing
  55. *tthe polygon
  56. */
  57. function __construct(&$Canvas,ColorAllocator &$ColorAllocator,$UseAntialias) {
  58. Shape::__construct($Canvas,$ColorAllocator,$UseAntialias);
  59. }
  60. /**
  61. *Adds a vertex to the polygon
  62. *@access public
  63. *@return void
  64. */
  65. public function add_Vertex($Vertex) {
  66. array_push($this->Vertices,$Vertex);
  67. }
  68. /**
  69. *Returns the polygon's vertices
  70. *@access public
  71. *@return string[]
  72. */
  73. public function get_Vertices() {
  74. return $this->Vertices;
  75. }
  76. /**
  77. *Specifies the font properties used to draw the captions of the polygon's vertices
  78. *@access public
  79. *@return void
  80. *@param Font &$Font
  81. *@param string $CaptionColor
  82. */
  83. public function set_CaptionProperties(Font &$Font,$CaptionColor) {
  84. $this->Font=$Font;
  85. $this->CaptionColor=$CaptionColor;
  86. }
  87. /**
  88. *Sets the caption for a vertex
  89. *@access public
  90. *@return void
  91. */
  92. public function set_CaptionVertex($Caption) {
  93. array_push($this->Caption,$Caption);
  94. }
  95. /**
  96. *Draws the caption for each vertex of the polygon
  97. *@access private
  98. *@return void
  99. */
  100. private function draw_Caption() {
  101. if (!($this->Font instanceof Font)) return;
  102. for ($vertex=0;$vertex<count($this->Vertices)-2;$vertex++) {
  103. $FontMetrics=$this->Font->get_FontMetrics($this->Caption[$vertex]);
  104. $VertexPos=explode(",",$this->Vertices[$vertex]);
  105. $CaptionXPos=$VertexPos[0]-($FontMetrics["FontWidth"]/2);
  106. $CaptionYPos=$VertexPos[1];
  107. $ZeroPos=explode(",",$this->Vertices[count($this->Vertices)-1]);
  108. if ($CaptionYPos<$ZeroPos[1])
  109. $CaptionYPos=$VertexPos[1]-$FontMetrics["FontHeight"]-2;
  110. else
  111. $CaptionYPos=$VertexPos[1]+2;
  112. $this->Font->draw_String($CaptionXPos,$CaptionYPos,$this->Caption[$vertex],$this->CaptionColor);
  113. }
  114. }
  115. /**
  116. *Draws the border of the polygon
  117. *@access public
  118. *@return void
  119. *@param string $BorderColor
  120. *@param integer 0-127 $Alpha
  121. */
  122. public function draw($BorderColor,$Alpha) {
  123. if ($this->UseAntialias=="Yes")
  124. ImageAntialias($this->Canvas,1);
  125. $ProperVertices=array();
  126. for ($vertex=0;$vertex<count($this->Vertices);$vertex++) {
  127. $Vertex=explode(",",$this->Vertices[$vertex]);
  128. array_push($ProperVertices,$Vertex[0]);
  129. array_push($ProperVertices,$Vertex[1]);
  130. }
  131. ImagePolygon($this->Canvas,$ProperVertices,count($this->Vertices),$ColorHandler);
  132. if ($this->UseAntialias=="Yes")
  133. ImageAntialias($this->Canvas,0);
  134. }
  135. /**
  136. *Draws a filled polygon
  137. *@access public
  138. *@return void
  139. *@param string $StartColor specifies the starting color of the filled polygon
  140. *@param string $FinishColor specifies the finishing color of the filled polygon.
  141. *IIf the $StartColor is different from $FinishColor then the polygon will be filled
  142. *wwith gradient color
  143. *@param integer 0-127 $Alpha the alpha value of the polygon's color
  144. */
  145. public function draw_Filled($StartColor,$FinishColor,$Alpha) {
  146. $FirstVertex=explode(",",$this->Vertices[count($this->Vertices)-2]);
  147. $LastVertex=explode(",",$this->Vertices[count($this->Vertices)-1]);
  148. $Range=$LastVertex[0]-$FirstVertex[0];
  149. $ColorHandler=$this->ColorAllocator->Allocate($this->Canvas,$StartColor,$FinishColor,$Alpha,$Range);
  150. if (is_array($ColorHandler))
  151. $this->draw_GradientPolygon($ColorHandler);
  152. else
  153. $this->draw_NormalPolygon($ColorHandler);
  154. }
  155. /**
  156. *Draws a uniform color filled polygon
  157. *@access private
  158. *@return void
  159. *@param $ColorHandler
  160. */
  161. private function draw_NormalPolygon($ColorHandler) {
  162. $ProperVertices=array();
  163. for ($vertex=0;$vertex<count($this->Vertices);$vertex++) {
  164. $Vertex=explode(",",$this->Vertices[$vertex]);
  165. array_push($ProperVertices,$Vertex[0]);
  166. array_push($ProperVertices,$Vertex[1]);
  167. }
  168. $this->draw_Caption();
  169. ImageFilledPolygon($this->Canvas,$ProperVertices,count($this->Vertices),$ColorHandler);
  170. }
  171. /**
  172. *Draws a gradient color filled polygon
  173. *@access private
  174. *@return void
  175. *@param $ColorHandler
  176. */
  177. private function draw_GradientPolygon($ColorHandler) {
  178. $ProperVertices=array();
  179. $CurrentColor=0;
  180. for ($vertex=0;$vertex<count($this->Vertices);$vertex++) {
  181. $Vertex=explode(",",$this->Vertices[$vertex]);
  182. array_push($ProperVertices,$Vertex[0]);
  183. array_push($ProperVertices,$Vertex[1]);
  184. }
  185. for ($i=0;$i<count($ProperVertices)-6;$i+=2) {
  186. $current=$i;
  187. $StartPointX=$ProperVertices[$i];
  188. $StartPointY=$ProperVertices[$i+1];
  189. $FinishPointX=$ProperVertices[$i+2];
  190. $FinishPointY=$ProperVertices[$i+3];
  191. $RangeX=$FinishPointX-$StartPointX;
  192. if ($RangeX==0) $RangeX=1;
  193. $Ratio=($FinishPointY-$StartPointY)/$RangeX;
  194. $YPos=$ProperVertices[count($ProperVertices)-1];
  195. for ($XPos=$StartPointX;$XPos<$FinishPointX;$XPos++)
  196. ImageLine($this->Canvas,$XPos,$StartPointY+$Ratio*($XPos-$StartPointX),$XPos,$YPos,$ColorHandler[$CurrentColor++]);
  197. }
  198. $this->draw_Caption();
  199. }
  200.  
  201. }
  202.  
  203. ?>

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