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

Source for file Chart.php

Documentation is available at Chart.php

  1. <?php
  2.  
  3. /*
  4. **************************************************
  5. Class: Chart.php
  6. **************************************************
  7. Author: Tsiavos Chris <jaames@freemail.gr>
  8. Date: October 2004
  9. **************************************************/
  10.  
  11. error_reporting(E_ALL ^ E_NOTICE);
  12.  
  13. /**
  14. *Includes the Rectangle Class for in-class scope operations
  15. */
  16. require_once("Rectangle.php");
  17.  
  18. /**
  19. *Includes the Line Class for in-class scope operations
  20. */
  21. require_once("Line.php");
  22.  
  23. /**
  24. *Base class for all chart types.
  25. *
  26. *Base class for all chart types.Subclasses must implement
  27. *the abstract function Chart::draw() to gain the parent class functionality
  28. *@abstract
  29. *@author Tsiavos Chris <jaames@freemail.gr>
  30. *@license http://opensource.org/licenses/gpl-license.php GNU Public License
  31. */
  32. abstract class Chart {
  33. /**
  34. *Holds the Chart Image Handler
  35. * @access protected
  36. */
  37. protected $Chart_Image;
  38. /**
  39. *Holds the xsize value of the charting area
  40. * @access protected
  41. * @var integer
  42. */
  43. protected $Area_xsize;
  44. /**
  45. *Holds the ysize value of the charting area
  46. *@access protected
  47. * @var integer
  48. */
  49. protected $Area_ysize;
  50. /**
  51. *Holds the yscale value of the chart
  52. *@access protected
  53. *@var integer
  54. */
  55. protected $Chart_Yscale;
  56. /**
  57. *Indicates the grid position with value 0
  58. *@access protected
  59. *@var integer
  60. */
  61. protected $Chart_ZeroPos;
  62. /**
  63. *Holds a reference to the ConfigParser instance
  64. * @access protected
  65. * @var ConfigParser
  66. */
  67. protected $ConfigParser_;
  68. /**
  69. *Holds a reference to the DataParser instance
  70. *@access protected
  71. *@var DataParser
  72. */
  73. protected $DataParser_;
  74. /**
  75. *Holds a reference to the ColorAllocator instance
  76. *@access protected
  77. *@var ColorAllocator
  78. */
  79. protected $ColorAllocator_;
  80. /**
  81. *Holds a reference to the selected caching strategy
  82. *@access protected
  83. */
  84. protected $Caching_Strategy_;
  85. /**
  86. *Holds a reference to the Font instance
  87. *@access protected
  88. */
  89. protected $Font_;
  90.  
  91. /**
  92. *Chart Constructor
  93. */
  94. function __construct() { }
  95.  
  96. /**
  97. *Specifies the DataParser Instance for in-class scope operations
  98. *@access public
  99. *@return void
  100. *@param DataParser $DataParser
  101. */
  102. public function set_DataParser(DataParser &$DataParser) {
  103. $this->DataParser_=$DataParser;
  104. }
  105.  
  106. /**
  107. *Specifies the ConfigParser Instance for in-class scope operations
  108. *@access public
  109. *@return void
  110. *@param ConfigParser $ConfigParser
  111. */
  112. public function set_ConfigParser(ConfigParser &$ConfigParser) {
  113. $this->ConfigParser_=$ConfigParser;
  114. }
  115.  
  116. /**
  117. *Specifies the ColorAllocator instance to use for in-class scope operations
  118. *@access public
  119. *@return void
  120. *@param ColorAllocator $ColorAllocator
  121. */
  122. public function set_ColorAllocator(ColorAllocator &$ColorAllocator) {
  123. $this->ColorAllocator_=$ColorAllocator;
  124. }
  125.  
  126. /**
  127. *Specifies the Font instance to use for in-class scope operations
  128. *@access public
  129. *@return void
  130. *@param Font $Font
  131. */
  132. public function set_Font(Font &$Font) {
  133. $this->Font_=$Font;
  134. }
  135.  
  136. /**
  137. *Specifies the Caching Strategy the class will follow. Caching Strategy objects provide
  138. *a common interface to the class for supporting multiple methods of caching the generated images.
  139. *<br>
  140. *The available Caching Strategy Objects are:
  141. *<ul>
  142. *<li>NullCaching_Strategy. Provides no caching strategy to the class. It should be used when we dont
  143. *want to cache the generated image</li>
  144. *<li>CacheToFile_Strategy. Caches the generated image in a file</li>
  145. *<li>CacheToPEARDB_Strategy. Caches the generated image in a database</li>
  146. *</ul>
  147. *For more info read the Strategy Pattern
  148. *@link http://c2.com/cgi/wiki?StrategyPattern
  149. *@return void
  150. *@param mixed $CachingStrategy_Object
  151. *@param integer $CacheForMinutes Specifies the validity period of the cached image
  152. *@param mixed $Params Specifies an arbitrary number of parameters, passed to the selected CachingStrategy object
  153. *@access public
  154. */
  155.  
  156. public function set_CachingStrategy(&$CachingStrategy_Object,$CacheForMinutes=NULL,$Params=NULL) {
  157. $this->Caching_Strategy_=$CachingStrategy_Object;
  158. ($ImageExists=$this->Caching_Strategy_->Initialize($this->ConfigParser_->get_ImageOutputType(),$CacheForMinutes,$Params)) && exit();
  159. }
  160.  
  161. /**
  162. *Colorizes the image and the calculated chart area with the appropriate colors
  163. *@return void
  164. *@access protected
  165. */
  166. protected function Colorize_ChartArea() {
  167.  
  168. //Colorize Image Area
  169. $ImageColorStart=$this->ConfigParser_->get_ImageColor_Start();
  170. $ImageColorFinish=$this->ConfigParser_->get_ImageColor_Finish();
  171. $ImageColorAlpha=$this->ConfigParser_->get_ImageColor_Alpha();
  172.  
  173. $ImageArea_XSize=$this->ConfigParser_->get_ImageWidth();
  174. $ImageArea_YSize=$this->ConfigParser_->get_ImageHeight();
  175. $ImageArea=new Rectangle($this->Chart_Image,$this->ColorAllocator_,$this->ConfigParser_->get_ChartUseAntialias(),0,0,$ImageArea_XSize,$ImageArea_YSize);
  176. $ImageArea->draw_Filled($ImageColorStart,$ImageColorFinish,$ImageColorAlpha);
  177.  
  178. //Colorize Chart Area
  179. $Chart_ColorStart=$this->ConfigParser_->get_ChartColor_Start();
  180. $Chart_ColorFinish=$this->ConfigParser_->get_ChartColor_Finish();
  181. $Chart_ColorAlpha=$this->ConfigParser_->get_ChartColor_Alpha();
  182.  
  183. $Chart_x_start=$this->ConfigParser_->get_ChartHmargin();
  184. $Chart_y_start=$this->ConfigParser_->get_ChartVmargin();
  185. $Chart_x_finish=$this->Area_xsize+$this->ConfigParser_->get_ChartHmargin();
  186. $Chart_y_finish=$this->Area_ysize+$this->ConfigParser_->get_ChartVmargin();
  187. $ChartTitle=$this->ConfigParser_->get_ChartTitle();
  188. $ChartTitleColor=$this->ConfigParser_->get_FontColor();
  189.  
  190. $ChartArea=new Rectangle($this->Chart_Image,$this->ColorAllocator_,$this->ConfigParser_->get_ChartUseAntialias(),$Chart_x_start,$Chart_y_start,$Chart_x_finish,$Chart_y_finish);
  191. $ChartArea->set_Caption($this->Font_,$ChartTitle,$ChartTitleColor);
  192. $ChartArea->draw_Filled($Chart_ColorStart,$Chart_ColorFinish,$Chart_ColorAlpha);
  193. }
  194.  
  195. /**
  196. *Initializes the Font instance
  197. *@return void
  198. *@access protected
  199. */
  200. protected function Initialize_Font() {
  201. $FontFileLocation=$this->ConfigParser_->get_FontFileLocation();
  202. $FontSize=$this->ConfigParser_->get_FontSize();
  203. $FontWidth=$this->ConfigParser_->get_FontWidth();
  204. $FontHeight=$this->ConfigParser_->get_FontHeight();
  205.  
  206. $this->Font_->set_Properties($this->Chart_Image,$this->ColorAllocator_,$FontFileLocation,$FontSize,$FontWidth,$FontHeight);
  207. }
  208.  
  209. /**
  210. *Calculates the dimensions of the chart drawing area
  211. *@return void
  212. *@access protected
  213. */
  214. protected function get_DrawingArea_Size() {
  215. $this->Area_xsize=$this->ConfigParser_->get_ImageWidth()-($this->ConfigParser_->get_ChartHmargin()+$this->ConfigParser_->get_ChartHmargin()/2);
  216. $FontMetrics=$this->Font_->get_FontMetrics("SampleText");
  217. $this->Area_ysize=$this->ConfigParser_->get_ImageHeight()-(2*$this->ConfigParser_->get_ChartVmargin());
  218. $this->Area_ysize-=$FontMetrics["FontHeight"]*$this->DataParser_->get_GroupItemsNum()+5; //plus 5 more pixels
  219. }
  220.  
  221. /**
  222. *Destructor. Destroyes the Chart Image handler
  223. */
  224. function __destruct() {
  225. ImageDestroy($this->Chart_Image);
  226. }
  227.  
  228. /**
  229. *Draws the legend
  230. *return void
  231. *@access protected
  232. */
  233. protected function draw_Legend() {
  234. $FontMetrics=$this->Font_->get_FontMetrics("SampleText");
  235. $FontColor=$this->ConfigParser_->get_FontColor();
  236. $Legend_YPos=2*$this->ConfigParser_->get_ChartVmargin()+$this->Area_ysize; //+18;
  237. $Legend_XPos_Start=$this->ConfigParser_->get_ChartHmargin();
  238. $Legend_XPos_End=$this->ConfigParser_->get_ChartHmargin()+$this->Area_xsize;
  239. $Line=new Line($this->Chart_Image,$this->ColorAllocator_,$this->ConfigParser_->get_ChartUseAntialias());
  240. $Line->add_Point("$Legend_XPos_Start,$Legend_YPos");
  241. $Line->add_Point("$Legend_XPos_End,$Legend_YPos");
  242. $Line->draw($FontColor,0);
  243. $Legend_Rec_Start=$this->ConfigParser_->get_ChartHmargin()/2;
  244. $LegendItems=$this->DataParser_->get_GroupItemsName();
  245. $LegendColors=$this->ConfigParser_->get_LegendColors();
  246. for($LegendItem=0;$LegendItem<count($LegendItems);$LegendItem++) {
  247. if ($this->ConfigParser_->get_FontFileLocation()!=NULL)
  248. $Legend_Rec_YStart=$Legend_YPos+($FontMetrics["FontHeight"]/2)+($FontMetrics["FontHeight"]*$LegendItem)+2; //plus 2 pixels from legend line
  249. else
  250. $Legend_Rec_YStart=$Legend_YPos+($FontMetrics["FontHeight"]/2-2)+($FontMetrics["FontHeight"]*$LegendItem)+2; //plus 2 pixels from legend line
  251. $Legend_TXT_Pos=$Legend_YPos+($LegendItem*$FontMetrics["FontHeight"])+2;
  252. $Rectangle_Colors=explode(",",$LegendColors[$LegendItem]);
  253. $Rectangle=new Rectangle($this->Chart_Image,$this->ColorAllocator_,$this->ConfigParser_->get_ChartUseAntialias(),$Legend_Rec_Start,$Legend_Rec_YStart,$Legend_Rec_Start+4,$Legend_Rec_YStart+4);
  254. $Rectangle->draw_Filled($Rectangle_Colors[0],$Rectangle_Colors[1],0);
  255. $this->Font_->draw_String($this->ConfigParser_->get_ChartHmargin(),$Legend_TXT_Pos,$LegendItems[$LegendItem],$FontColor);
  256. }
  257. }
  258.  
  259. /**
  260. *Calls Chart::Colorize_ChartArea(),Chart::get_DrawingArea_Size(),Chart::draw_Grids(),
  261. *Chart::draw_Legend and Chart::draw() to draw the complete chart image
  262. *@return void
  263. *@access public
  264. */
  265. public function draw_Chart() {
  266.  
  267. header("Content-Type: image/".$this->ConfigParser_->get_ImageOutputType());
  268. header("Cache-Control: no-cache");
  269. switch ($this->ConfigParser_->get_ImageOutputType()) {
  270. case "gif":
  271. if ($this->ConfigParser_->get_ChartBgImage()!=NULL)
  272. $this->Chart_Image=ImageCreateFromGIF($this->ConfigParser_->get_ChartBgImage());
  273. else
  274. $this->Chart_Image=ImageCreate($this->ConfigParser_->get_ImageWidth(),$this->ConfigParser_->get_ImageHeight());
  275. break;
  276. case "jpeg":
  277. if ($this->ConfigParser_->get_ChartBgImage()!=NULL)
  278. $this->Chart_Image=ImageCreateFromJPEG($this->ConfigParser_->get_ChartBgImage());
  279. else
  280. $this->Chart_Image=ImageCreateTrueColor($this->ConfigParser_->get_ImageWidth(),$this->ConfigParser_->get_ImageHeight());
  281. break;
  282. case "png":
  283. if ($this->ConfigParser_->get_ChartBgImage()!=NULL)
  284. $this->Chart_Image=ImageCreateFromPNG($this->ConfigParser_->get_ChartBgImage());
  285. else
  286. $this->Chart_Image=ImageCreateTrueColor($this->ConfigParser_->get_ImageWidth(),$this->ConfigParser_->get_ImageHeight());
  287. }
  288. if ($this->ConfigParser_->get_ImageOutputType()=="png" || $this->ConfigParser_->get_ImageOutputType()=="jpeg") {
  289. if ($this->ConfigParser_->get_ChartUseBlending()=="Yes")
  290. ImageAlphaBlending($this->Chart_Image,TRUE);
  291. else
  292. ImageAlphaBlending($this->Chart_Image,FALSE);
  293. }
  294. $this->Initialize_Font();
  295. $this->get_DrawingArea_Size();
  296. $this->Colorize_ChartArea();
  297. $this->draw_Grids();
  298. $this->draw_Legend();
  299. $this->draw();
  300. switch ($this->ConfigParser_->get_ImageOutputType()) {
  301. case "png":
  302. ImagePNG($this->Chart_Image);
  303. break;
  304. case "jpeg":
  305. ImageJPEG($this->Chart_Image,"",75);
  306. break;
  307. case "gif":
  308. ImageGIF($this->Chart_Image);
  309. }
  310. $this->Caching_Strategy_->CacheImage();
  311. }
  312.  
  313. /**
  314. *Abstract function,overriden by subclasses to draw the selected chart type (Bar, Line,Area,Pie)
  315. *@return void
  316. *@access protected
  317. *@abstract
  318. */
  319. protected abstract function draw();
  320.  
  321. /**
  322. /**
  323. *Draws the grids in the calculated chart area
  324. *@return void
  325. *@access protected
  326. */
  327. protected function draw_Grids() {
  328. $Dydat=$this->ConfigParser_->get_GridMaxValue()/$this->ConfigParser_->get_GridNum();
  329. $Gridarea=$this->Area_ysize-15;
  330. if ($this->ConfigParser_->get_GridMinValue()<0) $Gridarea-=15;
  331. if ($this->ConfigParser_->get_GridMinValue()<0) {
  332. $Total_Grids=$this->ConfigParser_->get_GridNum()*2;
  333. for ($Grid=0;$Grid<$Total_Grids;$Grid++) {
  334. $Grid_Val[$Grid]=(int)($this->ConfigParser_->get_GridMinValue()+($Grid*$Dydat));
  335. $Grid_Val[$this->ConfigParser_->get_GridNum()*2-$Grid]=-$Grid_Val[$Grid];
  336. }
  337. $Grid_Val[$this->ConfigParser_->get_GridNum()]=0;
  338. }
  339. else
  340. {
  341. $Total_Grids=$this->ConfigParser_->get_GridNum();
  342. $Grid_Val[0]=0;
  343. for ($Grid=1;$Grid<=$Total_Grids;$Grid++)
  344. $Grid_Val[$Grid]=$this->ConfigParser_->get_GridMinValue()+($Grid*$Dydat);
  345. }
  346. $Dypix=($Gridarea/$Total_Grids);
  347. $this->Chart_Yscale=$Dypix/$Dydat;
  348. $Grid_StartXPos=$this->ConfigParser_->get_ChartHmargin()-2;
  349. $Grid_FinishXPos=$this->ConfigParser_->get_ChartHmargin()+$this->Area_xsize;
  350. $Grid_Color=$this->ConfigParser_->get_GridColor();
  351. $Grid_FontColor=$this->ConfigParser_->get_FontColor();
  352. for ($Grid=0;$Grid<=$Total_Grids;$Grid++) {
  353. $Grid_Value=(int)($Grid_Val[$Grid]);
  354. $Grid_Value_Size=$this->Font_->get_FontMetrics($Grid_Value);
  355. $Grid_Value_XPos=(int)(($this->ConfigParser_->get_ChartHmargin()-$Grid_Value_Size["FontWidth"])/2);
  356. if ($this->ConfigParser_->get_GridMinValue()<0)
  357. $Grid_YPos=($this->ConfigParser_->get_ChartVmargin()+$this->Area_ysize-15)-($Grid*$Dypix);
  358. else
  359. $Grid_YPos=($this->ConfigParser_->get_ChartVmargin()+$this->Area_ysize)-($Grid*$Dypix);
  360. if ($Grid_Value==0) $this->Chart_ZeroPos=$Grid_YPos;
  361. $Grid_Value_YPos=$Grid_YPos-($Grid_Value_Size["FontHeight"]/2);
  362. $this->Font_->draw_String($Grid_Value_XPos,$Grid_Value_YPos,$Grid_Value,$Grid_FontColor);
  363. $Line=new Line($this->Chart_Image,$this->ColorAllocator_,$this->ConfigParser_->get_ChartUseAntialias());
  364. $Line->add_Point("$Grid_StartXPos,$Grid_YPos");
  365. $Line->add_Point("$Grid_FinishXPos,$Grid_YPos");
  366. $Line->draw($Grid_Color,0);
  367. }
  368. }
  369.  
  370. }
  371.  
  372. ?>

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