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.

110 lines
3.7 KiB

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Timeline | Grid styling</title>
  5. <script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.min.js"></script>
  6. <script src="../../../dist/vis.js"></script>
  7. <link href="../../../dist/vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css"/>
  8. <style type="text/css">
  9. body, html {
  10. font-family: sans-serif;
  11. }
  12. /* alternating column backgrounds */
  13. .vis-time-axis .vis-grid.vis-odd {
  14. background: #f5f5f5;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <p>
  20. Week numbers are calculated based on locales. For this to properly work, the timeline must be loaded with a version
  21. of moment.js including locales.</p>
  22. <p>To set a locale for the timeline, specify the option
  23. <code>{locale: STRING}</code>.</p>
  24. <p>To set the scale to use week numbers, use for example <code>{scale: 'week', step: 1}</code>.</p>
  25. <p>The following timeline is initialized with the 'de' locale and items are added with locally localized moment.js
  26. objects. The timeline locale can be switched to another locale at runtime. If you choose the 'en' locale, the week
  27. numbers will be calculated according to the US week calendar numbering scheme.</p>
  28. <p>
  29. <label for="locale">Select a locale:</label>
  30. <select id="locale">
  31. <option value="en">en</option>
  32. <option value="it">it</option>
  33. <option value="nl">nl</option>
  34. <option value="de" selected>de</option>
  35. </select>
  36. </p>
  37. <div id="visualization"></div>
  38. <script type="text/javascript">
  39. var itemCount = 26;
  40. // DOM element where the Timeline will be attached
  41. var container = document.getElementById('visualization');
  42. // just a group for the effects
  43. var groups = new vis.DataSet();
  44. groups.add([{id: 1, content: "ISO Weeks"}, {id: 2, content: "US Weeks"}]);
  45. // Create a DataSet (allows two way data-binding)
  46. var items = new vis.DataSet();
  47. // create a localized moment object based on the current date
  48. var USdate = moment().locale('en').hours(0).minutes(0).seconds(0).milliseconds(0);
  49. // use the locale-aware weekday function to move to the begin of the current week
  50. USdate.weekday(0);
  51. // Iterate and just add a week to use it again in the next iteration
  52. for (var i = 0; i < itemCount; i++) {
  53. var USweekNumber = USdate.format('w');
  54. var USweekStart = USdate.format();
  55. var USweekEnd = USdate.add(1, 'week').format();
  56. items.add({
  57. id: i,
  58. group: 2,
  59. content: 'US week ' + USweekNumber,
  60. start: USweekStart,
  61. end: USweekEnd
  62. });
  63. }
  64. // create another localized moment object - the 'de' locale works according to the ISO8601 leap week calendar system
  65. var DEdate = moment().locale('de').hours(0).minutes(0).seconds(0).milliseconds(0);
  66. DEdate.weekday(0);
  67. for (var j = 0; j < itemCount; j++) {
  68. var DEweekNumber = DEdate.format('w');
  69. var DEweekStart = DEdate.format();
  70. var DEweekEnd = DEdate.add(1, 'week').format();
  71. items.add({
  72. id: itemCount + j,
  73. group: 1,
  74. content: 'ISO week ' + DEweekNumber,
  75. start: DEweekStart,
  76. end: DEweekEnd
  77. });
  78. }
  79. // Configuration for the Timeline
  80. var options = {timeAxis: {scale: 'week', step: 1}, locale: 'de'};
  81. // Create a Timeline
  82. var timeline = new vis.Timeline(container, items, groups, options);
  83. // update the locale when changing the select box value
  84. var select = document.getElementById('locale');
  85. select.onchange = function () {
  86. timeline.setOptions({
  87. locale: this.value
  88. });
  89. };
  90. select.onchange();
  91. </script>
  92. </body>
  93. </html>