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.

92 lines
3.0 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. </head>
  14. <body>
  15. <p>
  16. The Timeline has functions to add multiple custom time bars which can be dragged by the user.
  17. </p>
  18. <p>
  19. <input type="button" id="add" value="Add custom vertical bar">
  20. <input type="text" id="barId" placeholder="custom bar ID">
  21. <input type="checkbox" id="isEdit" placeholder="user can edit time" checked>
  22. <label for="isEdit">Editable: User can change time.</label>
  23. </input>
  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. document.getElementById('add').onclick = function () {
  50. try {
  51. customDate = new Date(customDate.getFullYear(), customDate.getMonth(), customDate.getDate() + 1);
  52. var barId = document.getElementById('barId').value || undefined;
  53. var usereditable = document.getElementById('isEdit').checked || false;
  54. timeline.addCustomTime(customDate, barId, {editable: usereditable});
  55. document.getElementById('barId').value = '';
  56. }
  57. catch (err) {
  58. console.log(err);
  59. alert(err);
  60. }
  61. };
  62. document.getElementById('remove').onclick = function () {
  63. try {
  64. timeline.removeCustomTime(document.getElementById('barIndex').value);
  65. document.getElementById('barIndex').value = '';
  66. }
  67. catch (err) {
  68. console.log(err);
  69. alert(err);
  70. }
  71. };
  72. timeline.on('timechange', function (properties) {
  73. document.getElementById('timechangeBar').innerHTML = properties.id;
  74. document.getElementById('timechangeEvent').innerHTML = properties.time;
  75. });
  76. timeline.on('timechanged', function (properties) {
  77. document.getElementById('timechangedBar').innerHTML = properties.id;
  78. document.getElementById('timechangedEvent').innerHTML = properties.time;
  79. });
  80. </script>
  81. </body>
  82. </html>