utils.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Licensed Materials - Property of IBM IBM Cognos Products: Modeling UI (C) Copyright IBM Corp. 2016, 2020
  3. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP
  4. * Schedule Contract with IBM Corp.
  5. */
  6. // This function has never been able to achieve what it meant to achieve (e.g. cardinality position dynamically changed according to the length of the node box). Need to revisit.
  7. function calculateCardinalityPosition(source, target) {
  8. var opposite = source.y - target.y,
  9. adjacent = target.x - source.x,
  10. angle = Math.atan(opposite / adjacent) || 0;
  11. var tableDetailsOffset = source.displayTableInfo ? 15 : 0,
  12. cardinalityNodeOffset = 10,
  13. itemWidth = 200,
  14. itemHeight = 70,
  15. itemMidpoint = itemHeight / 2,
  16. theta1 = Math.PI + angle, // Quadrant 2, 3
  17. theta2 = Math.atan(itemHeight / itemWidth);
  18. // Quadrant 1
  19. if (source.x > target.x && source.y < target.y) {
  20. theta1 = angle;
  21. }
  22. // Quadrant 4
  23. else if (source.x > target.x && source.y > target.y) {
  24. theta1 = 2 * Math.PI + angle;
  25. }
  26. // Left
  27. if ((theta1 > 2 * Math.PI - theta2) || (theta1 < theta2)) {
  28. return {
  29. x: Math.floor(source.x - cardinalityNodeOffset - itemWidth / 2),
  30. y: Math.floor((itemWidth / 2) * (Math.tan(theta1)) + source.y - cardinalityNodeOffset + (tableDetailsOffset / 2))
  31. };
  32. }
  33. // Bottom
  34. else if ((theta1 > theta2) && (theta1 < Math.PI - theta2)) {
  35. return {
  36. x: Math.floor(source.x - cardinalityNodeOffset - ((itemHeight) / (2 * Math.tan(theta1)))),
  37. y: Math.floor(source.y - cardinalityNodeOffset + itemMidpoint + tableDetailsOffset)
  38. };
  39. }
  40. // Right
  41. else if ((theta1 > Math.PI - theta2) && (theta1 < Math.PI + theta2)) {
  42. return {
  43. x: Math.floor(source.x - cardinalityNodeOffset + itemWidth / 2),
  44. y: Math.floor((-1 * itemWidth / 2) * (Math.tan(theta1)) + source.y - cardinalityNodeOffset + (tableDetailsOffset / 2))
  45. };
  46. }
  47. // Top
  48. return {
  49. x: Math.floor(source.x - cardinalityNodeOffset + (itemHeight / (2 * Math.tan(theta1)))),
  50. y: Math.floor(source.y - cardinalityNodeOffset - itemMidpoint)
  51. };
  52. }
  53. define([], function() {
  54. return {
  55. d3Attrs: function(selection, map) {
  56. for (var name in map) selection.attr(name, map[name]);
  57. return selection;
  58. },
  59. d3Styles: function(selection, map) {
  60. for (var name in map) selection.style(name, map[name]);
  61. return selection;
  62. },
  63. isOffset: function (p1, p2, deadzone) {
  64. var zone = deadzone || 3;
  65. return Math.abs(p1.x - p2.x) > zone || Math.abs(p1.y - p2.y) > zone;
  66. },
  67. getCardinalityPosition: function(linkData, linkType) {
  68. if (linkType === 'source') {
  69. return calculateCardinalityPosition(linkData.source, linkData.target);
  70. }
  71. return calculateCardinalityPosition(linkData.target, linkData.source);
  72. }
  73. };
  74. });