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.

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