realtime.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/react@16.12/umd/react.production.min.js"></script>
  30. <script src="https://cdn.jsdelivr.net/npm/react-dom@16.12/umd/react-dom.production.min.js"></script>
  31. <script src="https://cdn.jsdelivr.net/npm/prop-types@15.7.2/prop-types.min.js"></script>
  32. <script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.34/browser.min.js"></script>
  33. <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
  34. <script src="https://cdn.jsdelivr.net/npm/react-apexcharts@1.3.6/dist/react-apexcharts.iife.min.js"></script>
  35. <script>
  36. var lastDate = 0;
  37. var data = []
  38. var TICKINTERVAL = 86400000
  39. let XAXISRANGE = 777600000
  40. function getDayWiseTimeSeries(baseval, count, yrange) {
  41. var i = 0;
  42. while (i < count) {
  43. var x = baseval;
  44. var y = Math.floor(Math.random() * (yrange.max - yrange.min + 1)) + yrange.min;
  45. data.push({
  46. x, y
  47. });
  48. lastDate = baseval
  49. baseval += TICKINTERVAL;
  50. i++;
  51. }
  52. }
  53. getDayWiseTimeSeries(new Date('11 Feb 2017 GMT').getTime(), 10, {
  54. min: 10,
  55. max: 90
  56. })
  57. function getNewSeries(baseval, yrange) {
  58. var newDate = baseval + TICKINTERVAL;
  59. lastDate = newDate
  60. for(var i = 0; i< data.length - 10; i++) {
  61. // IMPORTANT
  62. // we reset the x and y of the data which is out of drawing area
  63. // to prevent memory leaks
  64. data[i].x = newDate - XAXISRANGE - TICKINTERVAL
  65. data[i].y = 0
  66. }
  67. data.push({
  68. x: newDate,
  69. y: Math.floor(Math.random() * (yrange.max - yrange.min + 1)) + yrange.min
  70. })
  71. }
  72. function resetData(){
  73. // Alternatively, you can also reset the data at certain intervals to prevent creating a huge series
  74. data = data.slice(data.length - 10, data.length);
  75. }
  76. </script>
  77. </head>
  78. <body>
  79. <div id="app"></div>
  80. <div id="html">
  81. &lt;div id=&quot;chart&quot;&gt;
  82. &lt;ReactApexChart options={this.state.options} series={this.state.series} type=&quot;line&quot; height={350} /&gt;
  83. &lt;/div&gt;
  84. </div>
  85. <script type="text/babel">
  86. class ApexChart extends React.Component {
  87. constructor(props) {
  88. super(props);
  89. this.state = {
  90. series: [{
  91. data: data.slice()
  92. }],
  93. options: {
  94. chart: {
  95. id: 'realtime',
  96. height: 350,
  97. type: 'line',
  98. animations: {
  99. enabled: true,
  100. easing: 'linear',
  101. dynamicAnimation: {
  102. speed: 1000
  103. }
  104. },
  105. toolbar: {
  106. show: false
  107. },
  108. zoom: {
  109. enabled: false
  110. }
  111. },
  112. dataLabels: {
  113. enabled: false
  114. },
  115. stroke: {
  116. curve: 'smooth'
  117. },
  118. title: {
  119. text: 'Dynamic Updating Chart',
  120. align: 'left'
  121. },
  122. markers: {
  123. size: 0
  124. },
  125. xaxis: {
  126. type: 'datetime',
  127. range: XAXISRANGE,
  128. },
  129. yaxis: {
  130. max: 100
  131. },
  132. legend: {
  133. show: false
  134. },
  135. },
  136. };
  137. }
  138. componentDidMount() {
  139. window.setInterval(() => {
  140. getNewSeries(lastDate, {
  141. min: 10,
  142. max: 90
  143. })
  144. ApexCharts.exec('realtime', 'updateSeries', [{
  145. data: data
  146. }])
  147. }, 1000)
  148. }
  149. render() {
  150. return (
  151. <div>
  152. <div id="chart">
  153. <ReactApexChart options={this.state.options} series={this.state.series} type="line" height={350} />
  154. </div>
  155. <div id="html-dist"></div>
  156. </div>
  157. );
  158. }
  159. }
  160. const domContainer = document.querySelector('#app');
  161. ReactDOM.render(React.createElement(ApexChart), domContainer);
  162. </script>
  163. </body>
  164. </html>