Candlestick Chart

Live OHLC candles with volume and a crosshair price readout.

Preview

"use client";

import {
  type Candle,
  CandlestickChart,
  CandlestickChartLegend,
  CandlestickChartPlot,
} from "@/components/charts/candlestick-chart";

/** Pass candles oldest first with stable ids. Update the last candle as trades
 * print and append a new one when its interval opens. */
export function CandlestickChartExample({
  candles,
  lastPrice,
}: {
  candles: readonly Candle[];
  lastPrice?: number;
}) {
  return (
    <CandlestickChart
      candles={candles}
      lastPrice={lastPrice}
      label="ETH / USD, 5 minute candles"
      formatPrice={(value) => value.toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 })}
    >
      <CandlestickChartLegend />
      <CandlestickChartPlot height={320} className="mt-4" />
    </CandlestickChart>
  );
}

Install

Add it with the shadcn CLI, or copy the source manually.

$ bunx --bun shadcn add @beui/candlestick-chart

API Reference

CandlestickChart

candles
readonly Candle[]

Price candles in time order, each with open, high, low, close and volume.

Default—
lastPrice?
number

Draws a dashed line and tag at this price, usually the last trade.

Default—
activeId?
string | null

Controlled crosshair candle, by `id`, so it stays on the same candle as new ones arrive. Pair with `onActiveIdChange`.

Default—
defaultActiveId?
string | null

The initial crosshair candle id when uncontrolled. Default: null

Default—
onActiveIdChange?
((id: string | null) => void)

Called with the candle id, or `null`, when the crosshair moves or leaves.

Default—
formatPrice?
((value: number) => string)

Formats prices on the axis, readout and last-price tag. Defaults to up to 2 decimals.

Default—
formatVolume?
((value: number) => string)

Formats volume in the readout. Defaults to compact notation (1.2K).

Default—
label?
string

Accessible name for the chart.

Default—
labels?
Partial<{ label: string; candle: string; open: string; high: string; low: string; close: string; volume: string; valueText: string; }>

Override any built-in string.

Default—
className?
string

Classes merged onto the root element.

Default—

CandlestickChartLegend

children?
((candle: Candle, change: number) => ReactNode)

Custom readout. Receives the crosshair candle, or the latest one.

Default—
className?
string

Classes merged onto the root element.

Default—

CandlestickChartPlot

height?
number

Plot height in px.

Default320
className?
string

Classes merged onto the root element.

Default—

Updated