vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

395 lines
12 KiB

  1. /**
  2. * Created by Alex on 3/3/2015.
  3. */
  4. var util = require('../../util');
  5. class LayoutEngine {
  6. constructor(body) {
  7. this.body = body;
  8. this.options = {};
  9. this.defaultOptions = {
  10. hierarchical: {
  11. enabled:false,
  12. levelSeparation: 150,
  13. direction: "UD", // UD, DU, LR, RL
  14. sortMethod: "hubsize" // hubsize, directed
  15. }
  16. }
  17. util.extend(this.options, this.defaultOptions);
  18. this.hierarchicalLevels = {};
  19. this.body.emitter.on("_dataChanged", () => {
  20. this.setupHierarchicalLayout();
  21. })
  22. }
  23. setOptions(options, allOptions) {
  24. if (options !== undefined) {
  25. util.mergeOptions(this.options, options, 'hierarchical');
  26. if (this.options.hierarchical.enabled === true) {
  27. // make sure the level seperation is the right way up
  28. if (this.options.hierarchical.direction == "RL" || this.options.hierarchical.direction == "DU") {
  29. if (this.options.hierarchical.levelSeparation > 0) {
  30. this.options.hierarchical.levelSeparation *= -1;
  31. }
  32. }
  33. else {
  34. if (this.options.hierarchical.levelSeparation < 0) {
  35. this.options.hierarchical.levelSeparation *= -1;
  36. }
  37. }
  38. // because the hierarchical system needs it's own physics and smooth curve settings, we adapt the other options if needed.
  39. return this.adaptAllOptions(allOptions);
  40. }
  41. }
  42. return allOptions;
  43. }
  44. adaptAllOptions(allOptions) {
  45. if (this.options.hierarchical.enabled === true) {
  46. // set the physics
  47. if (allOptions.physics === undefined || allOptions.physics === true) {
  48. allOptions.physics = {solver: 'hierarchicalRepulsion'};
  49. }
  50. else if (options.physics !== false) {
  51. allOptions.physics['solver'] = 'hierarchicalRepulsion';
  52. }
  53. // get the type of static smooth curve in case it is required
  54. let type = 'horizontal';
  55. if (this.options.hierarchical.direction == "RL" || this.options.hierarchical.direction == "LR") {
  56. type = 'vertical';
  57. }
  58. // disable smooth curves if nothing is defined. If smooth curves have been turned on, turn them into static smooth curves.
  59. if (allOptions.edges === undefined) {
  60. allOptions.edges = {smooth: false};
  61. }
  62. else if (allOptions.edges.smooth === undefined) {
  63. allOptions.edges.smooth = false;
  64. }
  65. else {
  66. allOptions.edges.smooth = {enabled: true, dynamic: false, type:type}
  67. }
  68. // force all edges into static smooth curves.
  69. this.body.emitter.emit('_forceDisableDynamicCurves', type);
  70. }
  71. return allOptions;
  72. }
  73. positionInitially(nodesArray) {
  74. if (this.options.hierarchical.enabled !== true) {
  75. for (let i = 0; i < nodesArray.length; i++) {
  76. let node = nodesArray[i];
  77. if ((!node.isFixed()) && (node.x === undefined || node.y === undefined)) {
  78. let radius = 10 * 0.1 * nodesArray.length + 10;
  79. let angle = 2 * Math.PI * Math.random();
  80. if (node.options.fixed.x == false) {
  81. node.x = radius * Math.cos(angle);
  82. }
  83. if (node.options.fixed.x == false) {
  84. node.y = radius * Math.sin(angle);
  85. }
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. * This is the main function to layout the nodes in a hierarchical way.
  92. * It checks if the node details are supplied correctly
  93. *
  94. * @private
  95. */
  96. setupHierarchicalLayout() {
  97. if (this.options.hierarchical.enabled == true && this.body.nodeIndices.length > 0) {
  98. // get the size of the largest hubs and check if the user has defined a level for a node.
  99. let hubsize = 0;
  100. let node, nodeId;
  101. let definedLevel = false;
  102. let undefinedLevel = false;
  103. this.hierarchicalLevels = {};
  104. this.nodeSpacing = 100;
  105. for (nodeId in this.body.nodes) {
  106. if (this.body.nodes.hasOwnProperty(nodeId)) {
  107. node = this.body.nodes[nodeId];
  108. if (node.options.level !== undefined) {
  109. definedLevel = true;
  110. this.hierarchicalLevels[nodeId] = node.options.level;
  111. }
  112. else {
  113. undefinedLevel = true;
  114. }
  115. hubsize = hubsize < node.edges.length ? node.edges.length : hubsize;
  116. }
  117. }
  118. // if the user defined some levels but not all, alert and run without hierarchical layout
  119. if (undefinedLevel == true && definedLevel == true) {
  120. throw new Error("To use the hierarchical layout, nodes require either no predefined levels or levels have to be defined for all nodes.");
  121. return;
  122. }
  123. else {
  124. // setup the system to use hierarchical method.
  125. //this._changeConstants();
  126. // define levels if undefined by the users. Based on hubsize
  127. if (undefinedLevel == true) {
  128. if (this.options.hierarchical.sortMethod == "hubsize") {
  129. this._determineLevels(hubsize);
  130. }
  131. else if (this.options.hierarchical.sortMethod == "directed" || "direction") {
  132. this._determineLevelsDirected();
  133. }
  134. }
  135. // check the distribution of the nodes per level.
  136. let distribution = this._getDistribution();
  137. // place the nodes on the canvas.
  138. this._placeNodesByHierarchy(distribution);
  139. }
  140. }
  141. }
  142. /**
  143. * This function places the nodes on the canvas based on the hierarchial distribution.
  144. *
  145. * @param {Object} distribution | obtained by the function this._getDistribution()
  146. * @private
  147. */
  148. _placeNodesByHierarchy(distribution) {
  149. let nodeId, node;
  150. this.positionedNodes = {};
  151. // start placing all the level 0 nodes first. Then recursively position their branches.
  152. for (let level in distribution) {
  153. if (distribution.hasOwnProperty(level)) {
  154. for (nodeId in distribution[level].nodes) {
  155. if (distribution[level].nodes.hasOwnProperty(nodeId)) {
  156. node = distribution[level].nodes[nodeId];
  157. if (this.options.hierarchical.direction == "UD" || this.options.hierarchical.direction == "DU") {
  158. if (node.x === undefined) {node.x = distribution[level].distance;}
  159. distribution[level].distance = node.x + this.nodeSpacing;
  160. }
  161. else {
  162. if (node.y === undefined) {node.y = distribution[level].distance;}
  163. distribution[level].distance = node.y + this.nodeSpacing;
  164. }
  165. this.positionedNodes[nodeId] = true;
  166. this._placeBranchNodes(node.edges,node.id,distribution,level);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. /**
  173. * This function get the distribution of levels based on hubsize
  174. *
  175. * @returns {Object}
  176. * @private
  177. */
  178. _getDistribution() {
  179. let distribution = {};
  180. let nodeId, node;
  181. // we fix Y because the hierarchy is vertical, we fix X so we do not give a node an x position for a second time.
  182. // the fix of X is removed after the x value has been set.
  183. for (nodeId in this.body.nodes) {
  184. if (this.body.nodes.hasOwnProperty(nodeId)) {
  185. node = this.body.nodes[nodeId];
  186. if (this.options.hierarchical.direction == "UD" || this.options.hierarchical.direction == "DU") {
  187. node.y = this.options.hierarchical.levelSeparation * this.hierarchicalLevels[nodeId];
  188. node.options.fixed.y = true;
  189. }
  190. else {
  191. node.x = this.options.hierarchical.levelSeparation * this.hierarchicalLevels[nodeId];
  192. node.options.fixed.x = true;
  193. }
  194. if (distribution[this.hierarchicalLevels[nodeId]] === undefined) {
  195. distribution[this.hierarchicalLevels[nodeId]] = {amount: 0, nodes: {}, distance: 0};
  196. }
  197. distribution[this.hierarchicalLevels[nodeId]].amount += 1;
  198. distribution[this.hierarchicalLevels[nodeId]].nodes[nodeId] = node;
  199. }
  200. }
  201. return distribution;
  202. }
  203. /**
  204. * this function allocates nodes in levels based on the recursive branching from the largest hubs.
  205. *
  206. * @param hubsize
  207. * @private
  208. */
  209. _determineLevels(hubsize) {
  210. let nodeId, node;
  211. // determine hubs
  212. for (nodeId in this.body.nodes) {
  213. if (this.body.nodes.hasOwnProperty(nodeId)) {
  214. node = this.body.nodes[nodeId];
  215. if (node.edges.length == hubsize) {
  216. this.hierarchicalLevels[nodeId] = 0;
  217. }
  218. }
  219. }
  220. // branch from hubs
  221. for (nodeId in this.body.nodes) {
  222. if (this.body.nodes.hasOwnProperty(nodeId)) {
  223. node = this.body.nodes[nodeId];
  224. if (this.hierarchicalLevels[nodeId] == 0) {
  225. this._setLevel(1,node.edges,node.id);
  226. }
  227. }
  228. }
  229. }
  230. /**
  231. * this function is called recursively to enumerate the barnches of the largest hubs and give each node a level.
  232. *
  233. * @param level
  234. * @param edges
  235. * @param parentId
  236. * @private
  237. */
  238. _setLevel(level, edges, parentId) {
  239. for (let i = 0; i < edges.length; i++) {
  240. let childNode;
  241. if (edges[i].toId == parentId) {
  242. childNode = edges[i].from;
  243. }
  244. else {
  245. childNode = edges[i].to;
  246. }
  247. if (this.hierarchicalLevels[childNode.id] === undefined || this.hierarchicalLevels[childNode.id] > level) {
  248. this.hierarchicalLevels[childNode.id] = level;
  249. if (childNode.edges.length > 1) {
  250. this._setLevel(level+1, childNode.edges, childNode.id);
  251. }
  252. }
  253. }
  254. }
  255. /**
  256. * this function allocates nodes in levels based on the direction of the edges
  257. *
  258. * @param hubsize
  259. * @private
  260. */
  261. _determineLevelsDirected() {
  262. let nodeId, firstNode;
  263. let minLevel = 10000;
  264. // set first node to source
  265. firstNode = this.body.nodes[this.body.nodeIndices[0]];
  266. this._setLevelDirected(minLevel,firstNode);
  267. // get the minimum level
  268. for (nodeId in this.body.nodes) {
  269. if (this.body.nodes.hasOwnProperty(nodeId)) {
  270. minLevel = this.hierarchicalLevels[nodeId] < minLevel ? this.hierarchicalLevels[nodeId] : minLevel;
  271. }
  272. }
  273. // subtract the minimum from the set so we have a range starting from 0
  274. for (nodeId in this.body.nodes) {
  275. if (this.body.nodes.hasOwnProperty(nodeId)) {
  276. this.hierarchicalLevels[nodeId] -= minLevel;
  277. }
  278. }
  279. }
  280. /**
  281. * this function is called recursively to enumerate the branched of the first node and give each node a level based on edge direction
  282. *
  283. * @param level
  284. * @param edges
  285. * @param parentId
  286. * @private
  287. */
  288. _setLevelDirected(level, node) {
  289. if (this.hierarchicalLevels[node.id] !== undefined)
  290. return;
  291. let childNode;
  292. this.hierarchicalLevels[node.id] = level;
  293. for (let i = 0; i < node.edges.length; i++) {
  294. if (node.edges[i].toId == node.id) {
  295. childNode = node.edges[i].from;
  296. this._setLevelDirected(level - 1, childNode);
  297. }
  298. else {
  299. childNode = node.edges[i].to;
  300. this._setLevelDirected(level + 1, childNode);
  301. }
  302. }
  303. }
  304. /**
  305. * This is a recursively called function to enumerate the branches from the largest hubs and place the nodes
  306. * on a X position that ensures there will be no overlap.
  307. *
  308. * @param edges
  309. * @param parentId
  310. * @param distribution
  311. * @param parentLevel
  312. * @private
  313. */
  314. _placeBranchNodes(edges, parentId, distribution, parentLevel) {
  315. for (let i = 0; i < edges.length; i++) {
  316. let childNode = undefined;
  317. let parentNode = undefined;
  318. if (edges[i].toId == parentId) {
  319. childNode = edges[i].from;
  320. parentNode = edges[i].to;
  321. }
  322. else {
  323. childNode = edges[i].to;
  324. parentNode = edges[i].from;
  325. }
  326. let childNodeLevel = this.hierarchicalLevels[childNode.id];
  327. if (this.positionedNodes[childNode.id] === undefined) {
  328. // if a node is conneceted to another node on the same level (or higher (means lower level))!, this is not handled here.
  329. if (childNodeLevel > parentLevel) {
  330. if (this.options.hierarchical.direction == "UD" || this.options.hierarchical.direction == "DU") {
  331. if (childNode.x === undefined) {
  332. childNode.x = Math.max(distribution[childNodeLevel].distance, parentNode.x);
  333. }
  334. distribution[childNodeLevel].distance = childNode.x + this.nodeSpacing;
  335. this.positionedNodes[childNode.id] = true;
  336. }
  337. else {
  338. if (childNode.y === undefined) {
  339. childNode.y = Math.max(distribution[childNodeLevel].distance, parentNode.y)
  340. }
  341. distribution[childNodeLevel].distance = childNode.y + this.nodeSpacing;
  342. }
  343. this.positionedNodes[childNode.id] = true;
  344. if (childNode.edges.length > 1) {
  345. this._placeBranchNodes(childNode.edges, childNode.id, distribution, childNodeLevel);
  346. }
  347. }
  348. }
  349. }
  350. }
  351. }
  352. export default LayoutEngine;