OrbCharts Class
OrbCharts is the entry point to the library. You create an instance from a DOM container and an options object, then use its methods to dynamically update data, Encoding, Plugins, Theme, and size—and release resources when you no longer need it.
import { OrbCharts } from '@orbcharts/core'
const chart = new OrbCharts(element, options)Constructor
new OrbCharts(element: HTMLElement | Element, options?: PartialChartOptions)| Parameter | Type | Description |
|---|---|---|
element | HTMLElement | Element | The chart's container DOM. Must have an explicit width and height, or the chart won't render. |
options | PartialChartOptions | Optional initial configuration; any field not provided uses its default. |
All fields of PartialChartOptions are optional:
interface PartialChartOptions {
size?: SizeConfig
theme?: DeepPartial<Theme>
data?: RawData
encoding?: DeepPartial<Encoding>
plugins?: PluginEntity[]
}| Field | Description | Related method |
|---|---|---|
size | Width, height, and resize settings | resize |
theme | Colors, light/dark mode, font size | Theme |
data | The raw data RawData | setData |
encoding | Field mapping, sorting, aggregation | Encoding |
plugins | The array of Plugins to apply | setPlugins |
import { OrbCharts } from '@orbcharts/core'
import type { RawData } from '@orbcharts/core'
import { GridPlot } from '@orbcharts/plugin-basic'
const data: RawData = [
{ series: 'A', category: 'Q1', value: 30 },
{ series: 'A', category: 'Q2', value: 55 },
]
const chart = new OrbCharts(document.querySelector('#chart')!, {
data,
theme: { colorScheme: 'dark' },
plugins: [new GridPlot({ Bar: {}, CategoryAxis: {}, ValueAxis: {} })],
})Tip:
themeandencodingaccept aDeepPartial, so you only override the fields you want to change; the rest keep their defaults (deep merge).
Data
setData
setData(data: RawData): voidFully replaces the chart's raw data and re-renders. For the structure of RawData and each dimension's format, see Data formats overview.
chart.setData([
{ series: 'A', value: 30 },
{ series: 'B', value: 70 },
])Encoding
Encoding determines how raw-data fields map to the chart's dimensions (dataset, series, category, value, etc.), and lets you specify sorting and aggregation. See Encoding for the full field reference.
updateEncoding
updateEncoding(patch: DeepPartial<Encoding>): voidPartially updates the Encoding, deep-merged with the current value. Ideal for tweaking a single setting.
// Only remap the value field to "amount"; everything else stays the same
chart.updateEncoding({ value: { from: 'amount' } })forceReplaceEncoding
forceReplaceEncoding(full: Encoding): voidReplaces the current settings entirely with a complete Encoding object. Fields you omit do not keep their old values.
getEncoding
getEncoding(): Readonly<Encoding>Returns the currently effective Encoding (read-only).
Plugins
Plugins do the actual rendering. For operations common to all Plugins, see Plugin API.
setPlugins
setPlugins(plugins: PluginEntity[]): voidReplaces all current Plugins with a new array.
import { GridPlot, Tooltip } from '@orbcharts/plugin-basic'
chart.setPlugins([new GridPlot({ Line: {}, CategoryAxis: {}, ValueAxis: {} }), new Tooltip()])addPlugin
addPlugin(plugin: PluginEntity): voidAdds a single Plugin without affecting the other existing Plugins.
import { Legend } from '@orbcharts/plugin-basic'
chart.addPlugin(new Legend())removePlugin
removePlugin(id: string): voidRemoves a Plugin by its id.
chart.removePlugin('Legend')Theme
updateTheme
updateTheme(patch: DeepPartial<Theme>): voidPartially updates the theme, deep-merged with the current value. See Theme for the full structure.
chart.updateTheme({ colorScheme: 'dark' })forceReplaceTheme
forceReplaceTheme(full: Theme): voidReplaces the current theme entirely with a complete Theme object.
getTheme
getTheme(): Readonly<Theme>Returns the currently effective theme (read-only).
Sizing
resize
resize(sizeConfig: SizeConfig): voidSets the chart size and auto-resize behavior.
interface SizeConfig {
width: number | 'auto'
height: number | 'auto'
resizeDebounce: number
}| Field | Type | Default | Description |
|---|---|---|---|
width | number | 'auto' | 'auto' | Width (px); 'auto' fills the container |
height | number | 'auto' | 'auto' | Height (px); 'auto' fills the container |
resizeDebounce | number | 50 | Debounce delay (ms) for recomputing on container resize |
// Fixed width, auto height, with a shorter debounce
chart.resize({ width: 800, height: 'auto', resizeDebounce: 30 })Tip:
'auto'follows the container's size, so the container itself must have an explicit width and height. Sizing and the container mechanism are the most common beginner pitfall.
Lifecycle
destroy
destroy(): voidReleases all resources, removes event listeners, and cleans up its Plugins as well.
chart.destroy()Tip: On component unmount (React's
useEffectcleanup, Vue'sonUnmounted), always callchart.destroy()to avoid memory leaks and dangling event subscriptions.
The context property
chart.context: ChartContextcontext exposes the chart's internal RxJS streams (such as event$, encoding$, theme$) and root nodes (root, svg, canvas) for advanced subscription and integration. For event usage see Events; for the full list of streams see TypeScript Types.
Related
- Encoding — field mapping, sorting, and aggregation
- Theme — colors and light/dark mode
- Plugin API — operations common to all Plugins
- Events — listening to and triggering chart events
- TypeScript Types — the full list of types