import React from 'react'
import ReactApexChart from 'react-apexcharts'
import './styles.css'
import 'apexcharts/features/trellis'
// Six tickers whose price levels differ by 40x: a shared y scale would
// flatten the cheap ones into noise, so this trellis uses
// `scales: { y: 'independent' }`. Each panel gets its own honest domain, and
// the panels are STILL pixel-aligned: the trellis measures every panel's
// axis-label gutter and pushes the widest as a shared floor, so the plot
// rectangles agree to the pixel and the shapes stay comparable.
//
// One toolbar for the grid: drag or wheel-zoom in any panel and every panel
// follows on the shared x window; reset restores all six at once.
//
// Deterministic OHLC walks so the e2e snapshot is stable.
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 ohlcWalk(seed, start, vol) {
var rand = mulberry32(seed)
var r2 = function (v) {
return Math.round(v * 100) / 100
}
var out = []
var ts = new Date('01 Jan 2025').getTime()
var close = start
for (var i = 0; i < 40; i++) {
var open = close
close = Math.max(vol, open + (rand() - 0.48) * vol * 2)
var high = Math.max(open, close) + rand() * vol
var low = Math.max(vol * 0.2, Math.min(open, close) - rand() * vol)
out.push({ x: ts, y: [r2(open), r2(high), r2(low), r2(close)] })
ts += 86400000
}
return out
}
var TICKERS = [
{ ticker: 'ANVL', seed: 3, start: 24, vol: 0.9 },
{ ticker: 'BRCK', seed: 17, start: 61, vol: 2.1 },
{ ticker: 'CDER', seed: 29, start: 142, vol: 4.5 },
{ ticker: 'DELM', seed: 41, start: 310, vol: 9 },
{ ticker: 'ELMS', seed: 53, start: 78, vol: 2.6 },
{ ticker: 'FRST', seed: 67, start: 927, vol: 24 },
]
// One series NAME across the grid (the headers already name the tickers), so
// the shared legend has nothing to add and stays hidden.
var stockSeries = TICKERS.map(function (t) {
return {
name: 'OHLC',
ticker: t.ticker,
data: ohlcWalk(t.seed, t.start, t.vol),
}
})
const ApexChart = () => {
const [state, setState] = React.useState({
series: stockSeries,
options: {
chart: {
id: 'stockTrellis',
type: 'candlestick',
height: 620,
animations: {
enabled: false,
},
},
trellis: {
by: 'ticker',
columns: 3,
gap: 14,
scales: {
y: 'independent',
},
},
xaxis: {
type: 'datetime',
},
yaxis: {
labels: {
formatter: function (val) {
return '$' + Math.round(val)
},
},
},
dataLabels: {
enabled: false,
},
tooltip: {
x: {
format: 'dd MMM',
},
},
},
})
return (
<div>
<div className="wrap">
<h1>Six tickers, one grid</h1>
<p className="lead">
Price levels 40x apart, so each panel keeps its own y scale, yet all
six plot rectangles are pixel-identical. Zoom anywhere: every ticker
follows on the same date window, and one reset brings them all back.
</p>
<div className="chart-wrap">
<div id="chart">
<ReactApexChart
options={state.options}
series={state.series}
type="candlestick"
height={620}
/>
</div>
</div>
<div className="note">
<code>scales.y: 'independent'</code> gives every ticker its own domain
(a $24 stock and a $900 stock cannot share an axis honestly), and an
independent scale draws its own labels on every panel. The panels stay
aligned anyway: the trellis measures each panel's label gutter and
pushes the widest as a shared <code>yaxis.labels.minWidth</code>, so
shapes compare cleanly across the grid. The x window is shared:
drag-zoom any panel and the whole grid moves; the single toolbar's
reset restores it. Trellis is a premium feature; without a license it
renders with a trial watermark.
</div>
</div>
</div>
)
}
export default ApexChart