PageRenderTime 77ms CodeModel.GetById 18ms RepoModel.GetById 0ms 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
  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->validateData("drawPlotGraph",$Data);
  1037. $GraphID = 0;
  1038. $Ro = $R2; $Go = $G2; $Bo = $B2;
  1039. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1040. {
  1041. $ID = 0;
  1042. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1043. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1044. $R = $this->Palette[$ColorID]["R"];
  1045. $G = $this->Palette[$ColorID]["G"];
  1046. $B = $this->Palette[$ColorID]["B"];
  1047. $R2 = $Ro; $G2 = $Go; $B2 = $Bo;
  1048. if ( isset($DataDescription["Symbol"][$ColName]) )
  1049. {
  1050. $Is_Alpha = ((ord ( file_get_contents ($DataDescription["Symbol"][$ColName], false, null, 25, 1)) & 6) & 4) == 4;
  1051. $Infos = getimagesize($DataDescription["Symbol"][$ColName]);
  1052. $ImageWidth = $Infos[0];
  1053. $ImageHeight = $Infos[1];
  1054. $Symbol = imagecreatefromgif($DataDescription["Symbol"][$ColName]);
  1055. }
  1056. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  1057. $Hsize = round($BigRadius/2);
  1058. $R3 = -1; $G3 = -1; $B3 = -1;
  1059. foreach ( $Data as $Key => $Values )
  1060. {
  1061. $Value = $Data[$Key][$ColName];
  1062. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  1063. /* Save point into the image map if option activated */
  1064. if ( $this->BuildMap )
  1065. $this->addToImageMap($XPos-$Hsize,$YPos-$Hsize,$XPos+1+$Hsize,$YPos+$Hsize+1,$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"Plot");
  1066. if ( is_numeric($Value) )
  1067. {
  1068. if ( !isset($DataDescription["Symbol"][$ColName]) )
  1069. {
  1070. if ( $Shadow )
  1071. {
  1072. if ( $R3 !=-1 && $G3 !=-1 && $B3 !=-1 )
  1073. $this->drawFilledCircle($XPos+2,$YPos+2,$BigRadius,$R3,$G3,$B3);
  1074. else
  1075. {
  1076. $R3 = $this->Palette[$ColorID]["R"]-20; if ( $R3 < 0 ) { $R3 = 0; }
  1077. $G3 = $this->Palette[$ColorID]["G"]-20; if ( $G3 < 0 ) { $G3 = 0; }
  1078. $B3 = $this->Palette[$ColorID]["B"]-20; if ( $B3 < 0 ) { $B3 = 0; }
  1079. $this->drawFilledCircle($XPos+2,$YPos+2,$BigRadius,$R3,$G3,$B3);
  1080. }
  1081. }
  1082. $this->drawFilledCircle($XPos+1,$YPos+1,$BigRadius,$R,$G,$B);
  1083. if ( $SmallRadius != 0 )
  1084. {
  1085. if ( $R2 !=-1 && $G2 !=-1 && $B2 !=-1 )
  1086. $this->drawFilledCircle($XPos+1,$YPos+1,$SmallRadius,$R2,$G2,$B2);
  1087. else
  1088. {
  1089. $R2 = $this->Palette[$ColorID]["R"]-15; if ( $R2 < 0 ) { $R2 = 0; }
  1090. $G2 = $this->Palette[$ColorID]["G"]-15; if ( $G2 < 0 ) { $G2 = 0; }
  1091. $B2 = $this->Palette[$ColorID]["B"]-15; if ( $B2 < 0 ) { $B2 = 0; }
  1092. $this->drawFilledCircle($XPos+1,$YPos+1,$SmallRadius,$R2,$G2,$B2);
  1093. }
  1094. }
  1095. }
  1096. else
  1097. {
  1098. imagecopymerge($this->Picture,$Symbol,$XPos+1-$ImageWidth/2,$YPos+1-$ImageHeight/2,0,0,$ImageWidth,$ImageHeight,100);
  1099. }
  1100. }
  1101. $XPos = $XPos + $this->DivisionWidth;
  1102. }
  1103. $GraphID++;
  1104. }
  1105. }
  1106. /* This function draw a plot graph in an X/Y space */
  1107. function drawXYPlotGraph($Data,$DataDescription,$YSerieName,$XSerieName,$PaletteID=0,$BigRadius=5,$SmallRadius=2,$R2=-1,$G2=-1,$B2=-1,$Shadow=TRUE)
  1108. {
  1109. $R = $this->Palette[$PaletteID]["R"];
  1110. $G = $this->Palette[$PaletteID]["G"];
  1111. $B = $this->Palette[$PaletteID]["B"];
  1112. $R3 = -1; $G3 = -1; $B3 = -1;
  1113. $YLast = -1; $XLast = -1;
  1114. foreach ( $Data as $Key => $Values )
  1115. {
  1116. if ( isset($Data[$Key][$YSerieName]) && isset($Data[$Key][$XSerieName]) )
  1117. {
  1118. $X = $Data[$Key][$XSerieName];
  1119. $Y = $Data[$Key][$YSerieName];
  1120. $Y = $this->GArea_Y2 - (($Y-$this->VMin) * $this->DivisionRatio);
  1121. $X = $this->GArea_X1 + (($X-$this->VXMin) * $this->XDivisionRatio);
  1122. if ( $Shadow )
  1123. {
  1124. if ( $R3 !=-1 && $G3 !=-1 && $B3 !=-1 )
  1125. $this->drawFilledCircle($X+2,$Y+2,$BigRadius,$R3,$G3,$B3);
  1126. else
  1127. {
  1128. $R3 = $this->Palette[$PaletteID]["R"]-20; if ( $R < 0 ) { $R = 0; }
  1129. $G3 = $this->Palette[$PaletteID]["G"]-20; if ( $G < 0 ) { $G = 0; }
  1130. $B3 = $this->Palette[$PaletteID]["B"]-20; if ( $B < 0 ) { $B = 0; }
  1131. $this->drawFilledCircle($X+2,$Y+2,$BigRadius,$R3,$G3,$B3);
  1132. }
  1133. }
  1134. $this->drawFilledCircle($X+1,$Y+1,$BigRadius,$R,$G,$B);
  1135. if ( $R2 !=-1 && $G2 !=-1 && $B2 !=-1 )
  1136. $this->drawFilledCircle($X+1,$Y+1,$SmallRadius,$R2,$G2,$B2);
  1137. else
  1138. {
  1139. $R2 = $this->Palette[$PaletteID]["R"]+20; if ( $R > 255 ) { $R = 255; }
  1140. $G2 = $this->Palette[$PaletteID]["G"]+20; if ( $G > 255 ) { $G = 255; }
  1141. $B2 = $this->Palette[$PaletteID]["B"]+20; if ( $B > 255 ) { $B = 255; }
  1142. $this->drawFilledCircle($X+1,$Y+1,$SmallRadius,$R2,$G2,$B2);
  1143. }
  1144. }
  1145. }
  1146. }
  1147. /* This function draw an area between two series */
  1148. function drawArea($Data,$Serie1,$Serie2,$R,$G,$B,$Alpha = 50)
  1149. {
  1150. /* Validate the Data and DataDescription array */
  1151. $this->validateData("drawArea",$Data);
  1152. $LayerWidth = $this->GArea_X2-$this->GArea_X1;
  1153. $LayerHeight = $this->GArea_Y2-$this->GArea_Y1;
  1154. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  1155. $C_White =$this->AllocateColor($this->Layers[0],255,255,255);
  1156. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  1157. imagecolortransparent($this->Layers[0],$C_White);
  1158. $C_Graph =$this->AllocateColor($this->Layers[0],$R,$G,$B);
  1159. $XPos = $this->GAreaXOffset;
  1160. $LastXPos = -1;
  1161. foreach ( $Data as $Key => $Values )
  1162. {
  1163. $Value1 = $Data[$Key][$Serie1];
  1164. $Value2 = $Data[$Key][$Serie2];
  1165. $YPos1 = $LayerHeight - (($Value1-$this->VMin) * $this->DivisionRatio);
  1166. $YPos2 = $LayerHeight - (($Value2-$this->VMin) * $this->DivisionRatio);
  1167. if ( $LastXPos != -1 )
  1168. {
  1169. $Points = "";
  1170. $Points[] = $LastXPos; $Points[] = $LastYPos1;
  1171. $Points[] = $LastXPos; $Points[] = $LastYPos2;
  1172. $Points[] = $XPos; $Points[] = $YPos2;
  1173. $Points[] = $XPos; $Points[] = $YPos1;
  1174. imagefilledpolygon($this->Layers[0],$Points,4,$C_Graph);
  1175. }
  1176. $LastYPos1 = $YPos1;
  1177. $LastYPos2 = $YPos2;
  1178. $LastXPos = $XPos;
  1179. $XPos = $XPos + $this->DivisionWidth;
  1180. }
  1181. imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha);
  1182. imagedestroy($this->Layers[0]);
  1183. }
  1184. /* This function write the values of the specified series */
  1185. function writeValues($Data,$DataDescription,$Series)
  1186. {
  1187. /* Validate the Data and DataDescription array */
  1188. $this->validateDataDescription("writeValues",$DataDescription);
  1189. $this->validateData("writeValues",$Data);
  1190. if ( !is_array($Series) ) { $Series = array($Series); }
  1191. foreach($Series as $Key => $Serie)
  1192. {
  1193. $ID = 0;
  1194. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1195. { if ( $keyI == $Serie ) { $ColorID = $ID; }; $ID++; }
  1196. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  1197. $XLast = -1;
  1198. foreach ( $Data as $Key => $Values )
  1199. {
  1200. if ( isset($Data[$Key][$Serie]) && is_numeric($Data[$Key][$Serie]))
  1201. {
  1202. $Value = $Data[$Key][$Serie];
  1203. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  1204. $Positions = imagettfbbox($this->FontSize,0,$this->FontName,$Value);
  1205. $Width = $Positions[2] - $Positions[6]; $XOffset = $XPos - ($Width/2);
  1206. $Height = $Positions[3] - $Positions[7]; $YOffset = $YPos - 4;
  1207. $C_TextColor =$this->AllocateColor($this->Picture,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1208. imagettftext($this->Picture,$this->FontSize,0,$XOffset,$YOffset,$C_TextColor,$this->FontName,$Value);
  1209. }
  1210. $XPos = $XPos + $this->DivisionWidth;
  1211. }
  1212. }
  1213. }
  1214. /* This function draw a line graph */
  1215. function drawLineGraph($Data,$DataDescription,$SerieName="")
  1216. {
  1217. /* Validate the Data and DataDescription array */
  1218. $this->validateDataDescription("drawLineGraph",$DataDescription);
  1219. $this->validateData("drawLineGraph",$Data);
  1220. $GraphID = 0;
  1221. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1222. {
  1223. $ID = 0;
  1224. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1225. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1226. if ( $SerieName == "" || $SerieName == $ColName )
  1227. {
  1228. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  1229. $XLast = -1;
  1230. foreach ( $Data as $Key => $Values )
  1231. {
  1232. if ( isset($Data[$Key][$ColName]))
  1233. {
  1234. $Value = $Data[$Key][$ColName];
  1235. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  1236. /* Save point into the image map if option activated */
  1237. if ( $this->BuildMap )
  1238. $this->addToImageMap($XPos-3,$YPos-3,$XPos+3,$YPos+3,$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"Line");
  1239. if (!is_numeric($Value)) { $XLast = -1; }
  1240. if ( $XLast != -1 )
  1241. $this->drawLine($XLast,$YLast,$XPos,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE);
  1242. $XLast = $XPos;
  1243. $YLast = $YPos;
  1244. if (!is_numeric($Value)) { $XLast = -1; }
  1245. }
  1246. $XPos = $XPos + $this->DivisionWidth;
  1247. }
  1248. $GraphID++;
  1249. }
  1250. }
  1251. }
  1252. /* This function draw a line graph */
  1253. function drawXYGraph($Data,$DataDescription,$YSerieName,$XSerieName,$PaletteID=0)
  1254. {
  1255. $YLast = -1; $XLast = -1;
  1256. foreach ( $Data as $Key => $Values )
  1257. {
  1258. if ( isset($Data[$Key][$YSerieName]) && isset($Data[$Key][$XSerieName]) )
  1259. {
  1260. $X = $Data[$Key][$XSerieName];
  1261. $Y = $Data[$Key][$YSerieName];
  1262. $Y = $this->GArea_Y2 - (($Y-$this->VMin) * $this->DivisionRatio);
  1263. $X = $this->GArea_X1 + (($X-$this->VXMin) * $this->XDivisionRatio);
  1264. if ($XLast != -1 && $YLast != -1)
  1265. {
  1266. $this->drawLine($XLast,$YLast,$X,$Y,$this->Palette[$PaletteID]["R"],$this->Palette[$PaletteID]["G"],$this->Palette[$PaletteID]["B"],TRUE);
  1267. }
  1268. $XLast = $X;
  1269. $YLast = $Y;
  1270. }
  1271. }
  1272. }
  1273. /* This function draw a cubic curve */
  1274. function drawCubicCurve($Data,$DataDescription,$Accuracy=.1,$SerieName="")
  1275. {
  1276. /* Validate the Data and DataDescription array */
  1277. $this->validateDataDescription("drawCubicCurve",$DataDescription);
  1278. $this->validateData("drawCubicCurve",$Data);
  1279. $GraphID = 0;
  1280. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1281. {
  1282. if ( $SerieName == "" || $SerieName == $ColName )
  1283. {
  1284. $XIn = ""; $Yin = ""; $Yt = ""; $U = "";
  1285. $XIn[0] = 0; $YIn[0] = 0;
  1286. $ID = 0;
  1287. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1288. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1289. $Index = 1;
  1290. $XLast = -1; $Missing = "";
  1291. foreach ( $Data as $Key => $Values )
  1292. {
  1293. if ( isset($Data[$Key][$ColName]) )
  1294. {
  1295. $Value = $Data[$Key][$ColName];
  1296. $XIn[$Index] = $Index;
  1297. $YIn[$Index] = $Value;
  1298. if ( !is_numeric($Value) ) { $Missing[$Index] = TRUE; }
  1299. $Index++;
  1300. }
  1301. }
  1302. $Index--;
  1303. $Yt[0] = 0;
  1304. $Yt[1] = 0;
  1305. $U[1] = 0;
  1306. for($i=2;$i<=$Index-1;$i++)
  1307. {
  1308. $Sig = ($XIn[$i] - $XIn[$i-1]) / ($XIn[$i+1] - $XIn[$i-1]);
  1309. $p = $Sig * $Yt[$i-1] + 2;
  1310. $Yt[$i] = ($Sig - 1) / $p;
  1311. $U[$i] = ($YIn[$i+1] - $YIn[$i]) / ($XIn[$i+1] - $XIn[$i]) - ($YIn[$i] - $YIn[$i-1]) / ($XIn[$i] - $XIn[$i-1]);
  1312. $U[$i] = (6 * $U[$i] / ($XIn[$i+1] - $XIn[$i-1]) - $Sig * $U[$i-1]) / $p;
  1313. }
  1314. $qn = 0;
  1315. $un = 0;
  1316. $Yt[$Index] = ($un - $qn * $U[$Index-1]) / ($qn * $Yt[$Index-1] + 1);
  1317. for($k=$Index-1;$k>=1;$k--)
  1318. $Yt[$k] = $Yt[$k] * $Yt[$k+1] + $U[$k];
  1319. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  1320. for($X=1;$X<=$Index;$X=$X+$Accuracy)
  1321. {
  1322. $klo = 1;
  1323. $khi = $Index;
  1324. $k = $khi - $klo;
  1325. while($k > 1)
  1326. {
  1327. $k = $khi - $klo;
  1328. If ( $XIn[$k] >= $X )
  1329. $khi = $k;
  1330. else
  1331. $klo = $k;
  1332. }
  1333. $klo = $khi - 1;
  1334. $h = $XIn[$khi] - $XIn[$klo];
  1335. $a = ($XIn[$khi] - $X) / $h;
  1336. $b = ($X - $XIn[$klo]) / $h;
  1337. $Value = $a * $YIn[$klo] + $b * $YIn[$khi] + (($a*$a*$a - $a) * $Yt[$klo] + ($b*$b*$b - $b) * $Yt[$khi]) * ($h*$h) / 6;
  1338. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  1339. if ( $XLast != -1 && !isset($Missing[floor($X)]) && !isset($Missing[floor($X+1)]) )
  1340. $this->drawLine($XLast,$YLast,$XPos,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE);
  1341. $XLast = $XPos;
  1342. $YLast = $YPos;
  1343. $XPos = $XPos + $this->DivisionWidth * $Accuracy;
  1344. }
  1345. // Add potentialy missing values
  1346. $XPos = $XPos - $this->DivisionWidth * $Accuracy;
  1347. if ( $XPos < ($this->GArea_X2 - $this->GAreaXOffset) )
  1348. {
  1349. $YPos = $this->GArea_Y2 - (($YIn[$Index]-$this->VMin) * $this->DivisionRatio);
  1350. $this->drawLine($XLast,$YLast,$this->GArea_X2-$this->GAreaXOffset,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE);
  1351. }
  1352. $GraphID++;
  1353. }
  1354. }
  1355. }
  1356. /* This function draw a filled cubic curve */
  1357. function drawFilledCubicCurve($Data,$DataDescription,$Accuracy=.1,$Alpha=100,$AroundZero=FALSE)
  1358. {
  1359. /* Validate the Data and DataDescription array */
  1360. $this->validateDataDescription("drawFilledCubicCurve",$DataDescription);
  1361. $this->validateData("drawFilledCubicCurve",$Data);
  1362. $LayerWidth = $this->GArea_X2-$this->GArea_X1;
  1363. $LayerHeight = $this->GArea_Y2-$this->GArea_Y1;
  1364. $YZero = $LayerHeight - ((0-$this->VMin) * $this->DivisionRatio);
  1365. if ( $YZero > $LayerHeight ) { $YZero = $LayerHeight; }
  1366. $GraphID = 0;
  1367. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1368. {
  1369. $XIn = ""; $Yin = ""; $Yt = ""; $U = "";
  1370. $XIn[0] = 0; $YIn[0] = 0;
  1371. $ID = 0;
  1372. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1373. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1374. $Index = 1;
  1375. $XLast = -1; $Missing = "";
  1376. foreach ( $Data as $Key => $Values )
  1377. {
  1378. $Value = $Data[$Key][$ColName];
  1379. $XIn[$Index] = $Index;
  1380. $YIn[$Index] = $Value;
  1381. if ( !is_numeric($Value) ) { $Missing[$Index] = TRUE; }
  1382. $Index++;
  1383. }
  1384. $Index--;
  1385. $Yt[0] = 0;
  1386. $Yt[1] = 0;
  1387. $U[1] = 0;
  1388. for($i=2;$i<=$Index-1;$i++)
  1389. {
  1390. $Sig = ($XIn[$i] - $XIn[$i-1]) / ($XIn[$i+1] - $XIn[$i-1]);
  1391. $p = $Sig * $Yt[$i-1] + 2;
  1392. $Yt[$i] = ($Sig - 1) / $p;
  1393. $U[$i] = ($YIn[$i+1] - $YIn[$i]) / ($XIn[$i+1] - $XIn[$i]) - ($YIn[$i] - $YIn[$i-1]) / ($XIn[$i] - $XIn[$i-1]);
  1394. $U[$i] = (6 * $U[$i] / ($XIn[$i+1] - $XIn[$i-1]) - $Sig * $U[$i-1]) / $p;
  1395. }
  1396. $qn = 0;
  1397. $un = 0;
  1398. $Yt[$Index] = ($un - $qn * $U[$Index-1]) / ($qn * $Yt[$Index-1] + 1);
  1399. for($k=$Index-1;$k>=1;$k--)
  1400. $Yt[$k] = $Yt[$k] * $Yt[$k+1] + $U[$k];
  1401. $Points = "";
  1402. $Points[] = $this->GAreaXOffset;
  1403. $Points[] = $LayerHeight;
  1404. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  1405. $C_White =$this->AllocateColor($this->Layers[0],255,255,255);
  1406. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  1407. imagecolortransparent($this->Layers[0],$C_White);
  1408. $YLast = NULL;
  1409. $XPos = $this->GAreaXOffset; $PointsCount = 2;
  1410. for($X=1;$X<=$Index;$X=$X+$Accuracy)
  1411. {
  1412. $klo = 1;
  1413. $khi = $Index;
  1414. $k = $khi - $klo;
  1415. while($k > 1)
  1416. {
  1417. $k = $khi - $klo;
  1418. If ( $XIn[$k] >= $X )
  1419. $khi = $k;
  1420. else
  1421. $klo = $k;
  1422. }
  1423. $klo = $khi - 1;
  1424. $h = $XIn[$khi] - $XIn[$klo];
  1425. $a = ($XIn[$khi] - $X) / $h;
  1426. $b = ($X - $XIn[$klo]) / $h;
  1427. $Value = $a * $YIn[$klo] + $b * $YIn[$khi] + (($a*$a*$a - $a) * $Yt[$klo] + ($b*$b*$b - $b) * $Yt[$khi]) * ($h*$h) / 6;
  1428. $YPos = $LayerHeight - (($Value-$this->VMin) * $this->DivisionRatio);
  1429. if ( $YLast != NULL && $AroundZero && !isset($Missing[floor($X)]) && !isset($Missing[floor($X+1)]))
  1430. {
  1431. $aPoints = "";
  1432. $aPoints[] = $XLast;
  1433. $aPoints[] = $YLast;
  1434. $aPoints[] = $XPos;
  1435. $aPoints[] = $YPos;
  1436. $aPoints[] = $XPos;
  1437. $aPoints[] = $YZero;
  1438. $aPoints[] = $XLast;
  1439. $aPoints[] = $YZero;
  1440. $C_Graph =$this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1441. imagefilledpolygon($this->Layers[0],$aPoints,4,$C_Graph);
  1442. }
  1443. if ( !isset($Missing[floor($X)]) || $YLast == NULL )
  1444. {
  1445. $PointsCount++;
  1446. $Points[] = $XPos;
  1447. $Points[] = $YPos;
  1448. }
  1449. else
  1450. {
  1451. $PointsCount++; $Points[] = $XLast; $Points[] = $LayerHeight;
  1452. }
  1453. $YLast = $YPos; $XLast = $XPos;
  1454. $XPos = $XPos + $this->DivisionWidth * $Accuracy;
  1455. }
  1456. // Add potentialy missing values
  1457. $XPos = $XPos - $this->DivisionWidth * $Accuracy;
  1458. if ( $XPos < ($LayerWidth-$this->GAreaXOffset) )
  1459. {
  1460. $YPos = $LayerHeight - (($YIn[$Index]-$this->VMin) * $this->DivisionRatio);
  1461. if ( $YLast != NULL && $AroundZero )
  1462. {
  1463. $aPoints = "";
  1464. $aPoints[] = $XLast;
  1465. $aPoints[] = $YLast;
  1466. $aPoints[] = $LayerWidth-$this->GAreaXOffset;
  1467. $aPoints[] = $YPos;
  1468. $aPoints[] = $LayerWidth-$this->GAreaXOffset;
  1469. $aPoints[] = $YZero;
  1470. $aPoints[] = $XLast;
  1471. $aPoints[] = $YZero;
  1472. $C_Graph =$this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1473. imagefilledpolygon($this->Layers[0],$aPoints,4,$C_Graph);
  1474. }
  1475. if ( $YIn[$klo] != "" && $YIn[$khi] != "" || $YLast == NULL )
  1476. {
  1477. $PointsCount++;
  1478. $Points[] = $LayerWidth-$this->GAreaXOffset;
  1479. $Points[] = $YPos;
  1480. }
  1481. }
  1482. $Points[] = $LayerWidth-$this->GAreaXOffset;
  1483. $Points[] = $LayerHeight;
  1484. if ( !$AroundZero )
  1485. {
  1486. $C_Graph =$this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1487. imagefilledpolygon($this->Layers[0],$Points,$PointsCount,$C_Graph);
  1488. }
  1489. imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha);
  1490. imagedestroy($this->Layers[0]);
  1491. $this->drawCubicCurve($Data,$DataDescription,$Accuracy,$ColName);
  1492. $GraphID++;
  1493. }
  1494. }
  1495. /* This function draw a filled line graph */
  1496. function drawFilledLineGraph($Data,$DataDescription,$Alpha=100,$AroundZero=FALSE)
  1497. {
  1498. $Empty = -2147483647;
  1499. /* Validate the Data and DataDescription array */
  1500. $this->validateDataDescription("drawFilledLineGraph",$DataDescription);
  1501. $this->validateData("drawFilledLineGraph",$Data);
  1502. $LayerWidth = $this->GArea_X2-$this->GArea_X1;
  1503. $LayerHeight = $this->GArea_Y2-$this->GArea_Y1;
  1504. $GraphID = 0;
  1505. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1506. {
  1507. $ID = 0;
  1508. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1509. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1510. $aPoints = "";
  1511. $aPoints[] = $this->GAreaXOffset;
  1512. $aPoints[] = $LayerHeight;
  1513. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  1514. $C_White = $this->AllocateColor($this->Layers[0],255,255,255);
  1515. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  1516. imagecolortransparent($this->Layers[0],$C_White);
  1517. $XPos = $this->GAreaXOffset;
  1518. $XLast = -1; $PointsCount = 2;
  1519. $YZero = $LayerHeight - ((0-$this->VMin) * $this->DivisionRatio);
  1520. if ( $YZero > $LayerHeight ) { $YZero = $LayerHeight; }
  1521. $YLast = $Empty;
  1522. foreach ( $Data as $Key => $Values )
  1523. {
  1524. $Value = $Data[$Key][$ColName];
  1525. $YPos = $LayerHeight - (($Value-$this->VMin) * $this->DivisionRatio);
  1526. /* Save point into the image map if option activated */
  1527. if ( $this->BuildMap )
  1528. $this->addToImageMap($XPos-3,$YPos-3,$XPos+3,$YPos+3,$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"FLine");
  1529. if ( !is_numeric($Value) )
  1530. {
  1531. $PointsCount++;
  1532. $aPoints[] = $XLast;
  1533. $aPoints[] = $LayerHeight;
  1534. $YLast = $Empty;
  1535. }
  1536. else
  1537. {
  1538. $PointsCount++;
  1539. if ( $YLast <> $Empty )
  1540. { $aPoints[] = $XPos; $aPoints[] = $YPos; }
  1541. else
  1542. { $PointsCount++; $aPoints[] = $XPos; $aPoints[] = $LayerHeight; $aPoints[] = $XPos; $aPoints[] = $YPos; }
  1543. if ($YLast <> $Empty && $AroundZero)
  1544. {
  1545. $Points = "";
  1546. $Points[] = $XLast; $Points[] = $YLast;
  1547. $Points[] = $XPos;
  1548. $Points[] = $YPos;
  1549. $Points[] = $XPos;
  1550. $Points[] = $YZero;
  1551. $Points[] = $XLast;
  1552. $Points[] = $YZero;
  1553. $C_Graph = $this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1554. imagefilledpolygon($this->Layers[0],$Points,4,$C_Graph);
  1555. }
  1556. $YLast = $YPos;
  1557. }
  1558. $XLast = $XPos;
  1559. $XPos = $XPos + $this->DivisionWidth;
  1560. }
  1561. $aPoints[] = $LayerWidth - $this->GAreaXOffset;
  1562. $aPoints[] = $LayerHeight;
  1563. if ( $AroundZero == FALSE )
  1564. {
  1565. $C_Graph = $this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1566. imagefilledpolygon($this->Layers[0],$aPoints,$PointsCount,$C_Graph);
  1567. }
  1568. imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha);
  1569. imagedestroy($this->Layers[0]);
  1570. $GraphID++;
  1571. $this->drawLineGraph($Data,$DataDescription,$ColName);
  1572. }
  1573. }
  1574. /* This function draw a bar graph */
  1575. function drawOverlayBarGraph($Data,$DataDescription,$Alpha=50)
  1576. {
  1577. /* Validate the Data and DataDescription array */
  1578. $this->validateDataDescription("drawOverlayBarGraph",$DataDescription);
  1579. $this->validateData("drawOverlayBarGraph",$Data);
  1580. $LayerWidth = $this->GArea_X2-$this->GArea_X1;
  1581. $LayerHeight = $this->GArea_Y2-$this->GArea_Y1;
  1582. $GraphID = 0;
  1583. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1584. {
  1585. $ID = 0;
  1586. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1587. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1588. $this->Layers[$GraphID] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  1589. $C_White = $this->AllocateColor($this->Layers[$GraphID],255,255,255);
  1590. $C_Graph = $this->AllocateColor($this->Layers[$GraphID],$this->Palette[$GraphID]["R"],$this->Palette[$GraphID]["G"],$this->Palette[$GraphID]["B"]);
  1591. imagefilledrectangle($this->Layers[$GraphID],0,0,$LayerWidth,$LayerHeight,$C_White);
  1592. imagecolortransparent($this->Layers[$GraphID],$C_White);
  1593. $XWidth = $this->DivisionWidth / 4;
  1594. $XPos = $this->GAreaXOffset;
  1595. $YZero = $LayerHeight - ((0-$this->VMin) * $this->DivisionRatio);
  1596. $XLast = -1; $PointsCount = 2;
  1597. foreach ( $Data as $Key => $Values )
  1598. {
  1599. if ( isset($Data[$Key][$ColName]) )
  1600. {
  1601. $Value = $Data[$Key][$ColName];
  1602. if ( is_numeric($Value) )
  1603. {
  1604. $YPos = $LayerHeight - (($Value-$this->VMin) * $this->DivisionRatio);
  1605. imagefilledrectangle($this->Layers[$GraphID],$XPos-$XWidth,$YPos,$XPos+$XWidth,$YZero,$C_Graph);
  1606. $X1 = floor($XPos - $XWidth + $this->GArea_X1); $Y1 = floor($YPos+$this->GArea_Y1) + .2;
  1607. $X2 = floor($XPos + $XWidth + $this->GArea_X1); $Y2 = $this->GArea_Y2 - ((0-$this->VMin) * $this->DivisionRatio);
  1608. if ( $X1 <= $this->GArea_X1 ) { $X1 = $this->GArea_X1 + 1; }
  1609. if ( $X2 >= $this->GArea_X2 ) { $X2 = $this->GArea_X2 - 1; }
  1610. /* Save point into the image map if option activated */
  1611. if ( $this->BuildMap )
  1612. $this->addToImageMap($X1,min($Y1,$Y2),$X2,max($Y1,$Y2),$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"oBar");
  1613. $this->drawLine($X1,$Y1,$X2,$Y1,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE);
  1614. }
  1615. }
  1616. $XPos = $XPos + $this->DivisionWidth;
  1617. }
  1618. $GraphID++;
  1619. }
  1620. for($i=0;$i<=($GraphID-1);$i++)
  1621. {
  1622. imagecopymerge($this->Picture,$this->Layers[$i],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha);
  1623. imagedestroy($this->Layers[$i]);
  1624. }
  1625. }
  1626. /* This function draw a bar graph */
  1627. function drawBarGraph($Data,$DataDescription,$Shadow=FALSE,$Alpha=100)
  1628. {
  1629. /* Validate the Data and DataDescription array */
  1630. $this->validateDataDescription("drawBarGraph",$DataDescription);
  1631. $this->validateData("drawBarGraph",$Data);
  1632. $GraphID = 0;
  1633. $Series = count($DataDescription["Values"]);
  1634. $SeriesWidth = $this->DivisionWidth / ($Series+1);
  1635. $SerieXOffset = $this->DivisionWidth / 2 - $SeriesWidth / 2;
  1636. $YZero = $this->GArea_Y2 - ((0-$this->VMin) * $this->DivisionRatio);
  1637. if ( $YZero > $this->GArea_Y2 ) { $YZero = $this->GArea_Y2; }
  1638. $SerieID = 0;
  1639. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1640. {
  1641. $ID = 0;
  1642. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1643. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1644. $XPos = $this->GArea_X1 + $this->GAreaXOffset - $SerieXOffset + $SeriesWidth * $SerieID;
  1645. $XLast = -1;
  1646. foreach ( $Data as $Key => $Values )
  1647. {
  1648. $ColorID++; if ($ColorID > 7) $ColorID=1;
  1649. if ( isset($Data[$Key][$ColName]))
  1650. {
  1651. if ( is_numeric($Data[$Key][$ColName]) )
  1652. {
  1653. $Value = $Data[$Key][$ColName];
  1654. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  1655. /* Save point into the image map if option activated */
  1656. if ( $this->BuildMap )
  1657. {
  1658. $this->addToImageMap($XPos+1,min($YZero,$YPos),$XPos+$SeriesWidth-1,max($YZero,$YPos),$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"Bar");
  1659. }
  1660. if ( $Shadow && $Alpha == 100 )
  1661. $this->drawRectangle($XPos+1,$YZero,$XPos+$SeriesWidth-1,$YPos,25,25,25,TRUE,$Alpha);
  1662. $this->drawFilledRectangle($XPos+3,$YZero,$XPos+2+$SeriesWidth-1,$YPos+3,$this->Palette[6]["R"],$this->Palette[7]["G"],$this->Palette[5]["B"],TRUE,$Alpha);
  1663. $this->drawFilledRectangle($XPos+1,$YZero,$XPos+$SeriesWidth-1,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE,$Alpha);
  1664. }
  1665. }
  1666. $XPos = $XPos + $this->DivisionWidth;
  1667. }
  1668. $SerieID++;
  1669. }
  1670. }
  1671. /* This function draw a stacked bar graph */
  1672. function drawStackedBarGraph($Data,$DataDescription,$Alpha=50,$Contiguous=FALSE)
  1673. {
  1674. /* Validate the Data and DataDescription array */
  1675. $this->validateDataDescription("drawBarGraph",$DataDescription);
  1676. $this->validateData("drawBarGraph",$Data);
  1677. $GraphID = 0;
  1678. $Series = count($DataDescription["Values"]);
  1679. if ( $Contiguous )
  1680. $SeriesWidth = $this->DivisionWidth;
  1681. else
  1682. $SeriesWidth = $this->DivisionWidth * .8;
  1683. $YZero = $this->GArea_Y2 - ((0-$this->VMin) * $this->DivisionRatio);
  1684. if ( $YZero > $this->GArea_Y2 ) { $YZero = $this->GArea_Y2; }
  1685. $SerieID = 0; $LastValue = "";
  1686. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1687. {
  1688. $ID = 0;
  1689. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1690. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1691. $XPos = $this->GArea_X1 + $this->GAreaXOffset - $SeriesWidth / 2;
  1692. $XLast = -1;
  1693. foreach ( $Data as $Key => $Values )
  1694. {
  1695. if ( isset($Data[$Key][$ColName]))
  1696. {
  1697. if ( is_numeric($Data[$Key][$ColName]) )
  1698. {
  1699. $Value = $Data[$Key][$ColName];
  1700. if ( isset($LastValue[$Key]) )
  1701. {
  1702. $YPos = $this->GArea_Y2 - ((($Value+$LastValue[$Key])-$this->VMin) * $this->DivisionRatio);
  1703. $YBottom = $this->GArea_Y2 - (($LastValue[$Key]-$this->VMin) * $this->DivisionRatio);
  1704. $LastValue[$Key] += $Value;
  1705. }
  1706. else
  1707. {
  1708. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  1709. $YBottom = $YZero;
  1710. $LastValue[$Key] = $Value;
  1711. }
  1712. /* Save point into the image map if option activated */
  1713. if ( $this->BuildMap )
  1714. $this->addToImageMap($XPos+1,min($YBottom,$YPos),$XPos+$SeriesWidth-1,max($YBottom,$YPos),$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"sBar");
  1715. $this->drawFilledRectangle($XPos+1,$YBottom,$XPos+$SeriesWidth-1,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE,$Alpha);
  1716. }
  1717. }
  1718. $XPos = $XPos + $this->DivisionWidth;
  1719. }
  1720. $SerieID++;
  1721. }
  1722. }
  1723. /* This function draw a limits bar graphs */
  1724. function drawLimitsGraph($Data,$DataDescription,$R=0,$G=0,$B=0)
  1725. {
  1726. /* Validate the Data and DataDescription array */
  1727. $this->validateDataDescription("drawLimitsGraph",$DataDescription);
  1728. $this->validateData("drawLimitsGraph",$Data);
  1729. $XWidth = $this->DivisionWidth / 4;
  1730. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  1731. foreach ( $Data as $Key => $Values )
  1732. {
  1733. $Min = $Data[$Key][$DataDescription["Values"][0]];
  1734. $Max = $Data[$Key][$DataDescription["Values"][0]];
  1735. $GraphID = 0; $MaxID = 0; $MinID = 0;
  1736. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1737. {
  1738. if ( isset($Data[$Key][$ColName]) )
  1739. {
  1740. if ( $Data[$Key][$ColName] > $Max && is_numeric($Data[$Key][$ColName]))
  1741. { $Max = $Data[$Key][$ColName]; $MaxID = $GraphID; }
  1742. }
  1743. if ( isset($Data[$Key][$ColName]) && is_numeric($Data[$Key][$ColName]))
  1744. {
  1745. if ( $Data[$Key][$ColName] < $Min )
  1746. { $Min = $Data[$Key][$ColName]; $MinID = $GraphID; }
  1747. $GraphID++;
  1748. }
  1749. }
  1750. $YPos = $this->GArea_Y2 - (($Max-$this->VMin) * $this->DivisionRatio);
  1751. $X1 = floor($XPos - $XWidth); $Y1 = floor($YPos) - .2;
  1752. $X2 = floor($XPos + $XWidth);
  1753. if ( $X1 <= $this->GArea_X1 ) { $X1 = $this->GArea_X1 + 1; }
  1754. if ( $X2 >= $this->GArea_X2 ) { $X2 = $this->GArea_X2 - 1; }
  1755. $YPos = $this->GArea_Y2 - (($Min-$this->VMin) * $this->DivisionRatio);
  1756. $Y2 = floor($YPos) + .2;
  1757. $this->drawLine(floor($XPos)-.2,$Y1+1,floor($XPos)-.2,$Y2-1,$R,$G,$B,TRUE);
  1758. $this->drawLine(floor($XPos)+.2,$Y1+1,floor($XPos)+.2,$Y2-1,$R,$G,$B,TRUE);
  1759. $this->drawLine($X1,$Y1,$X2,$Y1,$this->Palette[$MaxID]["R"],$this->Palette[$MaxID]["G"],$this->Palette[$MaxID]["B"],FALSE);
  1760. $this->drawLine($X1,$Y2,$X2,$Y2,$this->Palette[$MinID]["R"],$this->Palette[$MinID]["G"],$this->Palette[$MinID]["B"],FALSE);
  1761. $XPos = $XPos + $this->DivisionWidth;
  1762. }
  1763. }
  1764. /* This function draw radar axis centered on the graph area */
  1765. function 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)
  1766. {
  1767. /* Validate the Data and DataDescription array */
  1768. $this->validateDataDescription("drawRadarAxis",$DataDescription);
  1769. $this->validateData("drawRadarAxis",$Data);
  1770. $C_TextColor = $this->AllocateColor($this->Picture,$A_R,$A_G,$A_B);
  1771. /* Draw radar axis */
  1772. $Points = count($Data);
  1773. $Radius = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 - $BorderOffset;
  1774. $XCenter = ( $this->GArea_X2 - $this->GArea_X1 ) / 2 + $this->GArea_X1;
  1775. $YCenter = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 + $this->GArea_Y1;
  1776. /* Search for the max value */
  1777. if ( $MaxValue == -1 )
  1778. {
  1779. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1780. {
  1781. foreach ( $Data as $Key => $Values )
  1782. {
  1783. if ( isset($Data[$Key][$ColName]))
  1784. if ( $Data[$Key][$ColName] > $MaxValue ) { $MaxValue = $Data[$Key][$ColName]; }
  1785. }
  1786. }
  1787. }
  1788. /* Draw the mosaic */
  1789. if ( $Mosaic )
  1790. {
  1791. $RadiusScale = $Radius / $MaxValue;
  1792. for ( $t=1; $t<=$MaxValue-1; $t++)
  1793. {
  1794. $TRadius = $RadiusScale * $t;
  1795. $LastX1 = -1;
  1796. for ( $i=0; $i<=$Points; $i++)
  1797. {
  1798. $Angle = -90 + $i * 360/$Points;
  1799. $X1 = cos($Angle * 3.1418 / 180 ) * $TRadius + $XCenter;
  1800. $Y1 = sin($Angle * 3.1418 / 180 ) * $TRadius + $YCenter;
  1801. $X2 = cos($Angle * 3.1418 / 180 ) * ($TRadius+$RadiusScale) + $XCenter;
  1802. $Y2 = sin($Angle * 3.1418 / 180 ) * ($TRadius+$RadiusScale) + $YCenter;
  1803. if ( $t % 2 == 1 && $LastX1 != -1)
  1804. {
  1805. $Plots = "";
  1806. $Plots[] = $X1; $Plots[] = $Y1;
  1807. $Plots[] = $X2; $Plots[] = $Y2;
  1808. $Plots[] = $LastX2; $Plots[] = $LastY2;
  1809. $Plots[] = $LastX1; $Plots[] = $LastY1;
  1810. $C_Graph = $this->AllocateColor($this->Picture,250,250,250);
  1811. imagefilledpolygon($this->Picture,$Plots,(count($Plots)+1)/2,$C_Graph);
  1812. }
  1813. $LastX1 = $X1; $LastY1= $Y1;
  1814. $LastX2 = $X2; $LastY2= $Y2;
  1815. }
  1816. }
  1817. }
  1818. /* Draw the spider web */
  1819. for ( $t=1; $t<=$MaxValue; $t++)
  1820. {
  1821. $TRadius = ( $Radius / $MaxValue ) * $t;
  1822. $LastX = -1;
  1823. for ( $i=0; $i<=$Points; $i++)
  1824. {
  1825. $Angle = -90 + $i * 360/$Points;
  1826. $X = cos($Angle * 3.1418 / 180 ) * $TRadius + $XCenter;
  1827. $Y = sin($Angle * 3.1418 / 180 ) * $TRadius + $YCenter;
  1828. if ( $LastX != -1 )
  1829. $this->drawDottedLine($LastX,$LastY,$X,$Y,4,$S_R,$S_G,$S_B);
  1830. $LastX = $X; $LastY= $Y;
  1831. }
  1832. }
  1833. /* Draw the axis */
  1834. for ( $i=0; $i<=$Points; $i++)
  1835. {
  1836. $Angle = -90 + $i * 360/$Points;
  1837. $X = cos($Angle * 3.1418 / 180 ) * $Radius + $XCenter;
  1838. $Y = sin($Angle * 3.1418 / 180 ) * $Radius + $YCenter;
  1839. $this->drawLine($XCenter,$YCenter,$X,$Y,$A_R,$A_G,$A_B);
  1840. $XOffset = 0; $YOffset = 0;
  1841. if (isset($Data[$i][$DataDescription["Position"]]))
  1842. {
  1843. $Label = $Data[$i][$DataDescription["Position"]];
  1844. $Positions = imagettfbbox($this->FontSize,0,$this->FontName,$Label);
  1845. $Width = $Positions[2] - $Positions[6];
  1846. $Height = $Positions[3] - $Positions[7];
  1847. if ( $Angle >= 0 && $Angle <= 90 )
  1848. $YOffset = $Height;
  1849. if ( $Angle > 90 && $Angle <= 180 )
  1850. { $YOffset = $Height; $XOffset = -$Width; }
  1851. if ( $Angle > 180 && $Angle <= 270 )
  1852. { $XOffset = -$Width; }
  1853. imagettftext($this->Picture,$this->FontSize,0,$X+$XOffset,$Y+$YOffset,$C_TextColor,$this->FontName,$Label);
  1854. }
  1855. }
  1856. /* Write the values */
  1857. for ( $t=1; $t<=$MaxValue; $t++)
  1858. {
  1859. $TRadius = ( $Radius / $MaxValue ) * $t;
  1860. $Angle = -90 + 360 / $Points;
  1861. $X1 = $XCenter;
  1862. $Y1 = $YCenter - $TRadius;
  1863. $X2 = cos($Angle * 3.1418 / 180 ) * $TRadius + $XCenter;
  1864. $Y2 = sin($Angle * 3.1418 / 180 ) * $TRadius + $YCenter;
  1865. $XPos = floor(($X2-$X1)/2) + $X1;
  1866. $YPos = floor(($Y2-$Y1)/2) + $Y1;
  1867. $Positions = imagettfbbox($this->FontSize,0,$this->FontName,$t);
  1868. $X = $XPos - ( $X+$Positions[2] - $X+$Positions[6] ) / 2;
  1869. $Y = $YPos + $this->FontSize;
  1870. $this->drawFilledRoundedRectangle($X+$Positions[6]-2,$Y+$Positions[7]-1,$X+$Positions[2]+4,$Y+$Positions[3]+1,2,240,240,240);
  1871. $this->drawRoundedRectangle($X+$Positions[6]-2,$Y+$Positions[7]-1,$X+$Positions[2]+4,$Y+$Positions[3]+1,2,220,220,220);
  1872. imagettftext($this->Picture,$this->FontSize,0,$X,$Y,$C_TextColor,$this->FontName,$t);
  1873. }
  1874. }
  1875. /* This function draw a radar graph centered on the graph area */
  1876. function drawRadar($Data,$DataDescription,$BorderOffset=10,$MaxValue=-1)
  1877. {
  1878. /* Validate the Data and DataDescription array */
  1879. $this->validateDataDescription("drawRadar",$DataDescription);
  1880. $this->validateData("drawRadar",$Data);
  1881. $Points = count($Data);
  1882. $Radius = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 - $BorderOffset;
  1883. $XCenter = ( $this->GArea_X2 - $this->GArea_X1 ) / 2 + $this->GArea_X1;
  1884. $YCenter = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 + $this->GArea_Y1;
  1885. /* Search for the max value */
  1886. if ( $MaxValue == -1 )
  1887. {
  1888. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1889. {
  1890. foreach ( $Data as $Key => $Values )
  1891. {
  1892. if ( isset($Data[$Key][$ColName]))
  1893. if ( $Data[$Key][$ColName] > $MaxValue ) { $MaxValue = $Data[$Key][$ColName]; }
  1894. }
  1895. }
  1896. }
  1897. $GraphID = 0;
  1898. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1899. {
  1900. $ID = 0;
  1901. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1902. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1903. $Angle = -90;
  1904. $XLast = -1;
  1905. foreach ( $Data as $Key => $Values )
  1906. {
  1907. if ( isset($Data[$Key][$ColName]))
  1908. {
  1909. $Value = $Data[$Key][$ColName];
  1910. $Strength = ( $Radius / $MaxValue ) * $Value;
  1911. $XPos = cos($Angle * 3.1418 / 180 ) * $Strength + $XCenter;
  1912. $YPos = sin($Angle * 3.1418 / 180 ) * $Strength + $YCenter;
  1913. if ( $XLast != -1 )
  1914. $this->drawLine($XLast,$YLast,$XPos,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1915. if ( $XLast == -1 )
  1916. { $FirstX = $XPos; $FirstY = $YPos; }
  1917. $Angle = $Angle + (360/$Points);
  1918. $XLast = $XPos;
  1919. $YLast = $YPos;
  1920. }
  1921. }
  1922. $this->drawLine($XPos,$YPos,$FirstX,$FirstY,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1923. $GraphID++;
  1924. }
  1925. }
  1926. /* This function draw a radar graph centered on the graph area */
  1927. function drawFilledRadar($Data,$DataDescription,$Alpha=50,$BorderOffset=10,$MaxValue=-1)
  1928. {
  1929. /* Validate the Data and DataDescription array */
  1930. $this->validateDataDescription("drawFilledRadar",$DataDescription);
  1931. $this->validateData("drawFilledRadar",$Data);
  1932. $Points = count($Data);
  1933. $LayerWidth = $this->GArea_X2-$this->GArea_X1;
  1934. $LayerHeight = $this->GArea_Y2-$this->GArea_Y1;
  1935. $Radius = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2 - $BorderOffset;
  1936. $XCenter = ( $this->GArea_X2 - $this->GArea_X1 ) / 2;
  1937. $YCenter = ( $this->GArea_Y2 - $this->GArea_Y1 ) / 2;
  1938. /* Search for the max value */
  1939. if ( $MaxValue == -1 )
  1940. {
  1941. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1942. {
  1943. foreach ( $Data as $Key => $Values )
  1944. {
  1945. if ( isset($Data[$Key][$ColName]))
  1946. if ( $Data[$Key][$ColName] > $MaxValue && is_numeric($Data[$Key][$ColName])) { $MaxValue = $Data[$Key][$ColName]; }
  1947. }
  1948. }
  1949. }
  1950. $GraphID = 0;
  1951. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  1952. {
  1953. $ID = 0;
  1954. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  1955. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  1956. $Angle = -90;
  1957. $XLast = -1;
  1958. $Plots = "";
  1959. foreach ( $Data as $Key => $Values )
  1960. {
  1961. if ( isset($Data[$Key][$ColName]))
  1962. {
  1963. $Value = $Data[$Key][$ColName];
  1964. if ( !is_numeric($Value) ) { $Value = 0; }
  1965. $Strength = ( $Radius / $MaxValue ) * $Value;
  1966. $XPos = cos($Angle * 3.1418 / 180 ) * $Strength + $XCenter;
  1967. $YPos = sin($Angle * 3.1418 / 180 ) * $Strength + $YCenter;
  1968. $Plots[] = $XPos;
  1969. $Plots[] = $YPos;
  1970. $Angle = $Angle + (360/$Points);
  1971. $XLast = $XPos;
  1972. $YLast = $YPos;
  1973. }
  1974. }
  1975. if (isset($Plots[0]))
  1976. {
  1977. $Plots[] = $Plots[0];
  1978. $Plots[] = $Plots[1];
  1979. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  1980. $C_White = $this->AllocateColor($this->Layers[0],255,255,255);
  1981. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  1982. imagecolortransparent($this->Layers[0],$C_White);
  1983. $C_Graph = $this->AllocateColor($this->Layers[0],$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1984. imagefilledpolygon($this->Layers[0],$Plots,(count($Plots)+1)/2,$C_Graph);
  1985. imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha);
  1986. imagedestroy($this->Layers[0]);
  1987. for($i=0;$i<=count($Plots)-4;$i=$i+2)
  1988. $this->drawLine($Plots[$i]+$this->GArea_X1,$Plots[$i+1]+$this->GArea_Y1,$Plots[$i+2]+$this->GArea_X1,$Plots[$i+3]+$this->GArea_Y1,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"]);
  1989. }
  1990. $GraphID++;
  1991. }
  1992. }
  1993. /* This function draw a flat pie chart */
  1994. function drawBasicPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$R=255,$G=255,$B=255,$Decimals=0)
  1995. {
  1996. /* Validate the Data and DataDescription array */
  1997. $this->validateDataDescription("drawBasicPieGraph",$DataDescription,FALSE);
  1998. $this->validateData("drawBasicPieGraph",$Data);
  1999. /* Determine pie sum */
  2000. $Series = 0; $PieSum = 0;
  2001. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  2002. {
  2003. if ( $ColName != $DataDescription["Position"] )
  2004. {
  2005. $Series++;
  2006. foreach ( $Data as $Key => $Values )
  2007. {
  2008. if ( isset($Data[$Key][$ColName]))
  2009. $PieSum = $PieSum + $Data[$Key][$ColName]; $iValues[] = $Data[$Key][$ColName]; $iLabels[] = $Data[$Key][$DataDescription["Position"]];
  2010. }
  2011. }
  2012. }
  2013. /* Validate serie */
  2014. if ( $Series != 1 )
  2015. RaiseFatal("Pie chart can only accept one serie of data.");
  2016. $SpliceRatio = 360 / $PieSum;
  2017. $SplicePercent = 100 / $PieSum;
  2018. /* Calculate all polygons */
  2019. $Angle = 0; $TopPlots = "";
  2020. foreach($iValues as $Key => $Value)
  2021. {
  2022. $TopPlots[$Key][] = $XPos;
  2023. $TopPlots[$Key][] = $YPos;
  2024. /* Process labels position & size */
  2025. $Caption = "";
  2026. if ( !($DrawLabels == PIE_NOLABEL) )
  2027. {
  2028. $TAngle = $Angle+($Value*$SpliceRatio/2);
  2029. if ($DrawLabels == PIE_PERCENTAGE)
  2030. $Caption = (round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2031. elseif ($DrawLabels == PIE_LABELS)
  2032. $Caption = $iLabels[$Key];
  2033. elseif ($DrawLabels == PIE_PERCENTAGE_LABEL)
  2034. $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2035. elseif ($DrawLabels == PIE_PERCENTAGE_LABEL)
  2036. $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2037. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Caption);
  2038. $TextWidth = $Position[2]-$Position[0];
  2039. $TextHeight = abs($Position[1])+abs($Position[3]);
  2040. $TX = cos(($TAngle) * 3.1418 / 180 ) * ($Radius+10) + $XPos;
  2041. if ( $TAngle > 0 && $TAngle < 180 )
  2042. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+10) + $YPos + 4;
  2043. else
  2044. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+4) + $YPos - ($TextHeight/2);
  2045. if ( $TAngle > 90 && $TAngle < 270 )
  2046. $TX = $TX - $TextWidth;
  2047. $C_TextColor = $this->AllocateColor($this->Picture,70,70,70);
  2048. imagettftext($this->Picture,$this->FontSize,0,$TX,$TY,$C_TextColor,$this->FontName,$Caption);
  2049. }
  2050. /* Process pie slices */
  2051. for($iAngle=$Angle;$iAngle<=$Angle+$Value*$SpliceRatio;$iAngle=$iAngle+.5)
  2052. {
  2053. $TopX = cos($iAngle * 3.1418 / 180 ) * $Radius + $XPos;
  2054. $TopY = sin($iAngle * 3.1418 / 180 ) * $Radius + $YPos;
  2055. $TopPlots[$Key][] = $TopX;
  2056. $TopPlots[$Key][] = $TopY;
  2057. }
  2058. $TopPlots[$Key][] = $XPos;
  2059. $TopPlots[$Key][] = $YPos;
  2060. $Angle = $iAngle;
  2061. }
  2062. $PolyPlots = $TopPlots;
  2063. /* Set array values type to float --- PHP Bug with imagefilledpolygon casting to integer */
  2064. foreach ($TopPlots as $Key => $Value)
  2065. { foreach ($TopPlots[$Key] as $Key2 => $Value2) { settype($TopPlots[$Key][$Key2],"float"); } }
  2066. /* Draw Top polygons */
  2067. foreach ($PolyPlots as $Key => $Value)
  2068. {
  2069. $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"]);
  2070. imagefilledpolygon($this->Picture,$PolyPlots[$Key],(count($PolyPlots[$Key])+1)/2,$C_GraphLo);
  2071. }
  2072. $this->drawCircle($XPos-.5,$YPos-.5,$Radius,$R,$G,$B);
  2073. $this->drawCircle($XPos-.5,$YPos-.5,$Radius+.5,$R,$G,$B);
  2074. /* Draw Top polygons */
  2075. foreach ($TopPlots as $Key => $Value)
  2076. {
  2077. for($j=0;$j<=count($TopPlots[$Key])-4;$j=$j+2)
  2078. $this->drawLine($TopPlots[$Key][$j],$TopPlots[$Key][$j+1],$TopPlots[$Key][$j+2],$TopPlots[$Key][$j+3],$R,$G,$B);
  2079. }
  2080. }
  2081. function drawFlatPieGraphWithShadow($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$SpliceDistance=0,$Decimals=0)
  2082. {
  2083. $this->drawFlatPieGraph($Data,$DataDescription,$XPos+$this->ShadowXDistance,$YPos+$this->ShadowYDistance,$Radius,PIE_NOLABEL,$SpliceDistance,$Decimals,TRUE);
  2084. $this->drawFlatPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius,$DrawLabels,$SpliceDistance,$Decimals,FALSE);
  2085. }
  2086. /* This function draw a flat pie chart */
  2087. function drawFlatPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$SpliceDistance=0,$Decimals=0,$AllBlack=FALSE)
  2088. {
  2089. /* Validate the Data and DataDescription array */
  2090. $this->validateDataDescription("drawFlatPieGraph",$DataDescription,FALSE);
  2091. $this->validateData("drawFlatPieGraph",$Data);
  2092. $ShadowStatus = $this->ShadowActive ; $this->ShadowActive = FALSE;
  2093. /* Determine pie sum */
  2094. $Series = 0; $PieSum = 0;
  2095. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  2096. {
  2097. if ( $ColName != $DataDescription["Position"] )
  2098. {
  2099. $Series++;
  2100. foreach ( $Data as $Key => $Values )
  2101. {
  2102. if ( isset($Data[$Key][$ColName]))
  2103. $PieSum = $PieSum + $Data[$Key][$ColName]; $iValues[] = $Data[$Key][$ColName]; $iLabels[] = $Data[$Key][$DataDescription["Position"]];
  2104. }
  2105. }
  2106. }
  2107. /* Validate serie */
  2108. if ( $Series != 1 )
  2109. {
  2110. RaiseFatal("Pie chart can only accept one serie of data.");
  2111. return(0);
  2112. }
  2113. $SpliceRatio = 360 / $PieSum;
  2114. $SplicePercent = 100 / $PieSum;
  2115. /* Calculate all polygons */
  2116. $Angle = 0; $TopPlots = "";
  2117. foreach($iValues as $Key => $Value)
  2118. {
  2119. $XOffset = cos(($Angle+($Value/2*$SpliceRatio)) * 3.1418 / 180 ) * $SpliceDistance;
  2120. $YOffset = sin(($Angle+($Value/2*$SpliceRatio)) * 3.1418 / 180 ) * $SpliceDistance;
  2121. $TopPlots[$Key][] = round($XPos + $XOffset);
  2122. $TopPlots[$Key][] = round($YPos + $YOffset);
  2123. if ( $AllBlack )
  2124. { $Rc = $this->ShadowRColor; $Gc = $this->ShadowGColor; $Bc = $this->ShadowBColor; }
  2125. else
  2126. { $Rc = $this->Palette[$Key]["R"]; $Gc = $this->Palette[$Key]["G"]; $Bc = $this->Palette[$Key]["B"]; }
  2127. $XLineLast = ""; $YLineLast = "";
  2128. /* Process labels position & size */
  2129. $Caption = "";
  2130. if ( !($DrawLabels == PIE_NOLABEL) )
  2131. {
  2132. $TAngle = $Angle+($Value*$SpliceRatio/2);
  2133. if ($DrawLabels == PIE_PERCENTAGE)
  2134. $Caption = (round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2135. elseif ($DrawLabels == PIE_LABELS)
  2136. $Caption = $iLabels[$Key];
  2137. elseif ($DrawLabels == PIE_PERCENTAGE_LABEL)
  2138. $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2139. elseif ($DrawLabels == PIE_PERCENTAGE_LABEL)
  2140. $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2141. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Caption);
  2142. $TextWidth = $Position[2]-$Position[0];
  2143. $TextHeight = abs($Position[1])+abs($Position[3]);
  2144. $TX = cos(($TAngle) * 3.1418 / 180 ) * ($Radius+10+$SpliceDistance) + $XPos;
  2145. if ( $TAngle > 0 && $TAngle < 180 )
  2146. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+10+$SpliceDistance) + $YPos + 4;
  2147. else
  2148. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+$SpliceDistance+4) + $YPos - ($TextHeight/2);
  2149. if ( $TAngle > 90 && $TAngle < 270 )
  2150. $TX = $TX - $TextWidth;
  2151. $C_TextColor = $this->AllocateColor($this->Picture,70,70,70);
  2152. imagettftext($this->Picture,$this->FontSize,0,$TX,$TY,$C_TextColor,$this->FontName,$Caption);
  2153. }
  2154. /* Process pie slices */
  2155. if ( !$AllBlack )
  2156. $LineColor = $this->AllocateColor($this->Picture,$Rc,$Gc,$Bc);
  2157. else
  2158. $LineColor = $this->AllocateColor($this->Picture,$Rc,$Gc,$Bc);
  2159. $XLineLast = ""; $YLineLast = "";
  2160. for($iAngle=$Angle;$iAngle<=$Angle+$Value*$SpliceRatio;$iAngle=$iAngle+.5)
  2161. {
  2162. $PosX = cos($iAngle * 3.1418 / 180 ) * $Radius + $XPos + $XOffset;
  2163. $PosY = sin($iAngle * 3.1418 / 180 ) * $Radius + $YPos + $YOffset;
  2164. $TopPlots[$Key][] = round($PosX); $TopPlots[$Key][] = round($PosY);
  2165. if ( $iAngle == $Angle || $iAngle == $Angle+$Value*$SpliceRatio || $iAngle +.5 > $Angle+$Value*$SpliceRatio)
  2166. $this->drawLine($XPos+$XOffset,$YPos+$YOffset,$PosX,$PosY,$Rc,$Gc,$Bc);
  2167. if ( $XLineLast != "" )
  2168. $this->drawLine($XLineLast,$YLineLast,$PosX,$PosY,$Rc,$Gc,$Bc);
  2169. $XLineLast = $PosX; $YLineLast = $PosY;
  2170. }
  2171. $TopPlots[$Key][] = round($XPos + $XOffset); $TopPlots[$Key][] = round($YPos + $YOffset);
  2172. $Angle = $iAngle;
  2173. }
  2174. $PolyPlots = $TopPlots;
  2175. /* Draw Top polygons */
  2176. foreach ($PolyPlots as $Key => $Value)
  2177. {
  2178. if ( !$AllBlack )
  2179. $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"]);
  2180. else
  2181. $C_GraphLo = $this->AllocateColor($this->Picture,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor);
  2182. imagefilledpolygon($this->Picture,$PolyPlots[$Key],(count($PolyPlots[$Key])+1)/2,$C_GraphLo);
  2183. }
  2184. $this->ShadowActive = $ShadowStatus;
  2185. }
  2186. /* This function draw a pseudo-3D pie chart */
  2187. function drawPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$EnhanceColors=TRUE,$Skew=60,$SpliceHeight=20,$SpliceDistance=0,$Decimals=0)
  2188. {
  2189. /* Validate the Data and DataDescription array */
  2190. $this->validateDataDescription("drawPieGraph",$DataDescription,FALSE);
  2191. $this->validateData("drawPieGraph",$Data);
  2192. /* Determine pie sum */
  2193. $Series = 0; $PieSum = 0; $rPieSum = 0;
  2194. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  2195. {
  2196. if ( $ColName != $DataDescription["Position"] )
  2197. {
  2198. $Series++;
  2199. foreach ( $Data as $Key => $Values )
  2200. if ( isset($Data[$Key][$ColName]))
  2201. {
  2202. if ( $Data[$Key][$ColName] == 0 )
  2203. { $iValues[] = 0; $rValues[] = 0; $iLabels[] = $Data[$Key][$DataDescription["Position"]]; }
  2204. // Removed : $PieSum++; $rValues[] = 1;
  2205. else
  2206. { $PieSum += $Data[$Key][$ColName]; $iValues[] = $Data[$Key][$ColName]; $iLabels[] = $Data[$Key][$DataDescription["Position"]]; $rValues[] = $Data[$Key][$ColName]; $rPieSum += $Data[$Key][$ColName];}
  2207. }
  2208. }
  2209. }
  2210. /* Validate serie */
  2211. if ( $Series != 1 )
  2212. RaiseFatal("Pie chart can only accept one serie of data.");
  2213. $SpliceDistanceRatio = $SpliceDistance;
  2214. $SkewHeight = ($Radius * $Skew) / 100;
  2215. $SpliceRatio = (360 - $SpliceDistanceRatio * count($iValues) ) / $PieSum;
  2216. $SplicePercent = 100 / $PieSum;
  2217. $rSplicePercent = 100 / $rPieSum;
  2218. /* Calculate all polygons */
  2219. $Angle = 0; $CDev = 5;
  2220. $TopPlots = ""; $BotPlots = "";
  2221. $aTopPlots = ""; $aBotPlots = "";
  2222. foreach($iValues as $Key => $Value)
  2223. {
  2224. $XCenterPos = cos(($Angle-$CDev+($Value*$SpliceRatio+$SpliceDistanceRatio)/2) * 3.1418 / 180 ) * $SpliceDistance + $XPos;
  2225. $YCenterPos = sin(($Angle-$CDev+($Value*$SpliceRatio+$SpliceDistanceRatio)/2) * 3.1418 / 180 ) * $SpliceDistance + $YPos;
  2226. $XCenterPos2 = cos(($Angle+$CDev+($Value*$SpliceRatio+$SpliceDistanceRatio)/2) * 3.1418 / 180 ) * $SpliceDistance + $XPos;
  2227. $YCenterPos2 = sin(($Angle+$CDev+($Value*$SpliceRatio+$SpliceDistanceRatio)/2) * 3.1418 / 180 ) * $SpliceDistance + $YPos;
  2228. $TopPlots[$Key][] = round($XCenterPos); $BotPlots[$Key][] = round($XCenterPos);
  2229. $TopPlots[$Key][] = round($YCenterPos); $BotPlots[$Key][] = round($YCenterPos + $SpliceHeight);
  2230. $aTopPlots[$Key][] = $XCenterPos; $aBotPlots[$Key][] = $XCenterPos;
  2231. $aTopPlots[$Key][] = $YCenterPos; $aBotPlots[$Key][] = $YCenterPos + $SpliceHeight;
  2232. /* Process labels position & size */
  2233. $Caption = "";
  2234. if ( !($DrawLabels == PIE_NOLABEL) )
  2235. {
  2236. $TAngle = $Angle+($Value*$SpliceRatio/2);
  2237. if ($DrawLabels == PIE_PERCENTAGE)
  2238. $Caption = (round($rValues[$Key] * pow(10,$Decimals) * $rSplicePercent)/pow(10,$Decimals))."%";
  2239. elseif ($DrawLabels == PIE_LABELS)
  2240. $Caption = $iLabels[$Key];
  2241. elseif ($DrawLabels == PIE_PERCENTAGE_LABEL)
  2242. $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  2243. $Position = imageftbbox($this->FontSize,0,$this->FontDIR.$this->FontName,$Caption);
  2244. $TextWidth = $Position[2]-$Position[0];
  2245. $TextHeight = abs($Position[1])+abs($Position[3]);
  2246. $TX = cos(($TAngle) * 3.1418 / 180 ) * ($Radius + 10)+ $XPos;
  2247. if ( $TAngle > 0 && $TAngle < 180 )
  2248. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($SkewHeight + 10) + $YPos + $SpliceHeight + 4;
  2249. else
  2250. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($SkewHeight + 4) + $YPos - ($TextHeight/2);
  2251. if ( $TAngle > 90 && $TAngle < 270 )
  2252. $TX = $TX - $TextWidth;
  2253. $C_TextColor = $this->AllocateColor($this->Picture,70,70,70);
  2254. imagettftext($this->Picture,$this->FontSize,0,$TX,$TY,$C_TextColor,$this->FontName,$Caption);
  2255. }
  2256. /* Process pie slices */
  2257. for($iAngle=$Angle;$iAngle<=$Angle+$Value*$SpliceRatio;$iAngle=$iAngle+.5)
  2258. {
  2259. $TopX = cos($iAngle * 3.1418 / 180 ) * $Radius + $XPos;
  2260. $TopY = sin($iAngle * 3.1418 / 180 ) * $SkewHeight + $YPos;
  2261. $TopPlots[$Key][] = round($TopX); $BotPlots[$Key][] = round($TopX);
  2262. $TopPlots[$Key][] = round($TopY); $BotPlots[$Key][] = round($TopY + $SpliceHeight);
  2263. $aTopPlots[$Key][] = $TopX; $aBotPlots[$Key][] = $TopX;
  2264. $aTopPlots[$Key][] = $TopY; $aBotPlots[$Key][] = $TopY + $SpliceHeight;
  2265. }
  2266. $TopPlots[$Key][] = round($XCenterPos2); $BotPlots[$Key][] = round($XCenterPos2);
  2267. $TopPlots[$Key][] = round($YCenterPos2); $BotPlots[$Key][] = round($YCenterPos2 + $SpliceHeight);
  2268. $aTopPlots[$Key][] = $XCenterPos2; $aBotPlots[$Key][] = $XCenterPos2;
  2269. $aTopPlots[$Key][] = $YCenterPos2; $aBotPlots[$Key][] = $YCenterPos2 + $SpliceHeight;
  2270. $Angle = $iAngle + $SpliceDistanceRatio;
  2271. }
  2272. /* Draw Bottom polygons */
  2273. foreach($iValues as $Key => $Value)
  2274. {
  2275. $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"],-20);
  2276. imagefilledpolygon($this->Picture,$BotPlots[$Key],(count($BotPlots[$Key])+1)/2,$C_GraphLo);
  2277. if ( $EnhanceColors ) { $En = -10; } else { $En = 0; }
  2278. for($j=0;$j<=count($aBotPlots[$Key])-4;$j=$j+2)
  2279. $this->drawLine($aBotPlots[$Key][$j],$aBotPlots[$Key][$j+1],$aBotPlots[$Key][$j+2],$aBotPlots[$Key][$j+3],$this->Palette[$Key]["R"]+$En,$this->Palette[$Key]["G"]+$En,$this->Palette[$Key]["B"]+$En);
  2280. }
  2281. /* Draw pie layers */
  2282. if ( $EnhanceColors ) { $ColorRatio = 30 / $SpliceHeight; } else { $ColorRatio = 25 / $SpliceHeight; }
  2283. for($i=$SpliceHeight-1;$i>=1;$i--)
  2284. {
  2285. foreach($iValues as $Key => $Value)
  2286. {
  2287. $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"],-10);
  2288. $Plots = ""; $Plot = 0;
  2289. foreach($TopPlots[$Key] as $Key2 => $Value2)
  2290. {
  2291. $Plot++;
  2292. if ( $Plot % 2 == 1 )
  2293. $Plots[] = $Value2;
  2294. else
  2295. $Plots[] = $Value2+$i;
  2296. }
  2297. imagefilledpolygon($this->Picture,$Plots,(count($Plots)+1)/2,$C_GraphLo);
  2298. $Index = count($Plots);
  2299. if ($EnhanceColors ) {$ColorFactor = -20 + ($SpliceHeight - $i) * $ColorRatio; } else { $ColorFactor = 0; }
  2300. $this->drawAntialiasPixel($Plots[0],$Plots[1],$this->Palette[$Key]["R"]+$ColorFactor,$this->Palette[$Key]["G"]+$ColorFactor,$this->Palette[$Key]["B"]+$ColorFactor);
  2301. $this->drawAntialiasPixel($Plots[2],$Plots[3],$this->Palette[$Key]["R"]+$ColorFactor,$this->Palette[$Key]["G"]+$ColorFactor,$this->Palette[$Key]["B"]+$ColorFactor);
  2302. $this->drawAntialiasPixel($Plots[$Index-4],$Plots[$Index-3],$this->Palette[$Key]["R"]+$ColorFactor,$this->Palette[$Key]["G"]+$ColorFactor,$this->Palette[$Key]["B"]+$ColorFactor);
  2303. }
  2304. }
  2305. /* Draw Top polygons */
  2306. for($Key=count($iValues)-1;$Key>=0;$Key--)
  2307. {
  2308. $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"]);
  2309. imagefilledpolygon($this->Picture,$TopPlots[$Key],(count($TopPlots[$Key])+1)/2,$C_GraphLo);
  2310. if ( $EnhanceColors ) { $En = 10; } else { $En = 0; }
  2311. for($j=0;$j<=count($aTopPlots[$Key])-4;$j=$j+2)
  2312. $this->drawLine($aTopPlots[$Key][$j],$aTopPlots[$Key][$j+1],$aTopPlots[$Key][$j+2],$aTopPlots[$Key][$j+3],$this->Palette[$Key]["R"]+$En,$this->Palette[$Key]["G"]+$En,$this->Palette[$Key]["B"]+$En);
  2313. }
  2314. }
  2315. /* This function can be used to set the background color */
  2316. function drawBackground($R,$G,$B)
  2317. {
  2318. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2319. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2320. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2321. $C_Background = $this->AllocateColor($this->Picture,$R,$G,$B);
  2322. imagefilledrectangle($this->Picture,0,0,$this->XSize,$this->YSize,$C_Background);
  2323. }
  2324. /* This function can be used to set the background color */
  2325. function drawGraphAreaGradient($R,$G,$B,$Decay,$Target=TARGET_GRAPHAREA)
  2326. {
  2327. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2328. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2329. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2330. if ( $Target == TARGET_GRAPHAREA ) { $X1 = $this->GArea_X1+1; $X2 = $this->GArea_X2-1; $Y1 = $this->GArea_Y1+1; $Y2 = $this->GArea_Y2; }
  2331. if ( $Target == TARGET_BACKGROUND ) { $X1 = 0; $X2 = $this->XSize; $Y1 = 0; $Y2 = $this->YSize; }
  2332. /* Positive gradient */
  2333. if ( $Decay > 0 )
  2334. {
  2335. $YStep = ($Y2 - $Y1 - 2) / $Decay;
  2336. for($i=0;$i<=$Decay;$i++)
  2337. {
  2338. $R-=1;$G-=1;$B-=1;
  2339. $Yi1 = $Y1 + ( $i * $YStep );
  2340. $Yi2 = ceil( $Yi1 + ( $i * $YStep ) + $YStep );
  2341. if ( $Yi2 >= $Yi2 ) { $Yi2 = $Y2-1; }
  2342. $C_Background = $this->AllocateColor($this->Picture,$R,$G,$B);
  2343. imagefilledrectangle($this->Picture,$X1,$Yi1,$X2,$Yi2,$C_Background);
  2344. }
  2345. }
  2346. /* Negative gradient */
  2347. if ( $Decay < 0 )
  2348. {
  2349. $YStep = ($Y2 - $Y1 - 2) / -$Decay;
  2350. $Yi1 = $Y1; $Yi2 = $Y1+$YStep;
  2351. for($i=-$Decay;$i>=0;$i--)
  2352. {
  2353. $R+=1;$G+=1;$B+=1;
  2354. $C_Background = $this->AllocateColor($this->Picture,$R,$G,$B);
  2355. imagefilledrectangle($this->Picture,$X1,$Yi1,$X2,$Yi2,$C_Background);
  2356. $Yi1+= $YStep;
  2357. $Yi2+= $YStep;
  2358. if ( $Yi2 >= $Yi2 ) { $Yi2 = $Y2-1; }
  2359. }
  2360. }
  2361. }
  2362. /* This function create a rectangle with antialias */
  2363. function drawRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B)
  2364. {
  2365. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2366. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2367. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2368. $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B);
  2369. $X1=$X1-.2;$Y1=$Y1-.2;
  2370. $X2=$X2+.2;$Y2=$Y2+.2;
  2371. $this->drawLine($X1,$Y1,$X2,$Y1,$R,$G,$B);
  2372. $this->drawLine($X2,$Y1,$X2,$Y2,$R,$G,$B);
  2373. $this->drawLine($X2,$Y2,$X1,$Y2,$R,$G,$B);
  2374. $this->drawLine($X1,$Y2,$X1,$Y1,$R,$G,$B);
  2375. }
  2376. /* This function create a filled rectangle with antialias */
  2377. function drawFilledRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B,$DrawBorder=TRUE,$Alpha=100,$NoFallBack=FALSE)
  2378. {
  2379. if ( $X2 < $X1 ) { list($X1, $X2) = array($X2, $X1); }
  2380. if ( $Y2 < $Y1 ) { list($Y1, $Y2) = array($Y2, $Y1); }
  2381. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2382. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2383. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2384. if ( $Alpha == 100 )
  2385. {
  2386. /* Process shadows */
  2387. if ( $this->ShadowActive && !$NoFallBack )
  2388. {
  2389. $this->drawFilledRectangle($X1+$this->ShadowXDistance,$Y1+$this->ShadowYDistance,$X2+$this->ShadowXDistance,$Y2+$this->ShadowYDistance,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,FALSE,$this->ShadowAlpha,TRUE);
  2390. if ( $this->ShadowBlur != 0 )
  2391. {
  2392. $AlphaDecay = ($this->ShadowAlpha / $this->ShadowBlur);
  2393. for($i=1; $i<=$this->ShadowBlur; $i++)
  2394. $this->drawFilledRectangle($X1+$this->ShadowXDistance-$i/2,$Y1+$this->ShadowYDistance-$i/2,$X2+$this->ShadowXDistance-$i/2,$Y2+$this->ShadowYDistance-$i/2,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,FALSE,$this->ShadowAlpha-$AlphaDecay*$i,TRUE);
  2395. for($i=1; $i<=$this->ShadowBlur; $i++)
  2396. $this->drawFilledRectangle($X1+$this->ShadowXDistance+$i/2,$Y1+$this->ShadowYDistance+$i/2,$X2+$this->ShadowXDistance+$i/2,$Y2+$this->ShadowYDistance+$i/2,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,FALSE,$this->ShadowAlpha-$AlphaDecay*$i,TRUE);
  2397. }
  2398. }
  2399. $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B);
  2400. imagefilledrectangle($this->Picture,round($X1),round($Y1),round($X2),round($Y2),$C_Rectangle);
  2401. }
  2402. else
  2403. {
  2404. $LayerWidth = abs($X2-$X1)+2;
  2405. $LayerHeight = abs($Y2-$Y1)+2;
  2406. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  2407. $C_White = $this->AllocateColor($this->Layers[0],255,255,255);
  2408. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  2409. imagecolortransparent($this->Layers[0],$C_White);
  2410. $C_Rectangle = $this->AllocateColor($this->Layers[0],$R,$G,$B);
  2411. imagefilledrectangle($this->Layers[0],round(1),round(1),round($LayerWidth-1),round($LayerHeight-1),$C_Rectangle);
  2412. imagecopymerge($this->Picture,$this->Layers[0],round(min($X1,$X2)-1),round(min($Y1,$Y2)-1),0,0,$LayerWidth,$LayerHeight,$Alpha);
  2413. imagedestroy($this->Layers[0]);
  2414. }
  2415. if ( $DrawBorder )
  2416. {
  2417. $ShadowSettings = $this->ShadowActive; $this->ShadowActive = FALSE;
  2418. $this->drawRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B);
  2419. $this->ShadowActive = $ShadowSettings;
  2420. }
  2421. }
  2422. /* This function create a rectangle with rounded corners and antialias */
  2423. function drawRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B)
  2424. {
  2425. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2426. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2427. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2428. $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B);
  2429. $Step = 90 / ((3.1418 * $Radius)/2);
  2430. for($i=0;$i<=90;$i=$i+$Step)
  2431. {
  2432. $X = cos(($i+180)*3.1418/180) * $Radius + $X1 + $Radius;
  2433. $Y = sin(($i+180)*3.1418/180) * $Radius + $Y1 + $Radius;
  2434. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2435. $X = cos(($i-90)*3.1418/180) * $Radius + $X2 - $Radius;
  2436. $Y = sin(($i-90)*3.1418/180) * $Radius + $Y1 + $Radius;
  2437. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2438. $X = cos(($i)*3.1418/180) * $Radius + $X2 - $Radius;
  2439. $Y = sin(($i)*3.1418/180) * $Radius + $Y2 - $Radius;
  2440. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2441. $X = cos(($i+90)*3.1418/180) * $Radius + $X1 + $Radius;
  2442. $Y = sin(($i+90)*3.1418/180) * $Radius + $Y2 - $Radius;
  2443. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2444. }
  2445. $X1=$X1-.2;$Y1=$Y1-.2;
  2446. $X2=$X2+.2;$Y2=$Y2+.2;
  2447. $this->drawLine($X1+$Radius,$Y1,$X2-$Radius,$Y1,$R,$G,$B);
  2448. $this->drawLine($X2,$Y1+$Radius,$X2,$Y2-$Radius,$R,$G,$B);
  2449. $this->drawLine($X2-$Radius,$Y2,$X1+$Radius,$Y2,$R,$G,$B);
  2450. $this->drawLine($X1,$Y2-$Radius,$X1,$Y1+$Radius,$R,$G,$B);
  2451. }
  2452. /* This function create a filled rectangle with rounded corners and antialias */
  2453. function drawFilledRoundedRectangle($X1,$Y1,$X2,$Y2,$Radius,$R,$G,$B)
  2454. {
  2455. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2456. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2457. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2458. $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B);
  2459. $Step = 90 / ((3.1418 * $Radius)/2);
  2460. for($i=0;$i<=90;$i=$i+$Step)
  2461. {
  2462. $Xi1 = cos(($i+180)*3.1418/180) * $Radius + $X1 + $Radius;
  2463. $Yi1 = sin(($i+180)*3.1418/180) * $Radius + $Y1 + $Radius;
  2464. $Xi2 = cos(($i-90)*3.1418/180) * $Radius + $X2 - $Radius;
  2465. $Yi2 = sin(($i-90)*3.1418/180) * $Radius + $Y1 + $Radius;
  2466. $Xi3 = cos(($i)*3.1418/180) * $Radius + $X2 - $Radius;
  2467. $Yi3 = sin(($i)*3.1418/180) * $Radius + $Y2 - $Radius;
  2468. $Xi4 = cos(($i+90)*3.1418/180) * $Radius + $X1 + $Radius;
  2469. $Yi4 = sin(($i+90)*3.1418/180) * $Radius + $Y2 - $Radius;
  2470. imageline($this->Picture,$Xi1,$Yi1,$X1+$Radius,$Yi1,$C_Rectangle);
  2471. imageline($this->Picture,$X2-$Radius,$Yi2,$Xi2,$Yi2,$C_Rectangle);
  2472. imageline($this->Picture,$X2-$Radius,$Yi3,$Xi3,$Yi3,$C_Rectangle);
  2473. imageline($this->Picture,$Xi4,$Yi4,$X1+$Radius,$Yi4,$C_Rectangle);
  2474. $this->drawAntialiasPixel($Xi1,$Yi1,$R,$G,$B);
  2475. $this->drawAntialiasPixel($Xi2,$Yi2,$R,$G,$B);
  2476. $this->drawAntialiasPixel($Xi3,$Yi3,$R,$G,$B);
  2477. $this->drawAntialiasPixel($Xi4,$Yi4,$R,$G,$B);
  2478. }
  2479. imagefilledrectangle($this->Picture,$X1,$Y1+$Radius,$X2,$Y2-$Radius,$C_Rectangle);
  2480. imagefilledrectangle($this->Picture,$X1+$Radius,$Y1,$X2-$Radius,$Y2,$C_Rectangle);
  2481. $X1=$X1-.2;$Y1=$Y1-.2;
  2482. $X2=$X2+.2;$Y2=$Y2+.2;
  2483. $this->drawLine($X1+$Radius,$Y1,$X2-$Radius,$Y1,$R,$G,$B);
  2484. $this->drawLine($X2,$Y1+$Radius,$X2,$Y2-$Radius,$R,$G,$B);
  2485. $this->drawLine($X2-$Radius,$Y2,$X1+$Radius,$Y2,$R,$G,$B);
  2486. $this->drawLine($X1,$Y2-$Radius,$X1,$Y1+$Radius,$R,$G,$B);
  2487. }
  2488. /* This function create a circle with antialias */
  2489. function drawCircle($Xc,$Yc,$Height,$R,$G,$B,$Width=0)
  2490. {
  2491. if ( $Width == 0 ) { $Width = $Height; }
  2492. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2493. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2494. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2495. $C_Circle = $this->AllocateColor($this->Picture,$R,$G,$B);
  2496. $Step = 360 / (2 * 3.1418 * max($Width,$Height));
  2497. for($i=0;$i<=360;$i=$i+$Step)
  2498. {
  2499. $X = cos($i*3.1418/180) * $Height + $Xc;
  2500. $Y = sin($i*3.1418/180) * $Width + $Yc;
  2501. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2502. }
  2503. }
  2504. /* This function create a filled circle/ellipse with antialias */
  2505. function drawFilledCircle($Xc,$Yc,$Height,$R,$G,$B,$Width=0)
  2506. {
  2507. if ( $Width == 0 ) { $Width = $Height; }
  2508. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2509. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2510. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2511. $C_Circle = $this->AllocateColor($this->Picture,$R,$G,$B);
  2512. $Step = 360 / (2 * 3.1418 * max($Width,$Height));
  2513. for($i=90;$i<=270;$i=$i+$Step)
  2514. {
  2515. $X1 = cos($i*3.1418/180) * $Height + $Xc;
  2516. $Y1 = sin($i*3.1418/180) * $Width + $Yc;
  2517. $X2 = cos((180-$i)*3.1418/180) * $Height + $Xc;
  2518. $Y2 = sin((180-$i)*3.1418/180) * $Width + $Yc;
  2519. $this->drawAntialiasPixel($X1-1,$Y1-1,$R,$G,$B);
  2520. $this->drawAntialiasPixel($X2-1,$Y2-1,$R,$G,$B);
  2521. if ( ($Y1-1) > $Yc - max($Width,$Height) )
  2522. imageline($this->Picture,$X1,$Y1-1,$X2-1,$Y2-1,$C_Circle);
  2523. }
  2524. }
  2525. /* This function will draw a filled ellipse */
  2526. function drawEllipse($Xc,$Yc,$Height,$Width,$R,$G,$B)
  2527. { $this->drawCircle($Xc,$Yc,$Height,$R,$G,$B,$Width); }
  2528. /* This function will draw an ellipse */
  2529. function drawFilledEllipse($Xc,$Yc,$Height,$Width,$R,$G,$B)
  2530. { $this->drawFilledCircle($Xc,$Yc,$Height,$R,$G,$B,$Width); }
  2531. /* This function create a line with antialias */
  2532. function drawLine($X1,$Y1,$X2,$Y2,$R,$G,$B,$GraphFunction=FALSE)
  2533. {
  2534. if ( $this->LineDotSize > 1 ) { $this->drawDottedLine($X1,$Y1,$X2,$Y2,$this->LineDotSize,$R,$G,$B,$GraphFunction); return(0); }
  2535. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2536. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2537. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2538. $Distance = sqrt(($X2-$X1)*($X2-$X1)+($Y2-$Y1)*($Y2-$Y1));
  2539. if ( $Distance == 0 )
  2540. return(-1);
  2541. $XStep = ($X2-$X1) / $Distance;
  2542. $YStep = ($Y2-$Y1) / $Distance;
  2543. for($i=0;$i<=$Distance;$i++)
  2544. {
  2545. $X = $i * $XStep + $X1;
  2546. $Y = $i * $YStep + $Y1;
  2547. if ( ($X >= $this->GArea_X1 && $X <= $this->GArea_X2 && $Y >= $this->GArea_Y1 && $Y <= $this->GArea_Y2) || !$GraphFunction )
  2548. {
  2549. if ( $this->LineWidth == 1 )
  2550. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2551. else
  2552. {
  2553. $StartOffset = -($this->LineWidth/2); $EndOffset = ($this->LineWidth/2);
  2554. for($j=$StartOffset;$j<=$EndOffset;$j++)
  2555. $this->drawAntialiasPixel($X+$j,$Y+$j,$R,$G,$B);
  2556. }
  2557. }
  2558. }
  2559. }
  2560. /* This function create a line with antialias */
  2561. function drawDottedLine($X1,$Y1,$X2,$Y2,$DotSize,$R,$G,$B,$GraphFunction=FALSE)
  2562. {
  2563. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2564. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2565. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2566. $Distance = sqrt(($X2-$X1)*($X2-$X1)+($Y2-$Y1)*($Y2-$Y1));
  2567. $XStep = ($X2-$X1) / $Distance;
  2568. $YStep = ($Y2-$Y1) / $Distance;
  2569. $DotIndex = 0;
  2570. for($i=0;$i<=$Distance;$i++)
  2571. {
  2572. $X = $i * $XStep + $X1;
  2573. $Y = $i * $YStep + $Y1;
  2574. if ( $DotIndex <= $DotSize)
  2575. {
  2576. if ( ($X >= $this->GArea_X1 && $X <= $this->GArea_X2 && $Y >= $this->GArea_Y1 && $Y <= $this->GArea_Y2) || !$GraphFunction )
  2577. {
  2578. if ( $this->LineWidth == 1 )
  2579. $this->drawAntialiasPixel($X,$Y,$R,$G,$B);
  2580. else
  2581. {
  2582. $StartOffset = -($this->LineWidth/2); $EndOffset = ($this->LineWidth/2);
  2583. for($j=$StartOffset;$j<=$EndOffset;$j++)
  2584. $this->drawAntialiasPixel($X+$j,$Y+$j,$R,$G,$B);
  2585. }
  2586. }
  2587. }
  2588. $DotIndex++;
  2589. if ( $DotIndex == $DotSize * 2 )
  2590. $DotIndex = 0;
  2591. }
  2592. }
  2593. /* Load a PNG file and draw it over the chart */
  2594. function drawFromPNG($FileName,$X,$Y,$Alpha=100)
  2595. { $this->drawFromPicture(1,$FileName,$X,$Y,$Alpha); }
  2596. /* Load a GIF file and draw it over the chart */
  2597. function drawFromGIF($FileName,$X,$Y,$Alpha=100)
  2598. { $this->drawFromPicture(2,$FileName,$X,$Y,$Alpha); }
  2599. /* Load a JPEG file and draw it over the chart */
  2600. function drawFromJPG($FileName,$X,$Y,$Alpha=100)
  2601. { $this->drawFromPicture(3,$FileName,$X,$Y,$Alpha); }
  2602. /* Generic loader function for external pictures */
  2603. function drawFromPicture($PicType,$FileName,$X,$Y,$Alpha=100)
  2604. {
  2605. if ( file_exists($FileName))
  2606. {
  2607. $Infos = getimagesize($FileName);
  2608. $Width = $Infos[0];
  2609. $Height = $Infos[1];
  2610. if ( $PicType == 1 ) { $Raster = imagecreatefrompng($FileName); }
  2611. if ( $PicType == 2 ) { $Raster = imagecreatefromgif($FileName); }
  2612. if ( $PicType == 3 ) { $Raster = imagecreatefromjpeg($FileName); }
  2613. imagecopymerge($this->Picture,$Raster,$X,$Y,0,0,$Width,$Height,$Alpha);
  2614. imagedestroy($Raster);
  2615. }
  2616. }
  2617. /* Draw an alpha pixel */
  2618. function drawAlphaPixel($X,$Y,$Alpha,$R,$G,$B)
  2619. {
  2620. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2621. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2622. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2623. if ( $X < 0 || $Y < 0 || $X >= $this->XSize || $Y >= $this->YSize )
  2624. return(-1);
  2625. $RGB2 = imagecolorat($this->Picture, $X, $Y);
  2626. $R2 = ($RGB2 >> 16) & 0xFF;
  2627. $G2 = ($RGB2 >> 8) & 0xFF;
  2628. $B2 = $RGB2 & 0xFF;
  2629. $iAlpha = (100 - $Alpha)/100;
  2630. $Alpha = $Alpha / 100;
  2631. $Ra = floor($R*$Alpha+$R2*$iAlpha);
  2632. $Ga = floor($G*$Alpha+$G2*$iAlpha);
  2633. $Ba = floor($B*$Alpha+$B2*$iAlpha);
  2634. $C_Aliased = $this->AllocateColor($this->Picture,$Ra,$Ga,$Ba);
  2635. imagesetpixel($this->Picture,$X,$Y,$C_Aliased);
  2636. }
  2637. /* Color helper */
  2638. function AllocateColor($Picture,$R,$G,$B,$Factor=0)
  2639. {
  2640. $R = $R + $Factor;
  2641. $G = $G + $Factor;
  2642. $B = $B + $Factor;
  2643. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2644. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2645. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2646. return(imagecolorallocate($Picture,$R,$G,$B));
  2647. }
  2648. /* Add a border to the picture */
  2649. function addBorder($Size=3,$R=0,$G=0,$B=0)
  2650. {
  2651. $Width = $this->XSize+2*$Size;
  2652. $Height = $this->YSize+2*$Size;
  2653. $Resampled = imagecreatetruecolor($Width,$Height);
  2654. $C_Background = $this->AllocateColor($Resampled,$R,$G,$B);
  2655. imagefilledrectangle($Resampled,0,0,$Width,$Height,$C_Background);
  2656. imagecopy($Resampled,$this->Picture,$Size,$Size,0,0,$this->XSize,$this->YSize);
  2657. imagedestroy($this->Picture);
  2658. $this->XSize = $Width;
  2659. $this->YSize = $Height;
  2660. $this->Picture = imagecreatetruecolor($this->XSize,$this->YSize);
  2661. $C_White = $this->AllocateColor($this->Picture,255,255,255);
  2662. imagefilledrectangle($this->Picture,0,0,$this->XSize,$this->YSize,$C_White);
  2663. imagecolortransparent($this->Picture,$C_White);
  2664. imagecopy($this->Picture,$Resampled,0,0,0,0,$this->XSize,$this->YSize);
  2665. }
  2666. /* Render the current picture to a file */
  2667. function Render($FileName)
  2668. {
  2669. if ( $this->ErrorReporting )
  2670. $this->printErrors($this->ErrorInterface);
  2671. /* Save image map if requested */
  2672. if ( $this->BuildMap )
  2673. $this->SaveImageMap();
  2674. imagepng($this->Picture,$FileName);
  2675. }
  2676. /* Render the current picture to STDOUT */
  2677. function Stroke()
  2678. {
  2679. if ( $this->ErrorReporting )
  2680. $this->printErrors("GD");
  2681. /* Save image map if requested */
  2682. if ( $this->BuildMap )
  2683. $this->SaveImageMap();
  2684. header('Content-type: image/png');
  2685. imagepng($this->Picture);
  2686. }
  2687. /* Private functions for internal processing */
  2688. function drawAntialiasPixel($X,$Y,$R,$G,$B,$Alpha=100,$NoFallBack=FALSE)
  2689. {
  2690. /* Process shadows */
  2691. if ( $this->ShadowActive && !$NoFallBack )
  2692. {
  2693. $this->drawAntialiasPixel($X+$this->ShadowXDistance,$Y+$this->ShadowYDistance,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,$this->ShadowAlpha,TRUE);
  2694. if ( $this->ShadowBlur != 0 )
  2695. {
  2696. $AlphaDecay = ($this->ShadowAlpha / $this->ShadowBlur);
  2697. for($i=1; $i<=$this->ShadowBlur; $i++)
  2698. $this->drawAntialiasPixel($X+$this->ShadowXDistance-$i/2,$Y+$this->ShadowYDistance-$i/2,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,$this->ShadowAlpha-$AlphaDecay*$i,TRUE);
  2699. for($i=1; $i<=$this->ShadowBlur; $i++)
  2700. $this->drawAntialiasPixel($X+$this->ShadowXDistance+$i/2,$Y+$this->ShadowYDistance+$i/2,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,$this->ShadowAlpha-$AlphaDecay*$i,TRUE);
  2701. }
  2702. }
  2703. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  2704. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  2705. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  2706. $Plot = "";
  2707. $Xi = floor($X);
  2708. $Yi = floor($Y);
  2709. if ( $Xi == $X && $Yi == $Y)
  2710. {
  2711. if ( $Alpha == 100 )
  2712. {
  2713. $C_Aliased = $this->AllocateColor($this->Picture,$R,$G,$B);
  2714. imagesetpixel($this->Picture,$X,$Y,$C_Aliased);
  2715. }
  2716. else
  2717. $this->drawAlphaPixel($X,$Y,$Alpha,$R,$G,$B);
  2718. }
  2719. else
  2720. {
  2721. $Alpha1 = (((1 - ($X - floor($X))) * (1 - ($Y - floor($Y))) * 100) / 100) * $Alpha;
  2722. if ( $Alpha1 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi,$Yi,$Alpha1,$R,$G,$B); }
  2723. $Alpha2 = ((($X - floor($X)) * (1 - ($Y - floor($Y))) * 100) / 100) * $Alpha;
  2724. if ( $Alpha2 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi+1,$Yi,$Alpha2,$R,$G,$B); }
  2725. $Alpha3 = (((1 - ($X - floor($X))) * ($Y - floor($Y)) * 100) / 100) * $Alpha;
  2726. if ( $Alpha3 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi,$Yi+1,$Alpha3,$R,$G,$B); }
  2727. $Alpha4 = ((($X - floor($X)) * ($Y - floor($Y)) * 100) / 100) * $Alpha;
  2728. if ( $Alpha4 > $this->AntialiasQuality ) { $this->drawAlphaPixel($Xi+1,$Yi+1,$Alpha4,$R,$G,$B); }
  2729. }
  2730. }
  2731. /* Validate data contained in the description array */
  2732. function validateDataDescription($FunctionName,&$DataDescription,$DescriptionRequired=TRUE)
  2733. {
  2734. if (!isset($DataDescription["Position"]))
  2735. {
  2736. $this->Errors[] = "[Warning] ".$FunctionName." - Y Labels are not set.";
  2737. $DataDescription["Position"] = "Name";
  2738. }
  2739. if ( $DescriptionRequired )
  2740. {
  2741. if (!isset($DataDescription["Description"]))
  2742. {
  2743. $this->Errors[] = "[Warning] ".$FunctionName." - Series descriptions are not set.";
  2744. foreach($DataDescription["Values"] as $key => $Value)
  2745. {
  2746. $DataDescription["Description"][$Value] = $Value;
  2747. }
  2748. }
  2749. if (count($DataDescription["Description"]) < count($DataDescription["Values"]))
  2750. {
  2751. $this->Errors[] = "[Warning] ".$FunctionName." - Some series descriptions are not set.";
  2752. foreach($DataDescription["Values"] as $key => $Value)
  2753. {
  2754. if ( !isset($DataDescription["Description"][$Value]))
  2755. $DataDescription["Description"][$Value] = $Value;
  2756. }
  2757. }
  2758. }
  2759. }
  2760. /* Validate data contained in the data array */
  2761. function validateData($FunctionName,&$Data)
  2762. {
  2763. $DataSummary = array();
  2764. foreach($Data as $key => $Values)
  2765. {
  2766. foreach($Values as $key2 => $Value)
  2767. {
  2768. if (!isset($DataSummary[$key2]))
  2769. $DataSummary[$key2] = 1;
  2770. else
  2771. $DataSummary[$key2]++;
  2772. }
  2773. }
  2774. if ( max($DataSummary) == 0 )
  2775. $this->Errors[] = "[Warning] ".$FunctionName." - No data set.";
  2776. foreach($DataSummary as $key => $Value)
  2777. {
  2778. if ($Value < max($DataSummary))
  2779. {
  2780. $this->Errors[] = "[Warning] ".$FunctionName." - Missing data in serie ".$key.".";
  2781. }
  2782. }
  2783. }
  2784. /* Print all error messages on the CLI or graphically */
  2785. function printErrors($Mode="CLI")
  2786. {
  2787. if (count($this->Errors) == 0)
  2788. return(0);
  2789. if ( $Mode == "CLI" )
  2790. {
  2791. foreach($this->Errors as $key => $Value)
  2792. echo $Value."\r\n";
  2793. }
  2794. elseif ( $Mode == "GD" )
  2795. {
  2796. $this->setLineStyle($Width=1);
  2797. $MaxWidth = 0;
  2798. foreach($this->Errors as $key => $Value)
  2799. {
  2800. $Position = imageftbbox($this->ErrorFontSize,0,$this->ErrorFontName,$Value);
  2801. $TextWidth = $Position[2]-$Position[0];
  2802. if ( $TextWidth > $MaxWidth ) { $MaxWidth = $TextWidth; }
  2803. }
  2804. $this->drawFilledRoundedRectangle($this->XSize-($MaxWidth+20),$this->YSize-(20+(($this->ErrorFontSize+4)*count($this->Errors))),$this->XSize-10,$this->YSize-10,6,233,185,185);
  2805. $this->drawRoundedRectangle($this->XSize-($MaxWidth+20),$this->YSize-(20+(($this->ErrorFontSize+4)*count($this->Errors))),$this->XSize-10,$this->YSize-10,6,193,145,145);
  2806. $C_TextColor = $this->AllocateColor($this->Picture,133,85,85);
  2807. $YPos = $this->YSize - (18 + (count($this->Errors)-1) * ($this->ErrorFontSize + 4));
  2808. foreach($this->Errors as $key => $Value)
  2809. {
  2810. imagettftext($this->Picture,$this->ErrorFontSize,0,$this->XSize-($MaxWidth+15),$YPos,$C_TextColor,$this->ErrorFontName,$Value);
  2811. $YPos = $YPos + ($this->ErrorFontSize + 4);
  2812. }
  2813. }
  2814. }
  2815. /* Activate the image map creation process */
  2816. function setImageMap($Mode=TRUE,$GraphID="MyGraph")
  2817. {
  2818. $this->BuildMap = $Mode;
  2819. $this->MapID = $GraphID;
  2820. }
  2821. /* Add a box into the image map */
  2822. function addToImageMap($X1,$Y1,$X2,$Y2,$SerieName,$Value,$CallerFunction)
  2823. {
  2824. if ( $this->MapFunction == NULL || $this->MapFunction == $CallerFunction )
  2825. {
  2826. $this->ImageMap[] = round($X1).",".round($Y1).",".round($X2).",".round($Y2).",".$SerieName.",".$Value;
  2827. $this->MapFunction = $CallerFunction;
  2828. }
  2829. }
  2830. /* Load and cleanup the image map from disk */
  2831. function getImageMap($MapName,$Flush=TRUE)
  2832. {
  2833. /* Strip HTML query strings */
  2834. $Values = $this->tmpFolder.$MapName;
  2835. $Value = split("\?",$Values);
  2836. $FileName = $Value[0];
  2837. if ( file_exists($FileName) )
  2838. {
  2839. $Handle = fopen($FileName, "r");
  2840. $MapContent = fread($Handle, filesize($FileName));
  2841. fclose($Handle);
  2842. echo $MapContent;
  2843. if ( $Flush )
  2844. unlink($FileName);
  2845. exit();
  2846. }
  2847. else
  2848. {
  2849. header("HTTP/1.0 404 Not Found");
  2850. exit();
  2851. }
  2852. }
  2853. /* Save the image map to the disk */
  2854. function SaveImageMap()
  2855. {
  2856. if ( !$this->BuildMap ) { return(-1); }
  2857. if ( $this->ImageMap == NULL )
  2858. {
  2859. $this->Errors[] = "[Warning] SaveImageMap - Image map is empty.";
  2860. return(-1);
  2861. }
  2862. $Handle = fopen($this->tmpFolder.$this->MapID, 'w');
  2863. if ( !$Handle )
  2864. {
  2865. $this->Errors[] = "[Warning] SaveImageMap - Cannot save the image map.";
  2866. return(-1);
  2867. }
  2868. else
  2869. {
  2870. foreach($this->ImageMap as $Key => $Value)
  2871. fwrite($Handle, htmlentities($Value)."\r");
  2872. }
  2873. fclose ($Handle);
  2874. }
  2875. /* Convert seconds to a time format string */
  2876. function ToTime($Value)
  2877. {
  2878. $Hour = floor($Value/3600);
  2879. $Minute = floor(($Value - $Hour*3600)/60);
  2880. $Second = floor($Value - $Hour*3600 - $Minute*60);
  2881. if (strlen($Hour) == 1 ) { $Hour = "0".$Hour; }
  2882. if (strlen($Minute) == 1 ) { $Minute = "0".$Minute; }
  2883. if (strlen($Second) == 1 ) { $Second = "0".$Second; }
  2884. return($Hour.":".$Minute.":".$Second);
  2885. }
  2886. /* Convert to metric system */
  2887. function ToMetric($Value)
  2888. {
  2889. $Go = floor($Value/1000000000);
  2890. $Mo = floor(($Value - $Go*1000000000)/1000000);
  2891. $Ko = floor(($Value - $Go*1000000000 - $Mo*1000000)/1000);
  2892. $o = floor($Value - $Go*1000000000 - $Mo*1000000 - $Ko*1000);
  2893. if ($Go != 0) { return($Go.".".$Mo."g"); }
  2894. if ($Mo != 0) { return($Mo.".".$ko."m"); }
  2895. if ($Ko != 0) { return($Ko.".".$o)."k"; }
  2896. return($o);
  2897. }
  2898. /* Convert to curency */
  2899. function ToCurrency($Value)
  2900. {
  2901. $Go = floor($Value/1000000000);
  2902. $Mo = floor(($Value - $Go*1000000000)/1000000);
  2903. $Ko = floor(($Value - $Go*1000000000 - $Mo*1000000)/1000);
  2904. $o = floor($Value - $Go*1000000000 - $Mo*1000000 - $Ko*1000);
  2905. if ( strlen($o) == 1 ) { $o = "00".$o; }
  2906. if ( strlen($o) == 2 ) { $o = "0".$o; }
  2907. $ResultString = $o;
  2908. if ( $Ko != 0 ) { $ResultString = $Ko.".".$ResultString; }
  2909. if ( $Mo != 0 ) { $ResultString = $Mo.".".$ResultString; }
  2910. if ( $Go != 0 ) { $ResultString = $Go.".".$ResultString; }
  2911. $ResultString = $this->Currency.$ResultString;
  2912. return($ResultString);
  2913. }
  2914. /* Set date format for axis labels */
  2915. function setDateFormat($Format)
  2916. {
  2917. $this->DateFormat = $Format;
  2918. }
  2919. /* Convert TS to a date format string */
  2920. function ToDate($Value)
  2921. {
  2922. return(date($this->DateFormat,$Value));
  2923. }
  2924. /* Check if a number is a full integer (for scaling) */
  2925. function isRealInt($Value)
  2926. {
  2927. if ($Value == floor($Value))
  2928. return(TRUE);
  2929. return(FALSE);
  2930. }
  2931. }
  2932. function RaiseFatal($Message)
  2933. {
  2934. echo "[FATAL] ".$Message."\r\n";
  2935. exit();
  2936. }
  2937. ?>