Skip to content

Candlestick Chart

Candlestick charts are essential for financial data visualization, showing OHLC (Open, High, Low, Close) data in an intuitive format.

Basic Usage

html
<CandleStickChart
  [data]="candleData"
  [legend]="legendConfig"
  [xAxis]="xAxisConfig"
  [yAxis]="yAxisConfig"
  [animation]="animationConfig">
</CandleStickChart>
tsx
<candlestickChart
  data={candleData}
  legend={legendConfig}
  xAxis={xAxisConfig}
  yAxis={yAxisConfig}
  animation={animationConfig}
/>
vue
<CandlestickChart
  :data="candleData"
  :legend="legendConfig"
  :xAxis="xAxisConfig"
  :yAxis="yAxisConfig"
  :animation="animationConfig"
/>
svelte
<candlestickChart
  data={candleData}
  legend={legendConfig}
  xAxis={xAxisConfig}
  yAxis={yAxisConfig}
  animation={animationConfig}
/>
jsx
<candlestickChart
  data={candleData()}
  legend={legendConfig}
  xAxis={xAxisConfig}
  yAxis={yAxisConfig}
  animation={animationConfig}
/>

Data Format

typescript
import type { CandleChartData, CandleDataSet, CandleDataEntry } from '@nstudio/ncharts';

const candleData: CandleChartData = {
  dataSets: [
    {
      label: 'AAPL',
      values: [
        { x: 0, shadowH: 175, shadowL: 168, open: 170, close: 173 },
        { x: 1, shadowH: 178, shadowL: 171, open: 173, close: 171 },
        { x: 2, shadowH: 176, shadowL: 169, open: 171, close: 175 },
        // ... more candles
      ],
      config: {
        increasingColor: '#10B981',
        decreasingColor: '#EF4444',
        shadowColorSameAsCandle: true,
      },
    },
  ],
};

CandleDataEntry

PropertyTypeDescription
xnumberX-axis position (time/index)
shadowHnumberHigh price (wick top)
shadowLnumberLow price (wick bottom)
opennumberOpening price
closenumberClosing price
markerstringCustom marker text

DataSet Configuration

Candle Styling

PropertyTypeDefaultDescription
increasingColorChartColor-Color when close > open
decreasingColorChartColor-Color when close < open
neutralColorChartColor-Color when close == open
increasingPaintStylePaintStyle'FILL'Style for increasing candles
decreasingPaintStylePaintStyle'FILL'Style for decreasing candles

Paint Styles

typescript
type PaintStyle = 'FILL' | 'STROKE' | 'FILL_AND_STROKE';

Shadow (Wick) Configuration

PropertyTypeDescription
shadowWidthnumberShadow line width
shadowColorChartColorShadow color (if not same as candle)
shadowColorSameAsCandlebooleanUse candle color for shadow

Spacing

PropertyTypeDescription
barSpacenumberSpace between candles

Complete Example

typescript
import type { CandleChartData, ChartAnimation, LegendConfig, XAxisConfig, YAxisConfigDual } from '@nstudio/ncharts';

// Generate stock data
function generateStockData(count: number, startPrice: number): CandleDataEntry[] {
  const data = [];
  let price = startPrice;
  
  for (let i = 0; i < count; i++) {
    const change = (Math.random() - 0.5) * 10;
    const open = price;
    const close = price + change;
    const high = Math.max(open, close) + Math.random() * 5;
    const low = Math.min(open, close) - Math.random() * 5;
    
    data.push({
      x: i,
      shadowH: high,
      shadowL: low,
      open: open,
      close: close,
    });
    
    price = close;
  }
  
  return data;
}

const stockData: CandleChartData = {
  dataSets: [
    {
      label: 'AAPL Daily',
      values: generateStockData(30, 150),
      config: {
        increasingColor: '#10B981',
        decreasingColor: '#EF4444',
        increasingPaintStyle: 'FILL',
        decreasingPaintStyle: 'FILL',
        shadowColorSameAsCandle: true,
        shadowWidth: 1.5,
        barSpace: 0.1,
        highlightEnabled: true,
        highlightColor: '#3B82F6',
        highlightLineWidth: 1,
      },
    },
  ],
};

const animation: ChartAnimation = {
  durationX: 1000,
  durationY: 1000,
  easingX: 'EaseOutQuad',
};

const legend: LegendConfig = {
  enabled: true,
  horizontalAlignment: 'CENTER',
  verticalAlignment: 'BOTTOM',
  form: 'SQUARE',
};

const xAxis: XAxisConfig = {
  enabled: true,
  position: 'BOTTOM',
  drawGridLines: false,
  granularity: 1,
  labelRotationAngle: -45,
  textSize: 9,
};

const yAxis: YAxisConfigDual = {
  left: {
    enabled: true,
    drawGridLines: true,
    gridColor: '#E5E7EB',
    textSize: 10,
  },
  right: { enabled: false },
};

Template

html
<CandleStickChart
  #candleChart
  [data]="stockData"
  [animation]="animation"
  [legend]="legend"
  [xAxis]="xAxis"
  [yAxis]="yAxis"
  [touchEnabled]="true"
  [dragEnabled]="true"
  [scaleEnabled]="true"
  [pinchZoom]="true"
  [highlightPerTapEnabled]="true"
  [highlightPerDragEnabled]="true"
  (select)="onCandleSelect($event)"
  (deselect)="onDeselect()"
  class="h-80 w-full">
</CandleStickChart>

Selection Handling

typescript
onCandleSelect(event: ChartSelectEvent): void {
  const { data } = event;
  if (data.data) {
    const candle = data.data;
    console.log(`
      Open: ${candle.open}
      High: ${candle.shadowH}
      Low: ${candle.shadowL}
      Close: ${candle.close}
    `);
  }
}

Styling Variations

Hollow Candles (Traditional Japanese Style)

typescript
config: {
  increasingColor: '#10B981',
  decreasingColor: '#EF4444',
  increasingPaintStyle: 'STROKE',  // Hollow for up days
  decreasingPaintStyle: 'FILL',    // Filled for down days
}

Monochrome

typescript
config: {
  increasingColor: '#374151',
  decreasingColor: '#374151',
  increasingPaintStyle: 'STROKE',
  decreasingPaintStyle: 'FILL',
  shadowColor: '#374151',
}

See Also