PageRenderTime 73ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 1ms

/protected/components/pChart.php

https://bitbucket.org/y_widyatama/ijepa2
PHP | 3492 lines | 2668 code | 550 blank | 274 comment | 776 complexity | c090f992083e1e32224cd570100b6bd9 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /*
  3. pChart - a PHP class to build charts!
  4. Copyright (C) 2008 Jean-Damien POGOLOTTI
  5. Version 1.27d last updated on 09/30/08
  6. http://pchart.sourceforge.net
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation, either version 1,2,3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. Class initialisation :
  18. pChart($XSize,$YSize)
  19. Draw methods :
  20. drawBackground($R,$G,$B)
  21. drawRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B)
  22. drawFilledRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B,$DrawBorder=TRUE,$Alpha=100)
  23. drawRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B)
  24. drawFilledRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B)
  25. drawCircle($Xc,$Yc,$Height,$R,$G,$B,$Width=0)
  26. drawFilledCircle($Xc,$Yc,$Height,$R,$G,$B,$Width=0)
  27. drawEllipse($Xc,$Yc,$Height,$Width,$R,$G,$B)
  28. drawFilledEllipse($Xc,$Yc,$Height,$Width,$R,$G,$B)
  29. drawLine($X1,$Y1,$X2,$Y2,$R,$G,$B,$GraphFunction=FALSE)
  30. drawDottedLine($X1,$Y1,$X2,$Y2,$DotSize,$R,$G,$B)
  31. drawAlphaPixel($X,$Y,$Alpha,$R,$G,$B)
  32. drawFromPNG($FileName,$X,$Y,$Alpha=100)
  33. drawFromGIF($FileName,$X,$Y,$Alpha=100)
  34. drawFromJPG($FileName,$X,$Y,$Alpha=100)
  35. Graph setup methods :
  36. addBorder($Width=3,$R=0,$G=0,$B=0)
  37. clearScale()
  38. clearShadow()
  39. createColorGradientPalette($R1,$G1,$B1,$R2,$G2,$B2,$Shades)
  40. drawGraphArea($R,$G,$B,$Stripe=FALSE)
  41. drawScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$Angle=0,$Decimals=1,$WithMargin=FALSE,$SkipLabels=1,$RightScale=FALSE)
  42. drawRightScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$Angle=0,$Decimals=1,$WithMargin=FALSE,$SkipLabels=1)
  43. drawXYScale($Data,$DataDescription,$YSerieName,$XSerieName,$R,$G,$B,$WithMargin=0,$Angle=0,$Decimals=1)
  44. drawGrid($LineWidth,$Mosaic=TRUE,$R=220,$G=220,$B=220,$Alpha=100)
  45. drawLegend($XPos,$YPos,$DataDescription,$R,$G,$B,$Rs=-1,$Gs=-1,$Bs=-1,$Rt=0,$Gt=0,$Bt=0,$Border=FALSE)
  46. drawPieLegend($XPos,$YPos,$Data,$DataDescription,$R,$G,$B)
  47. drawTitle($XPos,$YPos,$Value,$R,$G,$B,$XPos2=-1,$YPos2=-1,$Shadow=FALSE)
  48. drawTreshold($Value,$R,$G,$B,$ShowLabel=FALSE,$ShowOnRight=FALSE,$TickWidth=4,$FreeText=NULL)
  49. drawArea($Data,$Serie1,$Serie2,$R,$G,$B,$Alpha = 50)
  50. drawRadarAxis($Data,$DataDescription,$Mosaic=TRUE,$BorderOffset=10,$A_R=60,$A_G=60,$A_B=60,$S_R=200,$S_G=200,$S_B=200,$MaxValue=-1)
  51. drawGraphAreaGradient($R,$G,$B,$Decay,$Target=TARGET_GRAPHAREA)
  52. drawTextBox($X1,$Y1,$X2,$Y2,$Text,$Angle=0,$R=255,$G=255,$B=255,$Align=ALIGN_LEFT,$Shadow=TRUE,$BgR=-1,$BgG=-1,$BgB=-1,$Alpha=100)
  53. getLegendBoxSize($DataDescription)
  54. loadColorPalette($FileName,$Delimiter=",")
  55. reportWarnings($Interface="CLI")
  56. setGraphArea($X1,$Y1,$X2,$Y2)
  57. setLabel($Data,$DataDescription,$SerieName,$ValueName,$Caption,$R=210,$G=210,$B=210)
  58. setColorPalette($ID,$R,$G,$B)
  59. setCurrency($Currency)
  60. setDateFormat($Format)
  61. setFontProperties($FontName,$FontSize)
  62. setLineStyle($Width=1,$DotSize=0)
  63. setFixedScale($VMin,$VMax,$Divisions=5,$VXMin=0,$VXMin=0,$XDivisions=5)
  64. setShadowProperties($XDistance=1,$YDistance=1,$R=60,$G=60,$B=60,$Alpha)
  65. writeValues($Data,$DataDescription,$Series)
  66. Graphs methods :
  67. drawPlotGraph($Data,$DataDescription,$BigRadius=5,$SmallRadius=2,$R2=-1,$G2=-1,$B2=-1,$Shadow=FALSE)
  68. drawXYPlotGraph($Data,$DataDescription,$YSerieName,$XSerieName,$PaletteID=0,$BigRadius=5,$SmallRadius=2,$R2=-1,$G2=-1,$B2=-1)
  69. drawLineGraph($Data,$DataDescription,$SerieName="")
  70. drawXYGraph($Data,$DataDescription,$YSerieName,$XSerieName,$PaletteID=0)
  71. drawFilledLineGraph($Data,$DataDescription,$Alpha=100,$AroundZero=FALSE)
  72. drawCubicCurve($Data,$DataDescription,$Accuracy=.1,$SerieName="")
  73. drawFilledCubicCurve($Data,$DataDescription,$Accuracy=.1,$Alpha=100,$AroundZero=FALSE)
  74. drawOverlayBarGraph($Data,$DataDescription,$Alpha=50)
  75. drawBarGraph($Data,$DataDescription,$Shadow=FALSE)
  76. drawStackedBarGraph($Data,$DataDescription,$Alpha=50,$Contiguous=FALSE)
  77. drawLimitsGraph($Data,$DataDescription,$R=0,$G=0,$B=0)
  78. drawRadar($Data,$DataDescription,$BorderOffset=10,$MaxValue=-1)
  79. drawFilledRadar($Data,$DataDescription,$Alpha=50,$BorderOffset=10,$MaxValue=-1)
  80. drawBasicPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$R=255,$G=255,$B=255,$Decimals=0)
  81. drawFlatPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$SpliceDistance=0,$Decimals = 0)
  82. drawFlatPieGraphWithShadow($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$SpliceDistance=0,$Decimals = 0)
  83. drawPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$EnhanceColors=TRUE,$Skew=60,$SpliceHeight=20,$SpliceDistance=0,$Decimals=0)
  84. Other methods :
  85. setImageMap($Mode=TRUE,$GraphID="MyGraph")
  86. getImageMap($MapName,$Flush=TRUE)
  87. Render($FileName)
  88. Stroke()
  89. */
  90. /* Declare some script wide constants */
  91. define("SCALE_NORMAL",1);
  92. define("SCALE_ADDALL",2);
  93. define("SCALE_START0",3);
  94. define("SCALE_ADDALLSTART0",4);
  95. define("PIE_PERCENTAGE", 1);
  96. define("PIE_LABELS",2);
  97. define("PIE_NOLABEL",3);
  98. define("PIE_PERCENTAGE_LABEL", 4);
  99. define("TARGET_GRAPHAREA",1);
  100. define("TARGET_BACKGROUND",2);
  101. define("ALIGN_TOP_LEFT",1);
  102. define("ALIGN_TOP_CENTER",2);
  103. define("ALIGN_TOP_RIGHT",3);
  104. define("ALIGN_LEFT",4);
  105. define("ALIGN_CENTER",5);
  106. define("ALIGN_RIGHT",6);
  107. define("ALIGN_BOTTOM_LEFT",7);
  108. define("ALIGN_BOTTOM_CENTER",8);
  109. define("ALIGN_BOTTOM_RIGHT",9);
  110. /* pChart class definition */
  111. class pChart
  112. {
  113. /* Palettes definition */
  114. var $Palette = array("0"=>array("R"=>138,"G"=>167,"B"=>24),
  115. "1"=>array("R"=>224,"G"=>100,"B"=>46),
  116. "2"=>array("R"=>224,"G"=>214,"B"=>46),
  117. "3"=>array("R"=>46,"G"=>151,"B"=>224),
  118. "4"=>array("R"=>176,"G"=>46,"B"=>224),
  119. "5"=>array("R"=>224,"G"=>46,"B"=>117),
  120. "6"=>array("R"=>92,"G"=>224,"B"=>46),
  121. "7"=>array("R"=>224,"G"=>176,"B"=>46));
  122. /* Some static vars used in the class */
  123. var $XSize = NULL;
  124. var $YSize = NULL;
  125. var $Picture = NULL;
  126. var $ImageMap = NULL;
  127. /* Error management */
  128. var $ErrorReporting = FALSE;
  129. var $ErrorInterface = "CLI";
  130. var $Errors = NULL;
  131. var $ErrorFontName = "Fonts/pf_arma_five.ttf";
  132. var $ErrorFontSize = 6;
  133. /* vars related to the graphing area */
  134. var $GArea_X1 = NULL;
  135. var $GArea_Y1 = NULL;
  136. var $GArea_X2 = NULL;
  137. var $GArea_Y2 = NULL;
  138. var $GAreaXOffset = NULL;
  139. var $VMax = NULL;
  140. var $VMin = NULL;
  141. var $VXMax = NULL;
  142. var $VXMin = NULL;
  143. var $Divisions = NULL;
  144. var $XDivisions = NULL;
  145. var $DivisionHeight = NULL;
  146. var $XDivisionHeight = NULL;
  147. var $DivisionCount = NULL;
  148. var $XDivisionCount = NULL;
  149. var $DivisionRatio = NULL;
  150. var $XDivisionRatio = NULL;
  151. var $DivisionWidth = NULL;
  152. var $DataCount = NULL;
  153. var $Currency = "\$";
  154. var $FontDIR = '';
  155. /* Text format related vars */
  156. var $FontName = NULL;
  157. var $FontSize = NULL;
  158. var $DateFormat = "d/m/Y";
  159. /* Lines format related vars */
  160. var $LineWidth = 1;
  161. var $LineDotSize = 0;
  162. /* Layer related vars */
  163. var $Layers = NULL;
  164. /* Set antialias quality : 0 is maximum, 100 minimum*/
  165. var $AntialiasQuality = 0;
  166. /* Shadow settings */
  167. var $ShadowActive = FALSE;
  168. var $ShadowXDistance = 1;
  169. var $ShadowYDistance = 1;
  170. var $ShadowRColor = 60;
  171. var $ShadowGColor = 60;
  172. var $ShadowBColor = 60;
  173. var $ShadowAlpha = 50;
  174. var $ShadowBlur = 0;
  175. /* Image Map settings */
  176. var $BuildMap = FALSE;
  177. var $MapFunction = NULL;
  178. var $tmpFolder = "tmp/";
  179. var $MapID = NULL;
  180. /* This function create the background picture */
  181. function pChart($XSize,$YSize)
  182. {
  183. $this->XSize = $XSize;
  184. $this->YSize = $YSize;
  185. $this->Picture = imagecreatetruecolor($XSize,$YSize);
  186. $this->FontDIR = dirname($_SERVER['SCRIPT_FILENAME']).'/';
  187. // echo $this->FontDIR; exit;
  188. $C_White =$this->AllocateColor($this->Picture,255,255,255);
  189. imagefilledrectangle($this->Picture,0,0,$XSize,$YSize,$C_White);
  190. imagecolortransparent($this->Picture,$C_White);
  191. $this->setFontProperties("tahoma.ttf",8);
  192. }
  193. /* Set if warnings should be reported */
  194. function reportWarnings($Interface="CLI")
  195. {
  196. $this->ErrorReporting = TRUE;
  197. $this->ErrorInterface = $Interface;
  198. }
  199. /* Set the font properties */
  200. function setFontProperties($FontName,$FontSize)
  201. {
  202. $this->FontName = $FontName;
  203. $this->FontSize = $FontSize;
  204. }
  205. /* Set the shadow properties */
  206. function setShadowProperties($XDistance=1,$YDistance=1,$R=60,$G=60,$B=60,$Alpha=50,$Blur=0)
  207. {
  208. $this->ShadowActive = TRUE;
  209. $this->ShadowXDistance = $XDistance;
  210. $this->ShadowYDistance = $YDistance;
  211. $this->ShadowRColor = $R;
  212. $this->ShadowGColor = $G;
  213. $this->ShadowBColor = $B;
  214. $this->ShadowAlpha = $Alpha;
  215. $this->ShadowBlur = $Blur;
  216. }
  217. /* Remove shadow option */
  218. function clearShadow()
  219. {
  220. $this->ShadowActive = FALSE;
  221. }
  222. /* Set Palette color */
  223. function setColorPalette($ID,$R,$G,$B)
  224. {
  225. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  226. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  227. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  228. $this->Palette[$ID]["R"] = $R;
  229. $this->Palette[$ID]["G"] = $G;
  230. $this->Palette[$ID]["B"] = $B;
  231. }
  232. /* Create a color palette shading from one color to another */
  233. function createColorGradientPalette($R1,$G1,$B1,$R2,$G2,$B2,$Shades)
  234. {
  235. $RFactor = ($R2-$R1)/$Shades;
  236. $GFactor = ($G2-$G1)/$Shades;
  237. $BFactor = ($B2-$B1)/$Shades;
  238. for($i=0;$i<=$Shades-1;$i++)
  239. {
  240. $this->Palette[$i]["R"] = $R1+$RFactor*$i;
  241. $this->Palette[$i]["G"] = $G1+$GFactor*$i;
  242. $this->Palette[$i]["B"] = $B1+$BFactor*$i;
  243. }
  244. }
  245. /* Load Color Palette from file */
  246. function loadColorPalette($FileName,$Delimiter=",")
  247. {
  248. $handle = @fopen($FileName,"r");
  249. $ColorID = 0;
  250. if ($handle)
  251. {
  252. while (!feof($handle))
  253. {
  254. $buffer = fgets($handle, 4096);
  255. $buffer = str_replace(chr(10),"",$buffer);
  256. $buffer = str_replace(chr(13),"",$buffer);
  257. $Values = split($Delimiter,$buffer);
  258. if ( count($Values) == 3 )
  259. {
  260. $this->Palette[$ColorID]["R"] = $Values[0];
  261. $this->Palette[$ColorID]["G"] = $Values[1];
  262. $this->Palette[$ColorID]["B"] = $Values[2];
  263. $ColorID++;
  264. }
  265. }
  266. }
  267. }
  268. /* Set line style */
  269. function setLineStyle($Width=1,$DotSize=0)
  270. {
  271. $this->LineWidth = $Width;
  272. $this->LineDotSize = $DotSize;
  273. }
  274. /* Set currency symbol */
  275. function setCurrency($Currency)
  276. {
  277. $this->Currency = $Currency;
  278. }
  279. /* Set the graph area location */
  280. function setGraphArea($X1,$Y1,$X2,$Y2)
  281. {
  282. $this->GArea_X1 = $X1;
  283. $this->GArea_Y1 = $Y1;
  284. $this->GArea_X2 = $X2;
  285. $this->GArea_Y2 = $Y2;
  286. }
  287. /* Prepare the graph area */
  288. function drawGraphArea($R,$G,$B,$Stripe=FALSE)
  289. {
  290. $this->drawFilledRectangle($this->GArea_X1,$this->GArea_Y1,$this->GArea_X2,$this->GArea_Y2,$R,$G,$B,FALSE);
  291. $this->drawRectangle($this->GArea_X1,$this->GArea_Y1,$this->GArea_X2,$this->GArea_Y2,$R-40,$G-40,$B-40);
  292. if ( $Stripe )
  293. {
  294. $R2 = $R-15; if ( $R2 < 0 ) { $R2 = 0; }
  295. $G2 = $R-15; if ( $G2 < 0 ) { $G2 = 0; }
  296. $B2 = $R-15; if ( $B2 < 0 ) { $B2 = 0; }
  297. $LineColor =$this->AllocateColor($this->Picture,$R2,$G2,$B2);
  298. $SkewWidth = $this->GArea_Y2-$this->GArea_Y1-1;
  299. for($i=$this->GArea_X1-$SkewWidth;$i<=$this->GArea_X2;$i=$i+4)
  300. {
  301. $X1 = $i; $Y1 = $this->GArea_Y2;
  302. $X2 = $i+$SkewWidth; $Y2 = $this->GArea_Y1;
  303. if ( $X1 < $this->GArea_X1 )
  304. { $X1 = $this->GArea_X1; $Y1 = $this->GArea_Y1 + $X2 - $this->GArea_X1 + 1; }
  305. if ( $X2 >= $this->GArea_X2 )
  306. { $Y2 = $this->GArea_Y1 + $X2 - $this->GArea_X2 +1; $X2 = $this->GArea_X2 - 1; }
  307. // * Fixed in 1.27 * { $X2 = $this->GArea_X2 - 1; $Y2 = $this->GArea_Y2 - ($this->GArea_X2 - $X1); }
  308. imageline($this->Picture,$X1,$Y1,$X2,$Y2+1,$LineColor);
  309. }
  310. }
  311. }
  312. /* Allow you to clear the scale : used if drawing multiple charts */
  313. function clearScale()
  314. {
  315. $this->VMin = NULL;
  316. $this->VMax = NULL;
  317. $this->VXMin = NULL;
  318. $this->VXMax = NULL;
  319. $this->Divisions = NULL;
  320. $this->XDivisions = NULL; }
  321. /* Allow you to fix the scale, use this to bypass the automatic scaling */
  322. function setFixedScale($VMin,$VMax,$Divisions=5,$VXMin=0,$VXMax=0,$XDivisions=5)
  323. {
  324. $this->VMin = $VMin;
  325. $this->VMax = $VMax;
  326. $this->Divisions = $Divisions;
  327. if ( !$VXMin == 0 )
  328. {
  329. $this->VXMin = $VXMin;
  330. $this->VXMax = $VXMax;
  331. $this->XDivisions = $XDivisions;
  332. }
  333. }
  334. /* Wrapper to the drawScale() function allowing a second scale to be drawn */
  335. function drawRightScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$Angle=0,$Decimals=1,$WithMargin=FALSE,$SkipLabels=1)
  336. {
  337. $this->drawScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks,$Angle,$Decimals,$WithMargin,$SkipLabels,TRUE);
  338. }
  339. /* Compute and draw the scale */
  340. function drawScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$Angle=0,$Decimals=1,$WithMargin=FALSE,$SkipLabels=1,$RightScale=FALSE)
  341. {
  342. /* Validate the Data and DataDescription array */
  343. $this->validateData("drawScale",$Data);
  344. $C_TextColor =$this->AllocateColor($this->Picture,$R,$G,$B);
  345. $this->drawLine($this->GArea_X1,$this->GArea_Y1,$this->GArea_X1,$this->GArea_Y2,$R,$G,$B);
  346. $this->drawLine($this->GArea_X1,$this->GArea_Y2,$this->GArea_X2,$this->GArea_Y2,$R,$G,$B);
  347. if ( $this->VMin == NULL && $this->VMax == NULL)
  348. {
  349. if (isset($DataDescription["Values"][0]))
  350. {
  351. $this->VMin = $Data[0][$DataDescription["Values"][0]];
  352. $this->VMax = $Data[0][$DataDescription["Values"][0]];
  353. }
  354. else { $this->VMin = 2147483647; $this->VMax = -2147483647; }
  355. /* Compute Min and Max values */
  356. if ( $ScaleMode == SCALE_NORMAL || $ScaleMode == SCALE_START0 )
  357. {
  358. if ( $ScaleMode == SCALE_START0 ) { $this->VMin = 0; }
  359. foreach ( $Data as $Key => $Values )
  360. {
  361. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  362. {
  363. if (isset($Data[$Key][$ColName]))
  364. {
  365. $Value = $Data[$Key][$ColName];
  366. if ( is_numeric($Value) )
  367. {
  368. if ( $this->VMax < $Value) { $this->VMax = $Value; }
  369. if ( $this->VMin > $Value) { $this->VMin = $Value; }
  370. }
  371. }
  372. }
  373. }
  374. }
  375. elseif ( $ScaleMode == SCALE_ADDALL || $ScaleMode == SCALE_ADDALLSTART0 ) /* Experimental */
  376. {
  377. if ( $ScaleMode == SCALE_ADDALLSTART0 ) { $this->VMin = 0; }
  378. foreach ( $Data as $Key => $Values )
  379. {
  380. $Sum = 0;
  381. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  382. {
  383. if (isset($Data[$Key][$ColName]))
  384. {
  385. $Value = $Data[$Key][$ColName];
  386. if ( is_numeric($Value) )
  387. $Sum += $Value;
  388. }
  389. }
  390. if ( $this->VMax < $Sum) { $this->VMax = $Sum; }
  391. if ( $this->VMin > $Sum) { $this->VMin = $Sum; }
  392. }
  393. }
  394. if ( $this->VMax > preg_replace('/\.[0-9]+/','',$this->VMax) )
  395. $this->VMax = preg_replace('/\.[0-9]+/','',$this->VMax)+1;
  396. /* If all values are the same */
  397. if ( $this->VMax == $this->VMin )
  398. {
  399. if ( $this->VMax >= 0 ) { $this->VMax++; }
  400. else { $this->VMin--; }
  401. }
  402. $DataRange = $this->VMax - $this->VMin;
  403. if ( $DataRange == 0 ) { $DataRange = .1; }
  404. /* Compute automatic scaling */
  405. $ScaleOk = FALSE; $Factor = 1;
  406. $MinDivHeight = 25; $MaxDivs = ($this->GArea_Y2 - $this->GArea_Y1) / $MinDivHeight;
  407. if ( $this->VMin == 0 && $this->VMax == 0 )
  408. { $this->VMin = 0; $this->VMax = 2; $Scale = 1; $Divisions = 2;}
  409. elseif ($MaxDivs > 1)
  410. {
  411. while(!$ScaleOk)
  412. {
  413. $Scale1 = ( $this->VMax - $this->VMin ) / $Factor;
  414. $Scale2 = ( $this->VMax - $this->VMin ) / $Factor / 2;
  415. $Scale4 = ( $this->VMax - $this->VMin ) / $Factor / 4;
  416. if ( $Scale1 > 1 && $Scale1 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale1); $Scale = 1;}
  417. if ( $Scale2 > 1 && $Scale2 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale2); $Scale = 2;}
  418. if (!$ScaleOk)
  419. {
  420. if ( $Scale2 > 1 ) { $Factor = $Factor * 10; }
  421. if ( $Scale2 < 1 ) { $Factor = $Factor / 10; }
  422. }
  423. }
  424. if ( floor($this->VMax / $Scale / $Factor) != $this->VMax / $Scale / $Factor)
  425. {
  426. $GridID = floor ( $this->VMax / $Scale / $Factor) + 1;
  427. $this->VMax = $GridID * $Scale * $Factor;
  428. $Divisions++;
  429. }
  430. if ( floor($this->VMin / $Scale / $Factor) != $this->VMin / $Scale / $Factor)
  431. {
  432. $GridID = floor( $this->VMin / $Scale / $Factor);
  433. $this->VMin = $GridID * $Scale * $Factor;
  434. $Divisions++;
  435. }
  436. }
  437. else /* Can occurs for small graphs */
  438. $Scale = 1;
  439. if ( !isset($Divisions) )
  440. $Divisions = 2;
  441. if ($Scale == 1 && $Divisions%2 == 1)
  442. $Divisions--;
  443. }
  444. else
  445. $Divisions = $this->Divisions;
  446. $this->DivisionCount = $Divisions;
  447. $DataRange = $this->VMax - $this->VMin;
  448. if ( $DataRange == 0 ) { $DataRange = .1; }
  449. $this->DivisionHeight = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $Divisions;
  450. $this->DivisionRatio = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $DataRange;
  451. $this->GAreaXOffset = 0;
  452. if ( count($Data) > 1 )
  453. {
  454. if ( $WithMargin == FALSE )
  455. $this->DivisionWidth = ( $this->GArea_X2 - $this->GArea_X1 ) / (count($Data)-1);
  456. else
  457. {
  458. $this->DivisionWidth = ( $this->GArea_X2 - $this->GArea_X1 ) / (count($Data));
  459. $this->GAreaXOffset = $this->DivisionWidth / 2;
  460. }
  461. }
  462. else
  463. {
  464. $this->DivisionWidth = $this->GArea_X2 - $this->GArea_X1;
  465. $this->GAreaXOffset = $this->DivisionWidth / 2;
  466. }
  467. $this->DataCount = count($Data);
  468. if ( $DrawTicks == FALSE )
  469. return(0);
  470. $YPos = $this->GArea_Y2; $XMin = NULL;
  471. for($i=1;$i<=$Divisions+1;$i++)
  472. {
  473. if ( $RightScale )
  474. $this->drawLine($this->GArea_X2,$YPos,$this->GArea_X2+5,$YPos,$R,$G,$B);
  475. else
  476. $this->drawLine($this->GArea_X1,$YPos,$this->GArea_X1-5,$YPos,$R,$G,$B);
  477. $Value = $this->VMin + ($i-1) * (( $this->VMax - $this->VMin ) / $Divisions);
  478. $Value = round($Value * pow(10,$Decimals)) / pow(10,$Decimals);
  479. if ( $DataDescription["Format"]["Y"] == "number" )
  480. $Value = $Value.$DataDescription["Unit"]["Y"];
  481. if ( $DataDescription["Format"]["Y"] == "time" )
  482. $Value = $this->ToTime($Value);
  483. if ( $DataDescription["Format"]["Y"] == "date" )
  484. $Value = $this->ToDate($Value);
  485. if ( $DataDescription["Format"]["Y"] == "metric" )
  486. $Value = $this->ToMetric($Value);
  487. if ( $DataDescription["Format"]["Y"] == "currency" )
  488. $Value = $this->ToCurrency($Value);
  489. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  490. $TextWidth = $Position[2]-$Position[0];
  491. if ( $RightScale )
  492. {
  493. imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X2+10,$YPos+($this->FontSize/2),$C_TextColor,$this->FontName,$Value);
  494. if ( $XMin < $this->GArea_X2+15+$TextWidth || $XMin == NULL ) { $XMin = $this->GArea_X2+15+$TextWidth; }
  495. }
  496. else
  497. {
  498. imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X1-10-$TextWidth,$YPos+($this->FontSize/2),$C_TextColor,$this->FontName,$Value);
  499. if ( $XMin > $this->GArea_X1-10-$TextWidth || $XMin == NULL ) { $XMin = $this->GArea_X1-10-$TextWidth; }
  500. }
  501. $YPos = $YPos - $this->DivisionHeight;
  502. }
  503. /* Write the Y Axis caption if set */
  504. if ( isset($DataDescription["Axis"]["Y"]) )
  505. {
  506. $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["Y"]);
  507. $TextHeight = abs($Position[1])+abs($Position[3]);
  508. $TextTop = (($this->GArea_Y2 - $this->GArea_Y1) / 2) + $this->GArea_Y1 + ($TextHeight/2);
  509. if ( $RightScale )
  510. imagettftext($this->Picture,$this->FontSize,90,$XMin+$this->FontSize,$TextTop,$C_TextColor,$this->FontName,$DataDescription["Axis"]["Y"]);
  511. else
  512. imagettftext($this->Picture,$this->FontSize,90,$XMin-$this->FontSize,$TextTop,$C_TextColor,$this->FontName,$DataDescription["Axis"]["Y"]);
  513. }
  514. /* Horizontal Axis */
  515. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  516. $ID = 1; $YMax = NULL;
  517. foreach ( $Data as $Key => $Values )
  518. {
  519. if ( $ID % $SkipLabels == 0 )
  520. {
  521. $this->drawLine(floor($XPos),$this->GArea_Y2,floor($XPos),$this->GArea_Y2+5,$R,$G,$B);
  522. $Value = $Data[$Key][$DataDescription["Position"]];
  523. if ( $DataDescription["Format"]["X"] == "number" )
  524. $Value = $Value.$DataDescription["Unit"]["X"];
  525. if ( $DataDescription["Format"]["X"] == "time" )
  526. $Value = $this->ToTime($Value);
  527. if ( $DataDescription["Format"]["X"] == "date" )
  528. $Value = $this->ToDate($Value);
  529. if ( $DataDescription["Format"]["X"] == "metric" )
  530. $Value = $this->ToMetric($Value);
  531. if ( $DataDescription["Format"]["X"] == "currency" )
  532. $Value = $this->ToCurrency($Value);
  533. $Position = imageftbbox($this->FontSize,$Angle,$this->FontName,$Value);
  534. $TextWidth = abs($Position[2])+abs($Position[0]);
  535. $TextHeight = abs($Position[1])+abs($Position[3]);
  536. if ( $Angle == 0 )
  537. {
  538. $YPos = $this->GArea_Y2+18;
  539. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-floor($TextWidth/2),$YPos,$C_TextColor,$this->FontName,$Value);
  540. }
  541. else
  542. {
  543. $YPos = $this->GArea_Y2+10+$TextHeight;
  544. if ( $Angle <= 90 )
  545. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value);
  546. else
  547. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)+$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value);
  548. }
  549. if ( $YMax < $YPos || $YMax == NULL ) { $YMax = $YPos; }
  550. }
  551. $XPos = $XPos + $this->DivisionWidth;
  552. $ID++;
  553. }
  554. /* Write the X Axis caption if set */
  555. if ( isset($DataDescription["Axis"]["X"]) )
  556. {
  557. $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["X"]);
  558. $TextWidth = abs($Position[2])+abs($Position[0]);
  559. $TextLeft = (($this->GArea_X2 - $this->GArea_X1) / 2) + $this->GArea_X1 + ($TextWidth/2);
  560. imagettftext($this->Picture,$this->FontSize,0,$TextLeft,$YMax+$this->FontSize+5,$C_TextColor,$this->FontName,$DataDescription["Axis"]["X"]);
  561. }
  562. }
  563. /* Compute and draw the scale for X/Y charts */
  564. function drawXYScale($Data,$DataDescription,$YSerieName,$XSerieName,$R,$G,$B,$WithMargin=0,$Angle=0,$Decimals=1)
  565. {
  566. /* Validate the Data and DataDescription array */
  567. $this->validateData("drawScale",$Data);
  568. $C_TextColor =$this->AllocateColor($this->Picture,$R,$G,$B);
  569. $this->drawLine($this->GArea_X1,$this->GArea_Y1,$this->GArea_X1,$this->GArea_Y2,$R,$G,$B);
  570. $this->drawLine($this->GArea_X1,$this->GArea_Y2,$this->GArea_X2,$this->GArea_Y2,$R,$G,$B);
  571. /* Process Y scale */
  572. if ( $this->VMin == NULL && $this->VMax == NULL)
  573. {
  574. $this->VMin = $Data[0][$YSerieName];
  575. $this->VMax = $Data[0][$YSerieName];
  576. foreach ( $Data as $Key => $Values )
  577. {
  578. if (isset($Data[$Key][$YSerieName]))
  579. {
  580. $Value = $Data[$Key][$YSerieName];
  581. if ( $this->VMax < $Value) { $this->VMax = $Value; }
  582. if ( $this->VMin > $Value) { $this->VMin = $Value; }
  583. }
  584. }
  585. if ( $this->VMax > preg_replace('/\.[0-9]+/','',$this->VMax) )
  586. $this->VMax = preg_replace('/\.[0-9]+/','',$this->VMax)+1;
  587. $DataRange = $this->VMax - $this->VMin;
  588. if ( $DataRange == 0 ) { $DataRange = .1; }
  589. /* Compute automatic scaling */
  590. $ScaleOk = FALSE; $Factor = 1;
  591. $MinDivHeight = 25; $MaxDivs = ($this->GArea_Y2 - $this->GArea_Y1) / $MinDivHeight;
  592. if ( $this->VMin == 0 && $this->VMax == 0 )
  593. { $this->VMin = 0; $this->VMax = 2; $Scale = 1; $Divisions = 2;}
  594. elseif ($MaxDivs > 1)
  595. {
  596. while(!$ScaleOk)
  597. {
  598. $Scale1 = ( $this->VMax - $this->VMin ) / $Factor;
  599. $Scale2 = ( $this->VMax - $this->VMin ) / $Factor / 2;
  600. $Scale4 = ( $this->VMax - $this->VMin ) / $Factor / 4;
  601. if ( $Scale1 > 1 && $Scale1 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale1); $Scale = 1;}
  602. if ( $Scale2 > 1 && $Scale2 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale2); $Scale = 2;}
  603. if (!$ScaleOk)
  604. {
  605. if ( $Scale2 > 1 ) { $Factor = $Factor * 10; }
  606. if ( $Scale2 < 1 ) { $Factor = $Factor / 10; }
  607. }
  608. }
  609. if ( floor($this->VMax / $Scale / $Factor) != $this->VMax / $Scale / $Factor)
  610. {
  611. $GridID = floor ( $this->VMax / $Scale / $Factor) + 1;
  612. $this->VMax = $GridID * $Scale * $Factor;
  613. $Divisions++;
  614. }
  615. if ( floor($this->VMin / $Scale / $Factor) != $this->VMin / $Scale / $Factor)
  616. {
  617. $GridID = floor( $this->VMin / $Scale / $Factor);
  618. $this->VMin = $GridID * $Scale * $Factor;
  619. $Divisions++;
  620. }
  621. }
  622. else /* Can occurs for small graphs */
  623. $Scale = 1;
  624. if ( !isset($Divisions) )
  625. $Divisions = 2;
  626. if ( $this->isRealInt(($this->VMax-$this->VMin)/($Divisions-1)))
  627. $Divisions--;
  628. elseif ( $this->isRealInt(($this->VMax-$this->VMin)/($Divisions+1)))
  629. $Divisions++;
  630. }
  631. else
  632. $Divisions = $this->Divisions;
  633. $this->DivisionCount = $Divisions;
  634. $DataRange = $this->VMax - $this->VMin;
  635. if ( $DataRange == 0 ) { $DataRange = .1; }
  636. $this->DivisionHeight = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $Divisions;
  637. $this->DivisionRatio = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $DataRange;
  638. $YPos = $this->GArea_Y2; $XMin = NULL;
  639. for($i=1;$i<=$Divisions+1;$i++)
  640. {
  641. $this->drawLine($this->GArea_X1,$YPos,$this->GArea_X1-5,$YPos,$R,$G,$B);
  642. $Value = $this->VMin + ($i-1) * (( $this->VMax - $this->VMin ) / $Divisions);
  643. $Value = round($Value * pow(10,$Decimals)) / pow(10,$Decimals);
  644. if ( $DataDescription["Format"]["Y"] == "number" )
  645. $Value = $Value.$DataDescription["Unit"]["Y"];
  646. if ( $DataDescription["Format"]["Y"] == "time" )
  647. $Value = $this->ToTime($Value);
  648. if ( $DataDescription["Format"]["Y"] == "date" )
  649. $Value = $this->ToDate($Value);
  650. if ( $DataDescription["Format"]["Y"] == "metric" )
  651. $Value = $this->ToMetric($Value);
  652. if ( $DataDescription["Format"]["Y"] == "currency" )
  653. $Value = $this->ToCurrency($Value);
  654. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  655. $TextWidth = $Position[2]-$Position[0];
  656. imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X1-10-$TextWidth,$YPos+($this->FontSize/2),$C_TextColor,$this->FontName,$Value);
  657. if ( $XMin > $this->GArea_X1-10-$TextWidth || $XMin == NULL ) { $XMin = $this->GArea_X1-10-$TextWidth; }
  658. $YPos = $YPos - $this->DivisionHeight;
  659. }
  660. /* Process X scale */
  661. if ( $this->VXMin == NULL && $this->VXMax == NULL)
  662. {
  663. $this->VXMin = $Data[0][$XSerieName];
  664. $this->VXMax = $Data[0][$XSerieName];
  665. foreach ( $Data as $Key => $Values )
  666. {
  667. if (isset($Data[$Key][$XSerieName]))
  668. {
  669. $Value = $Data[$Key][$XSerieName];
  670. if ( $this->VXMax < $Value) { $this->VXMax = $Value; }
  671. if ( $this->VXMin > $Value) { $this->VXMin = $Value; }
  672. }
  673. }
  674. if ( $this->VXMax > preg_replace('/\.[0-9]+/','',$this->VXMax) )
  675. $this->VXMax = preg_replace('/\.[0-9]+/','',$this->VXMax)+1;
  676. $DataRange = $this->VMax - $this->VMin;
  677. if ( $DataRange == 0 ) { $DataRange = .1; }
  678. /* Compute automatic scaling */
  679. $ScaleOk = FALSE; $Factor = 1;
  680. $MinDivWidth = 25; $MaxDivs = ($this->GArea_X2 - $this->GArea_X1) / $MinDivWidth;
  681. if ( $this->VXMin == 0 && $this->VXMax == 0 )
  682. { $this->VXMin = 0; $this->VXMax = 2; $Scale = 1; $XDivisions = 2;}
  683. elseif ($MaxDivs > 1)
  684. {
  685. while(!$ScaleOk)
  686. {
  687. $Scale1 = ( $this->VXMax - $this->VXMin ) / $Factor;
  688. $Scale2 = ( $this->VXMax - $this->VXMin ) / $Factor / 2;
  689. $Scale4 = ( $this->VXMax - $this->VXMin ) / $Factor / 4;
  690. if ( $Scale1 > 1 && $Scale1 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $XDivisions = floor($Scale1); $Scale = 1;}
  691. if ( $Scale2 > 1 && $Scale2 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $XDivisions = floor($Scale2); $Scale = 2;}
  692. if (!$ScaleOk)
  693. {
  694. if ( $Scale2 > 1 ) { $Factor = $Factor * 10; }
  695. if ( $Scale2 < 1 ) { $Factor = $Factor / 10; }
  696. }
  697. }
  698. if ( floor($this->VXMax / $Scale / $Factor) != $this->VXMax / $Scale / $Factor)
  699. {
  700. $GridID = floor ( $this->VXMax / $Scale / $Factor) + 1;
  701. $this->VXMax = $GridID * $Scale * $Factor;
  702. $XDivisions++;
  703. }
  704. if ( floor($this->VXMin / $Scale / $Factor) != $this->VXMin / $Scale / $Factor)
  705. {
  706. $GridID = floor( $this->VXMin / $Scale / $Factor);
  707. $this->VXMin = $GridID * $Scale * $Factor;
  708. $XDivisions++;
  709. }
  710. }
  711. else /* Can occurs for small graphs */
  712. $Scale = 1;
  713. if ( !isset($XDivisions) )
  714. $XDivisions = 2;
  715. if ( $this->isRealInt(($this->VXMax-$this->VXMin)/($XDivisions-1)))
  716. $XDivisions--;
  717. elseif ( $this->isRealInt(($this->VXMax-$this->VXMin)/($XDivisions+1)))
  718. $XDivisions++;
  719. }
  720. else
  721. $XDivisions = $this->XDivisions;
  722. $this->XDivisionCount = $Divisions;
  723. $this->DataCount = $Divisions + 2;
  724. $XDataRange = $this->VXMax - $this->VXMin;
  725. if ( $XDataRange == 0 ) { $XDataRange = .1; }
  726. $this->DivisionWidth = ( $this->GArea_X2 - $this->GArea_X1 ) / $XDivisions;
  727. $this->XDivisionRatio = ( $this->GArea_X2 - $this->GArea_X1 ) / $XDataRange;
  728. $XPos = $this->GArea_X1; $YMax = NULL;
  729. for($i=1;$i<=$XDivisions+1;$i++)
  730. {
  731. $this->drawLine($XPos,$this->GArea_Y2,$XPos,$this->GArea_Y2+5,$R,$G,$B);
  732. $Value = $this->VXMin + ($i-1) * (( $this->VXMax - $this->VXMin ) / $XDivisions);
  733. $Value = round($Value * pow(10,$Decimals)) / pow(10,$Decimals);
  734. if ( $DataDescription["Format"]["Y"] == "number" )
  735. $Value = $Value.$DataDescription["Unit"]["Y"];
  736. if ( $DataDescription["Format"]["Y"] == "time" )
  737. $Value = $this->ToTime($Value);
  738. if ( $DataDescription["Format"]["Y"] == "date" )
  739. $Value = $this->ToDate($Value);
  740. if ( $DataDescription["Format"]["Y"] == "metric" )
  741. $Value = $this->ToMetric($Value);
  742. if ( $DataDescription["Format"]["Y"] == "currency" )
  743. $Value = $this->ToCurrency($Value);
  744. $Position = imageftbbox($this->FontSize,$Angle,$this->FontName,$Value);
  745. $TextWidth = abs($Position[2])+abs($Position[0]);
  746. $TextHeight = abs($Position[1])+abs($Position[3]);
  747. if ( $Angle == 0 )
  748. {
  749. $YPos = $this->GArea_Y2+18;
  750. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-floor($TextWidth/2),$YPos,$C_TextColor,$this->FontName,$Value);
  751. }
  752. else
  753. {
  754. $YPos = $this->GArea_Y2+10+$TextHeight;
  755. if ( $Angle <= 90 )
  756. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value);
  757. else
  758. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)+$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value);
  759. }
  760. if ( $YMax < $YPos || $YMax == NULL ) { $YMax = $YPos; }
  761. $XPos = $XPos + $this->DivisionWidth;
  762. }
  763. /* Write the Y Axis caption if set */
  764. if ( isset($DataDescription["Axis"]["Y"]) )
  765. {
  766. $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["Y"]);
  767. $TextHeight = abs($Position[1])+abs($Position[3]);
  768. $TextTop = (($this->GArea_Y2 - $this->GArea_Y1) / 2) + $this->GArea_Y1 + ($TextHeight/2);
  769. imagettftext($this->Picture,$this->FontSize,90,$XMin-$this->FontSize,$TextTop,$C_TextColor,$this->FontName,$DataDescription["Axis"]["Y"]);
  770. }
  771. /* Write the X Axis caption if set */
  772. if ( isset($DataDescription["Axis"]["X"]) )
  773. {
  774. $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["X"]);
  775. $TextWidth = abs($Position[2])+abs($Position[0]);
  776. $TextLeft = (($this->GArea_X2 - $this->GArea_X1) / 2) + $this->GArea_X1 + ($TextWidth/2);
  777. imagettftext($this->Picture,$this->FontSize,0,$TextLeft,$YMax+$this->FontSize+5,$C_TextColor,$this->FontName,$DataDescription["Axis"]["X"]);
  778. }
  779. }
  780. /* Compute and draw the scale */
  781. function drawGrid($LineWidth,$Mosaic=TRUE,$R=220,$G=220,$B=220,$Alpha=100)
  782. {
  783. /* Draw mosaic */
  784. if ( $Mosaic )
  785. {
  786. $LayerWidth = $this->GArea_X2-$this->GArea_X1;
  787. $LayerHeight = $this->GArea_Y2-$this->GArea_Y1;
  788. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  789. $C_White =$this->AllocateColor($this->Layers[0],255,255,255);
  790. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  791. imagecolortransparent($this->Layers[0],$C_White);
  792. $C_Rectangle =$this->AllocateColor($this->Layers[0],250,250,250);
  793. $YPos = $LayerHeight; //$this->GArea_Y2-1;
  794. $LastY = $YPos;
  795. for($i=0;$i<=$this->DivisionCount;$i++)
  796. {
  797. $LastY = $YPos;
  798. $YPos = $YPos - $this->DivisionHeight;
  799. if ( $YPos <= 0 ) { $YPos = 1; }
  800. if ( $i % 2 == 0 )
  801. {
  802. imagefilledrectangle($this->Layers[0],1,$YPos,$LayerWidth-1,$LastY,$C_Rectangle);
  803. }
  804. }
  805. imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha);
  806. imagedestroy($this->Layers[0]);
  807. }
  808. /* Horizontal lines */
  809. $YPos = $this->GArea_Y2 - $this->DivisionHeight;
  810. for($i=1;$i<=$this->DivisionCount;$i++)
  811. {
  812. if ( $YPos > $this->GArea_Y1 && $YPos < $this->GArea_Y2 )
  813. $this->drawDottedLine($this->GArea_X1,$YPos,$this->GArea_X2,$YPos,$LineWidth,$R,$G,$B);
  814. $YPos = $YPos - $this->DivisionHeight;
  815. }
  816. /* Vertical lines */
  817. if ( $this->GAreaXOffset == 0 )
  818. { $XPos = $this->GArea_X1 + $this->DivisionWidth + $this->GAreaXOffset; $ColCount = $this->DataCount-2; }
  819. else
  820. { $XPos = $this->GArea_X1 + $this->GAreaXOffset; $ColCount = floor( ($this->GArea_X2 - $this->GArea_X1) / $this->DivisionWidth ); }
  821. for($i=1;$i<=$ColCount;$i++)
  822. {
  823. if ( $XPos > $this->GArea_X1 && $XPos < $this->GArea_X2 )
  824. $this->drawDottedLine(floor($XPos),$this->GArea_Y1,floor($XPos),$this->GArea_Y2,$LineWidth,$R,$G,$B);
  825. $XPos = $XPos + $this->DivisionWidth;
  826. }
  827. }
  828. /* retrieve the legends size */
  829. function getLegendBoxSize($DataDescription)
  830. {
  831. if ( !isset($DataDescription["Description"]) )
  832. return(-1);
  833. /* <-10->[8]<-4->Text<-10-> */
  834. $MaxWidth = 0; $MaxHeight = 8;
  835. foreach($DataDescription["Description"] as $Key => $Value)
  836. {
  837. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  838. $TextWidth = $Position[2]-$Position[0];
  839. $TextHeight = $Position[1]-$Position[7];
  840. if ( $TextWidth > $MaxWidth) { $MaxWidth = $TextWidth; }
  841. $MaxHeight = $MaxHeight + $TextHeight + 4;
  842. }
  843. $MaxHeight = $MaxHeight - 3;
  844. $MaxWidth = $MaxWidth + 32;
  845. return(array($MaxWidth,$MaxHeight));
  846. }
  847. /* Draw the data legends */
  848. function drawLegend($XPos,$YPos,$DataDescription,$R,$G,$B,$Rs=-1,$Gs=-1,$Bs=-1,$Rt=0,$Gt=0,$Bt=0,$Border=TRUE)
  849. {
  850. /* Validate the Data and DataDescription array */
  851. $this->validateDataDescription("drawLegend",$DataDescription);
  852. if ( !isset($DataDescription["Description"]) )
  853. return(-1);
  854. $C_TextColor =$this->AllocateColor($this->Picture,$Rt,$Gt,$Bt);
  855. /* <-10->[8]<-4->Text<-10-> */
  856. $MaxWidth = 0; $MaxHeight = 8;
  857. foreach($DataDescription["Description"] as $Key => $Value)
  858. {
  859. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  860. $TextWidth = $Position[2]-$Position[0];
  861. $TextHeight = $Position[1]-$Position[7];
  862. if ( $TextWidth > $MaxWidth) { $MaxWidth = $TextWidth; }
  863. $MaxHeight = $MaxHeight + $TextHeight + 4;
  864. }
  865. $MaxHeight = $MaxHeight - 5;
  866. $MaxWidth = $MaxWidth + 32;
  867. if ( $Rs == -1 || $Gs == -1 || $Bs == -1 )
  868. { $Rs = $R-30; $Gs = $G-30; $Bs = $B-30; }
  869. if ( $Border )
  870. {
  871. $this->drawFilledRoundedRectangle($XPos+1,$YPos+1,$XPos+$MaxWidth+1,$YPos+$MaxHeight+1,5,$Rs,$Gs,$Bs);
  872. $this->drawFilledRoundedRectangle($XPos,$YPos,$XPos+$MaxWidth,$YPos+$MaxHeight,5,$R,$G,$B);
  873. }
  874. $YOffset = 4 + $this->FontSize; $ID = 0;
  875. foreach($DataDescription["Description"] as $Key => $Value)
  876. {
  877. $this->drawFilledRoundedRectangle($XPos+10,$YPos+$YOffset-4,$XPos+14,$YPos+$YOffset-4,2,$this->Palette[$ID]["R"],$this->Palette[$ID]["G"],$this->Palette[$ID]["B"]);
  878. imagettftext($this->Picture,$this->FontSize,0,$XPos+22,$YPos+$YOffset,$C_TextColor,$this->FontName,$Value);
  879. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  880. $TextHeight = $Position[1]-$Position[7];
  881. $YOffset = $YOffset + $TextHeight + 4;
  882. $ID++;
  883. }
  884. }
  885. /* Draw the data legends */
  886. function drawPieLegend($XPos,$YPos,$Data,$DataDescription,$R,$G,$B)
  887. {
  888. /* Validate the Data and DataDescription array */
  889. $this->validateDataDescription("drawPieLegend",$DataDescription,FALSE);
  890. $this->validateData("drawPieLegend",$Data);
  891. if ( !isset($DataDescription["Position"]) )
  892. return(-1);
  893. $C_TextColor =$this->AllocateColor($this->Picture,0,0,0);
  894. /* <-10->[8]<-4->Text<-10-> */
  895. $MaxWidth = 0; $MaxHeight = 8;
  896. foreach($Data as $Key => $Value)
  897. {
  898. $Value2 = $Value[$DataDescription["Position"]];
  899. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value2);
  900. $TextWidth = $Position[2]-$Position[0];
  901. $TextHeight = $Position[1]-$Position[7];
  902. if ( $TextWidth > $MaxWidth) { $MaxWidth = $TextWidth; }
  903. $MaxHeight = $MaxHeight + $TextHeight + 4;
  904. }
  905. $MaxHeight = $MaxHeight - 3;
  906. $MaxWidth = $MaxWidth + 32;
  907. $this->drawFilledRoundedRectangle($XPos+1,$YPos+1,$XPos+$MaxWidth+1,$YPos+$MaxHeight+1,5,$R-30,$G-30,$B-30);
  908. $this->drawFilledRoundedRectangle($XPos,$YPos,$XPos+$MaxWidth,$YPos+$MaxHeight,5,$R,$G,$B);
  909. $YOffset = 4 + $this->FontSize; $ID = 0;
  910. foreach($Data as $Key => $Value)
  911. {
  912. $Value2 = $Value[$DataDescription["Position"]];
  913. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value2);
  914. $TextHeight = $Position[1]-$Position[7];
  915. $this->drawFilledRectangle($XPos+10,$YPos+$YOffset-6,$XPos+14,$YPos+$YOffset-2,$this->Palette[$ID]["R"],$this->Palette[$ID]["G"],$this->Palette[$ID]["B"]);
  916. imagettftext($this->Picture,$this->FontSize,0,$XPos+22,$YPos+$YOffset,$C_TextColor,$this->FontName,$Value2);
  917. $YOffset = $YOffset + $TextHeight + 4;
  918. $ID++;
  919. }
  920. }
  921. /* Draw the graph title */
  922. function drawTitle($XPos,$YPos,$Value,$R,$G,$B,$XPos2=-1,$YPos2=-1,$Shadow=FALSE)
  923. {
  924. $C_TextColor = $this->AllocateColor($this->Picture,$R,$G,$B);
  925. if ( $XPos2 != -1 )
  926. {
  927. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  928. $TextWidth = $Position[2]-$Position[0];
  929. $XPos = floor(( $XPos2 - $XPos - $TextWidth ) / 2 ) + $XPos;
  930. }
  931. if ( $YPos2 != -1 )
  932. {
  933. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  934. $TextHeight = $Position[5]-$Position[3];
  935. $YPos = floor(( $YPos2 - $YPos - $TextHeight ) / 2 ) + $YPos;
  936. }
  937. if ( $Shadow )
  938. {
  939. $C_ShadowColor = $this->AllocateColor($this->Picture,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor);
  940. imagettftext($this->Picture,$this->FontSize,0,$XPos+$this->ShadowXDistance,$YPos+$this->ShadowYDistance,$C_ShadowColor,$this->FontName,$Value);
  941. }
  942. imagettftext($this->Picture,$this->FontSize,0,$XPos,$YPos,$C_TextColor,$this->FontName,$Value);
  943. }
  944. /* Draw a text box with text align & alpha properties */
  945. function drawTextBox($X1,$Y1,$X2,$Y2,$Text,$Angle=0,$R=255,$G=255,$B=255,$Align=ALIGN_LEFT,$Shadow=TRUE,$BgR=-1,$BgG=-1,$BgB=-1,$Alpha=100)
  946. {
  947. $Position = imageftbbox($this->FontSize,$Angle,$this->FontName,$Text);
  948. $TextWidth = $Position[2]-$Position[0];
  949. $TextHeight = $Position[5]-$Position[3];
  950. $AreaWidth = $X2 - $X1;
  951. $AreaHeight = $Y2 - $Y1;
  952. if ( $BgR != -1 && $BgG != -1 && $BgB != -1 )
  953. $this->drawFilledRectangle($X1,$Y1,$X2,$Y2,$BgR,$BgG,$BgB,FALSE,$Alpha);
  954. if ( $Align == ALIGN_TOP_LEFT ) { $X = $X1+1; $Y = $Y1+$this->FontSize+1; }
  955. if ( $Align == ALIGN_TOP_CENTER ) { $X = $X1+($AreaWidth/2)-($TextWidth/2); $Y = $Y1+$this->FontSize+1; }
  956. if ( $Align == ALIGN_TOP_RIGHT ) { $X = $X2-$TextWidth-1; $Y = $Y1+$this->FontSize+1; }
  957. if ( $Align == ALIGN_LEFT ) { $X = $X1+1; $Y = $Y1+($AreaHeight/2)-($TextHeight/2); }
  958. if ( $Align == ALIGN_CENTER ) { $X = $X1+($AreaWidth/2)-($TextWidth/2); $Y = $Y1+($AreaHeight/2)-($TextHeight/2); }
  959. if ( $Align == ALIGN_RIGHT ) { $X = $X2-$TextWidth-1; $Y = $Y1+($AreaHeight/2)-($TextHeight/2); }
  960. if ( $Align == ALIGN_BOTTOM_LEFT ) { $X = $X1+1; $Y = $Y2-1; }
  961. if ( $Align == ALIGN_BOTTOM_CENTER ) { $X = $X1+($AreaWidth/2)-($TextWidth/2); $Y = $Y2-1; }
  962. if ( $Align == ALIGN_BOTTOM_RIGHT ) { $X = $X2-$TextWidth-1; $Y = $Y2-1; }
  963. $C_TextColor =$this->AllocateColor($this->Picture,$R,$G,$B);
  964. $C_ShadowColor =$this->AllocateColor($this->Picture,0,0,0);
  965. if ( $Shadow )
  966. imagettftext($this->Picture,$this->FontSize,$Angle,$X+1,$Y+1,$C_ShadowColor,$this->FontName,$Text);
  967. imagettftext($this->Picture,$this->FontSize,$Angle,$X,$Y,$C_TextColor,$this->FontName,$Text);
  968. }
  969. /* Compute and draw the scale */
  970. function drawTreshold($Value,$R,$G,$B,$ShowLabel=FALSE,$ShowOnRight=FALSE,$TickWidth=4,$FreeText=NULL)
  971. {
  972. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  973. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  974. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  975. $C_TextColor =$this->AllocateColor($this->Picture,$R,$G,$B);
  976. $Y = $this->GArea_Y2 - ($Value - $this->VMin) * $this->DivisionRatio;
  977. if ( $Y <= $this->GArea_Y1 || $Y >= $this->GArea_Y2 )
  978. return(-1);
  979. if ( $TickWidth == 0 )
  980. $this->drawLine($this->GArea_X1,$Y,$this->GArea_X2,$Y,$R,$G,$B);
  981. else
  982. $this->drawDottedLine($this->GArea_X1,$Y,$this->GArea_X2,$Y,$TickWidth,$R,$G,$B);
  983. if ( $ShowLabel )
  984. {
  985. if ( $FreeText == NULL )
  986. { $Label = $Value; } else { $Label = $FreeText; }
  987. if ( $ShowOnRight )
  988. imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X2+2,$Y+($this->FontSize/2),$C_TextColor,$this->FontName,$Label);
  989. else
  990. imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X1+2,$Y-($this->FontSize/2),$C_TextColor,$this->FontName,$Label);
  991. }
  992. }
  993. /* This function put a label on a specific point */
  994. function setLabel($Data,$DataDescription,$SerieName,$ValueName,$Caption,$R=210,$G=210,$B=210)
  995. {
  996. /* Validate the Data and DataDescription array */
  997. $this->validateDataDescription("setLabel",$DataDescription);
  998. $this->validateData("setLabel",$Data);
  999. $ShadowFactor = 100;
  1000. $C_Label =$this->AllocateColor($this->Picture,$R,$G,$B);
  1001. $C_Shadow =$this->AllocateColor($this->Picture,$R-$ShadowFactor,$G-$ShadowFactor,$B-$ShadowFactor);
  1002. $C_TextColor =$this->AllocateColor($this->Picture,0,0,0);
  1003. $Cp = 0; $Found = FALSE;
  1004. foreach ( $Data as $Key => $Value )
  1005. {
  1006. if ( $Data[$Key][$DataDescription["Position"]] == $ValueName )
  1007. { $NumericalValue = $Data[$Key][$SerieName]; $Found = TRUE; }
  1008. if ( !$Found )
  1009. $Cp++;
  1010. }
  1011. $XPos = $this->GArea_X1 + $this->GAreaXOffset + ( $this->DivisionWidth * $Cp ) + 2;
  1012. $YPos = $this->GArea_Y2 - ($NumericalValue - $this->VMin) * $this->DivisionRatio;
  1013. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Caption);
  1014. $TextHeight = $Position[3] - $Position[5];
  1015. $TextWidth = $Position[2]-$Position[0] + 2;
  1016. $TextOffset = floor($TextHeight/2);
  1017. // Shadow
  1018. $Poly = array($XPos+1,$YPos+1,$XPos + 9,$YPos - $TextOffset,$XPos + 8,$YPos + $TextOffset + 2);
  1019. imagefilledpolygon($this->Picture,$Poly,3,$C_Shadow);
  1020. $this->drawLine($XPos,$YPos+1,$XPos + 9,$YPos - $TextOffset - .2,$R-$ShadowFactor,$G-$ShadowFactor,$B-$ShadowFactor);
  1021. $this->drawLine($XPos,$YPos+1,$XPos + 9,$YPos + $TextOffset + 2.2,$R-$ShadowFactor,$G-$ShadowFactor,$B-$ShadowFactor);
  1022. $this->drawFilledRectangle($XPos + 9,$YPos - $TextOffset-.2,$XPos + 13 + $TextWidth,$YPos + $TextOffset + 2.2,$R-$ShadowFactor,$G-$ShadowFactor,$B-$ShadowFactor);
  1023. // Label background
  1024. $Poly = array($XPos,$YPos,$XPos + 8,$YPos - $TextOffset - 1,$XPos + 8,$YPos + $TextOffset + 1);
  1025. imagefilledpolygon($this->Picture,$Poly,3,$C_Label);
  1026. $this->drawLine($XPos-1,$YPos,$XPos + 8,$YPos - $TextOffset - 1.2,$R,$G,$B);
  1027. $this->drawLine($XPos-1,$YPos,$XPos + 8,$YPos + $TextOffset + 1.2,$R,$G,$B);
  1028. $this->drawFilledRectangle($XPos + 8,$YPos - $TextOffset - 1.2,$XPos + 12 + $TextWidth,$YPos + $TextOffset + 1.2,$R,$G,$B);
  1029. imagettftext($this->Picture,$this->FontSize,0,$XPos + 10,$YPos + $TextOffset,$C_TextColor,$this->FontName,$Caption);
  1030. }
  1031. /* This function draw a plot graph */
  1032. function drawPlotGraph($Data,$DataDescription,$BigRadius=5,$SmallRadius=2,$R2=-1,$G2=-1,$B2=-1,$Shadow=FALSE)
  1033. {
  1034. /* Validate the Data and DataDescription array */
  1035. $this->validateDataDescription("drawPlotGraph",$DataDescription);
  1036. $this->validateDa

Large files files are truncated, but you can click here to view the full file