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.

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