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.

146 lines
3.9 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 'quarter':
  81. divider = 1000 * 60 * 60 * 24 * 30 * 3;
  82. break;
  83. case 'year':
  84. divider = 1000 * 60 * 60 * 24 * 365;
  85. break;
  86. default:
  87. return new Date(date);
  88. }
  89. return (Math.round(ago * step / divider)) + " " + scale + "s ago"
  90. },
  91. majorLabels: function(date, scale, step) {
  92. var now = new Date();
  93. var ago = now - date;
  94. var divider;
  95. switch (scale) {
  96. case 'millisecond':
  97. divider = 1;
  98. break;
  99. case 'second':
  100. divider = 1000;
  101. break;
  102. case 'minute':
  103. divider = 1000 * 60;
  104. break;
  105. case 'hour':
  106. divider = 1000 * 60 * 60;
  107. break;
  108. case 'day':
  109. divider = 1000 * 60 * 60 * 24;
  110. break;
  111. case 'weekday':
  112. divider = 1000 * 60 * 60 * 24 * 7;
  113. break;
  114. case 'month':
  115. divider = 1000 * 60 * 60 * 24 * 30;
  116. break;
  117. case 'quarter':
  118. divider = 1000 * 60 * 60 * 24 * 30 * 3;
  119. break;
  120. case 'year':
  121. divider = 1000 * 60 * 60 * 24 * 365;
  122. break;
  123. default:
  124. return new Date(date);
  125. }
  126. return (Math.round(ago * step / divider)) + " " + scale + "s ago"
  127. }
  128. }
  129. };
  130. var timeline = new vis.Timeline(container);
  131. timeline.setOptions(options);
  132. timeline.setGroups(groups);
  133. timeline.setItems(items);
  134. </script>
  135. </body>
  136. </html>