# Vanilla API

> The create* mount functions, MountOptions, the ChartInstance methods, the chart and table lifecycle, and the render-state attributes.

`@opendata-ai/openchart-vanilla` mounts a spec into a DOM element without a framework. The React, Vue and Svelte components wrap these functions, so their props map onto the mount options below. Source: `vanilla/src/index.ts` and `vanilla/src/mount-types.ts`.

See it: [Frameworks](../integration-guide.md) shows the same chart mounted from each package.

```bash
npm install @opendata-ai/openchart-vanilla
```

Import the stylesheet once: `import "@opendata-ai/openchart-vanilla/styles.css"`.

## Mount functions

Each function takes a container element, a spec and optional mount options, and returns an instance. The spec kind decides the function.

| Function | Spec | Options | Returns |
| --- | --- | --- | --- |
| `createChart` | `ChartSpec \| LayerSpec` | `MountOptions` | `ChartInstance` |
| `createTable` | `TableSpec` | `TableMountOptions` | `TableInstance` |
| `createGraph` | `GraphSpec` | `GraphMountOptions` | `GraphInstance` |
| `createSankey` | `SankeySpec` | `SankeyMountOptions` | `SankeyInstance` |
| `createTileMap` | `TileMapSpec` | `TileMapMountOptions` | `TileMapInstance` |
| `createGeoMap` | `GeoMapSpec` | `GeoMapMountOptions` | `GeoMapInstance` |
| `createBarList` | `BarListSpec` | `BarListMountOptions` | `BarListInstance` |

Other entry points:

- `renderStaticSVG(spec, options?)` from `@opendata-ai/openchart-vanilla/static` returns an SVG string with no DOM. See [static rendering](../static-rendering.md).
- `createChartStory` from `@opendata-ai/openchart-vanilla/story` drives a chart through scroll steps.
- `exportGIF` from `@opendata-ai/openchart-vanilla/gif` encodes an entrance animation. See [Export API](./export.md#animated-gif-export).
- `createReader(container, chart, spec, onChange, options?)` adds reader pinning to a mounted chart. See [Authoring and reader APIs](./authoring.md).
- `createGeoReader(container, map, spec, onChange, options?)` does the same for a mounted `GeoMapInstance`.

## MountOptions

Options for `createChart`. It extends `ChartEventHandlers`, so every handler in [Events](./events.md#charteventhandlers) is also an option.

| Option | Type | Default | Description |
| --- | --- | --- | --- |
| `theme` | `ThemeConfig` | `undefined` | Theme overrides, merged over the spec's `theme`. |
| `darkMode` | `'auto' \| 'force' \| 'off'` | `'off'` | `'auto'` follows the system setting. See [DarkMode](./theme.md#darkmode). |
| `interaction` | `'read' \| 'select' \| 'edit'` | inferred | What the reader can do with chart elements. Omitted, `onEdit` gives `'edit'`, `onSelect` or `onDeselect` gives `'select'`, and anything else `'read'`. See [Interaction mode](./events.md#interaction-mode). |
| `renderer` | `'auto' \| 'svg' \| 'canvas'` | `'auto'` | Backend for point marks. `'auto'` moves scatters above 1,000 points to canvas. Ignored, with a console warning, for other marks and for faceted, layered and sparkline charts. See [Canvas mark mode](./marks.md#canvas-mark-mode). |
| `autoResize` | `boolean` | `true` | Re-render when the container resizes, through `ResizeObserver`. |
| `watermark` | `boolean` | `true` | Show the OpenChart watermark. Overrides the spec's `watermark`. |
| `selectedElement` | `ElementRef` | `undefined` | Element selected at mount. |
| `selectionColor` | `string` | theme `colors.categorical[0]` | Outline color for the selected element in select and edit modes. |
| `rowHover` | `boolean` | `undefined` | Quiet row inspection on simple horizontal bar charts. |
| `revision` | `string \| number` | `undefined` | Source revision stamped on emitted edit transactions. |

## ChartInstance

What `createChart` returns.

| Member | Signature | Description |
| --- | --- | --- |
| `update` | `(spec, options?: { revision?, selectedElement? }) => void` | Recompile and re-render with a new spec. Data changes tween when animation is on. Omitting `selectedElement` keeps the current selection. |
| `resize` | `() => void` | Recompile at the container's current size. `autoResize` usually calls it for you. |
| `setTheme` | `(options: { theme?: ThemeConfig, darkMode?: DarkMode }) => void` | Restyle in place with a new theme and dark mode, keeping selection, focus, an open tooltip and a waiting entrance. Both fields replace the current values, so pass the current `darkMode` when only the theme changes. |
| `replayEntrance` | `() => void` | Play the current chart's entrance again in any interaction mode, keeping selection and the armed placement tool. Does nothing without a resolved entrance, for playback specs or after destroy. Settles immediately under reduced motion. |
| `export` | `(format, options?) => string \| Promise<Blob> \| Promise<string>` | `'svg'` and `'csv'` return a string; `'svg-with-fonts'` a promise of a string; `'png'`, `'jpg'`, `'webp'` and `'gif'` a promise of a `Blob`. See [Export API](./export.md). |
| `destroy` | `() => void` | Remove the chart's DOM and disconnect its observers. |
| `layout` | `ChartLayout` (read-only) | The current compiled layout: dimensions, scales and marks. |
| `sampleLayout` | `() => ChartLayout` | The geometry on screen right now, including mid-transition, for hit testing. |
| `beginManualUpdate` | `(spec) => TransitionHandle \| null` | Render a new spec and return a transition you step yourself with `handle.step(elapsedMs)`, for frame capture. `null` when no transition applies. |
| `getSelectedElement` | `() => ElementRef \| null` | The selected element. |
| `select` | `(ref: ElementRef) => void` | Select an element. Does nothing if it isn't found. |
| `deselect` | `() => void` | Clear the selection. |
| `setInteraction` | `(mode: InteractionMode) => void` | Switch `'read'`, `'select'` or `'edit'` in place, keeping the SVG root, reader state and highlight without replaying the entrance. |
| `getInteraction` | `() => InteractionMode` | The current interaction mode. |
| `setPlacementTool` | `(tool: AnnotationPlacementTool \| null) => void` | Arm a one-shot annotation tool in edit mode, or disarm with `null`. Ignored outside edit mode. |
| `getPlacementTool` | `() => AnnotationPlacementTool \| null` | The armed tool, or `null`. |
| `isTextEditing` | `boolean` (read-only) | True while the inline text editor is open. |
| `setHighlight` | `(values: string[] \| null) => void` | Highlight these color-field values and re-render. `null` clears. |
| `resetDrawing` | `() => void` | Clear a `youDrawIt` guess and return to drawing. |
| `revealDrawing` | `() => void` | Reveal a `youDrawIt` chart, as the reveal button does. |

Every other instance has `update`, `resize` and `destroy`, and all but `GraphInstance` have `export`. `TableInstance`, `GeoMapInstance` and `RaceInstance` also have `setTheme`, with the same signature as the chart's; the map keeps its camera, focus and open tooltip. The other instances pick up a theme change by being recreated, which the framework wrappers do for you. `TableInstance` adds `getState()` and `setState()`, shown below. `GraphInstance` adds search, selection, highlight and camera methods (see [GraphSpec](./graph-spec.md#vanilla)). `GeoMapInstance` adds `zoomTo`, `panTo`, `fitBounds`, `resetView`, `getCamera`, `getCameras` (every current view camera) and `setCamera`, plus `setLabelInventory(options?)`, which replaces the label disclosure host without resetting the camera. The Sankey, bar list, tile map and map instances also carry a read-only `layout`.

### Interaction and placement

Switching between select and edit keeps the selection. Entering read clears it without firing `onDeselect`. Leaving edit also disarms any placement tool and calls `onPlacementToolChange(null)`. Setting the current mode again, or calling `setInteraction` after `destroy`, does nothing.

`chart.replayEntrance()` cancels any running update transition or entrance and renders one fresh entrance. It redraws the chart's SVG, so hosts holding the SVG element should re-query it after replay. It keeps the current interaction mode, selection and armed placement tool, including in edit mode. Ordinary updates still skip the entrance.

In select and edit modes, clicking a line or area endpoint label selects the same series as clicking its mark, including muted series.

`AnnotationPlacementTool` has this shape:

```ts
interface AnnotationPlacementTool {
  type: 'text' | 'range' | 'refline';
  axis?: 'x' | 'y';
}
```

For text, click or tap the plot to open a text box. For a range, drag across the plot; without `axis`, the drag direction chooses x or y. For a reference line, click or tap; without `axis`, it uses y when y is continuous, otherwise x. `axis` is ignored for text. Band axes snap to categories and temporal anchors use ISO strings.

Arming a tool focuses the plot. Arrow keys move a cursor by one category or 2% of the plot on continuous axes; Shift moves five steps. Enter places text or a reference line. For a range, Enter sets the start, arrows move the end, and Enter opens the label box. Enter in the box commits one `add` through `onEdit` and disarms the tool; text requires non-empty text, while a range or line can have no label. Apply the transaction and update the spec to keep the annotation.

Escape cancels the current drag or text box while keeping the tool armed; Escape with no placement in progress disarms it and restores focus. `onPlacementToolChange` reports engine disarms, including completion and leaving edit, but not host calls to `setPlacementTool`. An update cancels a placement in progress and keeps the tool armed. Faceted panels without scale inversions cannot place annotations; the tool stays armed without placing.

```ts
chart.setInteraction('edit');
chart.setPlacementTool({ type: 'range', axis: 'x' });
console.log(chart.getInteraction(), chart.getPlacementTool());
// Disarm from the host without firing onPlacementToolChange.
chart.setPlacementTool(null);
```

## Vanilla lifecycle

### Chart lifecycle

```ts
import { createChart } from "@opendata-ai/openchart-vanilla";

// Mount
const chart = createChart(container, spec, {
  darkMode: "auto",
  autoResize: true,
  onMarkClick: (event) => console.log(event.datum),
});

// Update spec (triggers full recompile + re-render)
chart.update(newSpec);

// Force resize (usually handled by ResizeObserver)
chart.resize();

// Read current layout for custom overlays or debugging
const layout = chart.layout;
console.log(layout.dimensions, layout.marks.length);

// Export
const svg = chart.export("svg");

// Clean up: removes DOM, disconnects observers
chart.destroy();
```

### Render-state attributes

The vanilla mount stamps two attributes on the container so external code (tests, screenshot pipelines) can observe render state without reaching into internals. These are set by every mount type: chart, sankey, barlist, and tilemap.

- `data-oc-fonts-state`: `"pending"` or `"ready"`. Webfonts (e.g. Inter via `display=swap`) often swap in after first paint, which changes text metrics and can rewrap titles or shift labels. On mount the value is `"pending"` if a font is still loading, and flips to `"ready"` exactly once, after the one-shot recompile that runs on `document.fonts.ready`. When the font is already cached (typical on desktop), it starts at `"ready"`. Crucially, `"ready"` means the layout reflects final font metrics: if an entrance animation is in flight, the flip is deferred until the recompile actually runs, so it never claims ready while layout still uses fallback metrics.
- `data-oc-render-gen`: a counter that increments on every full render (initial mount, `update()`, `resize()`, and the post-font recompile). Watch it to detect that a re-render happened.

Wait for `data-oc-fonts-state="ready"` before screenshotting so you capture final-font layout rather than a fallback-metric frame:

```ts
const chart = createChart(container, spec, { autoResize: false });

// Wait until the layout reflects the final webfont metrics.
if (container.dataset.ocFontsState !== "ready") {
  await new Promise<void>((resolve) => {
    const observer = new MutationObserver(() => {
      if (container.dataset.ocFontsState === "ready") {
        observer.disconnect();
        resolve();
      }
    });
    observer.observe(container, {
      attributes: true,
      attributeFilter: ["data-oc-fonts-state"],
    });
  });
}

const png = await chart.export("png", { dpi: 2 });
```

In Playwright the same wait is one line: `await page.waitForSelector('[data-oc-fonts-state="ready"]')`.

### Table lifecycle

```ts
import { createTable } from "@opendata-ai/openchart-vanilla";

const table = createTable(container, tableSpec, {
  darkMode: "auto",
  autoResize: true,
  onRowClick: (row) => console.log(row),
  onStateChange: (state) => {
    // state: { sort: SortState | null, search: string, page: number }
    console.log("Table state:", state);
  },
});

// Update spec
table.update(newTableSpec);

// Read/set state programmatically
const state = table.getState();
table.setState({ sort: { column: "name", direction: "asc" } });
table.setState({ search: "query", page: 0 });

// Export
const csv = table.export("csv");

// Clean up
table.destroy();
```

### Controlled table (vanilla)

Pass `externalState` in mount options to use controlled mode:

```ts
const table = createTable(container, tableSpec, {
  externalState: { sort: null, search: "", page: 0 },
  onStateChange: (state) => {
    // Update your external state store, then call table.setState()
    myStore.update(state);
    table.setState(state);
  },
});
```
