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.

97 lines
3.2 KiB

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Timeline | Show current and custom time bars</title>
  5. <style type="text/css">
  6. body, html {
  7. font-family: 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. <script src="../../googleAnalytics.js"></script>
  14. </head>
  15. <body>
  16. <p>
  17. The Timeline has functions to add multiple custom time bars which can be dragged by the user.
  18. </p>
  19. <p>
  20. <input type="button" id="add" value="Add custom vertical bar">
  21. <input type="text" id="barId" placeholder="custom bar ID">
  22. <input type="checkbox" id="isEdit" placeholder="user can edit time" checked>
  23. <label for="isEdit">Editable: User can change time.</label>
  24. </p>
  25. <p>
  26. <input type="button" id="remove" value="Remove custom vertical bar">
  27. <input type="text" id="barIndex" value="t1" placeholder="custom bar ID">
  28. </p>
  29. <p>
  30. <code><strong>timechange</strong></code> event, index: <span id="timechangeBar"></span>, time: <span id="timechangeEvent"></span>
  31. </p>
  32. <p>
  33. <code><strong>timechanged</strong></code> event, index: <span id="timechangedBar"></span>, time: <span id="timechangedEvent"></span>
  34. </p><br>
  35. <div id="visualization"></div>
  36. <script type="text/javascript">
  37. var container = document.getElementById('visualization');
  38. var items = new vis.DataSet();
  39. var customDate = new Date();
  40. var options = {
  41. showCurrentTime: true,
  42. start: new Date(Date.now() - 1000 * 60 * 60 * 24),
  43. end: new Date(Date.now() + 1000 * 60 * 60 * 24 * 6)
  44. };
  45. var timeline = new vis.Timeline(container, items, options);
  46. // Set first time bar
  47. customDate = new Date(customDate.getFullYear(), customDate.getMonth(), customDate.getDate() + 1);
  48. timeline.addCustomTime(customDate, 't1');
  49. timeline.setCustomTimeTitle(function(time){
  50. return "I'm t1!";
  51. }, "t1");
  52. document.getElementById('add').onclick = function () {
  53. try {
  54. customDate = new Date(customDate.getFullYear(), customDate.getMonth(), customDate.getDate() + 1);
  55. var barId = document.getElementById('barId').value || undefined;
  56. var usereditable = document.getElementById('isEdit').checked || false;
  57. timeline.addCustomTime(customDate, barId, { editable: usereditable });
  58. timeline.setCustomTimeTitle(function(time){
  59. return "I'm "+barId+"!";
  60. }, barId);
  61. document.getElementById('barId').value = '';
  62. }
  63. catch (err) {
  64. console.log(err);
  65. alert(err);
  66. }
  67. };
  68. document.getElementById('remove').onclick = function () {
  69. try {
  70. timeline.removeCustomTime(document.getElementById('barIndex').value);
  71. document.getElementById('barIndex').value = '';
  72. }
  73. catch (err) {
  74. console.log(err);
  75. alert(err);
  76. }
  77. };
  78. timeline.on('timechange', function (properties) {
  79. document.getElementById('timechangeBar').innerHTML = properties.id;
  80. document.getElementById('timechangeEvent').innerHTML = properties.time;
  81. });
  82. timeline.on('timechanged', function (properties) {
  83. document.getElementById('timechangedBar').innerHTML = properties.id;
  84. document.getElementById('timechangedEvent').innerHTML = properties.time;
  85. });
  86. </script>
  87. </body>
  88. </html>