123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <meta http-equiv="X-UA-Compatible" content="ie=edge" />
- <title>Dynamic Updating Line Chart</title>
- <link href="../../assets/styles.css" rel="stylesheet" />
- <style>
-
- #chart {
- max-width: 650px;
- margin: 35px auto;
- }
-
- </style>
- <script>
- window.Promise ||
- document.write(
- '<script src="https://cdn.jsdelivr.net/npm/promise-polyfill@8/dist/polyfill.min.js"><\/script>'
- )
- window.Promise ||
- document.write(
- '<script src="https://cdn.jsdelivr.net/npm/eligrey-classlist-js-polyfill@1.2.20171210/classList.min.js"><\/script>'
- )
- window.Promise ||
- document.write(
- '<script src="https://cdn.jsdelivr.net/npm/findindex_polyfill_mdn"><\/script>'
- )
- </script>
-
- <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.min.js"></script>
- <script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
- <script src="https://cdn.jsdelivr.net/npm/vue-apexcharts"></script>
-
- <script>
- var lastDate = 0;
- var data = []
- var TICKINTERVAL = 86400000
- let XAXISRANGE = 777600000
- function getDayWiseTimeSeries(baseval, count, yrange) {
- var i = 0;
- while (i < count) {
- var x = baseval;
- var y = Math.floor(Math.random() * (yrange.max - yrange.min + 1)) + yrange.min;
-
- data.push({
- x, y
- });
- lastDate = baseval
- baseval += TICKINTERVAL;
- i++;
- }
- }
-
- getDayWiseTimeSeries(new Date('11 Feb 2017 GMT').getTime(), 10, {
- min: 10,
- max: 90
- })
-
- function getNewSeries(baseval, yrange) {
- var newDate = baseval + TICKINTERVAL;
- lastDate = newDate
-
- for(var i = 0; i< data.length - 10; i++) {
- // IMPORTANT
- // we reset the x and y of the data which is out of drawing area
- // to prevent memory leaks
- data[i].x = newDate - XAXISRANGE - TICKINTERVAL
- data[i].y = 0
- }
-
- data.push({
- x: newDate,
- y: Math.floor(Math.random() * (yrange.max - yrange.min + 1)) + yrange.min
- })
- }
-
- function resetData(){
- // Alternatively, you can also reset the data at certain intervals to prevent creating a huge series
- data = data.slice(data.length - 10, data.length);
- }
- </script>
- </head>
- <body>
-
- <div id="app">
- <div id="chart">
- <apexchart type="line" height="350" ref="chart" :options="chartOptions" :series="series"></apexchart>
- </div>
- </div>
- <!-- Below element is just for displaying source code. it is not required. DO NOT USE -->
- <div id="html">
- <div id="chart">
- <apexchart type="line" height="350" ref="chart" :options="chartOptions" :series="series"></apexchart>
- </div>
- </div>
- <script>
- new Vue({
- el: '#app',
- components: {
- apexchart: VueApexCharts,
- },
- data: {
-
- series: [{
- data: data.slice()
- }],
- chartOptions: {
- chart: {
- id: 'realtime',
- height: 350,
- type: 'line',
- animations: {
- enabled: true,
- easing: 'linear',
- dynamicAnimation: {
- speed: 1000
- }
- },
- toolbar: {
- show: false
- },
- zoom: {
- enabled: false
- }
- },
- dataLabels: {
- enabled: false
- },
- stroke: {
- curve: 'smooth'
- },
- title: {
- text: 'Dynamic Updating Chart',
- align: 'left'
- },
- markers: {
- size: 0
- },
- xaxis: {
- type: 'datetime',
- range: XAXISRANGE,
- },
- yaxis: {
- max: 100
- },
- legend: {
- show: false
- },
- },
-
-
- },
- mounted: function () {
- var me = this
- window.setInterval(function () {
- getNewSeries(lastDate, {
- min: 10,
- max: 90
- })
-
- me.$refs.chart.updateSeries([{
- data: data
- }])
- }, 1000)
-
- // every 60 seconds, we reset the data to prevent memory leaks
- window.setInterval(function () {
- resetData()
-
- me.$refs.chart.updateSeries([{
- data
- }], false, true)
- }, 60000)
- },
- })
- </script>
-
- </body>
- </html>
|