/modules/pChart/pSpring.class.php

https://gitlab.com/x33n/ampache · PHP · 868 lines · 703 code · 109 blank · 56 comment · 203 complexity · 6bdf41377d74c16d81522e948c1e66f3 MD5 · raw file

  1. <?php
  2. /*
  3. pSpring - class to draw spring graphs
  4. Version : 2.1.4
  5. Made by : Jean-Damien POGOLOTTI
  6. Last Update : 19/01/2014
  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("NODE_TYPE_FREE" , 690001);
  12. define("NODE_TYPE_CENTRAL" , 690002);
  13. define("NODE_SHAPE_CIRCLE" , 690011);
  14. define("NODE_SHAPE_TRIANGLE" , 690012);
  15. define("NODE_SHAPE_SQUARE" , 690013);
  16. define("ALGORITHM_RANDOM" , 690021);
  17. define("ALGORITHM_WEIGHTED" , 690022);
  18. define("ALGORITHM_CIRCULAR" , 690023);
  19. define("ALGORITHM_CENTRAL" , 690024);
  20. define("LABEL_CLASSIC" , 690031);
  21. define("LABEL_LIGHT" , 690032);
  22. /* pSpring class definition */
  23. class pSpring
  24. {
  25. var $History;
  26. var $pChartObject;
  27. var $Data;
  28. var $Links;
  29. var $X1;
  30. var $Y1;
  31. var $X2;
  32. var $Y2;
  33. var $AutoComputeFreeZone;
  34. var $Labels;
  35. /* Class creator */
  36. function pSpring()
  37. {
  38. /* Initialise data arrays */
  39. $this->Data = "";
  40. $this->Links = "";
  41. /* Set nodes defaults */
  42. $this->Default["R"] = 255;
  43. $this->Default["G"] = 255;
  44. $this->Default["B"] = 255;
  45. $this->Default["Alpha"] = 100;
  46. $this->Default["BorderR"] = 0;
  47. $this->Default["BorderG"] = 0;
  48. $this->Default["BorderB"] = 0;
  49. $this->Default["BorderAlpha"] = 100;
  50. $this->Default["Surrounding"] = NULL;
  51. $this->Default["BackgroundR"] = 255;
  52. $this->Default["BackgroundG"] = 255;
  53. $this->Default["BackgroundB"] = 255;
  54. $this->Default["BackgroundAlpha"] = 0;
  55. $this->Default["Force"] = 1;
  56. $this->Default["NodeType"] = NODE_TYPE_FREE;
  57. $this->Default["Size"] = 5;
  58. $this->Default["Shape"] = NODE_SHAPE_CIRCLE;
  59. $this->Default["FreeZone"] = 40;
  60. $this->Default["LinkR"] = 0;
  61. $this->Default["LinkG"] = 0;
  62. $this->Default["LinkB"] = 0;
  63. $this->Default["LinkAlpha"] = 0;
  64. $this->Labels["Type"] = LABEL_CLASSIC;
  65. $this->Labels["R"] = 0;
  66. $this->Labels["G"] = 0;
  67. $this->Labels["B"] = 0;
  68. $this->Labels["Alpha"] = 100;
  69. $this->AutoComputeFreeZone = FALSE;
  70. }
  71. /* Set default links options */
  72. function setLinkDefaults($Settings="")
  73. {
  74. if ( isset($Settings["R"]) ) { $this->Default["LinkR"] = $Settings["R"]; }
  75. if ( isset($Settings["G"]) ) { $this->Default["LinkG"] = $Settings["G"]; }
  76. if ( isset($Settings["B"]) ) { $this->Default["LinkB"] = $Settings["B"]; }
  77. if ( isset($Settings["Alpha"]) ) { $this->Default["LinkAlpha"] = $Settings["Alpha"]; }
  78. }
  79. /* Set default links options */
  80. function setLabelsSettings($Settings="")
  81. {
  82. if ( isset($Settings["Type"]) ) { $this->Labels["Type"] = $Settings["Type"]; }
  83. if ( isset($Settings["R"]) ) { $this->Labels["R"] = $Settings["R"]; }
  84. if ( isset($Settings["G"]) ) { $this->Labels["G"] = $Settings["G"]; }
  85. if ( isset($Settings["B"]) ) { $this->Labels["B"] = $Settings["B"]; }
  86. if ( isset($Settings["Alpha"]) ) { $this->Labels["Alpha"] = $Settings["Alpha"]; }
  87. }
  88. /* Auto compute the FreeZone size based on the number of connections */
  89. function autoFreeZone()
  90. {
  91. /* Check connections reciprocity */
  92. foreach($this->Data as $Key => $Settings)
  93. {
  94. if ( isset($Settings["Connections"]) )
  95. { $this->Data[$Key]["FreeZone"] = count($Settings["Connections"])*10 + 20; }
  96. else
  97. { $this->Data[$Key]["FreeZone"] = 20; }
  98. }
  99. }
  100. /* Set link properties */
  101. function linkProperties($FromNode,$ToNode,$Settings)
  102. {
  103. if ( !isset($this->Data[$FromNode]) ) { return(0); }
  104. if ( !isset($this->Data[$ToNode]) ) { return(0); }
  105. $R = isset($Settings["R"]) ? $Settings["R"] : 0;
  106. $G = isset($Settings["G"]) ? $Settings["G"] : 0;
  107. $B = isset($Settings["B"]) ? $Settings["B"] : 0;
  108. $Alpha = isset($Settings["Alpha"]) ? $Settings["Alpha"] : 100;
  109. $Name = isset($Settings["Name"]) ? $Settings["Name"] : NULL;
  110. $Ticks = isset($Settings["Ticks"]) ? $Settings["Ticks"] : NULL;
  111. $this->Links[$FromNode][$ToNode]["R"] = $R; $this->Links[$ToNode][$FromNode]["R"] = $R;
  112. $this->Links[$FromNode][$ToNode]["G"] = $G; $this->Links[$ToNode][$FromNode]["G"] = $G;
  113. $this->Links[$FromNode][$ToNode]["B"] = $B; $this->Links[$ToNode][$FromNode]["B"] = $B;
  114. $this->Links[$FromNode][$ToNode]["Alpha"] = $Alpha; $this->Links[$ToNode][$FromNode]["Alpha"] = $Alpha;
  115. $this->Links[$FromNode][$ToNode]["Name"] = $Name; $this->Links[$ToNode][$FromNode]["Name"] = $Name;
  116. $this->Links[$FromNode][$ToNode]["Ticks"] = $Ticks; $this->Links[$ToNode][$FromNode]["Ticks"] = $Ticks;
  117. }
  118. function setNodeDefaults($Settings="")
  119. {
  120. if ( isset($Settings["R"]) ) { $this->Default["R"] = $Settings["R"]; }
  121. if ( isset($Settings["G"]) ) { $this->Default["G"] = $Settings["G"]; }
  122. if ( isset($Settings["B"]) ) { $this->Default["B"] = $Settings["B"]; }
  123. if ( isset($Settings["Alpha"]) ) { $this->Default["Alpha"] = $Settings["Alpha"]; }
  124. if ( isset($Settings["BorderR"]) ) { $this->Default["BorderR"] = $Settings["BorderR"]; }
  125. if ( isset($Settings["BorderG"]) ) { $this->Default["BorderG"] = $Settings["BorderG"]; }
  126. if ( isset($Settings["BorderB"]) ) { $this->Default["BorderB"] = $Settings["BorderB"]; }
  127. if ( isset($Settings["BorderAlpha"]) ) { $this->Default["BorderAlpha"] = $Settings["BorderAlpha"]; }
  128. if ( isset($Settings["Surrounding"]) ) { $this->Default["Surrounding"] = $Settings["Surrounding"]; }
  129. if ( isset($Settings["BackgroundR"]) ) { $this->Default["BackgroundR"] = $Settings["BackgroundR"]; }
  130. if ( isset($Settings["BackgroundG"]) ) { $this->Default["BackgroundG"] = $Settings["BackgroundG"]; }
  131. if ( isset($Settings["BackgroundB"]) ) { $this->Default["BackgroundB"] = $Settings["BackgroundB"]; }
  132. if ( isset($Settings["BackgroundAlpha"]) ) { $this->Default["BackgroundAlpha"] = $Settings["BackgroundAlpha"]; }
  133. if ( isset($Settings["NodeType"]) ) { $this->Default["NodeType"] = $Settings["NodeType"]; }
  134. if ( isset($Settings["Size"]) ) { $this->Default["Size"] = $Settings["Size"]; }
  135. if ( isset($Settings["Shape"]) ) { $this->Default["Shape"] = $Settings["Shape"]; }
  136. if ( isset($Settings["FreeZone"]) ) { $this->Default["FreeZone"] = $Settings["FreeZone"]; }
  137. }
  138. /* Add a node */
  139. function addNode($NodeID,$Settings="")
  140. {
  141. /* if the node already exists, ignore */
  142. if (isset($this->Data[$NodeID])) { return(0); }
  143. $Name = isset($Settings["Name"]) ? $Settings["Name"] : "Node ".$NodeID;
  144. $Connections = isset($Settings["Connections"]) ? $Settings["Connections"] : NULL;
  145. $R = isset($Settings["R"]) ? $Settings["R"] : $this->Default["R"];
  146. $G = isset($Settings["G"]) ? $Settings["G"] : $this->Default["G"];
  147. $B = isset($Settings["B"]) ? $Settings["B"] : $this->Default["B"];
  148. $Alpha = isset($Settings["Alpha"]) ? $Settings["Alpha"] : $this->Default["Alpha"];
  149. $BorderR = isset($Settings["BorderR"]) ? $Settings["BorderR"] : $this->Default["BorderR"];
  150. $BorderG = isset($Settings["BorderG"]) ? $Settings["BorderG"] : $this->Default["BorderG"];
  151. $BorderB = isset($Settings["BorderB"]) ? $Settings["BorderB"] : $this->Default["BorderB"];
  152. $BorderAlpha = isset($Settings["BorderAlpha"]) ? $Settings["BorderAlpha"] : $this->Default["BorderAlpha"];
  153. $Surrounding = isset($Settings["Surrounding"]) ? $Settings["Surrounding"] : $this->Default["Surrounding"];
  154. $BackgroundR = isset($Settings["BackgroundR"]) ? $Settings["BackgroundR"] : $this->Default["BackgroundR"];
  155. $BackgroundG = isset($Settings["BackgroundG"]) ? $Settings["BackgroundG"] : $this->Default["BackgroundG"];
  156. $BackgroundB = isset($Settings["BackgroundB"]) ? $Settings["BackgroundB"] : $this->Default["BackgroundB"];
  157. $BackgroundAlpha = isset($Settings["BackgroundAlpha"]) ? $Settings["BackgroundAlpha"] : $this->Default["BackgroundAlpha"];
  158. $Force = isset($Settings["Force"]) ? $Settings["Force"] : $this->Default["Force"];
  159. $NodeType = isset($Settings["NodeType"]) ? $Settings["NodeType"] : $this->Default["NodeType"];
  160. $Size = isset($Settings["Size"]) ? $Settings["Size"] : $this->Default["Size"];
  161. $Shape = isset($Settings["Shape"]) ? $Settings["Shape"] : $this->Default["Shape"];
  162. $FreeZone = isset($Settings["FreeZone"]) ? $Settings["FreeZone"] : $this->Default["FreeZone"];
  163. if ( $Surrounding != NULL ) { $BorderR = $R + $Surrounding; $BorderG = $G + $Surrounding; $BorderB = $B + $Surrounding; }
  164. $this->Data[$NodeID]["R"] = $R; $this->Data[$NodeID]["G"] = $G; $this->Data[$NodeID]["B"] = $B; $this->Data[$NodeID]["Alpha"] = $Alpha;
  165. $this->Data[$NodeID]["BorderR"] = $BorderR; $this->Data[$NodeID]["BorderG"] = $BorderG; $this->Data[$NodeID]["BorderB"] = $BorderB; $this->Data[$NodeID]["BorderAlpha"] = $BorderAlpha;
  166. $this->Data[$NodeID]["BackgroundR"] = $BackgroundR; $this->Data[$NodeID]["BackgroundG"] = $BackgroundG; $this->Data[$NodeID]["BackgroundB"] = $BackgroundB; $this->Data[$NodeID]["BackgroundAlpha"] = $BackgroundAlpha;
  167. $this->Data[$NodeID]["Name"] = $Name;
  168. $this->Data[$NodeID]["Force"] = $Force;
  169. $this->Data[$NodeID]["Type"] = $NodeType;
  170. $this->Data[$NodeID]["Size"] = $Size;
  171. $this->Data[$NodeID]["Shape"] = $Shape;
  172. $this->Data[$NodeID]["FreeZone"] = $FreeZone;
  173. if ( $Connections != NULL )
  174. {
  175. if ( is_array($Connections ) )
  176. {
  177. foreach($Connections as $Key => $Value)
  178. $this->Data[$NodeID]["Connections"][] = $Value;
  179. }
  180. else
  181. $this->Data[$NodeID]["Connections"][] = $Connections;
  182. }
  183. }
  184. /* Set color attribute for a list of nodes */
  185. function setNodesColor($Nodes,$Settings="")
  186. {
  187. if ( is_array($Nodes) )
  188. {
  189. foreach ($Nodes as $Key => $NodeID)
  190. {
  191. if (isset($this->Data[$NodeID]) )
  192. {
  193. if ( isset($Settings["R"]) ) { $this->Data[$NodeID]["R"] = $Settings["R"]; }
  194. if ( isset($Settings["G"]) ) { $this->Data[$NodeID]["G"] = $Settings["G"]; }
  195. if ( isset($Settings["B"]) ) { $this->Data[$NodeID]["B"] = $Settings["B"]; }
  196. if ( isset($Settings["Alpha"]) ) { $this->Data[$NodeID]["Alpha"] = $Settings["Alpha"]; }
  197. if ( isset($Settings["BorderR"]) ) { $this->Data[$NodeID]["BorderR"] = $Settings["BorderR"]; }
  198. if ( isset($Settings["BorderG"]) ) { $this->Data[$NodeID]["BorderG"] = $Settings["BorderG"]; }
  199. if ( isset($Settings["BorderB"]) ) { $this->Data[$NodeID]["BorderB"] = $Settings["BorderB"]; }
  200. if ( isset($Settings["BorderAlpha"]) ) { $this->Data[$NodeID]["BorderAlpha"] = $Settings["BorderAlpha"]; }
  201. if ( isset($Settings["Surrounding"]) ) { $this->Data[$NodeID]["BorderR"] = $this->Data[$NodeID]["R"] + $Settings["Surrounding"]; $this->Data[$NodeID]["BorderG"] = $this->Data[$NodeID]["G"] + $Settings["Surrounding"]; $this->Data[$NodeID]["BorderB"] = $this->Data[$NodeID]["B"] + $Settings["Surrounding"]; }
  202. }
  203. }
  204. }
  205. else
  206. {
  207. if ( isset($Settings["R"]) ) { $this->Data[$Nodes]["R"] = $Settings["R"]; }
  208. if ( isset($Settings["G"]) ) { $this->Data[$Nodes]["G"] = $Settings["G"]; }
  209. if ( isset($Settings["B"]) ) { $this->Data[$Nodes]["B"] = $Settings["B"]; }
  210. if ( isset($Settings["Alpha"]) ) { $this->Data[$Nodes]["Alpha"] = $Settings["Alpha"]; }
  211. if ( isset($Settings["BorderR"]) ) { $this->Data[$Nodes]["BorderR"] = $Settings["BorderR"]; }
  212. if ( isset($Settings["BorderG"]) ) { $this->Data[$Nodes]["BorderG"] = $Settings["BorderG"]; }
  213. if ( isset($Settings["BorderB"]) ) { $this->Data[$Nodes]["BorderB"] = $Settings["BorderB"]; }
  214. if ( isset($Settings["BorderAlpha"]) ) { $this->Data[$Nodes]["BorderAlpha"] = $Settings["BorderAlpha"]; }
  215. if ( isset($Settings["Surrounding"]) ) { $this->Data[$Nodes]["BorderR"] = $this->Data[$NodeID]["R"] + $Settings["Surrounding"]; $this->Data[$NodeID]["BorderG"] = $this->Data[$NodeID]["G"] + $Settings["Surrounding"]; $this->Data[$NodeID]["BorderB"] = $this->Data[$NodeID]["B"] + $Settings["Surrounding"]; }
  216. }
  217. }
  218. /* Returns all the nodes details */
  219. function dumpNodes()
  220. { return($this->Data); }
  221. /* Check if a connection exists and create it if required */
  222. function checkConnection($SourceID, $TargetID)
  223. {
  224. if ( isset($this->Data[$SourceID]["Connections"]) )
  225. {
  226. foreach ($this->Data[$SourceID]["Connections"] as $Key => $ConnectionID)
  227. { if ( $TargetID == $ConnectionID ) { return(TRUE); } }
  228. }
  229. $this->Data[$SourceID]["Connections"][] = $TargetID;
  230. }
  231. /* Get the median linked nodes position */
  232. function getMedianOffset($Key,$X,$Y)
  233. {
  234. $Cpt = 1;
  235. if ( isset($this->Data[$Key]["Connections"]) )
  236. {
  237. foreach($this->Data[$Key]["Connections"] as $ID => $NodeID)
  238. {
  239. if ( isset($this->Data[$NodeID]["X"]) && isset($this->Data[$NodeID]["Y"]) )
  240. {
  241. $X = $X + $this->Data[$NodeID]["X"];
  242. $Y = $Y + $this->Data[$NodeID]["Y"];
  243. $Cpt++;
  244. }
  245. }
  246. }
  247. return(array("X"=>$X/$Cpt,"Y"=>$Y/$Cpt));
  248. }
  249. /* Return the ID of the attached partner with the biggest weight */
  250. function getBiggestPartner($Key)
  251. {
  252. if ( !isset($this->Data[$Key]["Connections"]) ) { return(""); }
  253. $MaxWeight = 0; $Result = "";
  254. foreach($this->Data[$Key]["Connections"] as $Key => $PeerID)
  255. {
  256. if ( $this->Data[$PeerID]["Weight"] > $MaxWeight )
  257. { $MaxWeight = $this->Data[$PeerID]["Weight"]; $Result = $PeerID; }
  258. }
  259. return($Result);
  260. }
  261. /* Do the initial node positions computing pass */
  262. function firstPass($Algorithm)
  263. {
  264. $CenterX = ($this->X2 - $this->X1) / 2 + $this->X1;
  265. $CenterY = ($this->Y2 - $this->Y1) / 2 + $this->Y1;
  266. /* Check connections reciprocity */
  267. foreach($this->Data as $Key => $Settings)
  268. {
  269. if ( isset($Settings["Connections"]) )
  270. {
  271. foreach($Settings["Connections"] as $ID => $ConnectionID)
  272. $this->checkConnection($ConnectionID,$Key);
  273. }
  274. }
  275. if ( $this->AutoComputeFreeZone ) { $this->autoFreeZone(); }
  276. /* Get the max number of connections */
  277. $MaxConnections = 0;
  278. foreach($this->Data as $Key => $Settings)
  279. { if ( isset($Settings["Connections"]) ) { if ( $MaxConnections < count($Settings["Connections"] ) ) { $MaxConnections = count($Settings["Connections"]); } } }
  280. if ( $Algorithm == ALGORITHM_WEIGHTED )
  281. {
  282. foreach($this->Data as $Key => $Settings)
  283. {
  284. if ( $Settings["Type"] == NODE_TYPE_CENTRAL ) { $this->Data[$Key]["X"] = $CenterX; $this->Data[$Key]["Y"] = $CenterY; }
  285. if ( $Settings["Type"] == NODE_TYPE_FREE )
  286. {
  287. if ( isset($Settings["Connections"]) )
  288. { $Connections = count($Settings["Connections"]); }
  289. else
  290. { $Connections = 0; }
  291. $Ring = $MaxConnections - $Connections;
  292. $Angle = rand(0,360);
  293. $this->Data[$Key]["X"] = cos(deg2rad($Angle)) * ($Ring*$this->RingSize) + $CenterX;
  294. $this->Data[$Key]["Y"] = sin(deg2rad($Angle)) * ($Ring*$this->RingSize) + $CenterY;
  295. }
  296. }
  297. }
  298. elseif ( $Algorithm == ALGORITHM_CENTRAL )
  299. {
  300. /* Put a weight on each nodes */
  301. foreach($this->Data as $Key => $Settings)
  302. {
  303. if ( isset($Settings["Connections"]) )
  304. $this->Data[$Key]["Weight"] = count($Settings["Connections"]);
  305. else
  306. $this->Data[$Key]["Weight"] = 0;
  307. }
  308. $MaxConnections = $MaxConnections + 1;
  309. for($i=$MaxConnections;$i>=0;$i--)
  310. {
  311. foreach($this->Data as $Key => $Settings)
  312. {
  313. if ( $Settings["Type"] == NODE_TYPE_CENTRAL ) { $this->Data[$Key]["X"] = $CenterX; $this->Data[$Key]["Y"] = $CenterY; }
  314. if ( $Settings["Type"] == NODE_TYPE_FREE )
  315. {
  316. if ( isset($Settings["Connections"]) )
  317. { $Connections = count($Settings["Connections"]); }
  318. else
  319. { $Connections = 0; }
  320. if ( $Connections == $i )
  321. {
  322. $BiggestPartner = $this->getBiggestPartner($Key);
  323. if ( $BiggestPartner != "" )
  324. {
  325. $Ring = $this->Data[$BiggestPartner]["FreeZone"];
  326. $Weight = $this->Data[$BiggestPartner]["Weight"];
  327. $AngleDivision = 360 / $this->Data[$BiggestPartner]["Weight"];
  328. $Done = FALSE; $Tries = 0;
  329. while (!$Done && $Tries <= $Weight*2)
  330. {
  331. $Tries++;
  332. $Angle = floor(rand(0,$Weight)*$AngleDivision);
  333. if ( !isset($this->Data[$BiggestPartner]["Angular"][$Angle]) || !isset($this->Data[$BiggestPartner]["Angular"]) )
  334. {
  335. $this->Data[$BiggestPartner]["Angular"][$Angle] = $Angle;
  336. $Done = TRUE;
  337. }
  338. }
  339. if ( !$Done )
  340. { $Angle = rand(0,360); $this->Data[$BiggestPartner]["Angular"][$Angle] = $Angle; }
  341. $X = cos(deg2rad($Angle)) * ($Ring) + $this->Data[$BiggestPartner]["X"];
  342. $Y = sin(deg2rad($Angle)) * ($Ring) + $this->Data[$BiggestPartner]["Y"];
  343. $this->Data[$Key]["X"] = $X;
  344. $this->Data[$Key]["Y"] = $Y;
  345. }
  346. }
  347. }
  348. }
  349. }
  350. }
  351. elseif ( $Algorithm == ALGORITHM_CIRCULAR )
  352. {
  353. $MaxConnections = $MaxConnections + 1;
  354. for($i=$MaxConnections;$i>=0;$i--)
  355. {
  356. foreach($this->Data as $Key => $Settings)
  357. {
  358. if ( $Settings["Type"] == NODE_TYPE_CENTRAL ) { $this->Data[$Key]["X"] = $CenterX; $this->Data[$Key]["Y"] = $CenterY; }
  359. if ( $Settings["Type"] == NODE_TYPE_FREE )
  360. {
  361. if ( isset($Settings["Connections"]) )
  362. { $Connections = count($Settings["Connections"]); }
  363. else
  364. { $Connections = 0; }
  365. if ( $Connections == $i )
  366. {
  367. $Ring = $MaxConnections - $Connections;
  368. $Angle = rand(0,360);
  369. $X = cos(deg2rad($Angle)) * ($Ring*$this->RingSize) + $CenterX;
  370. $Y = sin(deg2rad($Angle)) * ($Ring*$this->RingSize) + $CenterY;
  371. $MedianOffset = $this->getMedianOffset($Key,$X,$Y);
  372. $this->Data[$Key]["X"] = $MedianOffset["X"];
  373. $this->Data[$Key]["Y"] = $MedianOffset["Y"];
  374. }
  375. }
  376. }
  377. }
  378. }
  379. elseif ( $Algorithm == ALGORITHM_RANDOM )
  380. {
  381. foreach($this->Data as $Key => $Settings)
  382. {
  383. if ( $Settings["Type"] == NODE_TYPE_FREE )
  384. {
  385. $this->Data[$Key]["X"] = $CenterX + rand(-20,20);
  386. $this->Data[$Key]["Y"] = $CenterY + rand(-20,20);
  387. }
  388. if ( $Settings["Type"] == NODE_TYPE_CENTRAL ) { $this->Data[$Key]["X"] = $CenterX; $this->Data[$Key]["Y"] = $CenterY; }
  389. }
  390. }
  391. }
  392. /* Compute one pass */
  393. function doPass()
  394. {
  395. /* Compute vectors */
  396. foreach($this->Data as $Key => $Settings)
  397. {
  398. if ( $Settings["Type"] != NODE_TYPE_CENTRAL )
  399. {
  400. unset($this->Data[$Key]["Vectors"]);
  401. $X1 = $Settings["X"];
  402. $Y1 = $Settings["Y"];
  403. /* Repulsion vectors */
  404. foreach($this->Data as $Key2 => $Settings2)
  405. {
  406. if ( $Key != $Key2 )
  407. {
  408. $X2 = $this->Data[$Key2]["X"];
  409. $Y2 = $this->Data[$Key2]["Y"];
  410. $FreeZone = $this->Data[$Key2]["FreeZone"];
  411. $Distance = $this->getDistance($X1,$Y1,$X2,$Y2);
  412. $Angle = $this->getAngle($X1,$Y1,$X2,$Y2) + 180;
  413. /* Nodes too close, repulsion occurs */
  414. if ( $Distance < $FreeZone )
  415. {
  416. $Force = log(pow(2,$FreeZone-$Distance));
  417. if ( $Force > 1 )
  418. { $this->Data[$Key]["Vectors"][] = array("Type"=>"R","Angle"=>$Angle % 360,"Force"=>$Force); }
  419. }
  420. }
  421. }
  422. /* Attraction vectors */
  423. if ( isset($Settings["Connections"]) )
  424. {
  425. foreach($Settings["Connections"] as $ID => $NodeID)
  426. {
  427. if ( isset($this->Data[$NodeID]) )
  428. {
  429. $X2 = $this->Data[$NodeID]["X"];
  430. $Y2 = $this->Data[$NodeID]["Y"];
  431. $FreeZone = $this->Data[$Key2]["FreeZone"];
  432. $Distance = $this->getDistance($X1,$Y1,$X2,$Y2);
  433. $Angle = $this->getAngle($X1,$Y1,$X2,$Y2);
  434. if ( $Distance > $FreeZone )
  435. $Force = log(($Distance-$FreeZone)+1);
  436. else
  437. { $Force = log(($FreeZone-$Distance)+1); ($Angle = $Angle + 180); }
  438. if ( $Force > 1 )
  439. $this->Data[$Key]["Vectors"][] = array("Type"=>"A","Angle"=>$Angle % 360,"Force"=>$Force);
  440. }
  441. }
  442. }
  443. }
  444. }
  445. /* Move the nodes accoding to the vectors */
  446. foreach($this->Data as $Key => $Settings)
  447. {
  448. $X = $Settings["X"];
  449. $Y = $Settings["Y"];
  450. if ( isset($Settings["Vectors"]) && $Settings["Type"] != NODE_TYPE_CENTRAL )
  451. {
  452. foreach($Settings["Vectors"] as $ID => $Vector)
  453. {
  454. $Type = $Vector["Type"];
  455. $Force = $Vector["Force"];
  456. $Angle = $Vector["Angle"];
  457. $Factor = $Type == "A" ? $this->MagneticForceA : $this->MagneticForceR;
  458. $X = cos(deg2rad($Angle)) * $Force * $Factor + $X;
  459. $Y = sin(deg2rad($Angle)) * $Force * $Factor + $Y;
  460. }
  461. }
  462. $this->Data[$Key]["X"] = $X;
  463. $this->Data[$Key]["Y"] = $Y;
  464. }
  465. }
  466. function lastPass()
  467. {
  468. /* Put everything inside the graph area */
  469. foreach($this->Data as $Key => $Settings)
  470. {
  471. $X = $Settings["X"];
  472. $Y = $Settings["Y"];
  473. if ( $X < $this->X1 ) { $X = $this->X1; }
  474. if ( $X > $this->X2 ) { $X = $this->X2; }
  475. if ( $Y < $this->Y1 ) { $Y = $this->Y1; }
  476. if ( $Y > $this->Y2 ) { $Y = $this->Y2; }
  477. $this->Data[$Key]["X"] = $X;
  478. $this->Data[$Key]["Y"] = $Y;
  479. }
  480. /* Dump all links */
  481. $Links = "";
  482. foreach($this->Data as $Key => $Settings)
  483. {
  484. $X1 = $Settings["X"];
  485. $Y1 = $Settings["Y"];
  486. if ( isset($Settings["Connections"]) )
  487. {
  488. foreach ($Settings["Connections"] as $ID => $NodeID)
  489. {
  490. if ( isset($this->Data[$NodeID]) )
  491. {
  492. $X2 = $this->Data[$NodeID]["X"];
  493. $Y2 = $this->Data[$NodeID]["Y"];
  494. $Links[] = array("X1"=>$X1,"Y1"=>$Y1,"X2"=>$X2,"Y2"=>$Y2,"Source"=>$Settings["Name"],"Destination"=>$this->Data[$NodeID]["Name"]);
  495. }
  496. }
  497. }
  498. }
  499. /* Check collisions */
  500. $Conflicts = 0;
  501. foreach($this->Data as $Key => $Settings)
  502. {
  503. $X1 = $Settings["X"];
  504. $Y1 = $Settings["Y"];
  505. if ( isset($Settings["Connections"]) )
  506. {
  507. foreach ($Settings["Connections"] as $ID => $NodeID)
  508. {
  509. if ( isset($this->Data[$NodeID]) )
  510. {
  511. $X2 = $this->Data[$NodeID]["X"];
  512. $Y2 = $this->Data[$NodeID]["Y"];
  513. foreach($Links as $IDLinks => $Link)
  514. {
  515. $X3 = $Link["X1"]; $Y3 = $Link["Y1"]; $X4 = $Link["X2"]; $Y4 = $Link["Y2"];
  516. if ( !($X1 == $X3 && $X2 == $X4 && $Y1 == $Y3 && $Y2 == $Y4 ) )
  517. {
  518. if ( $this->intersect($X1,$Y1,$X2,$Y2,$X3,$Y3,$X4,$Y4) )
  519. {
  520. if ( $Link["Source"] != $Settings["Name"] && $Link["Source"] != $this->Data[$NodeID]["Name"] && $Link["Destination"] != $Settings["Name"] && $Link["Destination"] != $this->Data[$NodeID]["Name"] )
  521. { $Conflicts++; }
  522. }
  523. }
  524. }
  525. }
  526. }
  527. }
  528. }
  529. return($Conflicts/2);
  530. }
  531. /* Center the graph */
  532. function center()
  533. {
  534. /* Determine the real center */
  535. $TargetCenterX = ($this->X2 - $this->X1) / 2 + $this->X1;
  536. $TargetCenterY = ($this->Y2 - $this->Y1) / 2 + $this->Y1;
  537. /* Get current boundaries */
  538. $XMin = $this->X2; $XMax = $this->X1;
  539. $YMin = $this->Y2; $YMax = $this->Y1;
  540. foreach($this->Data as $Key => $Settings)
  541. {
  542. $X = $Settings["X"];
  543. $Y = $Settings["Y"];
  544. if ( $X < $XMin) { $XMin = $X; }
  545. if ( $X > $XMax) { $XMax = $X; }
  546. if ( $Y < $YMin) { $YMin = $Y; }
  547. if ( $Y > $YMax) { $YMax = $Y; }
  548. }
  549. $CurrentCenterX = ($XMax - $XMin) / 2 + $XMin;
  550. $CurrentCenterY = ($YMax - $YMin) / 2 + $YMin;
  551. /* Compute the offset to apply */
  552. $XOffset = $TargetCenterX - $CurrentCenterX;
  553. $YOffset = $TargetCenterY - $CurrentCenterY;
  554. /* Correct the points position */
  555. foreach($this->Data as $Key => $Settings)
  556. {
  557. $this->Data[$Key]["X"] = $Settings["X"] + $XOffset;
  558. $this->Data[$Key]["Y"] = $Settings["Y"] + $YOffset;
  559. }
  560. }
  561. /* Create the encoded string */
  562. function drawSpring($Object,$Settings="")
  563. {
  564. $this->pChartObject = $Object;
  565. $Pass = isset($Settings["Pass"]) ? $Settings["Pass"] : 50;
  566. $Retries = isset($Settings["Retry"]) ? $Settings["Retry"] : 10;
  567. $this->MagneticForceA = isset($Settings["MagneticForceA"]) ? $Settings["MagneticForceA"] : 1.5;
  568. $this->MagneticForceR = isset($Settings["MagneticForceR"]) ? $Settings["MagneticForceR"] : 2;
  569. $this->RingSize = isset($Settings["RingSize"]) ? $Settings["RingSize"] : 40;
  570. $DrawVectors = isset($Settings["DrawVectors"]) ? $Settings["DrawVectors"] : FALSE;
  571. $DrawQuietZone = isset($Settings["DrawQuietZone"]) ? $Settings["DrawQuietZone"] : FALSE;
  572. $CenterGraph = isset($Settings["CenterGraph"]) ? $Settings["CenterGraph"] : TRUE;
  573. $TextPadding = isset($Settings["TextPadding"]) ? $Settings["TextPadding"] : 4;
  574. $Algorithm = isset($Settings["Algorithm"]) ? $Settings["Algorithm"] : ALGORITHM_WEIGHTED;
  575. $FontSize = $Object->FontSize;
  576. $this->X1 = $Object->GraphAreaX1;
  577. $this->Y1 = $Object->GraphAreaY1;
  578. $this->X2 = $Object->GraphAreaX2;
  579. $this->Y2 = $Object->GraphAreaY2;
  580. $Conflicts = 1; $Jobs = 0; $this->History["MinimumConflicts"] = -1;
  581. while ($Conflicts != 0 && $Jobs < $Retries )
  582. {
  583. $Jobs++;
  584. /* Compute the initial settings */
  585. $this->firstPass($Algorithm);
  586. /* Apply the vectors */
  587. if ( $Pass > 0 )
  588. {
  589. for ($i=0; $i<=$Pass; $i++) { $this->doPass(); }
  590. }
  591. $Conflicts = $this->lastPass();
  592. if ( $this->History["MinimumConflicts"] == -1 || $Conflicts < $this->History["MinimumConflicts"] )
  593. { $this->History["MinimumConflicts"] = $Conflicts; $this->History["Result"] = $this->Data; }
  594. }
  595. $Conflicts = $this->History["MinimumConflicts"];
  596. $this->Data = $this->History["Result"];
  597. if ( $CenterGraph ) { $this->center(); }
  598. /* Draw the connections */
  599. $Drawn = "";
  600. foreach($this->Data as $Key => $Settings)
  601. {
  602. $X = $Settings["X"];
  603. $Y = $Settings["Y"];
  604. if ( isset($Settings["Connections"]) )
  605. {
  606. foreach ($Settings["Connections"] as $ID => $NodeID)
  607. {
  608. if ( !isset($Drawn[$Key]) ) { $Drawn[$Key] = ""; }
  609. if ( !isset($Drawn[$NodeID]) ) { $Drawn[$NodeID] = ""; }
  610. if ( isset($this->Data[$NodeID]) && !isset($Drawn[$Key][$NodeID]) && !isset($Drawn[$NodeID][$Key]) )
  611. {
  612. $Color = array("R"=>$this->Default["LinkR"],"G"=>$this->Default["LinkG"],"B"=>$this->Default["LinkB"],"Alpha"=>$this->Default["Alpha"]);
  613. if ( $this->Links != "" )
  614. {
  615. if ( isset($this->Links[$Key][$NodeID]["R"]) )
  616. { $Color = array("R"=>$this->Links[$Key][$NodeID]["R"],"G"=>$this->Links[$Key][$NodeID]["G"],"B"=>$this->Links[$Key][$NodeID]["B"],"Alpha"=>$this->Links[$Key][$NodeID]["Alpha"]); }
  617. if ( isset($this->Links[$Key][$NodeID]["Ticks"]) )
  618. { $Color["Ticks"] = $this->Links[$Key][$NodeID]["Ticks"]; }
  619. }
  620. $X2 = $this->Data[$NodeID]["X"];
  621. $Y2 = $this->Data[$NodeID]["Y"];
  622. $this->pChartObject->drawLine($X,$Y,$X2,$Y2,$Color);
  623. $Drawn[$Key][$NodeID] = TRUE;
  624. if ( isset($this->Links) && $this->Links != "" )
  625. {
  626. if ( isset($this->Links[$Key][$NodeID]["Name"]) || isset($this->Links[$NodeID][$Key]["Name"]) )
  627. {
  628. $Name = isset($this->Links[$Key][$NodeID]["Name"]) ? $this->Links[$Key][$NodeID]["Name"] : $this->Links[$NodeID][$Key]["Name"];
  629. $TxtX = ($X2 - $X)/2 + $X;
  630. $TxtY = ($Y2 - $Y)/2 + $Y;
  631. if ( $X <= $X2 )
  632. $Angle = (360-$this->getAngle($X,$Y,$X2,$Y2)) % 360;
  633. else
  634. $Angle = (360-$this->getAngle($X2,$Y2,$X,$Y)) % 360;
  635. $Settings = $Color;
  636. $Settings["Angle"] = $Angle;
  637. $Settings["Align"] = TEXT_ALIGN_BOTTOMMIDDLE;
  638. $this->pChartObject->drawText($TxtX,$TxtY,$Name,$Settings);
  639. }
  640. }
  641. }
  642. }
  643. }
  644. }
  645. /* Draw the quiet zones */
  646. if ( $DrawQuietZone )
  647. {
  648. foreach($this->Data as $Key => $Settings)
  649. {
  650. $X = $Settings["X"];
  651. $Y = $Settings["Y"];
  652. $FreeZone = $Settings["FreeZone"];
  653. $this->pChartObject->drawFilledCircle($X,$Y,$FreeZone,array("R"=>0,"G"=>0,"B"=>0,"Alpha"=>2));
  654. }
  655. }
  656. /* Draw the nodes */
  657. foreach($this->Data as $Key => $Settings)
  658. {
  659. $X = $Settings["X"];
  660. $Y = $Settings["Y"];
  661. $Name = $Settings["Name"];
  662. $FreeZone = $Settings["FreeZone"];
  663. $Shape = $Settings["Shape"];
  664. $Size = $Settings["Size"];
  665. $Color = array("R"=>$Settings["R"],"G"=>$Settings["G"],"B"=>$Settings["B"],"Alpha"=>$Settings["Alpha"],"BorderR"=>$Settings["BorderR"],"BorderG"=>$Settings["BorderG"],"BorderB"=>$Settings["BorderB"],"BorderApha"=>$Settings["BorderAlpha"]);
  666. if ( $Shape == NODE_SHAPE_CIRCLE )
  667. {
  668. $this->pChartObject->drawFilledCircle($X,$Y,$Size,$Color);
  669. }
  670. elseif ( $Shape == NODE_SHAPE_TRIANGLE )
  671. {
  672. $Points = "";
  673. $Points[] = cos(deg2rad(270)) * $Size + $X; $Points[] = sin(deg2rad(270)) * $Size + $Y;
  674. $Points[] = cos(deg2rad(45)) * $Size + $X; $Points[] = sin(deg2rad(45)) * $Size + $Y;
  675. $Points[] = cos(deg2rad(135)) * $Size + $X; $Points[] = sin(deg2rad(135)) * $Size + $Y;
  676. $this->pChartObject->drawPolygon($Points,$Color);
  677. }
  678. elseif ( $Shape == NODE_SHAPE_SQUARE )
  679. {
  680. $Offset = $Size/2; $Size = $Size / 2;
  681. $this->pChartObject->drawFilledRectangle($X-$Offset,$Y-$Offset,$X+$Offset,$Y+$Offset,$Color);
  682. }
  683. if ( $Name != "" )
  684. {
  685. $LabelOptions = array("R"=>$this->Labels["R"],"G"=>$this->Labels["G"],"B"=>$this->Labels["B"],"Alpha"=>$this->Labels["Alpha"]);
  686. if ( $this->Labels["Type"] == LABEL_LIGHT )
  687. {
  688. $LabelOptions["Align"] = TEXT_ALIGN_BOTTOMLEFT;
  689. $this->pChartObject->drawText($X,$Y,$Name,$LabelOptions);
  690. }
  691. elseif ( $this->Labels["Type"] == LABEL_CLASSIC )
  692. {
  693. $LabelOptions["Align"] = TEXT_ALIGN_TOPMIDDLE;
  694. $LabelOptions["DrawBox"] = TRUE;
  695. $LabelOptions["BoxAlpha"] = 50;
  696. $LabelOptions["BorderOffset"] = 4;
  697. $LabelOptions["RoundedRadius"] = 3;
  698. $LabelOptions["BoxRounded"] = TRUE;
  699. $LabelOptions["NoShadow"] = TRUE;
  700. $this->pChartObject->drawText($X,$Y+$Size+$TextPadding,$Name,$LabelOptions);
  701. }
  702. }
  703. }
  704. /* Draw the vectors */
  705. if ( $DrawVectors )
  706. {
  707. foreach($this->Data as $Key => $Settings)
  708. {
  709. $X1 = $Settings["X"];
  710. $Y1 = $Settings["Y"];
  711. if ( isset($Settings["Vectors"]) && $Settings["Type"] != NODE_TYPE_CENTRAL )
  712. {
  713. foreach($Settings["Vectors"] as $ID => $Vector)
  714. {
  715. $Type = $Vector["Type"];
  716. $Force = $Vector["Force"];
  717. $Angle = $Vector["Angle"];
  718. $Factor = $Type == "A" ? $this->MagneticForceA : $this->MagneticForceR;
  719. $Color = $Type == "A" ? array("FillR"=>255,"FillG"=>0,"FillB"=>0) : array("FillR"=>0,"FillG"=>255,"FillB"=>0);
  720. $X2 = cos(deg2rad($Angle)) * $Force * $Factor + $X1;
  721. $Y2 = sin(deg2rad($Angle)) * $Force * $Factor + $Y1;
  722. $this->pChartObject->drawArrow($X1,$Y1,$X2,$Y2,$Color);
  723. }
  724. }
  725. }
  726. }
  727. return(array("Pass"=>$Jobs,"Conflicts"=>$Conflicts));
  728. }
  729. /* Return the distance between two points */
  730. function getDistance($X1,$Y1,$X2,$Y2)
  731. { return (sqrt(($X2-$X1)*($X2-$X1)+($Y2-$Y1)*($Y2-$Y1))); }
  732. /* Return the angle made by a line and the X axis */
  733. function getAngle($X1,$Y1,$X2,$Y2)
  734. {
  735. $Opposite = $Y2 - $Y1; $Adjacent = $X2 - $X1;$Angle = rad2deg(atan2($Opposite,$Adjacent));
  736. if ($Angle > 0) { return($Angle); } else { return(360-abs($Angle)); }
  737. }
  738. function intersect($X1,$Y1,$X2,$Y2,$X3,$Y3,$X4,$Y4)
  739. {
  740. $A = (($X3 * $Y4 - $X4 * $Y3) * ($X1 - $X2) - ($X1 * $Y2 - $X2 * $Y1) * ($X3 - $X4));
  741. $B = (($Y1 - $Y2) * ($X3 - $X4) - ($Y3 - $Y4) * ($X1 - $X2));
  742. if ( $B == 0 ) { return(FALSE); }
  743. $Xi = $A / $B;
  744. $C = ($X1 - $X2);
  745. if ( $C == 0 ) { return(FALSE); }
  746. $Yi = $Xi * (($Y1 - $Y2)/$C) + (($X1 * $Y2 - $X2 * $Y1)/$C);
  747. if ( $Xi >= min($X1,$X2) && $Xi >= min($X3,$X4) && $Xi <= max($X1,$X2) && $Xi <= max($X3,$X4))
  748. {
  749. if ( $Yi >= min($Y1,$Y2) && $Yi >= min($Y3,$Y4) && $Yi <= max($Y1,$Y2) && $Yi <= max($Y3,$Y4))
  750. { return(TRUE); }
  751. }
  752. return(FALSE);
  753. }
  754. }
  755. ?>