PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/ImageGraph/ImageGraphObject.php

https://github.com/quarkness/piwik
PHP | 1079 lines | 813 code | 142 blank | 124 comment | 211 complexity | 1f216900d31d02ea8bcf1a16c8db1a32 MD5 | raw file
  1. <?php
  2. /**
  3. * Piwik - Open source web analytics
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. * @version $Id$
  8. *
  9. * @category Piwik_Plugins
  10. * @package Piwik_ImageGraph
  11. */
  12. require_once PIWIK_INCLUDE_PATH."/libs/pChart.1.27d/pChart/pData.php";
  13. require_once PIWIK_INCLUDE_PATH."/libs/pChart.1.27d/pChart/pChart.php";
  14. class Piwik_ImageGraph_ImageGraphObject extends pChart
  15. {
  16. private $data = null;
  17. private $abscissaSerie = null;
  18. private $ordinateSerie = null;
  19. private $abscissaName = null;
  20. private $ordinateName = null;
  21. private $widthImg;
  22. private $heightImg;
  23. private $fontSize;
  24. private $title;
  25. private $metricTitle;
  26. private $imageFontHeight;
  27. private $imageOrdinateLabelMaxWidth;
  28. private $aliasedGraph;
  29. public function __construct(
  30. $width = Piwik_ImageGraph_API::GRAPH_WIDTH,
  31. $height = Piwik_ImageGraph_API::GRAPH_HEIGHT,
  32. $fontSize = Piwik_ImageGraph_API::GRAPH_FONT_SIZE)
  33. {
  34. //validate $width and $height
  35. $minWidth = 150;
  36. $maxWidth = 1500;
  37. if( !is_numeric($width) ||
  38. $width < $minWidth ||
  39. $width > $maxWidth
  40. )
  41. {
  42. throw new Exception(Piwik_Translate("General_ParameterMustIntegerBetween", array('$width', $minWidth, $maxWidth)));
  43. }
  44. $minHeight = 150;
  45. $maxHeight = 1500;
  46. if( !is_numeric($height) ||
  47. $height < $minHeight ||
  48. $height > $maxHeight
  49. )
  50. {
  51. throw new Exception(Piwik_Translate("General_ParameterMustIntegerBetween", array('$height', $minHeight, $maxHeight)));
  52. }
  53. $minFontSize = 3;
  54. $maxFontSize = 25;
  55. if( !is_numeric($fontSize) ||
  56. $fontSize < $minFontSize ||
  57. $fontSize > $maxFontSize
  58. )
  59. {
  60. throw new Exception(Piwik_Translate("General_ParameterMustIntegerBetween", array('$fontSize', $minFontSize, $maxFontSize)));
  61. }
  62. $this->widthImg = $width;
  63. $this->heightImg = $height;
  64. //Contruct the inherited pChart
  65. //parent::__construct($this->widthImg, $this->heightImg);
  66. //Workaround for white background
  67. $this->XSize = $this->widthImg;
  68. $this->YSize = $this->heightImg;
  69. $this->Picture = imagecreatetruecolor($this->widthImg,$this->heightImg);
  70. $C_White =$this->AllocateColor($this->Picture,255,255,255);
  71. imagefilledrectangle($this->Picture,0,0,$this->widthImg,$this->heightImg,$C_White);
  72. //Set font and properties
  73. $this->fontSize = $fontSize;
  74. $this->setFontProperties(PIWIK_INCLUDE_PATH."/libs/pChart.1.27d/Fonts/tahoma.ttf", $fontSize);
  75. $Position = imageftbbox($this->FontSize, 0, $this->FontName, "Test");
  76. $this->imageFontHeight = $Position[1]-$Position[7];
  77. }
  78. public function setData($abscissaSerie, $ordinateSerie, $abscissaName, $ordinateName, $title = false, $metricTitle = false, $aliasedGraph = false)
  79. {
  80. $sum = 0;
  81. foreach($ordinateSerie as $osVal)
  82. {
  83. $sum += $osVal;
  84. }
  85. if($sum == 0)
  86. {
  87. //throw new Exception(Piwik_Translate("General_NoDataForGraph"));
  88. }
  89. $this->abscissaSerie = $abscissaSerie;
  90. $this->ordinateSerie = $ordinateSerie;
  91. $this->abscissaName = $abscissaName;
  92. $this->ordinateName = $ordinateName;
  93. $this->title = "";
  94. $this->metricTitle = "";
  95. $this->aliasedGraph = false;
  96. if(!empty($title) && is_string($title))
  97. $this->title = $title;
  98. if(!empty($metricTitle) && is_string($metricTitle))
  99. $this->metricTitle = $metricTitle;
  100. if(!empty($aliasedGraph) && $aliasedGraph)
  101. $this->aliasedGraph = true;
  102. $this->imageOrdinateLabelMaxWidth = 0;
  103. foreach($ordinateSerie as $oName)
  104. {
  105. $Position = imageftbbox($this->FontSize, 0, $this->FontName, $oName);
  106. $oTemp = $Position[2] - $Position[0];
  107. $this->imageOrdinateLabelMaxWidth = $oTemp > $this->imageOrdinateLabelMaxWidth ? $oTemp : $this->imageOrdinateLabelMaxWidth;
  108. }
  109. }
  110. private function _getDataObj()
  111. {
  112. //Setup the pData - Object (pChart Framework at http://pchart.sourceforge.net/) to feed the graph
  113. $data = new pData;
  114. $data->AddPoint($this->ordinateSerie, "ORDINATE");
  115. $data->AddSerie("ORDINATE");
  116. $data->SetYAxisName($this->ordinateName);
  117. $data->SetSerieName($this->metricTitle, "ORDINATE");
  118. $data->AddPoint($this->abscissaSerie, "ABSCISSA");
  119. $data->SetAbsciseLabelSerie("ABSCISSA");
  120. $data->SetXAxisName($this->abscissaName);
  121. return $data;
  122. }
  123. public function print3dPieGraph( $hexColor0 = false,
  124. $hexColor1 = false,
  125. $hexColor2 = false,
  126. $hexColor3 = false,
  127. $hexColor4 = false,
  128. $hexColor5 = false,
  129. $hexColor6 = false
  130. )
  131. {
  132. $this->_truncateSmallValues();
  133. $this->data = $this->_getDataObj();
  134. $rgbColor = $this->_hex2rgb($hexColor0, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_0);
  135. $this->setColorPalette(0, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  136. $rgbColor = $this->_hex2rgb($hexColor1, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_1);
  137. $this->setColorPalette(1, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  138. $rgbColor = $this->_hex2rgb($hexColor2, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_2);
  139. $this->setColorPalette(2, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  140. $rgbColor = $this->_hex2rgb($hexColor3, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_3);
  141. $this->setColorPalette(3, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  142. $rgbColor = $this->_hex2rgb($hexColor4, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_4);
  143. $this->setColorPalette(4, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  144. $rgbColor = $this->_hex2rgb($hexColor5, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_5);
  145. $this->setColorPalette(5, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  146. $radius = $this->heightImg < ($this->widthImg - 100) ? $this->heightImg/1.75 : ($this->widthImg - 100)/2.5;
  147. $this->drawPieGraph($this->data->GetData(), $this->data->GetDataDescription(), $this->widthImg/2, $this->heightImg/2 , $radius, PIE_PERCENTAGE, true, 40, 20, 10);
  148. $rgbColor = $this->_hex2rgb($hexColor6, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_6);
  149. $this->drawPieLegend(5, 25, $this->data->GetData(), $this->data->GetDataDescription(), $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  150. }
  151. public function printBasicPieGraph( $hexColor0 = false,
  152. $hexColor1 = false,
  153. $hexColor2 = false,
  154. $hexColor3 = false,
  155. $hexColor4 = false,
  156. $hexColor5 = false,
  157. $hexColor6 = false
  158. )
  159. {
  160. $this->_truncateSmallValues();
  161. $this->data = $this->_getDataObj();
  162. $rgbColor = $this->_hex2rgb($hexColor0, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_0);
  163. $this->setColorPalette(0, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  164. $rgbColor = $this->_hex2rgb($hexColor1, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_1);
  165. $this->setColorPalette(1, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  166. $rgbColor = $this->_hex2rgb($hexColor2, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_2);
  167. $this->setColorPalette(2, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  168. $rgbColor = $this->_hex2rgb($hexColor3, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_3);
  169. $this->setColorPalette(3, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  170. $rgbColor = $this->_hex2rgb($hexColor4, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_4);
  171. $this->setColorPalette(4, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  172. $rgbColor = $this->_hex2rgb($hexColor5, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_5);
  173. $this->setColorPalette(5, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  174. $radius = $this->heightImg < ($this->widthImg - 100) ? $this->heightImg/2.5 : ($this->widthImg - 100)/2.5;
  175. $rgbColor = $this->_hex2rgb($hexColor6, Piwik_ImageGraph_API::GRAPH_COLOR_PIE_6);
  176. $this->drawBasicPieGraph($this->data->GetData(), $this->data->GetDataDescription(), $this->widthImg/2, $this->heightImg/2 , $radius, PIE_LABELS, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  177. }
  178. public function printBasicBarGraph( $hexColor0 = false,
  179. $hexColor1 = false
  180. )
  181. {
  182. $this->_computeAndApplyAbscissaModVal();
  183. $this->data = $this->_getDataObj();
  184. $rgbColor = $this->_hex2rgb($hexColor0, Piwik_ImageGraph_API::GRAPH_COLOR_BAR_0, 0.5);
  185. $this->setColorPalette(0, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  186. $rgbColor = $this->_hex2rgb($hexColor1, Piwik_ImageGraph_API::GRAPH_COLOR_BAR_1, 0.5);
  187. $this->setColorPalette(1, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  188. $maxMarginTop = $this->imageFontHeight/2;
  189. $maxMarginTop = $maxMarginTop > 5 ? $maxMarginTop : 5;
  190. if(!empty($this->metricTitle))
  191. $this->setGraphArea(5 + $this->imageOrdinateLabelMaxWidth, $maxMarginTop + (3 + $this->imageFontHeight), $this->widthImg, $this->heightImg - (11 + $this->imageFontHeight));
  192. else
  193. $this->setGraphArea(5 + $this->imageOrdinateLabelMaxWidth, $maxMarginTop, $this->widthImg, $this->heightImg - (11 + $this->imageFontHeight));
  194. $this->drawGraphArea(255, 255, 255);
  195. $this->drawScale($this->data->GetData(), $this->data->GetDataDescription(), SCALE_START0, 0, 0, 0, true, 0, 2, true);
  196. $this->DivisionCount = 2;
  197. $this->drawGrid(4, false);
  198. $this->drawBarGraph($this->data->GetData(), $this->data->GetDataDescription(), true);
  199. if(!empty($this->metricTitle))
  200. $this->drawLegend(10 + $this->imageOrdinateLabelMaxWidth,0, $this->data->GetDataDescription(), 255,255,255, -1,-1,-1, 0,0,0, false);
  201. }
  202. public function printBasicLineGraph($hexColor = false)
  203. {
  204. $this->_computeAndApplyAbscissaModVal();
  205. $this->data = $this->_getDataObj();
  206. $rgbColor = $this->_hex2rgb($hexColor, Piwik_ImageGraph_API::GRAPH_COLOR_LINE);
  207. $this->setColorPalette(0, $rgbColor['r'], $rgbColor['g'], $rgbColor['b']);
  208. $maxMarginTop = $this->imageFontHeight/2;
  209. $maxMarginTop = $maxMarginTop > 5 ? $maxMarginTop : 5;
  210. if(!empty($this->metricTitle))
  211. $this->setGraphArea(5 + $this->imageOrdinateLabelMaxWidth, $maxMarginTop + (3 + $this->imageFontHeight), $this->widthImg, $this->heightImg - (11 + $this->imageFontHeight));
  212. else
  213. $this->setGraphArea(5 + $this->imageOrdinateLabelMaxWidth, $maxMarginTop, $this->widthImg, $this->heightImg - (11 + $this->imageFontHeight));
  214. $this->drawGraphArea(255, 255, 255);
  215. $this->drawScale($this->data->GetData(), $this->data->GetDataDescription(), SCALE_START0, 0, 0, 0, true, 0, 2, true);
  216. $this->DivisionCount = 2;
  217. $this->drawGrid(4, false);
  218. $this->drawLineGraph($this->data->GetData(), $this->data->GetDataDescription(), true);
  219. $this->drawPlotGraph($this->data->GetData(), $this->data->GetDataDescription(), 3, 2, 255, 255, 255);
  220. if(!empty($this->metricTitle))
  221. $this->drawLegend(10 + $this->imageOrdinateLabelMaxWidth,0, $this->data->GetDataDescription(), 255,255,255, -1,-1,-1, 0,0,0, false);
  222. }
  223. public function printException($e)
  224. {
  225. $C_TextColor = $this->AllocateColor($this->Picture,0,0,0);
  226. imagettftext($this->Picture,$this->FontSize,0,5,$this->FontSize+5,$C_TextColor,$this->FontName,$e->getMessage());
  227. }
  228. private function _truncateSmallValues()
  229. {
  230. $others = 0;
  231. $currCount = count($this->ordinateSerie);
  232. $newOrdinateSerie = array();
  233. $newAbscissaSerie = array();
  234. $tmpCount = 0;
  235. $tmpTmpCount = 0;
  236. $sum = 0;
  237. foreach($this->ordinateSerie as $osVal)
  238. {
  239. $sum += $osVal;
  240. }
  241. foreach($this->ordinateSerie as $osVal)
  242. {
  243. if($tmpCount == ($currCount-1))
  244. {
  245. break;
  246. }
  247. if(($osVal / $sum) > 0.01)
  248. {
  249. $newOrdinateSerie[$tmpTmpCount] = $osVal;
  250. $newAbscissaSerie[$tmpTmpCount] = $this->abscissaSerie[$tmpCount];
  251. $tmpTmpCount++;
  252. }
  253. else
  254. {
  255. $others += $osVal;
  256. }
  257. $tmpCount++;
  258. }
  259. $others += $this->ordinateSerie[$currCount-1];
  260. if(($others / $sum) > 0.01)
  261. {
  262. $newOrdinateSerie[$tmpTmpCount] = $others;
  263. $newAbscissaSerie[$tmpTmpCount] = $this->abscissaSerie[$currCount-1];
  264. }
  265. $this->ordinateSerie = $newOrdinateSerie;
  266. $this->abscissaSerie = $newAbscissaSerie;
  267. }
  268. private function _computeAndApplyAbscissaModVal()
  269. {
  270. //Compute and apply the $abscissaModVal to $abscissaSerie
  271. //if the graphType is bar or line
  272. $maxL = 0;
  273. $abscissaModVal = 1;
  274. $rowCount = @count($this->abscissaSerie);
  275. foreach($this->abscissaSerie as $val)
  276. {
  277. $temp = strlen($val);
  278. $maxL = $temp > $maxL ? $temp : $maxL;
  279. }
  280. $abscissaModVal = round(($maxL*$this->fontSize)/(($this->widthImg*0.85)/$rowCount));
  281. if($abscissaModVal <= 0)
  282. {
  283. $abscissaModVal = 1;
  284. }
  285. foreach($this->abscissaSerie as $idx => &$val)
  286. {
  287. $val = ($idx % $abscissaModVal == 0) ? substr($val, 0, 22).(strlen($val) > 22 ? "..." : "") : "";
  288. }
  289. }
  290. private function _hex2rgb($hexColor, $default, $alpha = 1.0, $backGrey = 255)
  291. {
  292. if( !is_string($hexColor) ||
  293. strlen($hexColor) != 6
  294. )
  295. {
  296. $hexColor = $default;
  297. }
  298. $hexColor = strtolower($hexColor);
  299. if(strspn($hexColor, '0123456789abcdef') != 6)
  300. {
  301. return false;
  302. }
  303. $hexR = substr($hexColor, 0, 2);
  304. $hexG = substr($hexColor, 2, 2);
  305. $hexB = substr($hexColor, 4, 2);
  306. $r = hexdec($hexR);
  307. $g = hexdec($hexG);
  308. $b = hexdec($hexB);
  309. if( is_numeric($alpha) &&
  310. $alpha < 1.0 &&
  311. $alpha > 0.0 &&
  312. is_numeric($backGrey) &&
  313. $backGrey < 256 &&
  314. $backGrey >= 0
  315. )
  316. {
  317. $r *= $alpha; $r += ((1.0-$alpha)*$backGrey);
  318. $g *= $alpha; $g += ((1.0-$alpha)*$backGrey);
  319. $b *= $alpha; $b += ((1.0-$alpha)*$backGrey);
  320. }
  321. return array("r" => $r, "g" => $g, "b" => $b);
  322. }
  323. /*
  324. * Override
  325. */
  326. function drawGrid($LineWidth,$Mosaic=TRUE,$R=220,$G=220,$B=220,$Alpha=100)
  327. {
  328. //Draw mosaic
  329. if ( $Mosaic )
  330. {
  331. $LayerWidth = $this->GArea_X2-$this->GArea_X1;
  332. $LayerHeight = $this->GArea_Y2-$this->GArea_Y1;
  333. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  334. $C_White =$this->AllocateColor($this->Layers[0],255,255,255);
  335. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  336. imagecolortransparent($this->Layers[0],$C_White);
  337. $C_Rectangle =$this->AllocateColor($this->Layers[0],250,250,250);
  338. $YPos = $LayerHeight; //$this->GArea_Y2-1;
  339. $LastY = $YPos;
  340. for($i=0;$i<=$this->DivisionCount;$i++)
  341. {
  342. $LastY = $YPos;
  343. $YPos = $YPos - $this->DivisionHeight;
  344. if ( $YPos <= 0 ) { $YPos = 1; }
  345. if ( $i % 2 == 0 )
  346. {
  347. imagefilledrectangle($this->Layers[0],1,$YPos,$LayerWidth-1,$LastY,$C_Rectangle);
  348. }
  349. }
  350. imagecopymerge($this->Picture,$this->Layers[0],$this->GArea_X1,$this->GArea_Y1,0,0,$LayerWidth,$LayerHeight,$Alpha);
  351. imagedestroy($this->Layers[0]);
  352. }
  353. //Horizontal lines
  354. $YPos = $this->GArea_Y2 - $this->DivisionHeight;
  355. for($i=1;$i<=($this->DivisionCount+1);$i++)
  356. {
  357. if ( $YPos >= $this->GArea_Y1 && $YPos <= $this->GArea_Y2 )
  358. $this->drawLine($this->GArea_X1,$YPos,$this->GArea_X2,$YPos,$R,$G,$B);//$this->drawDottedLine($this->GArea_X1,$YPos,$this->GArea_X2,$YPos,$LineWidth,$R,$G,$B);
  359. $YPos = $YPos - $this->DivisionHeight;
  360. }
  361. /* Vertical lines */
  362. if ( $this->GAreaXOffset == 0 )
  363. { $XPos = $this->GArea_X1 + $this->DivisionWidth + $this->GAreaXOffset; $ColCount = $this->DataCount-2; }
  364. else
  365. { $XPos = $this->GArea_X1 + $this->GAreaXOffset; $ColCount = floor( ($this->GArea_X2 - $this->GArea_X1) / $this->DivisionWidth ); }
  366. $dataTmp = $this->data->getData();
  367. for($i=1;$i<=$ColCount;$i++)
  368. {
  369. if ( $XPos > $this->GArea_X1 && $XPos < $this->GArea_X2 )
  370. if(strlen($dataTmp[$i-1]['ABSCISSA']) > 0)
  371. $this->drawLine(floor($XPos),$this->GArea_Y1,floor($XPos),$this->GArea_Y2,$R,$G,$B);//$this->drawDottedLine(floor($XPos),$this->GArea_Y1,floor($XPos),$this->GArea_Y2,$LineWidth,$R,$G,$B);
  372. $XPos = $XPos + $this->DivisionWidth;
  373. }
  374. }
  375. /*
  376. * Override
  377. */
  378. function drawScale($Data,$DataDescription,$ScaleMode,$R,$G,$B,$DrawTicks=TRUE,$Angle=0,$Decimals=1,$WithMargin=FALSE,$SkipLabels=1,$RightScale=FALSE)
  379. {
  380. //Validate the Data and DataDescription array
  381. $this->validateData("drawScale",$Data);
  382. $C_TextColor =$this->AllocateColor($this->Picture,$R,$G,$B);
  383. //$this->drawLine($this->GArea_X1,$this->GArea_Y1,$this->GArea_X1,$this->GArea_Y2,$R,$G,$B);
  384. $this->drawLine($this->GArea_X1,$this->GArea_Y2,$this->GArea_X2,$this->GArea_Y2,$R,$G,$B);
  385. if ( $this->VMin == NULL && $this->VMax == NULL)
  386. {
  387. if (isset($DataDescription["Values"][0]))
  388. {
  389. $this->VMin = $Data[0][$DataDescription["Values"][0]];
  390. $this->VMax = $Data[0][$DataDescription["Values"][0]];
  391. }
  392. else { $this->VMin = 2147483647; $this->VMax = -2147483647; }
  393. //Compute Min and Max values
  394. if ( $ScaleMode == SCALE_NORMAL || $ScaleMode == SCALE_START0 )
  395. {
  396. if ( $ScaleMode == SCALE_START0 ) { $this->VMin = 0; }
  397. foreach ( $Data as $Key => $Values )
  398. {
  399. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  400. {
  401. if (isset($Data[$Key][$ColName]))
  402. {
  403. $Value = $Data[$Key][$ColName];
  404. if ( is_numeric($Value) )
  405. {
  406. if ( $this->VMax < $Value) { $this->VMax = $Value; }
  407. if ( $this->VMin > $Value) { $this->VMin = $Value; }
  408. }
  409. }
  410. }
  411. }
  412. }
  413. elseif ( $ScaleMode == SCALE_ADDALL || $ScaleMode == SCALE_ADDALLSTART0 ) //Experimental
  414. {
  415. if ( $ScaleMode == SCALE_ADDALLSTART0 ) { $this->VMin = 0; }
  416. foreach ( $Data as $Key => $Values )
  417. {
  418. $Sum = 0;
  419. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  420. {
  421. if (isset($Data[$Key][$ColName]))
  422. {
  423. $Value = $Data[$Key][$ColName];
  424. if ( is_numeric($Value) )
  425. $Sum += $Value;
  426. }
  427. }
  428. if ( $this->VMax < $Sum) { $this->VMax = $Sum; }
  429. if ( $this->VMin > $Sum) { $this->VMin = $Sum; }
  430. }
  431. }
  432. if ( $this->VMax > preg_replace('/\.[0-9]+/','',$this->VMax) )
  433. $this->VMax = preg_replace('/\.[0-9]+/','',$this->VMax)+1;
  434. //If all values are the same
  435. if ( $this->VMax == $this->VMin )
  436. {
  437. if ( $this->VMax >= 0 ) { $this->VMax++; }
  438. else { $this->VMin--; }
  439. }
  440. $DataRange = $this->VMax - $this->VMin;
  441. if ( $DataRange == 0 ) { $DataRange = .1; }
  442. //Compute automatic scaling
  443. $ScaleOk = FALSE; $Factor = 1;
  444. $MinDivHeight = 25; $MaxDivs = ($this->GArea_Y2 - $this->GArea_Y1) / $MinDivHeight;
  445. if ( $this->VMin == 0 && $this->VMax == 0 )
  446. { $this->VMin = 0; $this->VMax = 2; $Scale = 1; $Divisions = 2;}
  447. elseif ($MaxDivs > 1)
  448. {
  449. while(!$ScaleOk)
  450. {
  451. $Scale1 = ( $this->VMax - $this->VMin ) / $Factor;
  452. $Scale2 = ( $this->VMax - $this->VMin ) / $Factor / 2;
  453. $Scale4 = ( $this->VMax - $this->VMin ) / $Factor / 4;
  454. if ( $Scale1 > 1 && $Scale1 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale1); $Scale = 1;}
  455. if ( $Scale2 > 1 && $Scale2 <= $MaxDivs && !$ScaleOk) { $ScaleOk = TRUE; $Divisions = floor($Scale2); $Scale = 2;}
  456. if (!$ScaleOk)
  457. {
  458. if ( $Scale2 > 1 ) { $Factor = $Factor * 10; }
  459. if ( $Scale2 < 1 ) { $Factor = $Factor / 10; }
  460. }
  461. if($Factor == 10)
  462. {
  463. $Divisions = 2;
  464. $Scale = 1;
  465. break;
  466. }
  467. }
  468. if ( floor($this->VMax / $Scale / $Factor) != $this->VMax / $Scale / $Factor)
  469. {
  470. $GridID = floor ( $this->VMax / $Scale / $Factor) + 1;
  471. $this->VMax = $GridID * $Scale * $Factor;
  472. $Divisions++;
  473. }
  474. if ( floor($this->VMin / $Scale / $Factor) != $this->VMin / $Scale / $Factor)
  475. {
  476. $GridID = floor( $this->VMin / $Scale / $Factor);
  477. $this->VMin = $GridID * $Scale * $Factor;
  478. $Divisions++;
  479. }
  480. }
  481. else //Can occurs for small graphs
  482. $Scale = 1;
  483. if ( !isset($Divisions) )
  484. $Divisions = 2;
  485. if ($Scale == 1 && $Divisions%2 == 1)
  486. $Divisions--;
  487. }
  488. else
  489. $Divisions = $this->Divisions;
  490. $this->DivisionCount = 2;
  491. $Divisions = 2;
  492. $DataRange = $this->VMax - $this->VMin;
  493. if ( $DataRange == 0 ) { $DataRange = .1; }
  494. $this->DivisionHeight = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $Divisions;
  495. $this->DivisionRatio = ( $this->GArea_Y2 - $this->GArea_Y1 ) / $DataRange;
  496. $this->GAreaXOffset = 0;
  497. if ( count($Data) > 1 )
  498. {
  499. if ( $WithMargin == FALSE )
  500. $this->DivisionWidth = ( $this->GArea_X2 - $this->GArea_X1 ) / (count($Data)-1);
  501. else
  502. {
  503. $this->DivisionWidth = ( $this->GArea_X2 - $this->GArea_X1 ) / (count($Data));
  504. $this->GAreaXOffset = $this->DivisionWidth / 2;
  505. }
  506. }
  507. else
  508. {
  509. $this->DivisionWidth = $this->GArea_X2 - $this->GArea_X1;
  510. $this->GAreaXOffset = $this->DivisionWidth / 2;
  511. }
  512. $this->DataCount = count($Data);
  513. if ( $DrawTicks == FALSE )
  514. return(0);
  515. $YPos = $this->GArea_Y2; $XMin = NULL;
  516. for($i=1;$i<=$Divisions+1;$i++)
  517. {
  518. if ( $RightScale )
  519. $this->drawLine($this->GArea_X2,$YPos,$this->GArea_X2+5,$YPos,$R,$G,$B);
  520. else
  521. //$this->drawLine($this->GArea_X1,$YPos,$this->GArea_X1-5,$YPos,$R,$G,$B);
  522. $Value = $this->VMin + ($i-1) * (( $this->VMax - $this->VMin ) / $Divisions);
  523. $Value = round($Value * pow(10,$Decimals)) / pow(10,$Decimals);
  524. if ( $DataDescription["Format"]["Y"] == "number" )
  525. $Value = $Value.$DataDescription["Unit"]["Y"];
  526. if ( $DataDescription["Format"]["Y"] == "time" )
  527. $Value = $this->ToTime($Value);
  528. if ( $DataDescription["Format"]["Y"] == "date" )
  529. $Value = $this->ToDate($Value);
  530. if ( $DataDescription["Format"]["Y"] == "metric" )
  531. $Value = $this->ToMetric($Value);
  532. if ( $DataDescription["Format"]["Y"] == "currency" )
  533. $Value = $this->ToCurrency($Value);
  534. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  535. $TextWidth = $Position[2]-$Position[0];
  536. if ( $RightScale )
  537. {
  538. imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X2+5,$YPos+($this->FontSize/2),$C_TextColor,$this->FontName,$Value);
  539. if ( $XMin < $this->GArea_X2+5+$TextWidth || $XMin == NULL ) { $XMin = $this->GArea_X2+5+$TextWidth; }
  540. }
  541. else
  542. {
  543. imagettftext($this->Picture,$this->FontSize,0,$this->GArea_X1-5-$TextWidth,$YPos+($this->FontSize/2),$C_TextColor,$this->FontName,$Value);
  544. if ( $XMin > $this->GArea_X1-5-$TextWidth || $XMin == NULL ) { $XMin = $this->GArea_X1-5-$TextWidth; }
  545. }
  546. $YPos = $YPos - $this->DivisionHeight;
  547. }
  548. //Write the Y Axis caption if set
  549. if ( isset($DataDescription["Axis"]["Y"]) )
  550. {
  551. $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["Y"]);
  552. $TextHeight = abs($Position[1])+abs($Position[3]);
  553. $TextTop = (($this->GArea_Y2 - $this->GArea_Y1) / 2) + $this->GArea_Y1 + ($TextHeight/2);
  554. if ( $RightScale )
  555. imagettftext($this->Picture,$this->FontSize,90,$XMin+$this->FontSize,$TextTop,$C_TextColor,$this->FontName,$DataDescription["Axis"]["Y"]);
  556. else
  557. ;//imagettftext($this->Picture,$this->FontSize,90,$XMin-$this->FontSize,$TextTop,$C_TextColor,$this->FontName,$DataDescription["Axis"]["Y"]);
  558. }
  559. //Horizontal Axis
  560. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  561. $ID = 1; $YMax = NULL;
  562. foreach ( $Data as $Key => $Values )
  563. {
  564. if ( $ID % $SkipLabels == 0 )
  565. {
  566. $this->drawLine(floor($XPos),$this->GArea_Y2,floor($XPos),$this->GArea_Y2+5,$R,$G,$B);
  567. $Value = $Data[$Key][$DataDescription["Position"]];
  568. if ( $DataDescription["Format"]["X"] == "number" )
  569. $Value = $Value.$DataDescription["Unit"]["X"];
  570. if ( $DataDescription["Format"]["X"] == "time" )
  571. $Value = $this->ToTime($Value);
  572. if ( $DataDescription["Format"]["X"] == "date" )
  573. $Value = $this->ToDate($Value);
  574. if ( $DataDescription["Format"]["X"] == "metric" )
  575. $Value = $this->ToMetric($Value);
  576. if ( $DataDescription["Format"]["X"] == "currency" )
  577. $Value = $this->ToCurrency($Value);
  578. $Position = imageftbbox($this->FontSize,$Angle,$this->FontName,$Value);
  579. $TextWidth = abs($Position[2])+abs($Position[0]);
  580. $TextHeight = abs($Position[1])+abs($Position[3]);
  581. if ( $Angle == 0 )
  582. {
  583. //$YPos = $this->GArea_Y2+18;
  584. $YPos = $this->GArea_Y2 + $this->imageFontHeight + 9;
  585. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-floor($TextWidth/2),$YPos,$C_TextColor,$this->FontName,$Value);
  586. }
  587. else
  588. {
  589. $YPos = $this->GArea_Y2+10+$TextHeight;
  590. if ( $Angle <= 90 )
  591. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)-$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value);
  592. else
  593. imagettftext($this->Picture,$this->FontSize,$Angle,floor($XPos)+$TextWidth+5,$YPos,$C_TextColor,$this->FontName,$Value);
  594. }
  595. if ( $YMax < $YPos || $YMax == NULL ) { $YMax = $YPos; }
  596. }
  597. $XPos = $XPos + $this->DivisionWidth;
  598. $ID++;
  599. }
  600. //Write the X Axis caption if set
  601. if ( isset($DataDescription["Axis"]["X"]) )
  602. {
  603. $Position = imageftbbox($this->FontSize,90,$this->FontName,$DataDescription["Axis"]["X"]);
  604. $TextWidth = abs($Position[2])+abs($Position[0]);
  605. $TextLeft = (($this->GArea_X2 - $this->GArea_X1) / 2) + $this->GArea_X1 + ($TextWidth/2);
  606. //imagettftext($this->Picture,$this->FontSize,0,$TextLeft,$YMax+$this->FontSize+5,$C_TextColor,$this->FontName,$DataDescription["Axis"]["X"]);
  607. }
  608. }
  609. /*
  610. * Override
  611. */
  612. function drawGraphArea($R,$G,$B,$Stripe=FALSE)
  613. {
  614. $this->drawFilledRectangle($this->GArea_X1,$this->GArea_Y1,$this->GArea_X2,$this->GArea_Y2,$R,$G,$B,FALSE);
  615. //$this->drawRectangle($this->GArea_X1,$this->GArea_Y1,$this->GArea_X2,$this->GArea_Y2,$R-40,$G-40,$B-40);
  616. if ( $Stripe )
  617. {
  618. $R2 = $R-15; if ( $R2 < 0 ) { $R2 = 0; }
  619. $G2 = $R-15; if ( $G2 < 0 ) { $G2 = 0; }
  620. $B2 = $R-15; if ( $B2 < 0 ) { $B2 = 0; }
  621. $LineColor =$this->AllocateColor($this->Picture,$R2,$G2,$B2);
  622. $SkewWidth = $this->GArea_Y2-$this->GArea_Y1-1;
  623. for($i=$this->GArea_X1-$SkewWidth;$i<=$this->GArea_X2;$i=$i+4)
  624. {
  625. $X1 = $i; $Y1 = $this->GArea_Y2;
  626. $X2 = $i+$SkewWidth; $Y2 = $this->GArea_Y1;
  627. if ( $X1 < $this->GArea_X1 )
  628. { $X1 = $this->GArea_X1; $Y1 = $this->GArea_Y1 + $X2 - $this->GArea_X1 + 1; }
  629. if ( $X2 >= $this->GArea_X2 )
  630. { $Y2 = $this->GArea_Y1 + $X2 - $this->GArea_X2 +1; $X2 = $this->GArea_X2 - 1; }
  631. // * Fixed in 1.27 * { $X2 = $this->GArea_X2 - 1; $Y2 = $this->GArea_Y2 - ($this->GArea_X2 - $X1); }
  632. imageline($this->Picture,$X1,$Y1,$X2,$Y2+1,$LineColor);
  633. }
  634. }
  635. }
  636. /*
  637. * Override
  638. */
  639. function drawBarGraph($Data,$DataDescription,$Shadow=FALSE,$Alpha=100)
  640. {
  641. //Validate the Data and DataDescription array
  642. $this->validateDataDescription("drawBarGraph",$DataDescription);
  643. $this->validateData("drawBarGraph",$Data);
  644. $GraphID = 0;
  645. $Series = count($DataDescription["Values"]);
  646. $SeriesWidth = $this->DivisionWidth / ($Series+1);
  647. $SeriesWidth *= 1.7;
  648. $SerieXOffset = $this->DivisionWidth*1.7 / 2 - $SeriesWidth / 2;
  649. $SeriesWidth = round($SeriesWidth); //EDIT
  650. $SerieXOffset = round($SerieXOffset); //EDIT
  651. $YZero = $this->GArea_Y2 - ((0-$this->VMin) * $this->DivisionRatio);
  652. if ( $YZero > $this->GArea_Y2 ) { $YZero = $this->GArea_Y2; }
  653. $SerieID = 0;
  654. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  655. {
  656. $ID = 0;
  657. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  658. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  659. $XPos = $this->GArea_X1 + $this->GAreaXOffset - $SerieXOffset + $SeriesWidth * $SerieID;
  660. $XPos = round($XPos); //EDIT
  661. $XLast = -1;
  662. foreach ( $Data as $Key => $Values )
  663. {
  664. if ( isset($Data[$Key][$ColName]))
  665. {
  666. if ( is_numeric($Data[$Key][$ColName]) )
  667. {
  668. $Value = $Data[$Key][$ColName];
  669. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  670. $YPos = round($YPos); //EDIT
  671. //Save point into the image map if option activated
  672. if ( $this->BuildMap )
  673. {
  674. $this->addToImageMap($XPos+1,min($YZero,$YPos),$XPos+$SeriesWidth-1,max($YZero,$YPos),$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"Bar");
  675. }
  676. $this->drawRectangle($XPos,$YZero,$XPos+$SeriesWidth,$YPos-1,25,25,25,$this->Palette[$ColorID+1]["R"],$this->Palette[$ColorID+1]["G"],$this->Palette[$ColorID+1]["B"],TRUE,$Alpha);
  677. $this->drawFilledRectangle($XPos+1,$YZero-1,$XPos+$SeriesWidth-1,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE,$Alpha);
  678. }
  679. }
  680. $XPos = $XPos + $this->DivisionWidth;
  681. }
  682. $SerieID++;
  683. }
  684. }
  685. function drawBasicPieGraph($Data,$DataDescription,$XPos,$YPos,$Radius=100,$DrawLabels=PIE_NOLABEL,$R=255,$G=255,$B=255,$Decimals=0)
  686. {
  687. /* Validate the Data and DataDescription array */
  688. $this->validateDataDescription("drawBasicPieGraph",$DataDescription,FALSE);
  689. $this->validateData("drawBasicPieGraph",$Data);
  690. /* Determine pie sum */
  691. $Series = 0; $PieSum = 0;
  692. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  693. {
  694. if ( $ColName != $DataDescription["Position"] )
  695. {
  696. $Series++;
  697. foreach ( $Data as $Key => $Values )
  698. {
  699. if ( isset($Data[$Key][$ColName]))
  700. $PieSum = $PieSum + $Data[$Key][$ColName]; $iValues[] = $Data[$Key][$ColName]; $iLabels[] = $Data[$Key][$DataDescription["Position"]];
  701. }
  702. }
  703. }
  704. /* Validate serie */
  705. if ( $Series != 1 )
  706. RaiseFatal("Pie chart can only accept one serie of data.");
  707. $SpliceRatio = 360 / $PieSum;
  708. $SplicePercent = 100 / $PieSum;
  709. /* Calculate all polygons */
  710. $Angle = 35; $TopPlots = "";
  711. foreach($iValues as $Key => $Value)
  712. {
  713. $TopPlots[$Key][] = $XPos;
  714. $TopPlots[$Key][] = $YPos;
  715. /* Process labels position & size */
  716. $Caption = "";
  717. if ( !($DrawLabels == PIE_NOLABEL) )
  718. {
  719. $TAngle = $Angle+($Value*$SpliceRatio/2);
  720. if ($DrawLabels == PIE_PERCENTAGE)
  721. $Caption = (round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  722. elseif ($DrawLabels == PIE_LABELS)
  723. $Caption = $iLabels[$Key];
  724. elseif ($DrawLabels == PIE_PERCENTAGE_LABEL)
  725. $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  726. elseif ($DrawLabels == PIE_PERCENTAGE_LABEL)
  727. $Caption = $iLabels[$Key]."\r\n".(round($Value * pow(10,$Decimals) * $SplicePercent)/pow(10,$Decimals))."%";
  728. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Caption);
  729. $TextWidth = $Position[2]-$Position[0];
  730. $TextHeight = abs($Position[1])+abs($Position[3]);
  731. $TX = cos(($TAngle) * 3.1418 / 180 ) * ($Radius+10) + $XPos;
  732. if ( $TAngle > 0 && $TAngle < 180 )
  733. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+10) + $YPos + 4;
  734. else
  735. $TY = sin(($TAngle) * 3.1418 / 180 ) * ($Radius+4) + $YPos - ($TextHeight/2);
  736. if ( $TAngle > 90 && $TAngle < 270 )
  737. $TX = $TX - $TextWidth;
  738. $C_TextColor = $this->AllocateColor($this->Picture,0,0,0);
  739. imagettftext($this->Picture,$this->FontSize,0,$TX,$TY,$C_TextColor,$this->FontName,$Caption);
  740. }
  741. /* Process pie slices */
  742. for($iAngle=$Angle;$iAngle<=$Angle+$Value*$SpliceRatio;$iAngle=$iAngle+.5)
  743. {
  744. $TopX = cos($iAngle * 3.1418 / 180 ) * $Radius + $XPos;
  745. $TopY = sin($iAngle * 3.1418 / 180 ) * $Radius + $YPos;
  746. $TopPlots[$Key][] = $TopX;
  747. $TopPlots[$Key][] = $TopY;
  748. }
  749. $TopPlots[$Key][] = $XPos;
  750. $TopPlots[$Key][] = $YPos;
  751. $Angle = $iAngle;
  752. }
  753. $PolyPlots = $TopPlots;
  754. /* Set array values type to float --- PHP Bug with imagefilledpolygon casting to integer */
  755. foreach ($TopPlots as $Key => $Value)
  756. { foreach ($TopPlots[$Key] as $Key2 => $Value2) { settype($TopPlots[$Key][$Key2],"float"); } }
  757. /* Draw Top polygons */
  758. foreach ($PolyPlots as $Key => $Value)
  759. {
  760. $C_GraphLo = $this->AllocateColor($this->Picture,$this->Palette[$Key]["R"],$this->Palette[$Key]["G"],$this->Palette[$Key]["B"]);
  761. imagefilledpolygon($this->Picture,$PolyPlots[$Key],(count($PolyPlots[$Key])+1)/2,$C_GraphLo);
  762. }
  763. $this->drawCircle($XPos-.5,$YPos-.5,$Radius,$R,$G,$B);
  764. $this->drawCircle($XPos-.5,$YPos-.5,$Radius+.5,$R,$G,$B);
  765. /* Draw Top polygons */
  766. foreach ($TopPlots as $Key => $Value)
  767. {
  768. for($j=0;$j<=count($TopPlots[$Key])-4;$j=$j+2)
  769. $this->drawLine($TopPlots[$Key][$j],$TopPlots[$Key][$j+1],$TopPlots[$Key][$j+2],$TopPlots[$Key][$j+3],$R,$G,$B);
  770. }
  771. }
  772. /* This function draw a line graph */
  773. function drawLineGraph($Data,$DataDescription,$SerieName="")
  774. {
  775. /* Validate the Data and DataDescription array */
  776. $this->validateDataDescription("drawLineGraph",$DataDescription);
  777. $this->validateData("drawLineGraph",$Data);
  778. $GraphID = 0;
  779. foreach ( $DataDescription["Values"] as $Key2 => $ColName )
  780. {
  781. $ID = 0;
  782. foreach ( $DataDescription["Description"] as $keyI => $ValueI )
  783. { if ( $keyI == $ColName ) { $ColorID = $ID; }; $ID++; }
  784. if ( $SerieName == "" || $SerieName == $ColName )
  785. {
  786. $XPos = $this->GArea_X1 + $this->GAreaXOffset;
  787. $XLast = -1;
  788. foreach ( $Data as $Key => $Values )
  789. {
  790. if ( isset($Data[$Key][$ColName]))
  791. {
  792. $Value = $Data[$Key][$ColName];
  793. $YPos = $this->GArea_Y2 - (($Value-$this->VMin) * $this->DivisionRatio);
  794. /* Save point into the image map if option activated */
  795. if ( $this->BuildMap )
  796. $this->addToImageMap($XPos-3,$YPos-3,$XPos+3,$YPos+3,$DataDescription["Description"][$ColName],$Data[$Key][$ColName].$DataDescription["Unit"]["Y"],"Line");
  797. if (!is_numeric($Value)) { $XLast = -1; }
  798. if ( $XLast != -1 )
  799. {
  800. /*if(abs(($YPos-$YLast) / ($XPos-$XLast)) > 1)
  801. {
  802. $this->drawLine($XLast-1,$YLast,$XPos-1,$YPos,($this->Palette[$ColorID]["R"]*0.15)+216,($this->Palette[$ColorID]["G"]*0.15)+216,($this->Palette[$ColorID]["B"]*0.15)+216,TRUE);
  803. $this->drawLine($XLast+1,$YLast,$XPos+1,$YPos,($this->Palette[$ColorID]["R"]*0.15)+216,($this->Palette[$ColorID]["G"]*0.15)+216,($this->Palette[$ColorID]["B"]*0.15)+216,TRUE);
  804. }
  805. else
  806. {
  807. $this->drawLine($XLast,$YLast-1,$XPos,$YPos-1,($this->Palette[$ColorID]["R"]*0.15)+216,($this->Palette[$ColorID]["G"]*0.15)+216,($this->Palette[$ColorID]["B"]*0.15)+216,TRUE);
  808. $this->drawLine($XLast,$YLast+1,$XPos,$YPos+1,($this->Palette[$ColorID]["R"]*0.15)+216,($this->Palette[$ColorID]["G"]*0.15)+216,($this->Palette[$ColorID]["B"]*0.15)+216,TRUE);
  809. }*/
  810. $this->drawLine($XLast,$YLast,$XPos,$YPos,$this->Palette[$ColorID]["R"],$this->Palette[$ColorID]["G"],$this->Palette[$ColorID]["B"],TRUE);
  811. }
  812. $XLast = $XPos;
  813. $YLast = $YPos;
  814. if (!is_numeric($Value)) { $XLast = -1; }
  815. }
  816. $XPos = $XPos + $this->DivisionWidth;
  817. }
  818. $GraphID++;
  819. }
  820. }
  821. }
  822. function drawLine($X1,$Y1,$X2,$Y2,$R,$G,$B,$GraphFunction=FALSE)
  823. {
  824. if(!$this->aliasedGraph)
  825. {
  826. imageline($this->Picture, $X1, $Y1, $X2, $Y2, imagecolorallocate($this->Picture, $R, $G, $B));
  827. return;
  828. }
  829. parent::drawLine($X1,$Y1,$X2,$Y2,$R,$G,$B,$GraphFunction);
  830. }
  831. function drawRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B)
  832. {
  833. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  834. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  835. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  836. $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B);
  837. imagerectangle($this->Picture, $X1,$Y1,$X2,$Y2, $C_Rectangle);
  838. /*$X1=$X1-.2;$Y1=$Y1-.2;
  839. $X2=$X2+.2;$Y2=$Y2+.2;
  840. $this->drawLine($X1,$Y1,$X2,$Y1,$R,$G,$B);
  841. $this->drawLine($X2,$Y1,$X2,$Y2,$R,$G,$B);
  842. $this->drawLine($X2,$Y2,$X1,$Y2,$R,$G,$B);
  843. $this->drawLine($X1,$Y2,$X1,$Y1,$R,$G,$B);*/
  844. }
  845. /* This function create a filled rectangle with antialias */
  846. function drawFilledRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B,$DrawBorder=TRUE,$Alpha=100,$NoFallBack=FALSE)
  847. {
  848. if ( $X2 < $X1 ) { list($X1, $X2) = array($X2, $X1); }
  849. if ( $Y2 < $Y1 ) { list($Y1, $Y2) = array($Y2, $Y1); }
  850. if ( $R < 0 ) { $R = 0; } if ( $R > 255 ) { $R = 255; }
  851. if ( $G < 0 ) { $G = 0; } if ( $G > 255 ) { $G = 255; }
  852. if ( $B < 0 ) { $B = 0; } if ( $B > 255 ) { $B = 255; }
  853. $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B);
  854. imagefilledrectangle($this->Picture, $X1,$Y1,$X2,$Y2, $C_Rectangle);
  855. /*if ( $Alpha == 100 )
  856. {
  857. if ( $this->ShadowActive && !$NoFallBack )
  858. {
  859. $this->drawFilledRectangle($X1+$this->ShadowXDistance,$Y1+$this->ShadowYDistance,$X2+$this->ShadowXDistance,$Y2+$this->ShadowYDistance,$this->ShadowRColor,$this->ShadowGColor,$this->ShadowBColor,FALSE,$this->ShadowAlpha,TRUE);
  860. if ( $this->ShadowBlur != 0 )
  861. {
  862. $AlphaDecay = ($this->ShadowAlpha / $this->ShadowBlur);
  863. for($i=1; $i<=$this->ShadowBlur; $i++)
  864. $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);
  865. for($i=1; $i<=$this->ShadowBlur; $i++)
  866. $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);
  867. }
  868. }
  869. $C_Rectangle = $this->AllocateColor($this->Picture,$R,$G,$B);
  870. imagefilledrectangle($this->Picture,round($X1),round($Y1),round($X2),round($Y2),$C_Rectangle);
  871. }
  872. else
  873. {
  874. $LayerWidth = abs($X2-$X1)+2;
  875. $LayerHeight = abs($Y2-$Y1)+2;
  876. $this->Layers[0] = imagecreatetruecolor($LayerWidth,$LayerHeight);
  877. $C_White = $this->AllocateColor($this->Layers[0],255,255,255);
  878. imagefilledrectangle($this->Layers[0],0,0,$LayerWidth,$LayerHeight,$C_White);
  879. imagecolortransparent($this->Layers[0],$C_White);
  880. $C_Rectangle = $this->AllocateColor($this->Layers[0],$R,$G,$B);
  881. imagefilledrectangle($this->Layers[0],round(1),round(1),round($LayerWidth-1),round($LayerHeight-1),$C_Rectangle);
  882. imagecopymerge($this->Picture,$this->Layers[0],round(min($X1,$X2)-1),round(min($Y1,$Y2)-1),0,0,$LayerWidth,$LayerHeight,$Alpha);
  883. imagedestroy($this->Layers[0]);
  884. }
  885. if ( $DrawBorder )
  886. {
  887. $ShadowSettings = $this->ShadowActive; $this->ShadowActive = FALSE;
  888. $this->drawRectangle($X1,$Y1,$X2,$Y2,$R,$G,$B);
  889. $this->ShadowActive = $ShadowSettings;
  890. }*/
  891. }
  892. /* Draw the data legends */
  893. function drawLegend($XPos,$YPos,$DataDescription,$R,$G,$B,$Rs=-1,$Gs=-1,$Bs=-1,$Rt=0,$Gt=0,$Bt=0,$Border=TRUE)
  894. {
  895. /* Validate the Data and DataDescription array */
  896. $this->validateDataDescription("drawLegend",$DataDescription);
  897. if ( !isset($DataDescription["Description"]) )
  898. return(-1);
  899. $C_TextColor =$this->AllocateColor($this->Picture,$Rt,$Gt,$Bt);
  900. /* <-10->[8]<-4->Text<-10-> */
  901. $MaxWidth = 0; $MaxHeight = 8;
  902. foreach($DataDescription["Description"] as $Key => $Value)
  903. {
  904. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  905. $TextWidth = $Position[2]-$Position[0];
  906. $TextHeight = $Position[1]-$Position[7];
  907. if ( $TextWidth > $MaxWidth) { $MaxWidth = $TextWidth; }
  908. $MaxHeight = $MaxHeight + $TextHeight + 4;
  909. }
  910. $MaxHeight = $MaxHeight - 5;
  911. $MaxWidth = $MaxWidth + 32;
  912. if ( $Rs == -1 || $Gs == -1 || $Bs == -1 )
  913. { $Rs = $R-30; $Gs = $G-30; $Bs = $B-30; }
  914. if ( $Border )
  915. {
  916. $this->drawFilledRoundedRectangle($XPos+1,$YPos+1,$XPos+$MaxWidth+1,$YPos+$MaxHeight+1,5,$Rs,$Gs,$Bs);
  917. $this->drawFilledRoundedRectangle($XPos,$YPos,$XPos+$MaxWidth,$YPos+$MaxHeight,5,$R,$G,$B);
  918. }
  919. $YOffset = $this->FontSize; $ID = 0;
  920. foreach($DataDescription["Description"] as $Key => $Value)
  921. {
  922. $Position = imageftbbox($this->FontSize,0,$this->FontName,$Value);
  923. $TextHeight = $Position[1]-$Position[7];
  924. $this->drawFilledRectangle($XPos,$YPos+$YOffset-($TextHeight/2)+1,$XPos+8,$YPos+$YOffset-($TextHeight/2),$this->Palette[$ID]["R"],$this->Palette[$ID]["G"],$this->Palette[$ID]["B"]);
  925. //$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"]);
  926. imagettftext($this->Picture,$this->FontSize,0,$XPos+12,$YPos+$YOffset,$C_TextColor,$this->FontName,$Value);
  927. $YOffset = $YOffset + $TextHeight + 4;
  928. $ID++;
  929. }
  930. }
  931. }