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.

292 lines
10 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++) { // eslint-disable-line no-redeclare
  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 within a single subgroup such that they
  69. * don't overlap each other.
  70. * @param {Item[]} items
  71. * All items withina subgroup
  72. * @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
  73. * Margins between items and between items and the axis.
  74. * @param {subgroup} subgroup
  75. * The subgroup that is being stacked
  76. */
  77. exports.substack = function (items, margin, subgroup) {
  78. for (var i = 0; i < items.length; i++) {
  79. items[i].top = null;
  80. }
  81. // Set the initial height
  82. var subgroupHeight = subgroup.height;
  83. // calculate new, non-overlapping positions
  84. for (i = 0; i < items.length; i++) {
  85. var item = items[i];
  86. if (item.stack && item.top === null) {
  87. // initialize top position
  88. item.top = item.baseTop;//margin.axis + item.baseTop;
  89. do {
  90. // TODO: optimize checking for overlap. when there is a gap without items,
  91. // you only need to check for items from the next item on, not from zero
  92. var collidingItem = null;
  93. for (var j = 0, jj = items.length; j < jj; j++) {
  94. var other = items[j];
  95. if (other.top !== null && other !== item /*&& other.stack*/ && exports.collision(item, other, margin.item, other.options.rtl)) {
  96. collidingItem = other;
  97. break;
  98. }
  99. }
  100. if (collidingItem != null) {
  101. // There is a collision. Reposition the items above the colliding element
  102. item.top = collidingItem.top + collidingItem.height + margin.item.vertical;// + item.baseTop;
  103. }
  104. if (item.top + item.height > subgroupHeight) {
  105. subgroupHeight = item.top + item.height;
  106. }
  107. } while (collidingItem);
  108. }
  109. }
  110. // Set the new height
  111. subgroup.height = subgroupHeight - subgroup.top + 0.5 * margin.item.vertical;
  112. };
  113. /**
  114. * Adjust vertical positions of the items without stacking them
  115. * @param {Item[]} items
  116. * All visible items
  117. * @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
  118. * Margins between items and between items and the axis.
  119. * @param {subgroups[]} subgroups
  120. * All subgroups
  121. * @param {boolean} stackSubgroups
  122. */
  123. exports.nostack = function(items, margin, subgroups, stackSubgroups) {
  124. for (var i = 0; i < items.length; i++) {
  125. if (items[i].data.subgroup == undefined) {
  126. items[i].top = margin.item.vertical;
  127. } else if (items[i].data.subgroup !== undefined && stackSubgroups) {
  128. var newTop = 0;
  129. for (var subgroup in subgroups) {
  130. if (subgroups.hasOwnProperty(subgroup)) {
  131. if (subgroups[subgroup].visible == true && subgroups[subgroup].index < subgroups[items[i].data.subgroup].index) {
  132. newTop += subgroups[subgroup].height;
  133. subgroups[items[i].data.subgroup].top = newTop;
  134. }
  135. }
  136. }
  137. items[i].top = newTop + 0.5 * margin.item.vertical;
  138. }
  139. }
  140. if (!stackSubgroups) {
  141. exports.stackSubgroups(items, margin, subgroups)
  142. }
  143. };
  144. /**
  145. * Adjust vertical positions of the subgroups such that they don't overlap each
  146. * other.
  147. * @param {Array.<vis.Item>} items
  148. * @param {{item: {horizontal: number, vertical: number}, axis: number}} margin Margins between items and between items and the axis.
  149. * @param {subgroups[]} subgroups
  150. * All subgroups
  151. */
  152. exports.stackSubgroups = function(items, margin, subgroups) {
  153. for (var subgroup in subgroups) {
  154. if (subgroups.hasOwnProperty(subgroup)) {
  155. subgroups[subgroup].top = 0;
  156. do {
  157. // TODO: optimize checking for overlap. when there is a gap without items,
  158. // you only need to check for items from the next item on, not from zero
  159. var collidingItem = null;
  160. for (var otherSubgroup in subgroups) {
  161. if (subgroups[otherSubgroup].top !== null && otherSubgroup !== subgroup && subgroups[subgroup].index > subgroups[otherSubgroup].index && exports.collisionByTimes(subgroups[subgroup], subgroups[otherSubgroup])) {
  162. collidingItem = subgroups[otherSubgroup];
  163. break;
  164. }
  165. }
  166. if (collidingItem != null) {
  167. // There is a collision. Reposition the subgroups above the colliding element
  168. subgroups[subgroup].top = collidingItem.top + collidingItem.height;
  169. }
  170. } while (collidingItem);
  171. }
  172. }
  173. for (var i = 0; i < items.length; i++) {
  174. if (items[i].data.subgroup !== undefined) {
  175. items[i].top = subgroups[items[i].data.subgroup].top + 0.5 * margin.item.vertical;
  176. }
  177. }
  178. };
  179. /**
  180. * Adjust vertical positions of the subgroups such that they don't overlap each
  181. * other, then stacks the contents of each subgroup individually.
  182. * @param {Item[]} subgroupItems
  183. * All the items in a subgroup
  184. * @param {{item: {horizontal: number, vertical: number}, axis: number}} margin
  185. * Margins between items and between items and the axis.
  186. * @param {subgroups[]} subgroups
  187. * All subgroups
  188. */
  189. exports.stackSubgroupsWithInnerStack = function (subgroupItems, margin, subgroups) {
  190. var doSubStack = false;
  191. // Run subgroups in their order (if any)
  192. var subgroupOrder = [];
  193. for(var subgroup in subgroups) {
  194. if (subgroups[subgroup].hasOwnProperty("index")) {
  195. subgroupOrder[subgroups[subgroup].index] = subgroup;
  196. }
  197. else {
  198. subgroupOrder.push(subgroup);
  199. }
  200. }
  201. for(var j = 0; j < subgroupOrder.length; j++) {
  202. subgroup = subgroupOrder[j];
  203. if (subgroups.hasOwnProperty(subgroup)) {
  204. doSubStack = doSubStack || subgroups[subgroup].stack;
  205. subgroups[subgroup].top = 0;
  206. for (var otherSubgroup in subgroups) {
  207. if (subgroups[otherSubgroup].visible && subgroups[subgroup].index > subgroups[otherSubgroup].index) {
  208. subgroups[subgroup].top += subgroups[otherSubgroup].height;
  209. }
  210. }
  211. var items = subgroupItems[subgroup];
  212. for(var i = 0; i < items.length; i++) {
  213. if (items[i].data.subgroup !== undefined) {
  214. items[i].top = subgroups[items[i].data.subgroup].top + 0.5 * margin.item.vertical;
  215. if (subgroups[subgroup].stack) {
  216. items[i].baseTop = items[i].top;
  217. }
  218. }
  219. }
  220. if (doSubStack && subgroups[subgroup].stack) {
  221. exports.substack(subgroupItems[subgroup], margin, subgroups[subgroup]);
  222. }
  223. }
  224. }
  225. };
  226. /**
  227. * Test if the two provided items collide
  228. * The items must have parameters left, width, top, and height.
  229. * @param {Item} a The first item
  230. * @param {Item} b The second item
  231. * @param {{horizontal: number, vertical: number}} margin
  232. * An object containing a horizontal and vertical
  233. * minimum required margin.
  234. * @param {boolean} rtl
  235. * @return {boolean} true if a and b collide, else false
  236. */
  237. exports.collision = function(a, b, margin, rtl) {
  238. if (rtl) {
  239. return ((a.right - margin.horizontal + EPSILON) < (b.right + b.width) &&
  240. (a.right + a.width + margin.horizontal - EPSILON) > b.right &&
  241. (a.top - margin.vertical + EPSILON) < (b.top + b.height) &&
  242. (a.top + a.height + margin.vertical - EPSILON) > b.top);
  243. } else {
  244. return ((a.left - margin.horizontal + EPSILON) < (b.left + b.width) &&
  245. (a.left + a.width + margin.horizontal - EPSILON) > b.left &&
  246. (a.top - margin.vertical + EPSILON) < (b.top + b.height) &&
  247. (a.top + a.height + margin.vertical - EPSILON) > b.top);
  248. }
  249. };
  250. /**
  251. * Test if the two provided objects collide
  252. * The objects must have parameters start, end, top, and height.
  253. * @param {Object} a The first Object
  254. * @param {Object} b The second Object
  255. * @return {boolean} true if a and b collide, else false
  256. */
  257. exports.collisionByTimes = function(a, b) {
  258. return (
  259. (a.start <= b.start && a.end >= b.start && a.top < (b.top + b.height) && (a.top + a.height) > b.top ) ||
  260. (b.start <= a.start && b.end >= a.start && b.top < (a.top + a.height) && (b.top + b.height) > a.top )
  261. )
  262. }