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.

173 lines
5.0 KiB

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Timeline | Select items</title>
  5. <style>
  6. body, html {
  7. font-family: arial, sans-serif;
  8. font-size: 11pt;
  9. }
  10. </style>
  11. <script src="../../../dist/vis.js"></script>
  12. <link href="../../../dist/vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css" />
  13. </head>
  14. <body>
  15. <h1>Set selection</h1>
  16. <p style="max-width: 600px;">
  17. Enter one or multiple ids of items, then press select to select the items. This demo uses the function <code>Timeline.setSelection(ids)</code>. Optionally, the window can be moved to the selected items.
  18. </p>
  19. <p>
  20. Select item(s): <input type="text" id="selection" value="5, 6"><input type="button" id="select" value="Select"><br>
  21. <label><input type="checkbox" id="focus" checked> Focus on selection</label>
  22. </p>
  23. <div id="visualization"></div>
  24. <br/>
  25. <p>If the height of the timeline is limited some items may be vertically offscreen. This demo uses <code>Timeline.setSelection(ids, {focus: true})</code> and demonstrates that focusing on an item will
  26. cause the timeline to scroll vertically to the item that is being focused on. You can use the buttons below select a random item either above or below the currently selected item.
  27. </p>
  28. <button id="prevFocus">Select Item Above</button>
  29. <button id="nextFocus">Select Item Below</button>
  30. <br/>
  31. <p>If focusing on multiple items only the first item will be scrolled to. Try entering several ids and hitting <em>select</em>.</p>
  32. <p>
  33. Select item(s): <input type="text" id="selectionVertical" value="g1_5, g2_3"><input type="button" id="selectVertical" value="Select"><br>
  34. </p>
  35. <div id="vertical-visualization"></div>
  36. <script>
  37. // create a dataset with items
  38. // we specify the type of the fields `start` and `end` here to be strings
  39. // containing an ISO date. The fields will be outputted as ISO dates
  40. // automatically getting data from the DataSet via items.get().
  41. var items = new vis.DataSet({
  42. type: { start: 'ISODate', end: 'ISODate' }
  43. });
  44. // add items to the DataSet
  45. items.add([
  46. {id: 1, content: 'item 1<br>start', start: '2014-01-23'},
  47. {id: 2, content: 'item 2', start: '2014-01-18'},
  48. {id: 3, content: 'item 3', start: '2014-01-21'},
  49. {id: 4, content: 'item 4', start: '2014-01-19', end: '2014-01-24'},
  50. {id: 5, content: 'item 5', start: '2014-01-28', type:'point'},
  51. {id: 6, content: 'item 6', start: '2014-01-26'}
  52. ]);
  53. var container = document.getElementById('visualization');
  54. var options = {
  55. editable: true
  56. };
  57. var timeline = new vis.Timeline(container, items, options);
  58. var selection = document.getElementById('selection');
  59. var select = document.getElementById('select');
  60. var focus = document.getElementById('focus');
  61. select.onclick = function () {
  62. var ids = selection.value.split(',').map(function (value) {
  63. return value.trim();
  64. });
  65. timeline.setSelection(ids, {focus: focus.checked});
  66. };
  67. function getRandomInt(min, max) {
  68. min = Math.ceil(min);
  69. max = Math.floor(max);
  70. return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
  71. };
  72. // Vertical scroll example
  73. var groups = [];
  74. var items = [];
  75. var groupItems = {};
  76. for (var g = 0; g < 10; g++) {
  77. groups.push({
  78. id: g,
  79. content: "Group " + g
  80. });
  81. groupItems[g] = [];
  82. for (var i = 0; i < 30; i++) {
  83. items.push({
  84. id: "g" + g + "_" + i,
  85. content: "g" + g + "_" + i,
  86. group: g,
  87. start: "2014-" + (g + 1) + "-" + getRandomInt(1, 20)
  88. });
  89. groupItems[g].push(items[items.length - 1]);
  90. }
  91. }
  92. var container2 = document.getElementById('vertical-visualization');
  93. var options = {
  94. editable: false,
  95. stack: true,
  96. height: 300,
  97. verticalScroll: true,
  98. groupOrder: 'id'
  99. };
  100. var timeline2 = new vis.Timeline(container2, items, groups, options);
  101. var groupIndex = 0;
  102. var itemIndex = 0;
  103. var moveToItem = function(direction) {
  104. itemIndex += direction;
  105. groupIndex += direction;
  106. if (groupIndex < 0) {
  107. groupIndex = groups.length - 1;
  108. } else if (groupIndex >= groups.length) {
  109. groupIndex = 0;
  110. }
  111. var items = groupItems[groupIndex];
  112. if (itemIndex < 0) {
  113. itemIndex = items.length - 1;
  114. } else if (itemIndex >= items.length) {
  115. itemIndex = 0;
  116. }
  117. var id = items[itemIndex].id;
  118. timeline2.setSelection(id, {focus: true});
  119. }
  120. var nextFocus = document.getElementById('nextFocus');
  121. var prevFocus = document.getElementById('prevFocus');
  122. var selectionVertical = document.getElementById('selectionVertical');
  123. var selectVertical = document.getElementById('selectVertical');
  124. selectVertical.onclick = function () {
  125. var ids = selectionVertical.value.split(',').map(function (value) {
  126. return value.trim();
  127. });
  128. timeline2.setSelection(ids, {focus: focus.checked});
  129. };
  130. nextFocus.onclick = function() {
  131. moveToItem(1);
  132. };
  133. prevFocus.onclick = function() {
  134. moveToItem(-1);
  135. };
  136. // Set the initial focus
  137. setTimeout(function() {
  138. moveToItem(0);
  139. }, 500);
  140. </script>
  141. </body>
  142. </html>