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.

110 lines
3.2 KiB

  1. let util = require("../util");
  2. /**
  3. * @class NetworkUtil
  4. */
  5. class NetworkUtil {
  6. /**
  7. * @constructor NetworkUtil
  8. */
  9. constructor() {}
  10. /**
  11. * Find the center position of the network considering the bounding boxes
  12. *
  13. * @param {Array<vis.Node>} allNodes
  14. * @param {Array<vis.Node>} [specificNodes=[]]
  15. * @returns {{minX: number, maxX: number, minY: number, maxY: number}}
  16. */
  17. static getRange(allNodes, specificNodes = []) {
  18. var minY = 1e9, maxY = -1e9, minX = 1e9, maxX = -1e9, node;
  19. if (specificNodes.length > 0) {
  20. for (var i = 0; i < specificNodes.length; i++) {
  21. node = allNodes[specificNodes[i]];
  22. if (minX > node.shape.boundingBox.left) {
  23. minX = node.shape.boundingBox.left;
  24. }
  25. if (maxX < node.shape.boundingBox.right) {
  26. maxX = node.shape.boundingBox.right;
  27. }
  28. if (minY > node.shape.boundingBox.top) {
  29. minY = node.shape.boundingBox.top;
  30. } // top is negative, bottom is positive
  31. if (maxY < node.shape.boundingBox.bottom) {
  32. maxY = node.shape.boundingBox.bottom;
  33. } // top is negative, bottom is positive
  34. }
  35. }
  36. if (minX === 1e9 && maxX === -1e9 && minY === 1e9 && maxY === -1e9) {
  37. minY = 0, maxY = 0, minX = 0, maxX = 0;
  38. }
  39. return {minX: minX, maxX: maxX, minY: minY, maxY: maxY};
  40. }
  41. /**
  42. * Find the center position of the network
  43. *
  44. * @param {Array<vis.Node>} allNodes
  45. * @param {Array<vis.Node>} [specificNodes=[]]
  46. * @returns {{minX: number, maxX: number, minY: number, maxY: number}}
  47. */
  48. static getRangeCore(allNodes, specificNodes = []) {
  49. var minY = 1e9, maxY = -1e9, minX = 1e9, maxX = -1e9, node;
  50. if (specificNodes.length > 0) {
  51. for (var i = 0; i < specificNodes.length; i++) {
  52. node = allNodes[specificNodes[i]];
  53. if (minX > node.x) {
  54. minX = node.x;
  55. }
  56. if (maxX < node.x) {
  57. maxX = node.x;
  58. }
  59. if (minY > node.y) {
  60. minY = node.y;
  61. } // top is negative, bottom is positive
  62. if (maxY < node.y) {
  63. maxY = node.y;
  64. } // top is negative, bottom is positive
  65. }
  66. }
  67. if (minX === 1e9 && maxX === -1e9 && minY === 1e9 && maxY === -1e9) {
  68. minY = 0, maxY = 0, minX = 0, maxX = 0;
  69. }
  70. return {minX: minX, maxX: maxX, minY: minY, maxY: maxY};
  71. }
  72. /**
  73. * @param {object} range = {minX: minX, maxX: maxX, minY: minY, maxY: maxY};
  74. * @returns {{x: number, y: number}}
  75. */
  76. static findCenter(range) {
  77. return {x: (0.5 * (range.maxX + range.minX)),
  78. y: (0.5 * (range.maxY + range.minY))};
  79. }
  80. /**
  81. * This returns a clone of the options or options of the edge or node to be used for construction of new edges or check functions for new nodes.
  82. * @param {vis.Item} item
  83. * @param {'node'|undefined} type
  84. * @returns {{}}
  85. */
  86. static cloneOptions(item, type) {
  87. let clonedOptions = {};
  88. if (type === undefined || type === 'node') {
  89. util.deepExtend(clonedOptions, item.options, true);
  90. clonedOptions.x = item.x;
  91. clonedOptions.y = item.y;
  92. clonedOptions.amountOfConnections = item.edges.length;
  93. }
  94. else {
  95. util.deepExtend(clonedOptions, item.options, true);
  96. }
  97. return clonedOptions;
  98. }
  99. }
  100. export default NetworkUtil;