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.

416 lines
13 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. }
  116. }
  117. // if the user defined some levels but not all, alert and run without hierarchical layout
  118. if (undefinedLevel == true && definedLevel == true) {
  119. throw new Error("To use the hierarchical layout, nodes require either no predefined levels or levels have to be defined for all nodes.");
  120. return;
  121. }
  122. else {
  123. // setup the system to use hierarchical method.
  124. //this._changeConstants();
  125. // define levels if undefined by the users. Based on hubsize
  126. if (undefinedLevel == true) {
  127. if (this.options.hierarchical.sortMethod == "hubsize") {
  128. this._determineLevelsByHubsize();
  129. }
  130. else if (this.options.hierarchical.sortMethod == "directed" || "direction") {
  131. this._determineLevelsDirected();
  132. }
  133. }
  134. console.log(this.hierarchicalLevels)
  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. * Get the hubsize from all remaining unlevelled nodes.
  205. *
  206. * @returns {number}
  207. * @private
  208. */
  209. _getHubSize() {
  210. let hubSize = 0;
  211. for (let nodeId in this.body.nodes) {
  212. if (this.body.nodes.hasOwnProperty(nodeId)) {
  213. let node = this.body.nodes[nodeId];
  214. if (this.hierarchicalLevels[nodeId] === undefined) {
  215. hubSize = node.edges.length < hubSize ? hubSize : node.edges.length;
  216. }
  217. }
  218. }
  219. return hubSize;
  220. }
  221. /**
  222. * this function allocates nodes in levels based on the recursive branching from the largest hubs.
  223. *
  224. * @param hubsize
  225. * @private
  226. */
  227. _determineLevelsByHubsize() {
  228. let nodeId, node;
  229. let hubSize = 1;
  230. while (hubSize > 0) {
  231. // determine hubs
  232. hubSize = this._getHubSize();
  233. if (hubSize == 0)
  234. break;
  235. for (nodeId in this.body.nodes) {
  236. if (this.body.nodes.hasOwnProperty(nodeId)) {
  237. node = this.body.nodes[nodeId];
  238. if (node.edges.length == hubSize) {
  239. this._setLevel(0, node);
  240. }
  241. }
  242. }
  243. }
  244. }
  245. /**
  246. * this function is called recursively to enumerate the barnches of the largest hubs and give each node a level.
  247. *
  248. * @param level
  249. * @param edges
  250. * @param parentId
  251. * @private
  252. */
  253. _setLevel(level, node) {
  254. if (this.hierarchicalLevels[node.id] !== undefined)
  255. return;
  256. let childNode;
  257. this.hierarchicalLevels[node.id] = level;
  258. for (let i = 0; i < node.edges.length; i++) {
  259. if (node.edges[i].toId == node.id) {
  260. childNode = node.edges[i].from;
  261. }
  262. else {
  263. childNode = node.edges[i].to;
  264. }
  265. this._setLevel(level + 1, childNode);
  266. }
  267. }
  268. /**
  269. * this function allocates nodes in levels based on the direction of the edges
  270. *
  271. * @param hubsize
  272. * @private
  273. */
  274. _determineLevelsDirected() {
  275. let nodeId, node;
  276. let minLevel = 10000;
  277. // set first node to source
  278. for (nodeId in this.body.nodes) {
  279. if (this.body.nodes.hasOwnProperty(nodeId)) {
  280. node = this.body.nodes[nodeId];
  281. this._setLevelDirected(minLevel,node);
  282. }
  283. }
  284. // get the minimum level
  285. for (nodeId in this.body.nodes) {
  286. if (this.body.nodes.hasOwnProperty(nodeId)) {
  287. minLevel = this.hierarchicalLevels[nodeId] < minLevel ? this.hierarchicalLevels[nodeId] : minLevel;
  288. }
  289. }
  290. // subtract the minimum from the set so we have a range starting from 0
  291. for (nodeId in this.body.nodes) {
  292. if (this.body.nodes.hasOwnProperty(nodeId)) {
  293. this.hierarchicalLevels[nodeId] -= minLevel;
  294. }
  295. }
  296. }
  297. /**
  298. * this function is called recursively to enumerate the branched of the first node and give each node a level based on edge direction
  299. *
  300. * @param level
  301. * @param edges
  302. * @param parentId
  303. * @private
  304. */
  305. _setLevelDirected(level, node) {
  306. if (this.hierarchicalLevels[node.id] !== undefined)
  307. return;
  308. let childNode;
  309. this.hierarchicalLevels[node.id] = level;
  310. for (let i = 0; i < node.edges.length; i++) {
  311. if (node.edges[i].toId == node.id) {
  312. childNode = node.edges[i].from;
  313. this._setLevelDirected(level - 1, childNode);
  314. }
  315. else {
  316. childNode = node.edges[i].to;
  317. this._setLevelDirected(level + 1, childNode);
  318. }
  319. }
  320. }
  321. /**
  322. * This is a recursively called function to enumerate the branches from the largest hubs and place the nodes
  323. * on a X position that ensures there will be no overlap.
  324. *
  325. * @param edges
  326. * @param parentId
  327. * @param distribution
  328. * @param parentLevel
  329. * @private
  330. */
  331. _placeBranchNodes(edges, parentId, distribution, parentLevel) {
  332. for (let i = 0; i < edges.length; i++) {
  333. let childNode = undefined;
  334. let parentNode = undefined;
  335. if (edges[i].toId == parentId) {
  336. childNode = edges[i].from;
  337. parentNode = edges[i].to;
  338. }
  339. else {
  340. childNode = edges[i].to;
  341. parentNode = edges[i].from;
  342. }
  343. let childNodeLevel = this.hierarchicalLevels[childNode.id];
  344. if (this.positionedNodes[childNode.id] === undefined) {
  345. // if a node is conneceted to another node on the same level (or higher (means lower level))!, this is not handled here.
  346. if (childNodeLevel > parentLevel) {
  347. if (this.options.hierarchical.direction == "UD" || this.options.hierarchical.direction == "DU") {
  348. if (childNode.x === undefined) {
  349. childNode.x = Math.max(distribution[childNodeLevel].distance, parentNode.x);
  350. }
  351. distribution[childNodeLevel].distance = childNode.x + this.nodeSpacing;
  352. this.positionedNodes[childNode.id] = true;
  353. }
  354. else {
  355. if (childNode.y === undefined) {
  356. childNode.y = Math.max(distribution[childNodeLevel].distance, parentNode.y)
  357. }
  358. distribution[childNodeLevel].distance = childNode.y + this.nodeSpacing;
  359. }
  360. this.positionedNodes[childNode.id] = true;
  361. if (childNode.edges.length > 1) {
  362. this._placeBranchNodes(childNode.edges, childNode.id, distribution, childNodeLevel);
  363. }
  364. }
  365. }
  366. }
  367. }
  368. }
  369. export default LayoutEngine;