Syncing Charts in JavaScript

Using ApexCharts with JavaScript

This Syncing Charts example uses ApexCharts.js directly in JavaScript, with no wrapper component.

Install it with npm install apexcharts, then mount the chart with new ApexCharts(element, options).render().

JavaScript installation guide
// The global window.Apex variable below can be used to set common options for all charts on the page
Apex = {
  chart: {
    height: 130,
  },
  title: {
    style: {
      fontSize: '14px',
    },
  },
  dataLabels: {
    enabled: false,
  },
  stroke: {
    curve: 'straight',
  },
  toolbar: {
    tools: {
      selection: false,
    },
  },
  markers: {
    size: 6,
    hover: {
      size: 10,
    },
  },
  tooltip: {
    followCursor: false,
    theme: 'dark',
    x: {
      show: false,
    },
    marker: {
      show: false,
    },
    y: {
      title: {
        formatter: function () {
          return ''
        },
      },
    },
  },
  grid: {
    clipMarkers: false,
  },
  yaxis: {
    tickAmount: 2,
  },
  xaxis: {
    type: 'datetime',
  },
}

// Deterministic daily random walks so every chart shares the same date range
// and the synced crosshair always has data under it.
function mulberry32(seed) {
  return function () {
    seed |= 0
    seed = (seed + 0x6d2b79f5) | 0
    var t = Math.imul(seed ^ (seed >>> 15), 1 | seed)
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296
  }
}

function dailySeries(seed, count, base, spread) {
  var rand = mulberry32(seed)
  var series = []
  var ts = new Date('01 Jun 2025').getTime()
  var val = base
  for (var i = 0; i < count; i++) {
    val += (rand() - 0.5) * spread
    series.push([ts, Math.round(val)])
    ts += 86400000
  }
  return series
}

var options = {
  series: [
    {
      name: 'Page Views',
      data: dailySeries(11, 20, 42, 18),
    },
  ],
  chart: {
    id: 'pageviews',
    group: 'website',
    type: 'line',
    height: 130,
  },
  title: {
    text: 'Page Views (k)',
  },
  colors: ['#008FFB'],
}

var chart = new ApexCharts(document.querySelector('#chart-line'), options)
chart.render()

var optionsLine2 = {
  series: [
    {
      name: 'Sessions',
      data: dailySeries(23, 20, 20, 8),
    },
  ],
  chart: {
    id: 'sessions',
    group: 'website',
    type: 'line',
    height: 130,
  },
  title: {
    text: 'Sessions (k)',
  },
  colors: ['#546E7A'],
}

var chartLine2 = new ApexCharts(
  document.querySelector('#chart-line2'),
  optionsLine2,
)
chartLine2.render()

var optionsArea = {
  series: [
    {
      name: 'Sign-ups',
      data: dailySeries(35, 20, 38, 16),
    },
  ],
  chart: {
    id: 'signups',
    group: 'website',
    type: 'area',
    height: 130,
  },
  title: {
    text: 'Sign-ups',
  },
  colors: ['#00E396'],
}

var chartArea = new ApexCharts(
  document.querySelector('#chart-area'),
  optionsArea,
)
chartArea.render()