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.

93 lines
2.3 KiB

  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  5. <meta content="utf-8" http-equiv="encoding">
  6. <title>Graph2d | Streaming data</title>
  7. <style type="text/css">
  8. body, html, select {
  9. font: 10pt sans-serif;
  10. }
  11. </style>
  12. <script src="../../dist/vis.js"></script>
  13. <link href="../../dist/vis.css" rel="stylesheet" type="text/css" />
  14. </head>
  15. <body>
  16. <h2>Graph2d | Streaming data</h2>
  17. <p>
  18. This example demonstrates how to apply streaming data input to the Graph2d.<br>
  19. </p>
  20. <p>
  21. <label for="strategy">Strategy:</label>
  22. <select id="strategy">
  23. <option value="continuous" selected>Continuous</option>
  24. <option value="discrete">Discrete</option>
  25. </select>
  26. </p>
  27. <div id="visualization"></div>
  28. <script type="text/javascript">
  29. var strategy = document.getElementById('strategy');
  30. // create a graph2d with an (currently empty) dataset
  31. var container = document.getElementById('visualization');
  32. var dataset = new vis.DataSet();
  33. var options = {
  34. start: vis.moment().add(-60, 'seconds'),
  35. end: vis.moment()
  36. };
  37. var graph2d = new vis.Graph2d(container, dataset, options);
  38. // a function to generate data points
  39. function y(x) {
  40. return (Math.sin(x / 2) + Math.cos(x / 4)) * 5;
  41. }
  42. var delay = 500; // delay in ms
  43. var x = 0;
  44. function next() {
  45. var now = vis.moment();
  46. // add a new data point to the data
  47. dataset.add({
  48. x: now,
  49. y: y(now / 1000)
  50. });
  51. x += 0.1;
  52. // move the window (you can think of different strategies).
  53. var range = graph2d.getWindow();
  54. var interval = range.end - range.start;
  55. switch (strategy.value) {
  56. case 'continuous':
  57. // continuously move the window
  58. graph2d.setWindow(now - interval, now, {animate: false});
  59. break;
  60. default: // 'discrete'
  61. // move the window 90% to the left when now is larger than the visible end
  62. if (now > range.end) {
  63. graph2d.setWindow(now - 0.1 * interval, now + 0.9 * interval);
  64. }
  65. break;
  66. }
  67. // remove all data points older than 1 minute
  68. var min = now.add(-60, 'seconds');
  69. var oldIds = dataset.getIds({
  70. filter: function (item) {
  71. return item.x < min;
  72. }
  73. });
  74. if (oldIds.length > 0) dataset.remove(oldIds);
  75. setTimeout(next, delay);
  76. }
  77. next();
  78. </script>
  79. </body>
  80. </html>