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.

114 lines
3.3 KiB

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