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.

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