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.

88 lines
2.7 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. </p>
  22. <p>
  23. <input type="button" id="remove" value="Remove custom vertical bar">
  24. <input type="text" id="barIndex" value="t1" placeholder="custom bar ID">
  25. </p>
  26. <p>
  27. <code><strong>timechange</strong></code> event, index: <span id="timechangeBar"></span>, time: <span id="timechangeEvent"></span>
  28. </p>
  29. <p>
  30. <code><strong>timechanged</strong></code> event, index: <span id="timechangedBar"></span>, time: <span id="timechangedEvent"></span>
  31. </p><br>
  32. <div id="visualization"></div>
  33. <script type="text/javascript">
  34. var container = document.getElementById('visualization');
  35. var items = new vis.DataSet();
  36. var customDate = new Date();
  37. var options = {
  38. showCurrentTime: true,
  39. start: new Date(Date.now() - 1000 * 60 * 60 * 24),
  40. end: new Date(Date.now() + 1000 * 60 * 60 * 24 * 6)
  41. };
  42. var timeline = new vis.Timeline(container, items, options);
  43. // Set first time bar
  44. customDate = new Date(customDate.getFullYear(), customDate.getMonth(), customDate.getDate() + 1);
  45. timeline.addCustomTime(customDate, 't1');
  46. document.getElementById('add').onclick = function () {
  47. try {
  48. customDate = new Date(customDate.getFullYear(), customDate.getMonth(), customDate.getDate() + 1);
  49. var barId = document.getElementById('barId').value || undefined;
  50. timeline.addCustomTime(customDate, barId);
  51. document.getElementById('barId').value = '';
  52. }
  53. catch (err) {
  54. console.log(err);
  55. alert(err);
  56. }
  57. };
  58. document.getElementById('remove').onclick = function () {
  59. try {
  60. timeline.removeCustomTime(document.getElementById('barIndex').value);
  61. document.getElementById('barIndex').value = '';
  62. }
  63. catch (err) {
  64. console.log(err);
  65. alert(err);
  66. }
  67. };
  68. timeline.on('timechange', function (properties) {
  69. document.getElementById('timechangeBar').innerHTML = properties.id;
  70. document.getElementById('timechangeEvent').innerHTML = properties.time;
  71. });
  72. timeline.on('timechanged', function (properties) {
  73. document.getElementById('timechangedBar').innerHTML = properties.id;
  74. document.getElementById('timechangedEvent').innerHTML = properties.time;
  75. });
  76. </script>
  77. </body>
  78. </html>