Realtime in Vue

Using ApexCharts with Vue

This Realtime example wires ApexCharts into Vue through the vue3-apexcharts component.

Install it with npm install vue3-apexcharts apexcharts, then mount the chart with <apexchart type="..." :options="options" :series="series" />.

Vue charts guide
<template>
  <div>
    <div id="chart">
      <apexchart
        type="line"
        height="350"
        ref="chart"
        :options="chartOptions"
        :series="series"
      ></apexchart>
    </div>

    <div class="explanation">
      Real-time streaming with <code>chart.streaming.enabled</code>: each tick
      appends one point with <code>appendData()</code>. The window slides left
      at constant velocity (no easing pulse, no in-place warp), and the series
      array is trimmed automatically to the visible
      <code>xaxis.range</code> window, so memory stays flat no matter how long
      the stream runs. The same scroll works when a fixed-length rolling window
      is passed to <code>updateSeries()</code>.
    </div>
  </div>
</template>

<script>
import VueApexCharts from 'vue-apexcharts'

var TICKINTERVAL = 1000
var XAXISRANGE = 29 * TICKINTERVAL
var lastX = new Date().getTime() - XAXISRANGE
var lastY = 50
var data = []

function nextValue() {
  lastY = Math.max(5, Math.min(95, lastY + (Math.random() - 0.5) * 20))
  return Math.round(lastY)
}

while (data.length < 30) {
  data.push({ x: lastX, y: nextValue() })
  lastX += TICKINTERVAL
}

export default {
  components: {
    apexchart: VueApexCharts,
  },
  data: function () {
    return {
      series: [
        {
          name: 'Signal',
          data: data.slice(),
        },
      ],
      chartOptions: {
        chart: {
          id: 'realtime-streaming',
          height: 350,
          type: 'line',
          streaming: {
            enabled: true,
          },
          animations: {
            dynamicAnimation: {
              speed: 1000,
            },
          },
          toolbar: {
            show: false,
          },
          zoom: {
            enabled: false,
          },
        },
        dataLabels: {
          enabled: false,
        },
        stroke: {
          curve: 'smooth',
        },
        title: {
          text: 'Streaming (bounded memory + linear scroll)',
          align: 'left',
        },
        markers: {
          size: 0,
        },
        xaxis: {
          type: 'datetime',
          range: XAXISRANGE,
        },
        yaxis: {
          min: 0,
          max: 100,
        },
        legend: {
          show: false,
        },
      },
    }
  },
  mounted: function () {
    var me = this
    window.setInterval(function () {
      lastX += TICKINTERVAL
      me.$refs.chart.appendData([
        {
          data: [{ x: lastX, y: nextValue() }],
        },
      ])
    }, TICKINTERVAL)
  },
}
</script>

<style>
#chart {
  max-width: 650px;
  margin: 35px auto;
}
.explanation {
  max-width: 620px;
  margin: 0 auto 12px;
  font-size: 13px;
  color: #777;
}
</style>