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.

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