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.

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