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.

121 lines
3.7 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. <script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', 'UA-61231638-1', 'auto');ga('send', 'pageview');</script></head>
  15. <body>
  16. <h2>Graph2d | Streaming data</h2>
  17. <p style="max-width: 700px;">
  18. This example demonstrates how to apply streaming data input to the Graph2d. The example shows two different ways to let the window move along with the new data, and there are more strategies for that. Note also that it is possible to disable moving and/or zooming the graph by setting options <code>moveable</code> and <code>zoomable</code> false.
  19. </p>
  20. <p>
  21. <label for="strategy">Strategy:</label>
  22. <select id="strategy">
  23. <option value="continuous" selected>Continuous (CPU intensive)</option>
  24. <option value="discrete">Discrete</option>
  25. <option value="static">Static</option>
  26. </select>
  27. </p>
  28. <div id="visualization"></div>
  29. <script type="text/javascript">
  30. var DELAY = 1000; // delay in ms to add new data points
  31. var strategy = document.getElementById('strategy');
  32. // create a graph2d with an (currently empty) dataset
  33. var container = document.getElementById('visualization');
  34. var dataset = new vis.DataSet();
  35. var options = {
  36. start: vis.moment().add(-30, 'seconds'), // changed so its faster
  37. end: vis.moment(),
  38. dataAxis: {
  39. left: {
  40. range: {
  41. min:-10, max: 10
  42. }
  43. }
  44. },
  45. drawPoints: {
  46. style: 'circle' // square, circle
  47. },
  48. shaded: {
  49. orientation: 'bottom' // top, bottom
  50. }
  51. };
  52. var graph2d = new vis.Graph2d(container, dataset, options);
  53. // a function to generate data points
  54. function y(x) {
  55. return (Math.sin(x / 2) + Math.cos(x / 4)) * 5;
  56. }
  57. function renderStep() {
  58. // move the window (you can think of different strategies).
  59. var now = vis.moment();
  60. var range = graph2d.getWindow();
  61. var interval = range.end - range.start;
  62. switch (strategy.value) {
  63. case 'continuous':
  64. // continuously move the window
  65. graph2d.setWindow(now - interval, now, {animation: false});
  66. requestAnimationFrame(renderStep);
  67. break;
  68. case 'discrete':
  69. graph2d.setWindow(now - interval, now, {animation: false});
  70. setTimeout(renderStep, DELAY);
  71. break;
  72. default: // 'static'
  73. // move the window 90% to the left when now is larger than the end of the window
  74. if (now > range.end) {
  75. graph2d.setWindow(now - 0.1 * interval, now + 0.9 * interval);
  76. }
  77. setTimeout(renderStep, DELAY);
  78. break;
  79. }
  80. }
  81. renderStep();
  82. /**
  83. * Add a new datapoint to the graph
  84. */
  85. function addDataPoint() {
  86. // add a new data point to the dataset
  87. var now = vis.moment();
  88. dataset.add({
  89. x: now,
  90. y: y(now / 1000)
  91. });
  92. // remove all data points which are no longer visible
  93. var range = graph2d.getWindow();
  94. var interval = range.end - range.start;
  95. var oldIds = dataset.getIds({
  96. filter: function (item) {
  97. return item.x < range.start - interval;
  98. }
  99. });
  100. dataset.remove(oldIds);
  101. setTimeout(addDataPoint, DELAY);
  102. }
  103. addDataPoint();
  104. </script>
  105. </body>
  106. </html>