import React from 'react'
import ReactApexChart from 'react-apexcharts'
import './styles.css'

// Deterministic daily traffic per channel; stacking shows total site visits
// split into its sources. Each channel is a mean-reverting walk around its
// own base so the composition shifts organically day to day.
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 channel(seed, base, spread) {
  var rand = mulberry32(seed)
  var series = []
  var ts = new Date('01 Jun 2025 GMT').getTime()
  var val = base
  for (var i = 0; i < 20; i++) {
    val += (rand() - 0.5) * spread
    val += (base - val) * 0.15
    series.push([ts, Math.max(2, Math.round(val))])
    ts += 86400000
  }
  return series
}

const ApexChart = () => {
  const [state, setState] = React.useState({
    series: [
      {
        name: 'Organic Search',
        data: channel(11, 42, 14),
      },
      {
        name: 'Direct',
        data: channel(23, 26, 9),
      },
      {
        name: 'Social',
        data: channel(35, 15, 8),
      },
    ],
    options: {
      chart: {
        type: 'area',
        height: 350,
        stacked: true,
        events: {
          selection: function (chart, e) {
            console.log(new Date(e.xaxis.min))
          },
        },
      },
      colors: ['#008FFB', '#00E396', '#FEB019'],
      dataLabels: {
        enabled: false,
      },
      stroke: {
        curve: 'monotoneCubic',
      },
      title: {
        text: 'Website Visits by Channel',
        align: 'left',
      },
      fill: {
        type: 'gradient',
        gradient: {
          opacityFrom: 0.6,
          opacityTo: 0.8,
        },
      },
      legend: {
        position: 'top',
        horizontalAlign: 'left',
      },
      yaxis: {
        labels: {
          formatter: function (val) {
            return val + 'k'
          },
        },
      },
      xaxis: {
        type: 'datetime',
      },
    },
  })

  return (
    <div>
      <div id="chart">
        <ReactApexChart
          options={state.options}
          series={state.series}
          type="area"
          height={350}
        />
      </div>
    </div>
  )
}

export default ApexChart
Stacked Area - React Area Charts | ApexCharts.js | ApexCharts.js