Chartsv26.10.18
Standard edition

Getting started

Install the packages, prepare bars, render a chart in React or with the custom element, apply a theme and drive the core directly.

This page takes you from an empty project to a chart with data, a theme and a locale. Both charts below are rendered on this page by the library itself, and the same chart appears in the Pro edition - Pro adds surfaces on top of it instead of replacing it:

Before you start

  • Node.js 18 or newer and a package manager (pnpm, npm or yarn).
  • Historical bars of the instrument you want to show, as an array of plain objects.
  • For live updates: a feed that pushes bars or ticks, for example over WebSocket or SSE.

Install the packages

pnpm add @unifin/chart-react     # React components and the core
pnpm add @unifin/chart-element   # the <unifin-chart> custom element
pnpm add @unifin/chart-core      # framework-free core only

Install the package that matches your host: chart-react already depends on the core, so a React application does not install chart-core separately.

Add the stylesheet once

The look of the kit controls (panels, dialogs, toolbars, the data table) ships as a stylesheet asset of the binding, not as code inside it, so a page pays for the CSS once and no CSS text travels in the JavaScript bundle:

import '@unifin/chart-react/styles.css';   // once, before the first kit control renders

A host that cannot load a file (a widget built by hand, a test harness) hands the text over instead: ensureUiStyles(document, { styles }). The chart itself needs no stylesheet at all.

Prepare the data

Bars and ticks are plain objects - no adapters, no classes:

type Bar = {
  time: number;      // UTC milliseconds of the bar open
  open: number;
  high: number;
  low: number;
  close: number;
  volume: number;
};

type Tick = { time: number; price: number; size: number; side?: 'buy' | 'sell' };

Note time is always UTC milliseconds of the bar open, never a local time and never seconds. Bars must be sorted by time; the library keeps the order it receives.

Create a chart in React

import { DARK_THEME, UnifinChart } from '@unifin/chart-react';

export function Terminal({ bars }: { bars: Bar[] }) {
  return (
    <UnifinChart
      symbol="AAPL"
      timeframe="5m"
      bars={bars}
      instrument={{ symbol: 'AAPL', tickStep: 0.01, digits: 2 }}
      seriesType="candles"
      theme={DARK_THEME}
      locale="en"
      height={480}
      onReady={(chart) => console.log('chart ready', chart)}
    />
  );
}

The component owns the canvas and forwards props to the core. Series, scales and interaction state live in the core, so nothing is duplicated in React state - read values through the handle from onReady or listen to events.

Create a chart on a plain page

<unifin-chart symbol="AAPL" timeframe="5m" theme="dark" series-type="candles"></unifin-chart>
<script type="module">
  import { defineUnifinChart } from '@unifin/chart-element';

  defineUnifinChart();
  const chart = document.querySelector('unifin-chart');
  chart.bars = await loadBars('AAPL', '5m');
  chart.addEventListener('unifin-ready', (event) => console.log(event.detail));
</script>

Attributes cover scalars, properties take data (bars, overlays, drawings), and events arrive as CustomEvent. The element sets role, aria-label and a keyboard map on the host unless you provide your own.

Apply a theme

theme accepts dark, light, corporate, capitalMarkets, orange, unifin and system (system follows prefers-color-scheme), or a theme object you build yourself:

<UnifinChart symbol="AAPL" timeframe="5m" bars={bars} theme="light" height={420} />

Every name resolves through the registry exported by the core, so a host can also pass the palette object directly:

import { CHART_THEMES, resolveThemeByName } from '@unifin/chart-core';

const corporate = CHART_THEMES.corporate;
const unifin = resolveThemeByName('unifin');
NameLookFits
darkNavy surface, blue accentTerminals, night layouts (default)
lightNear-white surface, deep blue accentDocuments, printing, daylight
corporateFlat light grey, restrained blueReports, portfolio and screening pages
capitalMarketsCharcoal surface, brass accent, compact densityDense professional terminals
orangeNear-black surface, amber accent, monospace readoutTrading desks and wall boards
unifinBrand navy with a teal accentBrand pages and product demos
systemdark or light by host preferenceShells that follow the OS

The switch never rebuilds the chart: a palette is a set of token values, so only the paint pass runs.

Colors, radius, font and row density come from the host design tokens (--ud-*, --ud-radius, --ud-density), so a white-label shell restyles the chart without touching layout.

Set the locale, the profile and the hover hold

OptionValuesWhat it changes
localeru, en, uz, kzAxis labels, numbers, dates, accessibility text
renderProfilesparkline, mini, standard, terminalWhich layers are painted and how dense they are
seriesTypeAny registered series typeHow bars are drawn (see Series types)
priceAxisWidthPixelsWidth of the value axis; 0 hides it
tooltipHoldMillisecondsHow long the hover card and the crosshair readout stay after the pointer leaves

The hover card does not vanish with the pointer: the last hover is kept for a hold window so the values stay readable. The default is five seconds (TOOLTIP_HOLD_MS in the core), any value up to 60000 ms is accepted, and 0 hides the card the moment the pointer leaves. React takes the prop, plain HTML takes the attribute:

<unifin-chart symbol="AAPL" timeframe="5m" tooltip-hold="8000"></unifin-chart>
<UnifinChart symbol="AAPL" timeframe="5m" bars={bars} tooltipHold={8_000} />
import { Canvas2DRenderer, DARK_THEME, Series, PriceScale, TimeScale } from '@unifin/chart-core';

const series = new Series('AAPL', '5m');
series.setHistory(bars);

const timeScale = new TimeScale({ width: 896, barSpacing: 5 });
timeScale.setBarCount(series.length);
timeScale.setVisibleRange(series.length - 90, series.length);

const priceScale = new PriceScale();
priceScale.setHeight(540);
priceScale.fitContent(series.toArray(), series.length - 90, series.length);

new Canvas2DRenderer().render(context, {
  bars: series.toArray(),
  width: 960,
  height: 540,
  timeScale,
  priceScale,
  theme: DARK_THEME,
  seriesType: 'candles',
  timeframe: '5m',
  locale: 'en',
  priceAxisWidth: 64,
  renderProfile: 'standard',
});

Drive the core directly

Hosts that own the canvas call the renderer themselves. The snippet above is the whole contract: one TimeScale, one PriceScale and the bars. Re-render on resize and on every data update; the renderer keeps no global state.

Caution A canvas with zero height paints nothing. Give the container a width and a height before the first render, and re-render after a layout change.

See also

On this page