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

Source for file Circle.php

Documentation is available at Circle.php

  1. <?php
  2.  
  3. /*
  4. **************************************************
  5. Class: Circle.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. *Subsidiary class utilized by the PieChart class
  18. *@author Tsiavos Chris <jaames@freemail.gr>
  19. *@license http://opensource.org/licenses/gpl-license.php GNU Public License
  20. */
  21. class Chunk {
  22. /**
  23. *the size of the chunk in degrees
  24. *@access private
  25. *@var integer
  26. */
  27. private $Angle;
  28.  
  29. /**
  30. *the caption of the chunk
  31. *@access private
  32. *@var string
  33. */
  34. private $Caption;
  35. /**
  36. *@access private
  37. *@var string
  38. */
  39. private $CaptionColor;
  40. /**
  41. *the starting color of the chunk
  42. *@access private
  43. *@var string
  44. */
  45. private $StartColor;
  46. /**
  47. *the finishing color of the chunk. If $FinishColor
  48. *is different from $StartColor then a gradient color chunk
  49. *will be drawn
  50. *@access private
  51. *@var string
  52. */
  53. private $FinishColor;
  54. /**
  55. *the Font instance used for drawing the chunk's caption
  56. *@access private
  57. *@var Font
  58. */
  59. private $Font;
  60. /**
  61. *Sets the size of the chunk in degrees
  62. *@access public
  63. *@return void
  64. *@param integer $Degree
  65. */
  66. public function set_Angle($Degree) {
  67. $this->Angle=$Degree;
  68. }
  69. /**
  70. *Returns (in degrees) the size of the chunk
  71. *@access public
  72. *@return integer
  73. */
  74. public function get_Angle() {
  75. return $this->Angle;
  76. }
  77. /**
  78. *Sets the caption of the chunk
  79. *@access public
  80. *@return void
  81. *@param Font &$Font
  82. *@param string $Caption
  83. *@param string $CaptionColor
  84. */
  85. public function set_Caption(Font &$Font,$Caption,$CaptionColor) {
  86. $this->Font=$Font;
  87. $this->Caption=$Caption;
  88. $this->CaptionColor=$CaptionColor;
  89. }
  90. /**
  91. *Returns the caption of the chunk
  92. *@access public
  93. *@return string
  94. */
  95. public function get_Caption() {
  96. return $this->Caption;
  97. }
  98. /**
  99. *Returns the font color of the chunk's caption
  100. *@access public
  101. *@return string
  102. */
  103. public function get_CaptionColor() {
  104. return $this->CaptionColor;
  105. }
  106. /**
  107. *Sets the color the chunk will be filled in.
  108. *@access public
  109. *@return void
  110. *@param string $StartColor The starting color of the chunk
  111. *@param string $FinishColor The finishing color of the chunk.If $StartColor
  112. *iis different from $FinishColor then a gradient color will be alllocated
  113. */
  114. public function set_Color($StartColor,$FinishColor) {
  115. $this->StartColor=$StartColor;
  116. $this->FinishColor=$FinishColor;
  117. }
  118. /**
  119. *Returns the starting color of the chunk
  120. *@access public
  121. *@return string
  122. */
  123. public function get_StartColor() {
  124. return $this->StartColor;
  125. }
  126. /**
  127. *Returns the finishing color of the chunk
  128. *@access public
  129. *@return string
  130. */
  131. public function get_FinishColor() {
  132. return $this->FinishColor;
  133. }
  134. /**
  135. *Returns the Font instance used for drawing the caption of the chunk
  136. *@access public
  137. *@var Font
  138. */
  139. public function get_Font() {
  140. return $this->Font;
  141. }
  142.  
  143. }
  144.  
  145. /**
  146. *Wrapper around gd's Image(Filled)Arc functions
  147. *This class is utilized by the PieChart class for generating
  148. *the pie chart type
  149. *@author Tsiavos Chris <jaames@freemail.gr>
  150. *@license http://opensource.org/licenses/gpl-license.php GNU Public License
  151. */
  152. class Circle extends Shape {
  153. /**
  154. *x value of the pie's center position
  155. *@access private
  156. *@var integer
  157. */
  158. private $Center_XPos;
  159. /**
  160. *y value of the pie's center position
  161. *@access private
  162. *@var integer
  163. */
  164. private $Center_YPos;
  165. /**
  166. *pie's width
  167. *@access private
  168. *@return integer
  169. */
  170. private $Width;
  171. /**
  172. *pie's height
  173. *@access private
  174. *@return integer
  175. */
  176. private $Height;
  177. /**
  178. *Array holding the chunks composing the pie
  179. *@access private
  180. *@return Chunk[]
  181. */
  182. private $Chunk=array();
  183. /**
  184. *@access private
  185. *@var integer
  186. */
  187. private $Current_Degree;
  188. /**
  189. *Constructor
  190. *@param mixed $Canvas Reference to the image handler the pie will be drawn in
  191. *@param ColorAllocator $ColorAllocator Reference to the ColorAllocator the class will use for allocating chunks' colors
  192. *@param integer $Center_XPos x value of the pie's center position
  193. *@param integer $Center_YPos y value of the pie's center position
  194. *@param integer $Width Pie's width
  195. *@param integer $Height Pie's height
  196. */
  197. function __construct(&$Canvas,ColorAllocator &$ColorAllocator,$UseAntialias,$Center_XPos,$Center_YPos,$Width,$Height) {
  198. Shape::__construct($Canvas,$ColorAllocator,$UseAntialias);
  199. $this->Center_XPos=$Center_XPos;
  200. $this->Center_YPos=$Center_YPos;
  201. $this->Width=$Width;
  202. $this->Height=$Height;
  203. $this->Current_Degree=-1;
  204. }
  205. /**
  206. *Adds a chunk to the pie
  207. *@access public
  208. *@return void
  209. *@param Chunk &$chunk
  210. */
  211. public function addChunk(Chunk $chunk) {
  212. array_push($this->Chunk,$chunk);
  213. }
  214.  
  215. /**
  216. *Draws a filled with color pie
  217. *@access public
  218. *@return void
  219. */
  220. public function draw_Filled() {
  221. for ($chunk=0;$chunk<count($this->Chunk);$chunk++) {
  222. $ColorRange=$this->Chunk[$chunk]->get_Angle();
  223. $StartColor=$this->Chunk[$chunk]->get_StartColor();
  224. $FinishColor=$this->Chunk[$chunk]->get_FinishColor();
  225. $ColorHandler=$this->ColorAllocator->Allocate($this->Canvas,$StartColor,$FinishColor,0,$ColorRange);
  226. if (is_array($ColorHandler))
  227. $this->draw_GradientChunk($chunk,$ColorHandler);
  228. else
  229. $this->draw_Chunk($chunk,$ColorHandler);
  230. }
  231. $this->draw_Caption();
  232. }
  233. /**
  234. *Draws the border of the pie
  235. *@access public
  236. *@return void
  237. *@param string $BorderColor
  238. */
  239. public function draw($BorderColor) {
  240. if ($this->UseAntialias=="Yes")
  241. ImageAntialias($this->Canvas,1);
  242. $ColorHandler=$this->ColorAllocator->Allocate($this->Canvas,$BorderColor,$BorderColor,0,1);
  243. ImageEllipse($this->Canvas,$this->Center_XPos,$this->Center_YPos,$this->Width,$this->Height,$ColorHandler);
  244. $this->Current_Degree=-1;
  245. for ($chunk=0;$chunk<count($this->Chunk);$chunk++) {
  246. $this->Current_Degree++;
  247. $Angle=($this->Current_Degree+$this->Chunk[$chunk]->get_Angle())*(-1);
  248. $Line_XPos=($this->Width/2)*cos(deg2rad($Angle+1));
  249. $Line_YPos=($this->Width/2)*sin(deg2rad($Angle+1));
  250. $LineXPos=$this->Center_XPos+$Line_XPos;
  251. $LineYPos=$this->Center_YPos+$Line_YPos*-1;
  252. ImageLine($this->Canvas,$LineXPos,$LineYPos,$this->Center_XPos,$this->Center_YPos,$ColorHandler);
  253. $this->Current_Degree+=$this->Chunk[$chunk]->get_Angle();
  254. }
  255. if ($this->UseAntialias=="Yes")
  256. ImageAntialias($this->Canvas,0);
  257. }
  258. /**
  259. *This function is called by Circle::draw() to draw a gradient color chunk if Chunk::$StartColor is different from Chunk::$FinishColor
  260. *@access private
  261. *@return void
  262. */
  263. private function draw_GradientChunk($chunk,$ColorHandler) {
  264. $CurrentDegree=$this->Current_Degree++;
  265. for ($Degree=$CurrentDegree;$Degree<$CurrentDegree+$this->Chunk[$chunk]->get_Angle();$Degree++)
  266. ImageFilledArc($this->Canvas,$this->Center_XPos,$this->Center_YPos,$this->Width,$this->Height,$Degree,$Degree+1,$ColorHandler[$Degree-$CurrentDegree],IMAGE_ARC_PIE);
  267. $this->Current_Degree+=$this->Chunk[$chunk]->get_Angle();
  268. }
  269. /**
  270. *This function is called by Circle::draw() to draw a uniform color chunk if Chunk::$StartColor and Chunk::$FinishColor are the same colors
  271. *@access private
  272. *@return void
  273. */
  274. private function draw_Chunk($chunk,$ColorHandler) {
  275. $StartDegree=$this->Current_Degree++;
  276. $EndDegree=$this->Current_Degree+$this->Chunk[$chunk]->get_Angle();
  277. ImageFilledArc($this->Canvas,$this->Center_XPos,$this->Center_YPos,$this->Width,$this->Height,$StartDegree,$EndDegree,$ColorHandler,IMAGE_ARC_PIE);
  278. $this->Current_Degree+=$this->Chunk[$chunk]->get_Angle();
  279. }
  280. /**
  281. *Draws the caption of each chunk
  282. *@access private
  283. *@return void
  284. */
  285. private function draw_Caption() {
  286. $this->Current_Degree=-1;
  287. for ($chunk=0;$chunk<count($this->Chunk);$chunk++) {
  288. $this->Current_Degree++;
  289. $Angle=($this->Current_Degree+$this->Chunk[$chunk]->get_Angle()/2)*(-1);
  290. $PreviousCaption_XPos=($this->Width/2)*cos(deg2rad($Angle));
  291. $PreviousCaption_YPos=($this->Width/2)*sin(deg2rad($Angle));
  292. $Caption_XPos=$this->Center_XPos+$PreviousCaption_XPos/2;
  293. $Caption_YPos=$this->Center_YPos+($PreviousCaption_YPos/2)*-1;
  294.  
  295. $Caption=$this->Chunk[$chunk]->get_Caption();
  296. $CaptionColor=$this->Chunk[$chunk]->get_CaptionColor();
  297. $this->Chunk[$chunk]->get_Font()->draw_String($Caption_XPos,$Caption_YPos,$Caption,$CaptionColor);
  298. $this->Current_Degree+=$this->Chunk[$chunk]->get_Angle();
  299. }
  300. }
  301. }
  302.  
  303. ?>

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