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.

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