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.

76 lines
2.2 KiB

  1. class NetworkUtil {
  2. constructor() {}
  3. /**
  4. * Find the center position of the network considering the bounding boxes
  5. * @private
  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. * @private
  34. */
  35. static _getRangeCore(allNodes, specificNodes = []) {
  36. var minY = 1e9, maxY = -1e9, minX = 1e9, maxX = -1e9, node;
  37. if (specificNodes.length > 0) {
  38. for (var i = 0; i < specificNodes.length; i++) {
  39. node = allNodes[specificNodes[i]];
  40. if (minX > node.x) {
  41. minX = node.x;
  42. }
  43. if (maxX < node.x) {
  44. maxX = node.x;
  45. }
  46. if (minY > node.y) {
  47. minY = node.y;
  48. } // top is negative, bottom is positive
  49. if (maxY < node.y) {
  50. maxY = node.y;
  51. } // top is negative, bottom is positive
  52. }
  53. }
  54. if (minX === 1e9 && maxX === -1e9 && minY === 1e9 && maxY === -1e9) {
  55. minY = 0, maxY = 0, minX = 0, maxX = 0;
  56. }
  57. return {minX: minX, maxX: maxX, minY: minY, maxY: maxY};
  58. }
  59. /**
  60. * @param {object} range = {minX: minX, maxX: maxX, minY: minY, maxY: maxY};
  61. * @returns {{x: number, y: number}}
  62. * @private
  63. */
  64. static _findCenter(range) {
  65. return {x: (0.5 * (range.maxX + range.minX)),
  66. y: (0.5 * (range.maxY + range.minY))};
  67. }
  68. }
  69. export default NetworkUtil;