Version v26.11.20 Size 25.9 KB gzip
Why Prism?
Prism renders charts as SVG in a host element and keeps application state outside the chart. You can replace chart data explicitly, respond to container resizing, and style the output with CSS custom properties.
ts
import { createLineChart } from '@vielzeug/prism';
const host = document.createElement('div');
host.style.cssText = 'width:640px;height:320px';
document.body.append(host);
// Before: replace a hand-drawn SVG each time values change.
function drawLine(values: number[]) {
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
const low = Math.min(...values);
const span = Math.max(...values) - low || 1;
const points = values.map((value, index) => {
const x = (index / Math.max(1, values.length - 1)) * 640;
const y = 320 - ((value - low) / span) * 320;
return `${index === 0 ? 'M' : 'L'}${x},${y}`;
});
svg.setAttribute('viewBox', '0 0 640 320');
path.setAttribute('d', points.join(' '));
path.setAttribute('fill', 'none');
path.setAttribute('stroke', 'currentColor');
svg.append(path);
host.replaceChildren(svg);
}
drawLine([10, 25, 18]);
drawLine([10, 25, 18, 32]);
// After: keep the chart instance and replace its series data.
const chart = createLineChart(host, {
a11y: { ariaLabel: 'Revenue by month' },
series: [
{
data: [
{ key: 1, value: 10 },
{ key: 2, value: 25 },
{ key: 3, value: 18 },
],
name: 'Revenue',
},
],
});
chart.update([
{
data: [
{ key: 1, value: 10 },
{ key: 2, value: 25 },
{ key: 3, value: 18 },
{ key: 4, value: 32 },
],
name: 'Revenue',
},
]);
chart.dispose();| Feature | Prism | Hand-written SVG |
|---|---|---|
| Bundle size | 25.9 KB | No chart package |
| Zero dependencies | ||
| Data updates | ChartHandle.update() replaces chart data | You implement the update path |
| Responsive sizing | ResizeObserver recalculates the chart layout | You implement the resize behavior |
| Chart interactions | Built-in pointer and keyboard interactions on supported charts | You implement them |
| SVG accessibility | Informative charts support an accessible name and keyboard focus | You implement the accessible behavior |
Use Prism when you need chart factories, responsive SVG output, and an explicit data-update boundary without adopting a charting framework.
Consider hand-written SVG when you need a single specialized graphic and prefer to own its scales, resizing, labels, and interactions directly.
Installation
sh
pnpm add @vielzeug/prismsh
npm install @vielzeug/prismsh
yarn add @vielzeug/prismQuick Start
Create and size a host element before creating the chart. Import theme.css once to load Prism's default styles and CSS custom properties.
ts
import { createLineChart } from '@vielzeug/prism';
import '@vielzeug/prism/theme.css';
const host = document.createElement('div');
host.style.cssText = 'width:640px;height:320px';
document.body.append(host);
const chart = createLineChart(host, {
a11y: { ariaLabel: 'Revenue by month' },
series: [
{
data: [
{ key: 1, value: 10 },
{ key: 2, value: 25 },
{ key: 3, value: 18 },
],
name: 'Revenue',
},
],
tooltip: true,
});
chart.update([
{
data: [
{ key: 1, value: 10 },
{ key: 2, value: 25 },
{ key: 3, value: 18 },
{ key: 4, value: 32 },
],
name: 'Revenue',
},
]);
chart.dispose();Features
createLineChart()/createAreaChart(): plot continuous numeric or date-keyed seriescreateBarChart(): compare categories with grouped, stacked, or horizontal barscreateScatterChart(): plot independent numeric(x, y)pointscreatePieChart(): render pie, donut, or semi-circle slicescreateRadarChart(): compare series across fixed, labelled axescreateHeatmapChart(): render a row/column matrix of colored cells with a continuous or binned rampcreateSparkline(): render compact line, area, bar, or stacked values without axesChartHandle.update(data): replace chart data without handing state ownership to PrismChartHandle.dispose()/Symbol.dispose: release the SVG, observer, overlays, and chart-owned animationChartHandle.tap(): observe resize and disposal eventssetTheme()/resetTheme(): set supported palette, typography, axis, grid, and tooltip tokens globally or on one element- CSS custom properties: style the chart palette, marks, axes, labels, tooltip, legend, and motion
- Resize observation: recalculate SVG layout when the host element changes size
- Accessible interaction: labelled interactive charts expose keyboard navigation and polite value announcements; sparklines are pointer-only without value announcements