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.

219 lines
5.4 KiB

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Timeline | Group example</title>
  5. <style>
  6. body, html {
  7. font-family: arial, sans-serif;
  8. font-size: 11pt;
  9. }
  10. #visualization {
  11. box-sizing: border-box;
  12. width: 100%;
  13. }
  14. .vis.timeline .item.red,
  15. .vis.timeline .item.red.selected {
  16. background-color: red;
  17. color: white;
  18. }
  19. .vis.timeline .item.green,
  20. .vis.timeline .item.green.selected {
  21. background-color: green;
  22. color: white;
  23. }
  24. </style>
  25. <script src="../dist/vis.js"></script>
  26. <link href="../dist/vis.css" rel="stylesheet" type="text/css" />
  27. </head>
  28. <body>
  29. <div>
  30. <label for="orientation">Orientation</label>
  31. <select id="orientation">
  32. <option value="both">both</option>
  33. <option value="bottom" selected>bottom</option>
  34. <option value="top">top</option>
  35. </select>
  36. </div>
  37. <script>
  38. var o = document.getElementById('orientation');
  39. o.onchange = function () {
  40. timeline.setOptions({
  41. orientation: o.value
  42. });
  43. };
  44. </script>
  45. <br>
  46. <div id="visualization"></div>
  47. <script>
  48. var moment = vis.moment;
  49. var now = moment().minutes(0).seconds(0).milliseconds(0);
  50. var groupCount = 3;
  51. var itemCount = 7;
  52. // create a data set with groups
  53. var names = ['John (0)', 'Alston (1)', 'Lee (2)', 'Grant (3)'];
  54. var groups = new vis.DataSet();
  55. for (var g = 0; g < groupCount; g++) {
  56. groups.add({id: g, content: names[g], title: 'Title of group ' + g, 'className': 'myGroup'});
  57. }
  58. // create a dataset with items
  59. var items = new vis.DataSet();
  60. for (var i = 0; i < itemCount; i++) {
  61. var start = now.clone().add(Math.random() * 200, 'hours');
  62. var end = Math.random() > 0.5 ? start.clone().add(24, 'hours') : undefined;
  63. var group = Math.floor(Math.random() * groupCount);
  64. items.add({
  65. id: i,
  66. group: group,
  67. content: 'item ' + i +
  68. ' <span style="color:#97B0F8;">(' + names[group] + ')</span>',
  69. start: start,
  70. end: end,
  71. title: 'Title for item ' + i,
  72. //type: 'box',
  73. className: 'myItem'
  74. });
  75. }
  76. // create visualization
  77. var container = document.getElementById('visualization');
  78. var options = {
  79. //orientation: 'top',
  80. editable: {
  81. add: true,
  82. remove: true,
  83. updateTime: true,
  84. updateGroup: true
  85. },
  86. // orientation: {
  87. // item: 'top',
  88. // axis: 'both'
  89. // },
  90. onAdd: function (item, callback) {
  91. item.content = prompt('Enter text content for new item:', item.content);
  92. if (item.content != null) {
  93. callback(item); // send back adjusted new item
  94. }
  95. else {
  96. callback(null); // cancel item creation
  97. }
  98. },
  99. onMove: function (item, callback) {
  100. if (confirm('Do you really want to move the item to\n' +
  101. 'start: ' + item.start + '\n' +
  102. 'end: ' + item.end + '?')) {
  103. callback(item); // send back item as confirmation (can be changed)
  104. }
  105. else {
  106. callback(null); // cancel editing item
  107. }
  108. },
  109. onMoving: function (item, callback) {
  110. var min = moment().minutes(0).seconds(0).milliseconds(0).add(-2, 'day').toDate();
  111. if (item.start < min) {
  112. item.start = min;
  113. }
  114. //item.className = item.id > 3 ? 'red' : 'green';
  115. //item.group = Math.random() > 0.5 ? 2 : 1;
  116. //item.hasMoved = true;
  117. //item.content = Math.round(Math.random() * 4);
  118. callback(item); // send back item as confirmation (can be changed)
  119. },
  120. onUpdate: function (item, callback) {
  121. item.content = prompt('Edit items text:', item.content);
  122. if (item.content != null) {
  123. callback(item); // send back adjusted item
  124. }
  125. else {
  126. callback(null); // cancel updating the item
  127. }
  128. },
  129. onRemove: function (item, callback) {
  130. if (confirm('Remove item ' + item.content + '?')) {
  131. callback(item); // confirm deletion
  132. }
  133. else {
  134. callback(null); // cancel deletion
  135. }
  136. },
  137. //stack: false,
  138. //height: 200,
  139. groupOrder: 'content'
  140. };
  141. console.time('create timeline');
  142. var timeline = new vis.Timeline(container);
  143. console.timeEnd('create timeline');
  144. console.time('set options');
  145. timeline.setOptions(options);
  146. console.timeEnd('set options');
  147. console.time('set groups');
  148. timeline.setGroups(groups);
  149. console.timeEnd('set groups');
  150. console.time('set items');
  151. timeline.setItems(items);
  152. console.timeEnd('set items');
  153. timeline.on('select', function (selection) {
  154. console.log('select', selection);
  155. });
  156. /*
  157. timeline.on('rangechange', function (range) {
  158. console.log('rangechange', range);
  159. });
  160. timeline.on('rangechanged', function (range) {
  161. console.log('rangechanged', range);
  162. });
  163. */
  164. timeline.on('click', function (props) {
  165. console.log('click', props);
  166. });
  167. timeline.on('doubleClick', function (props) {
  168. console.log('doubleClick', props);
  169. });
  170. timeline.on('contextmenu', function (props) {
  171. console.log('contextmenu', props);
  172. });
  173. items.on('add', console.log.bind(console));
  174. items.on('update', console.log.bind(console));
  175. items.on('remove', console.log.bind(console));
  176. groups.on('add', console.log.bind(console));
  177. groups.on('update', console.log.bind(console));
  178. groups.on('remove', console.log.bind(console));
  179. function log (msg) {
  180. var logs = document.getElementById('logs');
  181. logs.innerHTML = msg + '<br>' + logs.innerHTML;
  182. }
  183. </script>
  184. <div id="logs"></div>
  185. </body>
  186. </html>