CandlestickChart
Overview
The CandlestickChart component draws each period's open, high, low and close prices as a candle or an OHLC bar, with an optional pane of volume bars under them.
It's built on Recharts' BarChart, with a bar
shape of its own, and comes from the @allxsmith/bestax-charts package, which the
Charts guide shows how to install. You give it rows of data,
one per period, and a dataKey that names each row's period, prices and volume, and it draws
them with Bulma's colors, a tooltip in a Bulma box, and a data table screen readers read.
The prices on this page are made up to show the chart. They aren't financial advice.
Import
import { CandlestickChart } from '@allxsmith/bestax-charts';
Usage
Basic Usage
Each row of data is a period, such as a day, and dataKey names the keys of a row that hold its
period (x) and its open, high, low and close prices. Each period is a candle: a box from
the open to the close, and a line from the low to the high through it. A period that closed at or
above its open is hollow, in Bulma's success color, and one that closed below it is filled, in
danger, so the shape tells them apart as well as the color. label names the chart; it's
required, because it's what a screen reader announces.
function example() { const prices = [ { day: 'Jun 1', open: 47.76, high: 48.4, low: 47.57, close: 47.87 }, { day: 'Jun 2', open: 47.72, high: 49.28, low: 47.34, close: 48.9 }, { day: 'Jun 3', open: 48.71, high: 50.2, low: 48.38, close: 49.53 }, { day: 'Jun 4', open: 49.4, high: 49.64, low: 48.6, close: 48.68 }, { day: 'Jun 5', open: 48.7, high: 50.14, low: 48.64, close: 49.59 }, { day: 'Jun 8', open: 49.41, high: 50.71, low: 49.06, close: 50.36 }, { day: 'Jun 9', open: 50.23, high: 50.25, low: 48.33, close: 49.06 }, { day: 'Jun 10', open: 49.2, high: 49.99, low: 48.74, close: 49.42 }, ]; return ( <CandlestickChart label="Daily prices of a made-up stock" data={prices} dataKey={{ x: 'day', open: 'open', high: 'high', low: 'low', close: 'close', }} /> ); }
OHLC Bars
type="ohlc" draws each period as an OHLC bar instead: a line from the low to the high, with a
tick to its left at the open and one to its right at the close. Which tick is higher says which
way the period went, as well as the color.
function example() { const prices = [ { day: 'Jun 1', open: 47.76, high: 48.4, low: 47.57, close: 47.87 }, { day: 'Jun 2', open: 47.72, high: 49.28, low: 47.34, close: 48.9 }, { day: 'Jun 3', open: 48.71, high: 50.2, low: 48.38, close: 49.53 }, { day: 'Jun 4', open: 49.4, high: 49.64, low: 48.6, close: 48.68 }, { day: 'Jun 5', open: 48.7, high: 50.14, low: 48.64, close: 49.59 }, { day: 'Jun 8', open: 49.41, high: 50.71, low: 49.06, close: 50.36 }, { day: 'Jun 9', open: 50.23, high: 50.25, low: 48.33, close: 49.06 }, { day: 'Jun 10', open: 49.2, high: 49.99, low: 48.74, close: 49.42 }, ]; return ( <CandlestickChart label="Daily prices of a made-up stock" type="ohlc" data={prices} dataKey={{ x: 'day', open: 'open', high: 'high', low: 'low', close: 'close', }} /> ); }
Volume
dataKey.volume names the key that holds each period's volume, such as the shares traded, here
vol. The
chart then draws the volume as bars in a pane under the prices, each in its period's color, and the
data table and the tooltip list it. The pane takes the bottom quarter of the plot, and
volumeHeight changes that, as a percentage. volumeValueFormatter writes the volumes.
function example() { const prices = [ { day: 'Mon', open: 47.8, high: 48.4, low: 47.6, close: 47.9, vol: 91200 }, { day: 'Tue', open: 47.7, high: 49.3, low: 47.3, close: 48.9, vol: 118400 }, { day: 'Wed', open: 48.7, high: 50.2, low: 48.4, close: 49.5, vol: 104600 }, { day: 'Thu', open: 49.4, high: 49.6, low: 48.6, close: 48.7, vol: 63100 }, { day: 'Fri', open: 48.7, high: 50.1, low: 48.6, close: 49.6, vol: 87400 }, ]; const compact = new Intl.NumberFormat('en-US', { notation: 'compact', maximumFractionDigits: 1, }); return ( <CandlestickChart label="Daily prices and volume of a made-up stock" data={prices} dataKey={{ x: 'day', open: 'open', high: 'high', low: 'low', close: 'close', volume: 'vol', }} volumeHeight="30%" volumeValueFormatter={value => compact.format(value)} /> ); }
Three Months of Daily Prices
A long series reads the same way. Here, three months of trading days come from a seeded walk, so
the chart draws the same made-up prices each time, and the x axis leaves room between its labels
with Recharts' minTickGap.
function example() { // A seeded walk: the same made-up prices on every visit. let seed = 42; const random = () => { seed = (seed * 16807) % 2147483647; return seed / 2147483647; }; const dayName = new Intl.DateTimeFormat('en-US', { month: 'short', day: 'numeric', timeZone: 'UTC', }); const days = []; const date = new Date(Date.UTC(2026, 5, 1)); let close = 48; while (days.length < 63) { const weekday = date.getUTCDay(); if (weekday !== 0 && weekday !== 6) { const open = close * (1 + (random() - 0.5) * 0.01); close = open * (1 + (random() - 0.48) * 0.05); days.push({ day: dayName.format(date), open, high: Math.max(open, close) * (1 + random() * 0.015), low: Math.min(open, close) * (1 - random() * 0.015), close, volume: Math.round(400000 + random() * 900000), }); } date.setUTCDate(date.getUTCDate() + 1); } const dollars = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', }); const compact = new Intl.NumberFormat('en-US', { notation: 'compact', maximumFractionDigits: 1, }); return ( <CandlestickChart label="Three months of a made-up stock" data={days} dataKey={{ x: 'day', open: 'open', high: 'high', low: 'low', close: 'close', volume: 'volume', }} valueFormatter={value => dollars.format(value)} volumeValueFormatter={value => compact.format(value)} xAxisProps={{ minTickGap: 24 }} height={360} /> ); }
Colors
upColor and downColor take a Bulma color name, a shade of one of Bulma's six palette colors, or
any CSS color. Bulma names draw with Bulma's own CSS variables, so they follow
Theme and dark mode. A candle stays hollow or filled whatever its color.
function example() { const prices = [ { day: 'Mon', open: 20.1, high: 21.4, low: 19.8, close: 21.2 }, { day: 'Tue', open: 21.2, high: 21.5, low: 20.3, close: 20.6 }, { day: 'Wed', open: 20.6, high: 22.1, low: 20.5, close: 21.9 }, { day: 'Thu', open: 21.9, high: 22.0, low: 20.9, close: 21.1 }, { day: 'Fri', open: 21.1, high: 21.8, low: 20.7, close: 21.6 }, ]; return ( <CandlestickChart label="A week of a made-up stock" data={prices} dataKey={{ x: 'day', open: 'open', high: 'high', low: 'low', close: 'close', }} upColor="info" downColor="warning-dark" height={220} /> ); }
Axes, Grid and Reference Lines
xAxisLabel and yAxisLabel title the axes, grid picks the grid lines, and referenceLines
draws lines across the chart: at a price with y, or at a period with x. A line at a price past
the price axis isn't drawn, so with a volume pane, none crosses the pane.
function example() { const prices = [ { day: 'Mon', open: 20.1, high: 21.4, low: 19.8, close: 21.2 }, { day: 'Tue', open: 21.2, high: 21.5, low: 20.3, close: 20.6 }, { day: 'Wed', open: 20.6, high: 22.1, low: 20.5, close: 21.9 }, { day: 'Thu', open: 21.9, high: 22.0, low: 20.9, close: 21.1 }, { day: 'Fri', open: 21.1, high: 21.8, low: 20.7, close: 21.6 }, ]; return ( <CandlestickChart label="A week of a made-up stock" data={prices} dataKey={{ x: 'day', open: 'open', high: 'high', low: 'low', close: 'close', }} xAxisLabel="Day" yAxisLabel="Price" grid="both" referenceLines={[ { y: 21.5, label: 'Target', color: 'info' }, { x: 'Wed', label: 'Results' }, ]} height={260} /> ); }
Formatting Values
Values are written as useNumberFormat writes a number, in the
LocaleProvider's locale. valueFormatter and unit write the prices your way, on the axis, in
the tooltip and in the data table, and volumeValueFormatter and volumeUnit do the same for the
volumes.
function example() { const prices = [ { day: 'Mon', open: 20.1, high: 21.4, low: 19.8, close: 21.2, vol: 5200 }, { day: 'Tue', open: 21.2, high: 21.5, low: 20.3, close: 20.6, vol: 7400 }, { day: 'Wed', open: 20.6, high: 22.1, low: 20.5, close: 21.9, vol: 6100 }, ]; return ( <LocaleProvider locale="de-DE"> <CandlestickChart label="Tageskurse einer erfundenen Aktie" data={prices} dataKey={{ x: 'day', open: 'open', high: 'high', low: 'low', close: 'close', volume: 'vol', }} unit=" €" volumeUnit=" Stück" showTable height={220} /> </LocaleProvider> ); }
Translating the Headings
The data table and the tooltip head the prices and the volume with words of the chart's own:
Open, High, Low, Close and Volume. labels translates them, and xAxisLabel heads the
periods, which the table heads with dataKey.x without one.
function example() { const prices = [ { jour: 'Lun', ouverture: 20.1, haut: 21.4, bas: 19.8, cloture: 21.2 }, { jour: 'Mar', ouverture: 21.2, haut: 21.5, bas: 20.3, cloture: 20.6 }, ]; return ( <CandlestickChart label="Cours d’une action imaginaire" data={prices} dataKey={{ x: 'jour', open: 'ouverture', high: 'haut', low: 'bas', close: 'cloture', }} xAxisLabel="Jour" labels={{ open: 'Ouverture', high: 'Plus haut', low: 'Plus bas', close: 'Clôture', }} showTable height={200} /> ); }
Showing the Data Table
The data table is always there for screen readers, with a row per period and a column for each
price, and for the volume when there is one. showTable puts it on screen too, below the chart,
and showLabel shows the chart's label above it as a caption.
function example() { const prices = [ { day: 'Mon', open: 20.1, high: 21.4, low: 19.8, close: 21.2, vol: 5200 }, { day: 'Tue', open: 21.2, high: 21.5, low: 20.3, close: 20.6, vol: 7400 }, ]; return ( <CandlestickChart label="Two days of a made-up stock" showLabel showTable data={prices} dataKey={{ x: 'day', open: 'open', high: 'high', low: 'low', close: 'close', volume: 'vol', }} height={200} /> ); }
Recharts Props and Children
Each Recharts part the chart draws takes its own props after the chart's: barProps for the Bar
that draws the candles (here, thinner ones), volumeBarProps for the Bar that draws the volume,
chartProps for Recharts' BarChart, and xAxisProps, yAxisProps (here, the prices on the
right) and tooltipProps. children adds more Recharts elements, such as a ReferenceArea or a
Brush.
With a volume pane, the chart sets the price axis' domain and ticks itself, from the prices in
every row of data, to keep them clear of the pane, with about as many ticks as tickCount in
yAxisProps asks for, five unless you set it. So a Brush that shows some of the rows keeps that
scale, and a series you draw as children that reaches past the prices needs a domain and
ticks of its own. A domain, ticks or a scale other than linear in yAxisProps lays the
axis out instead, and the prices can then reach into the pane. A reversed price axis turns the
pane over too, to hang from the top. The barSize and maxBarSize in barProps size the volume
bars as well, so they stay as wide as the candles, and volumeBarProps can size them apart.
function example() { const prices = [ { day: 'Mon', open: 20.1, high: 21.4, low: 19.8, close: 21.2, vol: 5200 }, { day: 'Tue', open: 21.2, high: 21.5, low: 20.3, close: 20.6, vol: 7400 }, { day: 'Wed', open: 20.6, high: 22.1, low: 20.5, close: 21.9, vol: 6100 }, { day: 'Thu', open: 21.9, high: 22.0, low: 20.9, close: 21.1, vol: 8800 }, { day: 'Fri', open: 21.1, high: 21.8, low: 20.7, close: 21.6, vol: 4900 }, ]; return ( <CandlestickChart label="A week of a made-up stock" data={prices} dataKey={{ x: 'day', open: 'open', high: 'high', low: 'low', close: 'close', volume: 'vol', }} barProps={{ maxBarSize: 12 }} volumeBarProps={{ fillOpacity: 0.3 }} chartProps={{ margin: { top: 10, right: 10, bottom: 0, left: 10 } }} yAxisProps={{ orientation: 'right' }} /> ); }
Size
The chart fills its container's width and is 300 pixels tall. height changes that, and width
and height take pixels or a percentage of the container. A chart sized in percent draws once the
browser has measured its container, and its data table renders on the server either way.
<CandlestickChart label="Two days of a made-up stock" data={[ { day: 'Mon', open: 20.1, high: 21.4, low: 19.8, close: 21.2 }, { day: 'Tue', open: 21.2, high: 21.5, low: 20.3, close: 20.6 }, ]} dataKey={{ x: 'day', open: 'open', high: 'high', low: 'low', close: 'close' }} width={320} height={180} />
Theming
The chart draws its grid, axes and text from the same --bulma-chart-* variables as every chart
in the package; BarChart's Theming section lists them. The band that marks
the focused period takes --bulma-chart-cursor-color. The periods take upColor and downColor,
Bulma's success and danger unless you set them, so they follow your theme's colors.
Accessibility
- The chart is a
<figure>named bylabel, and its drawing is named by the same caption.descriptionis read with the name of both. The data table takes the label as its caption. - A data table of every period renders beside the drawing, visually hidden with Bulma's
is-sr-only, so a screen reader reads the prices the candles show (WAI: complex images). Each row's header names its period, and its cells give the open, high, low and close, and the volume when there's a volume pane.showTableputs it on screen. - The drawing is a tab stop. Once it has focus, the left and right arrow keys step through the periods, and the tooltip shows each one's prices and volume. A band behind the period marks it.
- Which way a period went isn't told by color alone. A candle that went up is hollow and one that went down is filled, and an OHLC bar's close tick sits above or below its open tick. The table gives the open and the close as numbers.
- The chart has no legend, since its one price series has nothing to hide or show. The tooltip and the table name each value.
Related Components
BarChart: Values drawn as bars, one group per category.AreaChart: Values drawn as areas, or one series split at zero in two colors.Table: The Bulma table the chart's data table is drawn with.LocaleProvider: The locale the chart writes its numbers in.- Helper Props: Bulma helper props for spacing, color, etc.
Additional Resources
- Charts guide
- In the gallery, Markets and Investing: Price with Volume
- Recharts: BarChart
- Recharts: Bar
- Storybook: CandlestickChart Stories
Props
| Prop | Type | Default | Description |
|---|---|---|---|
data | readonly ChartRow[] | — | Required. The rows to draw: one per period, such as a day, in order. |
dataKey | CandlestickChartDataKey | — | Required. The keys of each row that hold its period (x), its prices (open, high, low and close) and, when set, its volume. A key with dots or brackets, such as prices.open, reads a nested value, as Recharts does, unless a row has that key itself. A price or volume may be a number or a string of one, such as '101.25'. |
type | 'candle' | 'ohlc' | 'candle' | How each period is drawn. A candle is a box from the open to the close, with a line from the low to the high through it: hollow when the period closes at or above its open, and filled when it closes below. An ohlc bar is a line from the low to the high, with a tick to its left at the open and one to its right at the close. Either way, the shape shows which way the period went, as well as its color. |
upColor | ChartColor | 'success' | The color of a period that closes at or above its open, and of its volume bar. |
downColor | ChartColor | 'danger' | The color of a period that closes below its open, and of its volume bar. |
volumeHeight | CandlestickChartVolumeHeight | '25%' | How much of the plot's height the volume pane takes, as a percentage, from 5% to 80%. The prices are drawn above it. Ignored without dataKey.volume. |
grid | 'horizontal' | 'vertical' | 'both' | 'none' | 'horizontal' | Which grid lines to draw. |
gridProps | Partial<CartesianGridProps> | — | Props passed to Recharts' CartesianGrid, after the chart's own. |
hasXAxis | boolean | true | Draws the x axis, which writes the periods. |
hasYAxis | boolean | true | Draws the y axis, which writes the prices. |
xAxisLabel | string | — | A title for the x axis. It also heads the periods in the data table, which uses dataKey.x without one. |
yAxisLabel | string | — | A title for the y axis, such as "Price". |
xAxisProps | Partial<XAxisProps> | — | Props passed to Recharts' XAxis, after the chart's own. |
yAxisProps | Partial<YAxisProps> | — | Props passed to Recharts' YAxis for the prices, after the chart's own. With a volume pane, the chart sets the axis' domain and ticks from the prices in every row of data, to keep them clear of the pane: about tickCount ticks, five unless set. So a Brush that shows some of the rows keeps that scale, and a series drawn as children that reaches past the prices needs a domain and ticks of its own here. A domain, ticks or scale other than linear here lays the axis out in place of the chart, and the prices can then reach into the pane. A reversed axis turns the volume pane over too, to hang from the top. |
referenceLines | { x?: string | number; y?: number; label?: string; color?: ChartColor }[] | — | Lines drawn across the chart at given values, such as a price target. x is a period of data, and y a price. A line at a price outside the price axis isn't drawn, so with a volume pane, none crosses the pane. |
valueFormatter | ChartValueFormatter | — | Writes each price on the y axis, in the tooltip and in the data table. Without one, they're written as useNumberFormat writes a number, in the LocaleProvider's locale. volumeValueFormatter writes the volumes. |
unit | string | — | Written after every price, such as €. volumeUnit follows the volumes. |
volumeValueFormatter | ChartValueFormatter | — | Writes each volume in the tooltip and in the data table. Without one, they're written as useNumberFormat writes a number, in the LocaleProvider's locale. |
volumeUnit | string | — | Written after every volume, such as shares. |
labels | CandlestickChartLabels | — | The headings the chart writes itself, in the data table and the tooltip, to translate them. |
tooltipProps | Partial<TooltipProps> | — | Props passed to Recharts' Tooltip, after the chart's own. |
barProps | Partial<Omit<BarProps, 'ref'>> | — | Props passed to the Recharts Bar that draws the candles or OHLC bars, after the chart's own, such as maxBarSize or strokeWidth. Its barSize and maxBarSize size the volume bars too, so they stay as wide as the candles. |
volumeBarProps | Partial<Omit<BarProps, 'ref'>> | — | Props passed to the Recharts Bar that draws the volume, after the chart's own, such as fillOpacity or radius. |
chartProps | Partial<React.ComponentProps<typeof RechartsBarChart>> | — | Props passed to Recharts' BarChart, which draws the chart, after the chart's own, such as margin or barCategoryGap. |
className | string | — | Additional CSS classes to apply. |
label | string | — | Required. What the chart shows, such as "Monthly sales". It names the chart for assistive tech, and captions its data table. Shown above the chart with showLabel. |
description | string | — | A sentence or two on what the chart says, such as the trend it shows. A screen reader reads it with the chart's name. Not shown on screen. |
showLabel | boolean | false | Shows label above the chart as its caption. |
showTable | boolean | false | Shows the chart's data table below it. The table is always there for screen readers, which read the numbers from it; this puts it on screen as well. |
hasTooltip | boolean | true | Shows a tooltip with the values under the pointer, or under the keyboard focus as the arrow keys move through the chart. |
width | ChartSize | '100%' | The chart's width, in pixels or as a percentage of its container. A percentage makes the chart follow its container's size as the browser measures it. Recharts draws in the browser either way: a server render holds the figure, its caption, legend and data table, and the drawing appears once the page hydrates. |
height | ChartSize | 300 | The chart's height, in pixels or as a percentage of its container, which then needs a height of its own. |
children | React.ReactNode | — | More Recharts elements to draw inside the chart, such as a Brush, a ReferenceArea, or <defs> for a pattern a color names with url(#id). |
ref | React.Ref<HTMLElement> | — | Ref forwarded to the root <figure>. |
... | All standard HTML attributes and Bulma helper props | — | See Helper Props |
Types:
CandlestickChartDataKey: The keys of each row that hold its period, its prices and its volume. Its fields:x:string— Required. The key that holds the row's period, such as a date, along the x axis.open:string— Required. The key that holds the price the period opened at.high:string— Required. The key that holds the period's highest price.low:string— Required. The key that holds the period's lowest price.close:string— Required. The key that holds the price the period closed at.volume:string— The key that holds the period's volume, such as the shares traded. With one, the chart draws each period's volume as a bar in a pane under the prices, and the data table and the tooltip list it.
CandlestickChartLabels: The text the chart writes in the data table and the tooltip itself. Its fields:open:string— The heading of the opening prices. Default'Open'.high:string— The heading of the highest prices. Default'High'.low:string— The heading of the lowest prices. Default'Low'.close:string— The heading of the closing prices. Default'Close'.volume:string— The heading of the volumes, written whendataKey.volumeis set. Default'Volume'.
CandlestickChartVolumeHeight:`${number}%`— The volume pane's height, as a percentage of the plot's, such as'25%'.ChartColor: Bulma color |`${(typeof paletteColors)[number]}-${(typeof validColorShades)[number]}`|(string & {})— A color for a series, a slice or a mark. A Bulma color name such asprimaryorgrey-light, a shade of one of Bulma's six palette colors such asprimary-60orinfo-dark, or any CSS color, such as#3e8ed0,var(--brand)orurl(#stripes)for a pattern the chart's children define. A Bulma name draws with Bulma's own CSS variable, so it followsThemeand dark mode.ChartRow:Record<string, any>— One row of a chart'sdata: an object whose keys the chart'sdataKeyand series name.ChartSize:number|`${number}%`— A chart's width or height: pixels, or a percentage of its container.ChartValueFormatter:(value: number) => string— Writes a value for the axes, the tooltip, the labels and the data table.