realtime.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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/vue/dist/vue.min.js"></script>
  30. <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
  31. <script src="https://cdn.jsdelivr.net/npm/vue-apexcharts"></script>
  32. <script>
  33. var lastDate = 0;
  34. var data = []
  35. var TICKINTERVAL = 86400000
  36. let XAXISRANGE = 777600000
  37. function getDayWiseTimeSeries(baseval, count, yrange) {
  38. var i = 0;
  39. while (i < count) {
  40. var x = baseval;
  41. var y = Math.floor(Math.random() * (yrange.max - yrange.min + 1)) + yrange.min;
  42. data.push({
  43. x, y
  44. });
  45. lastDate = baseval
  46. baseval += TICKINTERVAL;
  47. i++;
  48. }
  49. }
  50. getDayWiseTimeSeries(new Date('11 Feb 2017 GMT').getTime(), 10, {
  51. min: 10,
  52. max: 90
  53. })
  54. function getNewSeries(baseval, yrange) {
  55. var newDate = baseval + TICKINTERVAL;
  56. lastDate = newDate
  57. for(var i = 0; i< data.length - 10; i++) {
  58. // IMPORTANT
  59. // we reset the x and y of the data which is out of drawing area
  60. // to prevent memory leaks
  61. data[i].x = newDate - XAXISRANGE - TICKINTERVAL
  62. data[i].y = 0
  63. }
  64. data.push({
  65. x: newDate,
  66. y: Math.floor(Math.random() * (yrange.max - yrange.min + 1)) + yrange.min
  67. })
  68. }
  69. function resetData(){
  70. // Alternatively, you can also reset the data at certain intervals to prevent creating a huge series
  71. data = data.slice(data.length - 10, data.length);
  72. }
  73. </script>
  74. </head>
  75. <body>
  76. <div id="app">
  77. <div id="chart">
  78. <apexchart type="line" height="350" ref="chart" :options="chartOptions" :series="series"></apexchart>
  79. </div>
  80. </div>
  81. <!-- Below element is just for displaying source code. it is not required. DO NOT USE -->
  82. <div id="html">
  83. &lt;div id=&quot;chart&quot;&gt;
  84. &lt;apexchart type=&quot;line&quot; height=&quot;350&quot; ref=&quot;chart&quot; :options=&quot;chartOptions&quot; :series=&quot;series&quot;&gt;&lt;/apexchart&gt;
  85. &lt;/div&gt;
  86. </div>
  87. <script>
  88. new Vue({
  89. el: '#app',
  90. components: {
  91. apexchart: VueApexCharts,
  92. },
  93. data: {
  94. series: [{
  95. data: data.slice()
  96. }],
  97. chartOptions: {
  98. chart: {
  99. id: 'realtime',
  100. height: 350,
  101. type: 'line',
  102. animations: {
  103. enabled: true,
  104. easing: 'linear',
  105. dynamicAnimation: {
  106. speed: 1000
  107. }
  108. },
  109. toolbar: {
  110. show: false
  111. },
  112. zoom: {
  113. enabled: false
  114. }
  115. },
  116. dataLabels: {
  117. enabled: false
  118. },
  119. stroke: {
  120. curve: 'smooth'
  121. },
  122. title: {
  123. text: 'Dynamic Updating Chart',
  124. align: 'left'
  125. },
  126. markers: {
  127. size: 0
  128. },
  129. xaxis: {
  130. type: 'datetime',
  131. range: XAXISRANGE,
  132. },
  133. yaxis: {
  134. max: 100
  135. },
  136. legend: {
  137. show: false
  138. },
  139. },
  140. },
  141. mounted: function () {
  142. var me = this
  143. window.setInterval(function () {
  144. getNewSeries(lastDate, {
  145. min: 10,
  146. max: 90
  147. })
  148. me.$refs.chart.updateSeries([{
  149. data: data
  150. }])
  151. }, 1000)
  152. // every 60 seconds, we reset the data to prevent memory leaks
  153. window.setInterval(function () {
  154. resetData()
  155. me.$refs.chart.updateSeries([{
  156. data
  157. }], false, true)
  158. }, 60000)
  159. },
  160. })
  161. </script>
  162. </body>
  163. </html>