Candlestick Chart
Plot prices over time as candles, each showing a period's open, high, low and close, with a touch tooltip, pan and zoom, and dark mode built in.
Installation
This also adds Chart, which draws the preset, and installs the npm packages @tanstack/charts and d3-shape.
Usage
datais an array of{ x, open, high, low, close }items, all numbers.xis the period, such as a session index.- Each candle has a thin wick from low to high and a wider body from open to close.
- The y axis fits the lowest low to the highest high, with room for the candles at each end.
- Touch a candle to see its four prices in a tooltip. Tap to pin it. Set
labelso a screen reader can say what the chart shows.
Customize
Colors
upColor colors candles that closed at or above their open (default green). downColor colors those that closed below (default red).
Shape
bodyWidthRatio sets how much of its slot each body fills, from 0 to 1 (default 0.6). A slot is the smallest gap between two x values. wickWidth is the wick's thickness in px.
Axes and range
xAxisLabel and yAxisLabel add axis titles. Horizontal gridlines are on by default: showGrid turns them off, and showXGrid adds vertical ones.
xMin,xMax,yMinandyMaxfix a bound instead of fitting the data, for example to compare two charts on one scale.xTickFormatandyTickFormatturn tick values into labels, for example a session index into a date.tickCountsets roughly how many ticks each axis gets. The axis picks round values near it.
Padding
padding insets the plot, each side as a fraction (0 to 1) of its size.
Size
The chart fills its container's width and is 200px tall. Set height for another height and width for a fixed width, both in px. Use height rather than a height class, because the preset always sets a height.
Interaction
Taps
candleTap emits the tapped candle's { x, open, high, low, close } item. A tap on empty space emits nothing. Set tooltip to false to turn the tooltip off.
candleTap fires inside the touch, so wrap router navigation in setTimeout (see the setTimeout rule).
Pan and zoom
Set zoomable to let users drag to pan, with +, − and 1:1 buttons. Zooming in on the x axis spreads the candles apart, which suits a long price history.
To zoom from your own controls, set showZoomControls to false and call zoomIn(), zoomOut() or resetZoom() on the component. See Pan and zoom for how it behaves.
API
UiCandlestickChart
CandlestickDatum is { x: number; open: number; high: number; low: number; close: number }, exported from ../components/ui/candlestick-chart. ChartPadding is { top?, right?, bottom?, left? }, each a fraction (0–1) of the plot's size, with omitted sides 0. It's exported from ../components/ui/chart.
candlestickChartDefinition
Returns the TanStack Charts definition <ui-candlestick-chart> renders: a link mark for the wicks, a rect mark for the bodies, and a tooltip that lists all four prices. The options are the inputs that shape the chart, with the same defaults: data, upColor, downColor, wickWidth, bodyWidthRatio, tickCount, showGrid, showXGrid, xAxisLabel, yAxisLabel, padding, xMin, xMax, yMin, yMax, xTickFormat, yTickFormat and tooltip. Size, label, color scheme and zoom are inputs of <ui-chart> instead.
Start from it to add marks of your own, such as a price level or a moving average, and render the result with <ui-chart>. Added marks get a plain x and y tooltip.
Related
- Chart: moving averages, volume bars and other marks
- Line Chart: a price as a single line
- Bar Chart: values as bars
