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.

182 lines
6.5 KiB

  1. // Utility functions for ordering and stacking of items
  2. var EPSILON = 0.001; // used when checking collisions, to prevent round-off errors
  3. /**
  4. * Order items by their start data
  5. * @param {Item[]} items
  6. */
  7. exports.orderByStart = function(items) {
  8. items.sort(function (a, b) {
  9. return a.data.start - b.data.start;
  10. });
  11. };
  12. /**
  13. * Order items by their end date. If they have no end date, their start date
  14. * is used.
  15. * @param {Item[]} items
  16. */
  17. exports.orderByEnd = function(items) {
  18. items.sort(function (a, b) {
  19. var aTime = ('end' in a.data) ? a.data.end : a.data.start,
  20. bTime = ('end' in b.data) ? b.data.end : b.data.start;
  21. return aTime - bTime;
  22. });
  23. };
  24. /**
  25. * Adjust vertical positions of the items such that they don't overlap each
  26. * other.
  27. * @param {Item[]} items
  28. * All visible items
  29. * @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
  30. * Margins between items and between items and the axis.
  31. * @param {boolean} [force=false]
  32. * If true, all items will be repositioned. If false (default), only
  33. * items having a top===null will be re-stacked
  34. */
  35. exports.stack = function(items, margin, force) {
  36. if (force) {
  37. // reset top position of all items
  38. for (var i = 0; i < items.length; i++) {
  39. items[i].top = null;
  40. }
  41. }
  42. // calculate new, non-overlapping positions
  43. for (var i = 0; i < items.length; i++) {
  44. var item = items[i];
  45. if (item.stack && item.top === null) {
  46. // initialize top position
  47. item.top = margin.axis;
  48. do {
  49. // TODO: optimize checking for overlap. when there is a gap without items,
  50. // you only need to check for items from the next item on, not from zero
  51. var collidingItem = null;
  52. for (var j = 0, jj = items.length; j < jj; j++) {
  53. var other = items[j];
  54. if (other.top !== null && other !== item && other.stack && exports.collision(item, other, margin.item, other.options.rtl)) {
  55. collidingItem = other;
  56. break;
  57. }
  58. }
  59. if (collidingItem != null) {
  60. // There is a collision. Reposition the items above the colliding element
  61. item.top = collidingItem.top + collidingItem.height + margin.item.vertical;
  62. }
  63. } while (collidingItem);
  64. }
  65. }
  66. };
  67. /**
  68. * Adjust vertical positions of the items without stacking them
  69. * @param {Item[]} items
  70. * All visible items
  71. * @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
  72. * Margins between items and between items and the axis.
  73. * @param {subgroups[]} subgroups
  74. * All subgroups
  75. */
  76. exports.nostack = function(items, margin, subgroups, stackSubgroups) {
  77. for (var i = 0; i < items.length; i++) {
  78. if (items[i].data.subgroup == undefined) {
  79. items[i].top = margin.item.vertical;
  80. } else if (items[i].data.subgroup !== undefined && stackSubgroups) {
  81. var newTop = 0;
  82. for (var subgroup in subgroups) {
  83. if (subgroups.hasOwnProperty(subgroup)) {
  84. if (subgroups[subgroup].visible == true && subgroups[subgroup].index < subgroups[items[i].data.subgroup].index) {
  85. newTop += subgroups[subgroup].height;
  86. subgroups[items[i].data.subgroup].top = newTop;
  87. }
  88. }
  89. }
  90. items[i].top = newTop + 0.5 * margin.item.vertical;
  91. }
  92. }
  93. if (!stackSubgroups) {
  94. exports.stackSubgroups(items, margin, subgroups)
  95. }
  96. };
  97. /**
  98. * Adjust vertical positions of the subgroups such that they don't overlap each
  99. * other.
  100. * @param {subgroups[]} subgroups
  101. * All subgroups
  102. * @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
  103. * Margins between items and between items and the axis.
  104. */
  105. exports.stackSubgroups = function(items, margin, subgroups) {
  106. for (var subgroup in subgroups) {
  107. if (subgroups.hasOwnProperty(subgroup)) {
  108. subgroups[subgroup].top = 0;
  109. do {
  110. // TODO: optimize checking for overlap. when there is a gap without items,
  111. // you only need to check for items from the next item on, not from zero
  112. var collidingItem = null;
  113. for (var otherSubgroup in subgroups) {
  114. if (subgroups[otherSubgroup].top !== null && otherSubgroup !== subgroup && subgroups[subgroup].index > subgroups[otherSubgroup].index && exports.collisionByTimes(subgroups[subgroup], subgroups[otherSubgroup])) {
  115. collidingItem = subgroups[otherSubgroup];
  116. break;
  117. }
  118. }
  119. if (collidingItem != null) {
  120. // There is a collision. Reposition the subgroups above the colliding element
  121. subgroups[subgroup].top = collidingItem.top + collidingItem.height;
  122. }
  123. } while (collidingItem);
  124. }
  125. }
  126. for (var i = 0; i < items.length; i++) {
  127. if (items[i].data.subgroup !== undefined) {
  128. items[i].top = subgroups[items[i].data.subgroup].top + 0.5 * margin.item.vertical;
  129. }
  130. }
  131. }
  132. /**
  133. * Test if the two provided items collide
  134. * The items must have parameters left, width, top, and height.
  135. * @param {Item} a The first item
  136. * @param {Item} b The second item
  137. * @param {{horizontal: number, vertical: number}} margin
  138. * An object containing a horizontal and vertical
  139. * minimum required margin.
  140. * @param {boolean} rtl
  141. * @return {boolean} true if a and b collide, else false
  142. */
  143. exports.collision = function(a, b, margin, rtl) {
  144. if (rtl) {
  145. return ((a.right - margin.horizontal + EPSILON) < (b.right + b.width) &&
  146. (a.right + a.width + margin.horizontal - EPSILON) > b.right &&
  147. (a.top - margin.vertical + EPSILON) < (b.top + b.height) &&
  148. (a.top + a.height + margin.vertical - EPSILON) > b.top);
  149. } else {
  150. return ((a.left - margin.horizontal + EPSILON) < (b.left + b.width) &&
  151. (a.left + a.width + margin.horizontal - EPSILON) > b.left &&
  152. (a.top - margin.vertical + EPSILON) < (b.top + b.height) &&
  153. (a.top + a.height + margin.vertical - EPSILON) > b.top);
  154. }
  155. };
  156. /**
  157. * Test if the two provided objects collide
  158. * The objects must have parameters start, end, top, and height.
  159. * @param {Object} a The first Object
  160. * @param {Object} b The second Object
  161. * @return {boolean} true if a and b collide, else false
  162. */
  163. exports.collisionByTimes = function(a, b) {
  164. return (
  165. (a.start < b.start && a.end > b.start && a.top < (b.top + b.height) && (a.top + a.height) > b.top ) ||
  166. (b.start < a.start && b.end > a.start && b.top < (a.top + a.height) && (b.top + b.height) > a.top )
  167. )
  168. }