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.

99 lines
2.9 KiB

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