realtime.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*=========================================================================================
  2. File Name: realtime.js
  3. Description: Flot realtime chart
  4. ----------------------------------------------------------------------------------------
  5. Item Name: Robust - Responsive Admin Theme
  6. Version: 1.2
  7. Author: PIXINVENT
  8. Author URL: http://www.themeforest.net/user/pixinvent
  9. ==========================================================================================*/
  10. // Realtime chart
  11. // ------------------------------
  12. $(window).on("load", function(){
  13. // We use an inline data source in the example, usually data would
  14. // be fetched from a server
  15. var data = [],
  16. totalPoints = 300;
  17. function getRandomData() {
  18. if (data.length > 0)
  19. data = data.slice(1);
  20. // Do a random walk
  21. while (data.length < totalPoints) {
  22. var prev = data.length > 0 ? data[data.length - 1] : 50,
  23. y = prev + Math.random() * 10 - 5;
  24. if (y < 0) {
  25. y = 0;
  26. } else if (y > 100) {
  27. y = 100;
  28. }
  29. data.push(y);
  30. }
  31. // Zip the generated y values with the x values
  32. var res = [];
  33. for (var i = 0; i < data.length; ++i) {
  34. res.push([i, data[i]])
  35. }
  36. return res;
  37. }
  38. // Set up the control widget
  39. var updateInterval = 30;
  40. $("#updateInterval").val(updateInterval).change(function () {
  41. var v = $(this).val();
  42. if (v && !isNaN(+v)) {
  43. updateInterval = +v;
  44. if (updateInterval < 1) {
  45. updateInterval = 1;
  46. } else if (updateInterval > 2000) {
  47. updateInterval = 2000;
  48. }
  49. $(this).val("" + updateInterval);
  50. }
  51. });
  52. var plot = $.plot("#realtime", [ getRandomData() ], {
  53. grid: {
  54. borderWidth: 1,
  55. borderColor: "transparent",
  56. color: '#999',
  57. minBorderMargin: 20,
  58. labelMargin: 10,
  59. margin: {
  60. top: 8,
  61. bottom: 20,
  62. left: 20
  63. },
  64. },
  65. series: {
  66. shadowSize: 0, // Drawing is faster without shadows
  67. lines: {
  68. fill: true,
  69. fillColor: { colors: [ { opacity: 0.8 }, { opacity: 0.1 } ] }
  70. }
  71. },
  72. yaxis: {
  73. min: 0,
  74. max: 100
  75. },
  76. xaxis: {
  77. show: false
  78. },
  79. colors: ['#ffc107']
  80. });
  81. function update() {
  82. plot.setData([getRandomData()]);
  83. // Since the axes don't change, we don't need to call plot.setupGrid()
  84. plot.draw();
  85. setTimeout(update, updateInterval);
  86. }
  87. update();
  88. });