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.

230 lines
6.2 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="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.js"></script>
  26. <script src="../dist/vis.js"></script>
  27. <link href="../dist/vis.css" rel="stylesheet" type="text/css" />
  28. <script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-61231638-1', 'auto');ga('send', 'pageview');</script></head>
  29. <body>
  30. <div>
  31. <label for="orientation">Orientation</label>
  32. <select id="orientation">
  33. <option value="both">both</option>
  34. <option value="bottom" selected>bottom</option>
  35. <option value="top">top</option>
  36. </select>
  37. </div>
  38. <script>
  39. var o = document.getElementById('orientation');
  40. o.onchange = function () {
  41. timeline.setOptions({
  42. orientation: o.value
  43. });
  44. };
  45. </script>
  46. <br>
  47. <div id="visualization"></div>
  48. <div style="height: 1000px;"></div><!-- for testing vertical scroll on touch devices-->
  49. <script>
  50. var moment = vis.moment;
  51. var now = moment().minutes(0).seconds(0).milliseconds(0);
  52. var groupCount = 3;
  53. var itemCount = 20;
  54. // create a data set with groups
  55. var names = ['John (0)', 'Alston (1)', 'Lee (2)', 'Grant (3)'];
  56. var groups = new vis.DataSet();
  57. for (var g = 0; g < groupCount; g++) {
  58. groups.add({id: g, content: '<a href="http://google.com"><span style="color:#97B0F8;">' + names[g] + '</a>', title: 'Title of group ' + g, 'className': 'myGroup'});
  59. }
  60. // create a dataset with items
  61. var items = new vis.DataSet({
  62. type: {start: 'Moment', end: 'Moment'}
  63. });
  64. for (var i = 0; i < itemCount; i++) {
  65. var start = now.clone().add(Math.random() * 200, 'hours');
  66. var end = Math.random() > 0.5 ? start.clone().add(24, 'hours') : undefined;
  67. var group = Math.floor(Math.random() * groupCount);
  68. items.add({
  69. id: i,
  70. group: group,
  71. content: 'item ' + i +
  72. ' <a href="http://google.com"><span style="color:#97B0F8;">(' + names[group] + ')</span></a> ',
  73. start: start,
  74. end: end,
  75. title: 'Title for item ' + i,
  76. //type: 'box',
  77. className: 'myItem'
  78. });
  79. }
  80. // create visualization
  81. var container = document.getElementById('visualization');
  82. var options = {
  83. //orientation: 'top',
  84. multiselect: true,
  85. editable: {
  86. add: true,
  87. remove: true,
  88. updateTime: true,
  89. updateGroup: true
  90. },
  91. // orientation: {
  92. // item: 'top',
  93. // axis: 'both'
  94. // },
  95. onAdd: function (item, callback) {
  96. item.content = prompt('Enter text content for new item:', item.content);
  97. if (item.content != null) {
  98. callback(item); // send back adjusted new item
  99. }
  100. else {
  101. callback(null); // cancel item creation
  102. }
  103. },
  104. onMove: function (item, callback) {
  105. console.log('onMove', item)
  106. if (confirm('Do you really want to move the item to\n' +
  107. 'start: ' + item.start + '\n' +
  108. 'end: ' + item.end + '?')) {
  109. callback(item); // send back item as confirmation (can be changed)
  110. }
  111. else {
  112. callback(null); // cancel editing item
  113. }
  114. },
  115. onMoving: function (item, callback) {
  116. var min = moment().minutes(0).seconds(0).milliseconds(0).add(-2, 'day').toDate();
  117. if (item.start < min) {
  118. item.start = min;
  119. }
  120. //item.className = item.id > 3 ? 'red' : 'green';
  121. //item.group = Math.random() > 0.5 ? 2 : 1;
  122. //item.hasMoved = true;
  123. //item.content = Math.round(Math.random() * 4);
  124. callback(item); // send back item as confirmation (can be changed)
  125. },
  126. onUpdate: function (item, callback) {
  127. item.content = prompt('Edit items text:', item.content);
  128. if (item.content != null) {
  129. callback(item); // send back adjusted item
  130. }
  131. else {
  132. callback(null); // cancel updating the item
  133. }
  134. },
  135. onRemove: function (item, callback) {
  136. if (confirm('Remove item ' + item.content + '?')) {
  137. callback(item); // confirm deletion
  138. }
  139. else {
  140. callback(null); // cancel deletion
  141. }
  142. },
  143. //stack: false,
  144. //height: 200,
  145. groupOrder: 'content'
  146. };
  147. console.time('create timeline');
  148. var timeline = new vis.Timeline(container);
  149. console.timeEnd('create timeline');
  150. console.time('set options');
  151. timeline.setOptions(options);
  152. console.timeEnd('set options');
  153. console.time('set groups');
  154. timeline.setGroups(groups);
  155. console.timeEnd('set groups');
  156. console.time('set items');
  157. timeline.setItems(items);
  158. console.timeEnd('set items');
  159. timeline.on('select', function (selection) {
  160. console.log('select', selection);
  161. });
  162. /*
  163. timeline.on('rangechange', function (range) {
  164. console.log('rangechange', range);
  165. });
  166. timeline.on('rangechanged', function (range) {
  167. console.log('rangechanged', range);
  168. });
  169. */
  170. //
  171. timeline.on('click', function (props) {
  172. console.log('click', props);
  173. });
  174. $(container).click(function (event) {
  175. console.log('click jquery', timeline.getEventProperties(event));
  176. });
  177. timeline.on('doubleClick', function (props) {
  178. console.log('doubleClick', props);
  179. });
  180. timeline.on('contextmenu', function (props) {
  181. console.log('contextmenu', props);
  182. });
  183. items.on('add', console.log.bind(console));
  184. items.on('update', console.log.bind(console));
  185. items.on('remove', console.log.bind(console));
  186. groups.on('add', console.log.bind(console));
  187. groups.on('update', console.log.bind(console));
  188. groups.on('remove', console.log.bind(console));
  189. function log (msg) {
  190. var logs = document.getElementById('logs');
  191. logs.innerHTML = msg + '<br>' + logs.innerHTML;
  192. }
  193. </script>
  194. <div id="logs"></div>
  195. </body>
  196. </html>