PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/pChart/class/pSurface.class.php

https://gitlab.com/billyprice1/status2
PHP | 315 lines | 251 code | 41 blank | 23 comment | 119 complexity | fee9dbc6d16ab4b4394a000c681c16c3 MD5 | raw file
  1. <?php
  2. /*
  3. pSurface - class to draw surface charts
  4. Version : 2.1.3
  5. Made by : Jean-Damien POGOLOTTI
  6. Last Update : 09/09/11
  7. This file can be distributed under the license you can find at :
  8. http://www.pchart.net/license
  9. You can find the whole class documentation on the pChart web site.
  10. */
  11. define("UNKNOWN" , 0.123456789);
  12. define("IGNORED" , -1);
  13. define("LABEL_POSITION_LEFT" , 880001);
  14. define("LABEL_POSITION_RIGHT" , 880002);
  15. define("LABEL_POSITION_TOP" , 880003);
  16. define("LABEL_POSITION_BOTTOM" , 880004);
  17. /* pStock class definition */
  18. class pSurface
  19. {
  20. var $pChartObject;
  21. var $GridSizeX;
  22. var $GridSizeY;
  23. var $Points;
  24. /* Class creator */
  25. function pSurface($pChartObject)
  26. {
  27. $this->pChartObject = $pChartObject;
  28. $this->GridSize = 10;
  29. $this->Points = "";
  30. }
  31. /* Define the grid size and initialise the 2D matrix */
  32. function setGrid($XSize=10,$YSize=10)
  33. {
  34. for($X=0; $X<=$XSize; $X++) { for($Y=0; $Y<=$YSize; $Y++) { $this->Points[$X][$Y]=UNKNOWN; } }
  35. $this->GridSizeX = $XSize;
  36. $this->GridSizeY = $YSize;
  37. }
  38. /* Add a point on the grid */
  39. function addPoint($X,$Y,$Value,$Force=TRUE)
  40. {
  41. if ( $X < 0 || $X >$this->GridSizeX ) { return(0); }
  42. if ( $Y < 0 || $Y >$this->GridSizeY ) { return(0); }
  43. if ( $this->Points[$X][$Y] == UNKNOWN || $Force )
  44. $this->Points[$X][$Y] = $Value;
  45. elseif ( $this->Points[$X][$Y] == UNKNOWN )
  46. $this->Points[$X][$Y] = $Value;
  47. else
  48. $this->Points[$X][$Y] = ($this->Points[$X][$Y] + $Value)/2;
  49. }
  50. /* Write the X labels */
  51. function writeXLabels($Format="")
  52. {
  53. $R = isset($Format["R"]) ? $Format["R"] : $this->pChartObject->FontColorR;
  54. $G = isset($Format["G"]) ? $Format["G"] : $this->pChartObject->FontColorG;
  55. $B = isset($Format["B"]) ? $Format["B"] : $this->pChartObject->FontColorB;
  56. $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : $this->pChartObject->FontColorA;
  57. $Angle = isset($Format["Angle"]) ? $Format["Angle"] : 0;
  58. $Padding = isset($Format["Padding"]) ? $Format["Padding"] : 5;
  59. $Position = isset($Format["Position"]) ? $Format["Position"] : LABEL_POSITION_TOP;
  60. $Labels = isset($Format["Labels"]) ? $Format["Labels"] : NULL;
  61. $CountOffset = isset($Format["CountOffset"]) ? $Format["CountOffset"] : 0;
  62. if ( $Labels != NULL && !is_array($Labels) ) { $Label = $Labels; $Labels = ""; $Labels[] = $Label; }
  63. $X0 = $this->pChartObject->GraphAreaX1;
  64. $XSize = ($this->pChartObject->GraphAreaX2 - $this->pChartObject->GraphAreaX1) / ($this->GridSizeX+1);
  65. $Settings = array("Angle"=>$Angle,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha);
  66. if ( $Position == LABEL_POSITION_TOP )
  67. {
  68. $YPos = $this->pChartObject->GraphAreaY1 - $Padding;
  69. if ($Angle == 0 ) { $Settings["Align"] = TEXT_ALIGN_BOTTOMMIDDLE; }
  70. if ($Angle != 0 ) { $Settings["Align"] = TEXT_ALIGN_MIDDLELEFT; }
  71. }
  72. elseif ( $Position == LABEL_POSITION_BOTTOM )
  73. {
  74. $YPos = $this->pChartObject->GraphAreaY2 + $Padding;
  75. if ($Angle == 0 ) { $Settings["Align"] = TEXT_ALIGN_TOPMIDDLE; }
  76. if ($Angle != 0 ) { $Settings["Align"] = TEXT_ALIGN_MIDDLERIGHT; }
  77. }
  78. else
  79. return(-1);
  80. for($X=0;$X<=$this->GridSizeX;$X++)
  81. {
  82. $XPos = floor($X0+$X*$XSize + $XSize/2);
  83. if( $Labels == NULL || !isset($Labels[$X]) )
  84. $Value = $X+$CountOffset;
  85. else
  86. $Value = $Labels[$X];
  87. $this->pChartObject->drawText($XPos,$YPos,$Value,$Settings);
  88. }
  89. }
  90. /* Write the Y labels */
  91. function writeYLabels($Format="")
  92. {
  93. $R = isset($Format["R"]) ? $Format["R"] : $this->pChartObject->FontColorR;
  94. $G = isset($Format["G"]) ? $Format["G"] : $this->pChartObject->FontColorG;
  95. $B = isset($Format["B"]) ? $Format["B"] : $this->pChartObject->FontColorB;
  96. $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : $this->pChartObject->FontColorA;
  97. $Angle = isset($Format["Angle"]) ? $Format["Angle"] : 0;
  98. $Padding = isset($Format["Padding"]) ? $Format["Padding"] : 5;
  99. $Position = isset($Format["Position"]) ? $Format["Position"] : LABEL_POSITION_LEFT;
  100. $Labels = isset($Format["Labels"]) ? $Format["Labels"] : NULL;
  101. $CountOffset = isset($Format["CountOffset"]) ? $Format["CountOffset"] : 0;
  102. if ( $Labels != NULL && !is_array($Labels) ) { $Label = $Labels; $Labels = ""; $Labels[] = $Label; }
  103. $Y0 = $this->pChartObject->GraphAreaY1;
  104. $YSize = ($this->pChartObject->GraphAreaY2 - $this->pChartObject->GraphAreaY1) / ($this->GridSizeY+1);
  105. $Settings = array("Angle"=>$Angle,"R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha);
  106. if ( $Position == LABEL_POSITION_LEFT )
  107. { $XPos = $this->pChartObject->GraphAreaX1 - $Padding; $Settings["Align"] = TEXT_ALIGN_MIDDLERIGHT; }
  108. elseif ( $Position == LABEL_POSITION_RIGHT )
  109. { $XPos = $this->pChartObject->GraphAreaX2 + $Padding; $Settings["Align"] = TEXT_ALIGN_MIDDLELEFT; }
  110. else
  111. return(-1);
  112. for($Y=0;$Y<=$this->GridSizeY;$Y++)
  113. {
  114. $YPos = floor($Y0+$Y*$YSize + $YSize/2);
  115. if( $Labels == NULL || !isset($Labels[$Y]) )
  116. $Value = $Y+$CountOffset;
  117. else
  118. $Value = $Labels[$Y];
  119. $this->pChartObject->drawText($XPos,$YPos,$Value,$Settings);
  120. }
  121. }
  122. /* Draw the area arround the specified Threshold */
  123. function drawContour($Threshold,$Format="")
  124. {
  125. $R = isset($Format["R"]) ? $Format["R"] : 0;
  126. $G = isset($Format["G"]) ? $Format["G"] : 0;
  127. $B = isset($Format["B"]) ? $Format["B"] : 0;
  128. $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100;
  129. $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : 3;
  130. $Padding = isset($Format["Padding"]) ? $Format["Padding"] : 0;
  131. $X0 = $this->pChartObject->GraphAreaX1;
  132. $Y0 = $this->pChartObject->GraphAreaY1;
  133. $XSize = ($this->pChartObject->GraphAreaX2 - $this->pChartObject->GraphAreaX1) / ($this->GridSizeX+1);
  134. $YSize = ($this->pChartObject->GraphAreaY2 - $this->pChartObject->GraphAreaY1) / ($this->GridSizeY+1);
  135. $Color = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha,"Ticks"=>$Ticks);
  136. for($X=0;$X<=$this->GridSizeX;$X++)
  137. {
  138. for($Y=0;$Y<=$this->GridSizeY;$Y++)
  139. {
  140. $Value = $this->Points[$X][$Y];
  141. if ( $Value != UNKNOWN && $Value != IGNORED && $Value >= $Threshold)
  142. {
  143. $X1 = floor($X0+$X*$XSize)+$Padding;
  144. $Y1 = floor($Y0+$Y*$YSize)+$Padding;
  145. $X2 = floor($X0+$X*$XSize+$XSize);
  146. $Y2 = floor($Y0+$Y*$YSize+$YSize);
  147. if ( $X > 0 && $this->Points[$X-1][$Y] != UNKNOWN && $this->Points[$X-1][$Y] != IGNORED && $this->Points[$X-1][$Y] < $Threshold)
  148. $this->pChartObject->drawLine($X1,$Y1,$X1,$Y2,$Color);
  149. if ( $Y > 0 && $this->Points[$X][$Y-1] != UNKNOWN && $this->Points[$X][$Y-1] != IGNORED && $this->Points[$X][$Y-1] < $Threshold)
  150. $this->pChartObject->drawLine($X1,$Y1,$X2,$Y1,$Color);
  151. if ( $X < $this->GridSizeX && $this->Points[$X+1][$Y] != UNKNOWN && $this->Points[$X+1][$Y] != IGNORED && $this->Points[$X+1][$Y] < $Threshold)
  152. $this->pChartObject->drawLine($X2,$Y1,$X2,$Y2,$Color);
  153. if ( $Y < $this->GridSizeY && $this->Points[$X][$Y+1] != UNKNOWN && $this->Points[$X][$Y+1] != IGNORED && $this->Points[$X][$Y+1] < $Threshold)
  154. $this->pChartObject->drawLine($X1,$Y2,$X2,$Y2,$Color);
  155. }
  156. }
  157. }
  158. }
  159. /* Draw the surface chart */
  160. function drawSurface($Format="")
  161. {
  162. $Palette = isset($Format["Palette"]) ? $Format["Palette"] : NULL;
  163. $ShadeR1 = isset($Format["ShadeR1"]) ? $Format["ShadeR1"] : 77;
  164. $ShadeG1 = isset($Format["ShadeG1"]) ? $Format["ShadeG1"] : 205;
  165. $ShadeB1 = isset($Format["ShadeB1"]) ? $Format["ShadeB1"] : 21;
  166. $ShadeA1 = isset($Format["ShadeA1"]) ? $Format["ShadeA1"] : 40;
  167. $ShadeR2 = isset($Format["ShadeR2"]) ? $Format["ShadeR2"] : 227;
  168. $ShadeG2 = isset($Format["ShadeG2"]) ? $Format["ShadeG2"] : 135;
  169. $ShadeB2 = isset($Format["ShadeB2"]) ? $Format["ShadeB2"] : 61;
  170. $ShadeA2 = isset($Format["ShadeA2"]) ? $Format["ShadeA2"] : 100;
  171. $Border = isset($Format["Border"]) ? $Format["Border"] : FALSE;
  172. $BorderR = isset($Format["BorderR"]) ? $Format["BorderR"] : 0;
  173. $BorderG = isset($Format["BorderG"]) ? $Format["BorderG"] : 0;
  174. $BorderB = isset($Format["BorderB"]) ? $Format["BorderB"] : 0;
  175. $Surrounding = isset($Format["Surrounding"]) ? $Format["Surrounding"] : -1;
  176. $Padding = isset($Format["Padding"]) ? $Format["Padding"] : 1;
  177. $X0 = $this->pChartObject->GraphAreaX1;
  178. $Y0 = $this->pChartObject->GraphAreaY1;
  179. $XSize = ($this->pChartObject->GraphAreaX2 - $this->pChartObject->GraphAreaX1) / ($this->GridSizeX+1);
  180. $YSize = ($this->pChartObject->GraphAreaY2 - $this->pChartObject->GraphAreaY1) / ($this->GridSizeY+1);
  181. for($X=0;$X<=$this->GridSizeX;$X++)
  182. {
  183. for($Y=0;$Y<=$this->GridSizeY;$Y++)
  184. {
  185. $Value = $this->Points[$X][$Y];
  186. if ( $Value != UNKNOWN && $Value != IGNORED )
  187. {
  188. $X1 = floor($X0+$X*$XSize)+$Padding;
  189. $Y1 = floor($Y0+$Y*$YSize)+$Padding;
  190. $X2 = floor($X0+$X*$XSize+$XSize);
  191. $Y2 = floor($Y0+$Y*$YSize+$YSize);
  192. if ( $Palette != NULL )
  193. {
  194. if ( isset($Palette[$Value]) && isset($Palette[$Value]["R"]) ) { $R = $Palette[$Value]["R"]; } else { $R = 0; }
  195. if ( isset($Palette[$Value]) && isset($Palette[$Value]["G"]) ) { $G = $Palette[$Value]["G"]; } else { $G = 0; }
  196. if ( isset($Palette[$Value]) && isset($Palette[$Value]["B"]) ) { $B = $Palette[$Value]["B"]; } else { $B = 0; }
  197. if ( isset($Palette[$Value]) && isset($Palette[$Value]["Alpha"]) ) { $Alpha = $Palette[$Value]["Alpha"]; } else { $Alpha = 1000; }
  198. }
  199. else
  200. {
  201. $R = (($ShadeR2-$ShadeR1)/100)*$Value + $ShadeR1;
  202. $G = (($ShadeG2-$ShadeG1)/100)*$Value + $ShadeG1;
  203. $B = (($ShadeB2-$ShadeB1)/100)*$Value + $ShadeB1;
  204. $Alpha = (($ShadeA2-$ShadeA1)/100)*$Value + $ShadeA1;
  205. }
  206. $Settings = array("R"=>$R,"G"=>$G,"B"=>$B,"Alpha"=>$Alpha);
  207. if ( $Border ) { $Settings["BorderR"] = $BorderR; $Settings["BorderG"] = $BorderG; $Settings["BorderB"] = $BorderB; }
  208. if ( $Surrounding != -1 ) { $Settings["BorderR"] = $R+$Surrounding; $Settings["BorderG"] = $G+$Surrounding; $Settings["BorderB"] = $B+$Surrounding; }
  209. $this->pChartObject->drawFilledRectangle($X1,$Y1,$X2-1,$Y2-1,$Settings);
  210. }
  211. }
  212. }
  213. }
  214. /* Compute the missing points */
  215. function computeMissing()
  216. {
  217. $Missing = "";
  218. for($X=0;$X<=$this->GridSizeX;$X++)
  219. {
  220. for($Y=0;$Y<=$this->GridSizeY;$Y++)
  221. {
  222. if ( $this->Points[$X][$Y] == UNKNOWN )
  223. $Missing[] = $X.",".$Y;
  224. }
  225. }
  226. shuffle($Missing);
  227. foreach($Missing as $Key => $Pos)
  228. {
  229. $Pos = preg_split("/,/",$Pos);
  230. $X = $Pos[0];
  231. $Y = $Pos[1];
  232. if ( $this->Points[$X][$Y] == UNKNOWN )
  233. {
  234. $NearestNeighbor = $this->getNearestNeighbor($X,$Y);
  235. $Value = 0; $Points = 0;
  236. for($Xi=$X-$NearestNeighbor;$Xi<=$X+$NearestNeighbor;$Xi++)
  237. {
  238. for($Yi=$Y-$NearestNeighbor;$Yi<=$Y+$NearestNeighbor;$Yi++)
  239. {
  240. if ($Xi >=0 && $Yi >= 0 && $Xi <= $this->GridSizeX && $Yi <= $this->GridSizeY && $this->Points[$Xi][$Yi] != UNKNOWN && $this->Points[$Xi][$Yi] != IGNORED)
  241. {
  242. $Value = $Value + $this->Points[$Xi][$Yi]; $Points++;
  243. }
  244. }
  245. }
  246. if ( $Points != 0 ) { $this->Points[$X][$Y] = $Value / $Points; }
  247. }
  248. }
  249. }
  250. /* Return the nearest Neighbor distance of a point */
  251. function getNearestNeighbor($Xp,$Yp)
  252. {
  253. $Nearest = UNKNOWN;
  254. for($X=0;$X<=$this->GridSizeX;$X++)
  255. {
  256. for($Y=0;$Y<=$this->GridSizeY;$Y++)
  257. {
  258. if ( $this->Points[$X][$Y] != UNKNOWN && $this->Points[$X][$Y] != IGNORED )
  259. {
  260. $DistanceX = max($Xp,$X)-min($Xp,$X);
  261. $DistanceY = max($Yp,$Y)-min($Yp,$Y);
  262. $Distance = max($DistanceX,$DistanceY);
  263. if ( $Distance < $Nearest || $Nearest == UNKNOWN ) { $Nearest = $Distance; }
  264. }
  265. }
  266. }
  267. return($Nearest);
  268. }
  269. }
  270. ?>