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.

112 lines
3.3 KiB

  1. <html>
  2. <head>
  3. <title>Timeline | A lot of grouped data</title>
  4. <script src="../../../docs/js/jquery.min.js"></script>
  5. <script src="../../../dist/vis.js"></script>
  6. <link href="../../../dist/vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css" />
  7. <style type="text/css">
  8. body {
  9. color: #4D4D4D;
  10. font: 10pt arial;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <h1>Timeline visible Groups</h1>
  16. <button onclick="showVisibleGroups()">Show current visible items</button>
  17. <div>
  18. <h2>visible groups:</h2>
  19. <h3 id="visibleGroupsContainer"></h3>
  20. <h2>(Scroll with the mouse and see the items being focus automatically on the timeline)</h2>
  21. </div>
  22. <div id="mytimeline"></div>
  23. <br>
  24. <script>
  25. function showVisibleGroups() {
  26. var a = timeline.getVisibleGroups();
  27. document.getElementById("visibleGroupsContainer").innerHTML = ""
  28. document.getElementById("visibleGroupsContainer").innerHTML += a;
  29. };
  30. var now = Date.now()
  31. var options = {
  32. stack: true,
  33. maxHeight: 640,
  34. horizontalScroll: false,
  35. verticalScroll: true,
  36. zoomKey: "ctrlKey",
  37. start: Date.now() - 1000 * 60 * 60 * 24 * 3, // minus 3 days
  38. end: Date.now() + 1000 * 60 * 60 * 24 * 21, // plus 1 months aprox.
  39. orientation: {
  40. axis: "both",
  41. item: "top"
  42. },
  43. };
  44. var groups = new vis.DataSet();
  45. var items = new vis.DataSet();
  46. var count = 300;
  47. for (var i = 0; i < count; i++) {
  48. var start = now + 1000 * 60 * 60 * 24 * (i + Math.floor(Math.random() * 7))
  49. var end = start + 1000 * 60 * 60 * 24 * (1 + Math.floor(Math.random() * 5))
  50. groups.add({
  51. id: i,
  52. content: 'Task ' + i,
  53. order: i
  54. })
  55. items.add({
  56. id: i,
  57. group: i,
  58. start: start,
  59. end: end,
  60. type: 'range',
  61. content: 'Item ' + i
  62. });
  63. }
  64. // create a Timeline
  65. var container = document.getElementById('mytimeline');
  66. timeline = new vis.Timeline(container, null, options);
  67. timeline.setGroups(groups);
  68. timeline.setItems(items);
  69. function debounce(func, wait = 100) {
  70. let timeout;
  71. return function (...args) {
  72. clearTimeout(timeout);
  73. timeout = setTimeout(() => {
  74. func.apply(this, args);
  75. }, wait);
  76. };
  77. }
  78. let groupFocus = (e) => {
  79. let vGroups = timeline.getVisibleGroups()
  80. let vItems = vGroups.reduce((res, groupId) => {
  81. let group = timeline.itemSet.groups[groupId]
  82. if (group.items) {
  83. res = res.concat(Object.keys(group.items))
  84. }
  85. return res
  86. }, [])
  87. timeline.focus(vItems)
  88. }
  89. this.timeline.on("scroll", debounce(groupFocus, 200))
  90. // Enabling the next line leads to a continuous since calling focus might scroll vertically even if it shouldn't
  91. // this.timeline.on("scrollSide", debounce(groupFocus, 200))
  92. </script>
  93. </body>
  94. </html>