Skip to content

Rendering in code

The render() function is the primary programmatic entry point for @blueprint-chart/lib. It accepts a BPC source string and returns a chart handle — an object with methods to produce SVG, PNG, or a live DOM mount.

Node — write SVG and PNG to disk

ts
import { render } from '@blueprint-chart/lib'
import { writeFile } from 'node:fs/promises'

const chart = await render(bpc)
await writeFile('chart.svg', await chart.toSvg())
await writeFile('chart.png', await chart.toPng({ width: 1200 }))

PNG export and headless SVG in Node require optional native deps (jsdom, @napi-rs/canvas, @resvg/resvg-js). These are installed automatically as optionalDependencies. If a binary failed to install, the Node methods throw MissingNodeRenderDepsError naming what to reinstall.

Browser — mount and step scenes

ts
const { mount, scene } = await render(bpc)
mount('#chart')   // element or selector
scene(2)          // advance to scene index 2

mount accepts an HTMLElement or a CSS selector string and returns the handle for chaining. scene(n) steps to scene index n and is also chainable.

Options

Pass a second argument to render() to control the output:

OptionDefaultDescription
themesystem'light' or 'dark'
scene0Initial scene index
width640Output width in px (headless only)
height400Output height in px (headless only)
frametruefalse renders frameless (no title/source/etc.)
ts
const chart = await render(bpc, { theme: 'dark', width: 1200, height: 630 })

The handle

render() resolves to a ChartHandle. Its methods are bound — destructure freely — and mount/scene are chainable.

MethodBrowserNodeReturns
toSvg(opts?)Promise<string>
toHtml(opts?)Promise<string>
toPng(opts?)❌ throws PngBrowserUnsupportedErrorPromise<Uint8Array>
mount(target)no-op (warns)ChartHandle
scene(n)ChartHandle

See the API reference for the full surface of @blueprint-chart/lib, including low-level rendering primitives and the chart-type registry.

Released under the MIT License. Built static-first — your data never leaves the page.