Getting started
From an empty folder to a live, interactive chart, one small step at a time. Every step ends with something you can see, and most come with a chart running right here.
Before you start
| You need | Version | Why |
|---|---|---|
| Node.js | 22 or newer | The package requires it (engines) |
| React and React DOM | 18 or 19 | They are peer dependencies |
| A bundler | Vite, Next.js, or similar | The package is ESM and ships its CSS separately |
The ten steps
Create a project
Skip this if you already have a React app.
bash npm create vite@latest my-charts -- --template react-ts cd my-chartsYou should see: a
my-chartsfolder withsrc/App.tsxin it.Install Astroneum
npm npm install astroneumYou should see:
astroneumunderdependenciesinpackage.json.Render your first chart
Replace
src/App.tsxwith this. The chart below is exactly this code, running on live Bitcoin data.src/App.tsx import { AstroneumChart, createStandardCryptoDatafeed, STANDARD_CRYPTO_SYMBOLS } from 'astroneum' import 'astroneum/style.css' // Create the datafeed once, outside the component. const datafeed = createStandardCryptoDatafeed() export default function App() { return ( <AstroneumChart symbol={STANDARD_CRYPTO_SYMBOLS[0]} period={{ multiplier: 1, timespan: 'hour', text: '1H' }} datafeed={datafeed} theme="dark" style={{ width: '100%', height: 360 }} /> ) }Loads when scrolled into viewYou should see: a live candlestick chart with three moving-average lines (the default) and a volume pane underneath. Drag to pan, scroll to zoom, and use the toolbar to switch timeframe or add indicators.
If the chart above stays empty, your network may be blocking the exchange. Step 5 shows data that needs no network.
How data gets into the chart
Before using your own data, see what the chart does with a datafeed. A datafeed is an object with four methods. The chart calls them; you never call them yourself.
Drawing diagram…Method The chart calls it when You return searchSymbols(search?)you type in the symbol search box Promise<SymbolInfo[]>getHistoryData(symbol, period, from, to)it loads the first bars, and when you scroll back Promise<CandleData[]>, oldest firstsubscribe(symbol, period, callback)history has loaded nothing; call callback(bar)for each live updateunsubscribe(symbol, period)the symbol or period changes nothing; stop sending updates A bar (
CandleData) is{ timestamp, open, high, low, close, volume? }, withtimestampin milliseconds.The one rule for live updates
Send a bar with the same timestamp as the latest bar to update it, or a newer timestamp to start a new bar. Older timestamps are ignored. Try it:
// press a button: this is what your datafeed does
Use your own data
This is a complete datafeed that needs no server. It makes up a random price and keeps it moving, which is handy for testing. It is the code below, running:
src/App.tsx import { AstroneumChart } from 'astroneum' import type { CandleData, Datafeed, Period, SymbolInfo } from 'astroneum' import 'astroneum/style.css' const MINUTE = 60_000 const symbol: SymbolInfo = { ticker: 'DEMO', name: 'Demo market', pricePrecision: 2 } const period: Period = { multiplier: 1, timespan: 'minute', text: '1m' } let price = 100 function nextBar(timestamp: number): CandleData { const open = price price = Math.max(1, price + (Math.random() - 0.5) * 2) return { timestamp, open, high: Math.max(open, price) + Math.random() * 0.4, low: Math.min(open, price) - Math.random() * 0.4, close: price, volume: Math.round(Math.random() * 1000), } } let timer: ReturnType<typeof setInterval> | undefined const datafeed: Datafeed = { // The symbol search box. One symbol is enough for this demo. searchSymbols: async () => [symbol], // Called with a time range. Return bars inside it, oldest first. getHistoryData: async (_symbol, _period, from, to) => { const bars: CandleData[] = [] for (let t = Math.ceil(from / MINUTE) * MINUTE; t <= to; t += MINUTE) bars.push(nextBar(t)) return bars }, // Send a bar every second. Same minute = update it; new minute = new bar. subscribe: (_symbol, _period, callback) => { let last = nextBar(Math.floor(Date.now() / MINUTE) * MINUTE) timer = setInterval(() => { const minute = Math.floor(Date.now() / MINUTE) * MINUTE if (minute > last.timestamp) { last = nextBar(minute) } else { price = Math.max(1, price + (Math.random() - 0.5)) last = { ...last, close: price, high: Math.max(last.high, price), low: Math.min(last.low, price) } } callback(last) }, 1000) }, unsubscribe: () => clearInterval(timer), } export default function App() { return ( <AstroneumChart symbol={symbol} period={period} datafeed={datafeed} theme="dark" style={{ width: '100%', height: 360 }} /> ) }Loads when scrolled into viewYou should see: a chart of the made-up market, with the last candle twitching every second. That is the whole contract: history in, then a stream of updates.
To connect a real source, keep the same four methods and replace the bodies with
fetchcalls and a WebSocket. The Datafeeds page goes further.Choose a chart type
There are two kinds of chart type, and the difference decides how you set them up.
Drawing diagram…Types the chart draws itself: one style prop
tsx <AstroneumChart symbol={symbol} period={period} datafeed={datafeed} styles={{ candle: { type: 'line' } }} // the only change style={{ width: '100%', height: 360 }} />Loads when scrolled into viewYou should see: a single price line where the candles were. You can change
styleswhile the chart is mounted; it updates in place.Types built from your data: wrap the datafeed
tsx import { createTransformedDatafeed, generateRenko, heikinAshi } from 'astroneum' // Heikin-Ashi: same timestamps, smoothed values. const heikin = createTransformedDatafeed(datafeed, () => heikinAshi) // Renko: fixed-size bricks. The factory runs once on the loaded history, // so the brick size stays fixed while live updates arrive. const renko = createTransformedDatafeed(datafeed, (history) => { const range = history.slice(-100).reduce((sum, bar) => sum + (bar.high - bar.low), 0) / 100 const brick = Number(range.toPrecision(2)) return (bars) => generateRenko(bars, brick) }) <AstroneumChart key="renko" datafeed={renko} /* ...the same props as before... */ />Loads when scrolled into viewYou should see: stepped bricks that only move when the price moves a full brick. Remount with a different
keywhen you switch datafeeds. Renko ignores time, so its time axis shows synthetic times.Add indicators
Indicators are props. Overlays such as moving averages draw on the price chart; everything else gets its own pane underneath.
tsx <AstroneumChart symbol={symbol} period={period} datafeed={datafeed} mainIndicators={[{ name: 'EMA', calcParams: [7, 25, 99] }, { name: 'BOLL' }]} subIndicators={['VOL', 'RSI', 'MACD']} style={{ width: '100%', height: 460 }} />Drawing diagram…Loads when scrolled into viewYou should see: the EMA lines and a Bollinger band on the price chart, plus VOL, RSI and MACD panes beneath it. The props follow changes: add or remove a name and the chart updates without remounting. There are about 50 indicators; the Indicators page lets you toggle them.
Control the chart from code
Attach a
refto get a handle. It lets your own buttons drive the chart. Click them:src/App.tsx import { useRef } from 'react' import { AstroneumChart, createStandardCryptoDatafeed, STANDARD_CRYPTO_SYMBOLS } from 'astroneum' import type { AstroneumHandle } from 'astroneum' import 'astroneum/style.css' const datafeed = createStandardCryptoDatafeed() export default function App() { const chart = useRef<AstroneumHandle>(null) return ( <> <button onClick={() => chart.current?.setSymbol(STANDARD_CRYPTO_SYMBOLS[1])}>Switch symbol</button> <button onClick={() => chart.current?.setPeriod({ multiplier: 15, timespan: 'minute', text: '15m' })}>15m</button> <button onClick={() => chart.current?.setTheme('light')}>Light theme</button> <button onClick={() => chart.current?.setTheme('dark')}>Dark theme</button> <AstroneumChart ref={chart} symbol={STANDARD_CRYPTO_SYMBOLS[0]} period={{ multiplier: 1, timespan: 'hour', text: '1H' }} datafeed={datafeed} theme="dark" style={{ width: '100%', height: 360 }} /> </> ) }Loads when scrolled into viewMethod What it does setSymbol(symbol)/getSymbol()change or read the symbol setPeriod(period)/getPeriod()change or read the timeframe setTheme('dark' | 'light' | 'high-contrast')switch the theme setStyles(styles)change chart styles, such as the chart type serializeState()/loadState(state)save and restore the chart, drawings included Saving the user's chart is two calls.
serializeState()returns plain JSON, so it goes anywhere:localStorage, a URL, or your database.Drawing diagram…Faster and offline (optional)
Both features are off by default. Turn them on when you need them.
History cache
Remembers loaded bars in the browser, so a reload only fetches what is new and the chart still opens when the data source is down.
tsx <AstroneumChart historyCache /* ...other props... */ />Drawing diagram…Indicator workers
For very long histories (tens of thousands of bars), this moves heavy indicator maths off the main thread so scrolling stays smooth.
tsx import { configureIndicatorWorkers } from 'astroneum' configureIndicatorWorkers({ enabled: true }) // MA, EMA, RSI and BOLL, 20,000+ barsDrawing diagram…See Speed and offline for a live look at the cache.
Ship it
Next.js
Add the package to
transpilePackages, and render the chart from a client component:ts // next.config.ts const nextConfig = { transpilePackages: ['astroneum'] } export default nextConfigtsx // app/components/Chart.tsx 'use client' import { AstroneumChart } from 'astroneum' import 'astroneum/style.css' // ...The package is safe to import on the server (it does not touch
windowat import time), so server-side rendering will not crash. The chart itself draws in the browser.Everything else
Build as usual. The CSS file is separate (
astroneum/style.css) so you control how it loads. Optional features are separate imports, so apps that do not use them do not pay for them:Import Adds astroneumthe chart and the common datafeeds astroneum/replaybar replay astroneum/multichartmulti-chart grids astroneum/datafeeds/cryptothe crypto datafeed on its own astroneum/datafeeds/polygonPolygon.io datafeeds astroneum/datafeeds/webtransportexperimental WebTransport datafeed
Troubleshooting
| What you see | Why | Fix |
|---|---|---|
| Nothing renders, no error | The container has no height | Set style={{ height: 560 }} or give the parent a height |
| The chart looks broken or unstyled | The CSS is missing | import 'astroneum/style.css' once in your app |
window is not defined | Rendering on the server | In Next.js, put the chart in a component marked 'use client' |
| Empty chart, no bars | getHistoryData returned nothing or threw | Log what it returns; times are in milliseconds, oldest first |
| Chart never updates live | subscribe never calls callback | Call callback(bar) with the same or a newer timestamp |
| A new bar shows up in the wrong place | Timestamps are in seconds | Multiply by 1000 |
| Switching to Renko or Heikin-Ashi shows the old chart | The chart keeps its first datafeed | Give the chart a different key for each datafeed |
Unsupported engine on install | Node is older than 22 | Upgrade Node |
| Crypto data does not load | A network or region block on the exchange | Try another symbol, or use your own datafeed |