Skip to content

Chart

The main entry point. Creates and manages the two-canvas rendering surface, panels, tools, and pointer events.

Constructor

ts
new Chart(container: HTMLElement | string, options?: ChartOptions)
ParameterTypeDescription
containerHTMLElement | stringDOM element or CSS selector. Must have a defined width and height.
options.candlesCandle[]Initial candle data.
options.timeframeTimeframeInitial timeframe label ('1m', '5m', '15m', '1h', '4h', '1d').
ts
const chart = new Chart('#chart', {
  candles: myCandles,
  timeframe: '1h',
});

Panel Management

addPanel(panel)

ts
chart.addPanel(panel: Panel): this

Appends a panel and triggers a redraw. Panels stack vertically in insertion order.

removePanel(panel)

ts
chart.removePanel(panel: Panel): this

Removes a panel by reference.

getPanel(Class)

ts
chart.getPanel(RSIPanel): RSIPanel | undefined

Returns the first panel matching the given class, or undefined.

Tool Management

addTool(tool)

ts
chart.addTool(tool: DrawingTool): this

Registers a drawing tool. Registered tools receive pointer events when active and their draw() is called on every redraw.

activateTool(tool | null)

ts
chart.activateTool(trendLine)  // activate
chart.activateTool(null)       // deactivate all

Sets the active tool. Only one tool is active at a time.

Data

setCandles(candles)

ts
chart.setCandles(candles: Candle[]): this

Replaces the full dataset and resets the viewport to show all candles.

appendCandle(candle)

ts
chart.appendCandle(candle: Candle): this

Appends a single candle. If the viewport is at the trailing edge, it advances automatically.

getCandles()

ts
chart.getCandles(): Candle[]

Returns the current candle array (mutable reference).

getViewport()

ts
chart.getViewport(): ViewPort

Returns a copy of { start, end }.

View Controls

fitAll()

ts
chart.fitAll(): this

Resets viewport to show all candles.

zoom(factor, pivotX)

ts
chart.zoom(factor: number, pivotX: number): void

Zooms in (factor < 1) or out (factor > 1) around a pixel X position.

ts
// Zoom in at center
chart.zoom(1 / 1.3, container.clientWidth / 2);
chart.redraw();

redraw()

ts
chart.redraw(): void

Forces a full redraw of all panels and tools. Called automatically on most state changes.

Timeframe

setTimeframe(tf)

ts
chart.setTimeframe('5m'): this

Sets the timeframe label (used for time-axis formatting). Fires timeframeChange.

getTimeframe()

ts
chart.getTimeframe(): Timeframe

Events

on(event, handler) / off(event, handler)

ts
chart.on('viewportChange', (vp: ViewPort) => { ... });
chart.on('timeframeChange', (tf: Timeframe) => { ... });
chart.off('viewportChange', handler);

Lifecycle

destroy()

ts
chart.destroy(): void

Removes canvases and clears all event listeners.

Types

ts
interface Candle {
  open:      number;
  high:      number;
  low:       number;
  close:     number;
  timestamp: number;   // Unix ms
  volume?:   number;
}

interface ViewPort {
  start: number;  // fractional candle index
  end:   number;
}

type Timeframe = '1m' | '5m' | '15m' | '1h' | '4h' | '1d';

Released under the MIT License.