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.

140 lines
3.7 KiB

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Timeline | Custom function label format example</title>
  5. <style>
  6. body, html {
  7. font-family: arial, sans-serif;
  8. font-size: 11pt;
  9. }
  10. #visualization {
  11. box-sizing: border-box;
  12. width: 100%;
  13. height: 300px;
  14. }
  15. </style>
  16. <!-- note: moment.js must be loaded before vis.js, else vis.js uses its embedded version of moment.js -->
  17. <script src="http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.8.4/moment.min.js"></script>
  18. <script src="../../../dist/vis.js"></script>
  19. <link href="../../../dist/vis-timeline-graph2d.min.css" rel="stylesheet" type="text/css" />
  20. </head>
  21. <body>
  22. <p>
  23. This example demonstrate using custom function label formats.
  24. </p>
  25. <div id="visualization"></div>
  26. <script>
  27. var now = moment().minutes(0).seconds(0).milliseconds(0);
  28. var groupCount = 3;
  29. var itemCount = 20;
  30. // create a data set with groups
  31. var names = ['John', 'Alston', 'Lee', 'Grant'];
  32. var groups = new vis.DataSet();
  33. for (var g = 0; g < groupCount; g++) {
  34. groups.add({id: g, content: names[g]});
  35. }
  36. // create a dataset with items
  37. var items = new vis.DataSet();
  38. for (var i = 0; i < itemCount; i++) {
  39. var start = now.clone().add(Math.random() * 200, 'hours');
  40. var group = Math.floor(Math.random() * groupCount);
  41. items.add({
  42. id: i,
  43. group: group,
  44. content: 'item ' + i +
  45. ' <span style="color:#97B0F8;">(' + names[group] + ')</span>',
  46. start: start,
  47. type: 'box'
  48. });
  49. }
  50. // create visualization
  51. var container = document.getElementById('visualization');
  52. var options = {
  53. format: {
  54. minorLabels: function(date, scale, step) {
  55. var now = new Date();
  56. var ago = now - date;
  57. var divider;
  58. switch (scale) {
  59. case 'millisecond':
  60. divider = 1;
  61. break;
  62. case 'second':
  63. divider = 1000;
  64. break;
  65. case 'minute':
  66. divider = 1000 * 60;
  67. break;
  68. case 'hour':
  69. divider = 1000 * 60 * 60;
  70. break;
  71. case 'day':
  72. divider = 1000 * 60 * 60 * 24;
  73. break;
  74. case 'weekday':
  75. divider = 1000 * 60 * 60 * 24 * 7;
  76. break;
  77. case 'month':
  78. divider = 1000 * 60 * 60 * 24 * 30;
  79. break;
  80. case 'year':
  81. divider = 1000 * 60 * 60 * 24 * 365;
  82. break;
  83. default:
  84. return new Date(date);
  85. }
  86. return (Math.round(ago * step / divider)) + " " + scale + "s ago"
  87. },
  88. majorLabels: function(date, scale, step) {
  89. var now = new Date();
  90. var ago = now - date;
  91. var divider;
  92. switch (scale) {
  93. case 'millisecond':
  94. divider = 1;
  95. break;
  96. case 'second':
  97. divider = 1000;
  98. break;
  99. case 'minute':
  100. divider = 1000 * 60;
  101. break;
  102. case 'hour':
  103. divider = 1000 * 60 * 60;
  104. break;
  105. case 'day':
  106. divider = 1000 * 60 * 60 * 24;
  107. break;
  108. case 'weekday':
  109. divider = 1000 * 60 * 60 * 24 * 7;
  110. break;
  111. case 'month':
  112. divider = 1000 * 60 * 60 * 24 * 30;
  113. break;
  114. case 'year':
  115. divider = 1000 * 60 * 60 * 24 * 365;
  116. break;
  117. default:
  118. return new Date(date);
  119. }
  120. return (Math.round(ago * step / divider)) + " " + scale + "s ago"
  121. }
  122. }
  123. };
  124. var timeline = new vis.Timeline(container);
  125. timeline.setOptions(options);
  126. timeline.setGroups(groups);
  127. timeline.setItems(items);
  128. </script>
  129. </body>
  130. </html>