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. <script src="../../googleAnalytics.js"></script>
  21. </head>
  22. <body>
  23. <p>
  24. This example demonstrate using custom function label formats.
  25. </p>
  26. <div id="visualization"></div>
  27. <script>
  28. var now = moment().minutes(0).seconds(0).milliseconds(0);
  29. var groupCount = 3;
  30. var itemCount = 20;
  31. // create a data set with groups
  32. var names = ['John', 'Alston', 'Lee', 'Grant'];
  33. var groups = new vis.DataSet();
  34. for (var g = 0; g < groupCount; g++) {
  35. groups.add({id: g, content: names[g]});
  36. }
  37. // create a dataset with items
  38. var items = new vis.DataSet();
  39. for (var i = 0; i < itemCount; i++) {
  40. var start = now.clone().add(Math.random() * 200, 'hours');
  41. var group = Math.floor(Math.random() * groupCount);
  42. items.add({
  43. id: i,
  44. group: group,
  45. content: 'item ' + i +
  46. ' <span style="color:#97B0F8;">(' + names[group] + ')</span>',
  47. start: start,
  48. type: 'box'
  49. });
  50. }
  51. // create visualization
  52. var container = document.getElementById('visualization');
  53. var options = {
  54. format: {
  55. minorLabels: function(date, scale, step) {
  56. var now = new Date();
  57. var ago = now - date;
  58. var divider;
  59. switch (scale) {
  60. case 'millisecond':
  61. divider = 1;
  62. break;
  63. case 'second':
  64. divider = 1000;
  65. break;
  66. case 'minute':
  67. divider = 1000 * 60;
  68. break;
  69. case 'hour':
  70. divider = 1000 * 60 * 60;
  71. break;
  72. case 'day':
  73. divider = 1000 * 60 * 60 * 24;
  74. break;
  75. case 'weekday':
  76. divider = 1000 * 60 * 60 * 24 * 7;
  77. break;
  78. case 'month':
  79. divider = 1000 * 60 * 60 * 24 * 30;
  80. break;
  81. case 'year':
  82. divider = 1000 * 60 * 60 * 24 * 365;
  83. break;
  84. default:
  85. return new Date(date);
  86. }
  87. return (Math.round(ago * step / divider)) + " " + scale + "s ago"
  88. },
  89. majorLabels: function(date, scale, step) {
  90. var now = new Date();
  91. var ago = now - date;
  92. var divider;
  93. switch (scale) {
  94. case 'millisecond':
  95. divider = 1;
  96. break;
  97. case 'second':
  98. divider = 1000;
  99. break;
  100. case 'minute':
  101. divider = 1000 * 60;
  102. break;
  103. case 'hour':
  104. divider = 1000 * 60 * 60;
  105. break;
  106. case 'day':
  107. divider = 1000 * 60 * 60 * 24;
  108. break;
  109. case 'weekday':
  110. divider = 1000 * 60 * 60 * 24 * 7;
  111. break;
  112. case 'month':
  113. divider = 1000 * 60 * 60 * 24 * 30;
  114. break;
  115. case 'year':
  116. divider = 1000 * 60 * 60 * 24 * 365;
  117. break;
  118. default:
  119. return new Date(date);
  120. }
  121. return (Math.round(ago * step / divider)) + " " + scale + "s ago"
  122. }
  123. }
  124. };
  125. var timeline = new vis.Timeline(container);
  126. timeline.setOptions(options);
  127. timeline.setGroups(groups);
  128. timeline.setItems(items);
  129. </script>
  130. </body>
  131. </html>