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.

427 lines
13 KiB

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