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.

178 lines
4.5 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. </style>
  15. <!-- note: moment.js must be loaded before vis.js, else vis.js uses its embedded version of moment.js -->
  16. <script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.1/moment.min.js"></script>
  17. <script src="../dist/vis.js"></script>
  18. <link href="../dist/vis.css" rel="stylesheet" type="text/css" />
  19. </head>
  20. <body>
  21. <div>
  22. <label for="orientation">Orientation</label>
  23. <select id="orientation">
  24. <option value="top">top</option>
  25. <option value="bottom" selected>bottom</option>
  26. </select>
  27. </div>
  28. <script>
  29. var o = document.getElementById('orientation');
  30. o.onchange = function () {
  31. timeline.setOptions({
  32. orientation: o.value
  33. });
  34. };
  35. </script>
  36. <br>
  37. <div id="visualization"></div>
  38. <script>
  39. var now = moment().minutes(0).seconds(0).milliseconds(0);
  40. var groupCount = 3;
  41. var itemCount = 20;
  42. // create a data set with groups
  43. var names = ['John (0)', 'Alston (1)', 'Lee (2)', 'Grant (3)'];
  44. var groups = new vis.DataSet();
  45. for (var g = 0; g < groupCount; g++) {
  46. groups.add({id: g, content: names[g], title: 'Title of group ' + g, 'className': 'myGroup'});
  47. }
  48. // create a dataset with items
  49. var items = new vis.DataSet();
  50. for (var i = 0; i < itemCount; i++) {
  51. var start = now.clone().add(Math.random() * 200, 'hours');
  52. var group = Math.floor(Math.random() * groupCount);
  53. items.add({
  54. id: i,
  55. group: group,
  56. content: 'item ' + i +
  57. ' <span style="color:#97B0F8;">(' + names[group] + ')</span>',
  58. start: start,
  59. title: 'Title for item ' + i,
  60. type: 'box',
  61. className: 'myItem'
  62. });
  63. }
  64. // create visualization
  65. var container = document.getElementById('visualization');
  66. var options = {
  67. //orientation: 'top',
  68. editable: {
  69. add: true,
  70. remove: true,
  71. updateTime: true,
  72. updateGroup: true
  73. },
  74. onAdd: function (item, callback) {
  75. item.content = prompt('Enter text content for new item:', item.content);
  76. if (item.content != null) {
  77. callback(item); // send back adjusted new item
  78. }
  79. else {
  80. callback(null); // cancel item creation
  81. }
  82. },
  83. onMove: function (item, callback) {
  84. if (confirm('Do you really want to move the item to\n' +
  85. 'start: ' + item.start + '\n' +
  86. 'end: ' + item.end + '?')) {
  87. callback(item); // send back item as confirmation (can be changed)
  88. }
  89. else {
  90. callback(null); // cancel editing item
  91. }
  92. },
  93. onMoving: function (item, callback) {
  94. var min = moment().minutes(0).seconds(0).milliseconds(0).add(2, 'day').toDate();
  95. if (item.start < min) {
  96. item.start = min;
  97. }
  98. callback(item); // send back item as confirmation (can be changed)
  99. },
  100. onUpdate: function (item, callback) {
  101. item.content = prompt('Edit items text:', item.content);
  102. if (item.content != null) {
  103. callback(item); // send back adjusted item
  104. }
  105. else {
  106. callback(null); // cancel updating the item
  107. }
  108. },
  109. onRemove: function (item, callback) {
  110. if (confirm('Remove item ' + item.content + '?')) {
  111. callback(item); // confirm deletion
  112. }
  113. else {
  114. callback(null); // cancel deletion
  115. }
  116. },
  117. //stack: false,
  118. //height: 200,
  119. groupOrder: 'content'
  120. };
  121. console.time('create timeline');
  122. var timeline = new vis.Timeline(container);
  123. console.timeEnd('create timeline');
  124. console.time('set options');
  125. timeline.setOptions(options);
  126. console.timeEnd('set options');
  127. console.time('set groups');
  128. timeline.setGroups(groups);
  129. console.timeEnd('set groups');
  130. console.time('set items');
  131. timeline.setItems(items);
  132. console.timeEnd('set items');
  133. timeline.on('select', function (selection) {
  134. console.log('select', selection);
  135. });
  136. /*
  137. timeline.on('rangechange', function (range) {
  138. console.log('rangechange', range);
  139. });
  140. timeline.on('rangechanged', function (range) {
  141. console.log('rangechanged', range);
  142. });
  143. */
  144. items.on('add', console.log.bind(console));
  145. items.on('update', console.log.bind(console));
  146. items.on('remove', console.log.bind(console));
  147. groups.on('add', console.log.bind(console));
  148. groups.on('update', console.log.bind(console));
  149. groups.on('remove', console.log.bind(console));
  150. </script>
  151. </body>
  152. </html>