realtime.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7. <title>Dynamic Updating Line Chart</title>
  8. <link href="../../assets/styles.css" rel="stylesheet" />
  9. <style>
  10. #chart {
  11. max-width: 650px;
  12. margin: 35px auto;
  13. }
  14. </style>
  15. <script>
  16. window.Promise ||
  17. document.write(
  18. '<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"><\/script>'
  19. )
  20. window.Promise ||
  21. document.write(
  22. '<script src="https://cdn.jsdelivr.net/npm/eligrey-classlist-js-polyfill@1.2.20171210/classList.min.js"><\/script>'
  23. )
  24. window.Promise ||
  25. document.write(
  26. '<script src="https://cdn.jsdelivr.net/npm/findindex_polyfill_mdn"><\/script>'
  27. )
  28. </script>
  29. <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
  30. <script>
  31. var lastDate = 0;
  32. var data = []
  33. var TICKINTERVAL = 86400000
  34. let XAXISRANGE = 777600000
  35. function getDayWiseTimeSeries(baseval, count, yrange) {
  36. var i = 0;
  37. while (i < count) {
  38. var x = baseval;
  39. var y = Math.floor(Math.random() * (yrange.max - yrange.min + 1)) + yrange.min;
  40. data.push({
  41. x, y
  42. });
  43. lastDate = baseval
  44. baseval += TICKINTERVAL;
  45. i++;
  46. }
  47. }
  48. getDayWiseTimeSeries(new Date('11 Feb 2017 GMT').getTime(), 10, {
  49. min: 10,
  50. max: 90
  51. })
  52. function getNewSeries(baseval, yrange) {
  53. var newDate = baseval + TICKINTERVAL;
  54. lastDate = newDate
  55. for(var i = 0; i< data.length - 10; i++) {
  56. // IMPORTANT
  57. // we reset the x and y of the data which is out of drawing area
  58. // to prevent memory leaks
  59. data[i].x = newDate - XAXISRANGE - TICKINTERVAL
  60. data[i].y = 0
  61. }
  62. data.push({
  63. x: newDate,
  64. y: Math.floor(Math.random() * (yrange.max - yrange.min + 1)) + yrange.min
  65. })
  66. }
  67. function resetData(){
  68. // Alternatively, you can also reset the data at certain intervals to prevent creating a huge series
  69. data = data.slice(data.length - 10, data.length);
  70. }
  71. </script>
  72. </head>
  73. <body>
  74. <div id="chart"></div>
  75. <script>
  76. var options = {
  77. series: [{
  78. data: data.slice()
  79. }],
  80. chart: {
  81. id: 'realtime',
  82. height: 350,
  83. type: 'line',
  84. animations: {
  85. enabled: true,
  86. easing: 'linear',
  87. dynamicAnimation: {
  88. speed: 1000
  89. }
  90. },
  91. toolbar: {
  92. show: false
  93. },
  94. zoom: {
  95. enabled: false
  96. }
  97. },
  98. dataLabels: {
  99. enabled: false
  100. },
  101. stroke: {
  102. curve: 'smooth'
  103. },
  104. title: {
  105. text: 'Dynamic Updating Chart',
  106. align: 'left'
  107. },
  108. markers: {
  109. size: 0
  110. },
  111. xaxis: {
  112. type: 'datetime',
  113. range: XAXISRANGE,
  114. },
  115. yaxis: {
  116. max: 100
  117. },
  118. legend: {
  119. show: false
  120. },
  121. };
  122. var chart = new ApexCharts(document.querySelector("#chart"), options);
  123. chart.render();
  124. window.setInterval(function () {
  125. getNewSeries(lastDate, {
  126. min: 10,
  127. max: 90
  128. })
  129. chart.updateSeries([{
  130. data: data
  131. }])
  132. }, 1000)
  133. </script>
  134. </body>
  135. </html>