d3pict
v0.3.2
Published
Depict your data: SVG charts built from objects, on top of d3. Interactive, animated, and yours to extend, in plain TypeScript, Vue or React.
Maintainers
Readme
Why D3Pict
Most chart libraries hand you a finished chart with a few knobs. d3 hands you a blank page. D3Pict sits in between: you assemble a chart from small objects (a series, two axes, a renderer), and everything else, a tooltip, pan and zoom, a legend, a title, is one more object you add. Or don't.
That one idea has some nice consequences:
- Polar is a change of words. Turn
position: 'left'into'radius'and'bottom'into'angle': the same series, renderer and tooltip now draw a polar chart. - Two charts that zoom together share one
D3PAxisDomain. That is the whole API for it, and it works between a cartesian chart and a polar one. - The legend is not special. Titles, axis titles and legends are built with the same public pieces you get. Replacing one is writing the same thing, not fighting it.
- New data just moves.
series.data = [...series.data, row], and the points glide to where they belong. Each point is matched with the one that has the same x, so nothing swaps places.
Install
npm install d3pict d3d3 is a peer dependency. vue and react are optional: you only need them for d3pict/vue and d3pict/react. No build step? Import from a CDN, as below.
A first chart
Save this as chart.html and open it. Nothing to install.
<div id="app" style="height: 360px"></div>
<script type="module">
import { D3PAxis, D3PAxisDomain, D3PChart, D3PLineRenderer, D3PSeries, getGlobalBus } from 'https://esm.sh/d3pict'
const bus = getGlobalBus() // the channel the objects talk through
// your data: rows of [x, value]
const series = new D3PSeries({ bus, name: 'temperature', data: [[0, 12], [4, 9], [8, 14], [12, 21], [16, 24], [20, 17], [24, 13]] })
// two axes: each one shows a range of values
const x = new D3PAxis({ bus, name: 'hour', domain: new D3PAxisDomain({ bus, domain: [0, 24] }) })
const y = new D3PAxis({ bus, name: 'temperature', domain: new D3PAxisDomain({ bus, domain: [0, 30] }), position: 'left' })
// a renderer draws a series; its style is a prop
const line = new D3PLineRenderer({ bus, series, props: { style: { color: '#e15759', curve: 'monotone', strokeWidth: 2.5 } } })
// a chart shows what you attach to it
const chart = new D3PChart({ container: document.getElementById('app'), bus })
for (const object of [x, y, line]) object.addToChart(chart)
</script>That is a chart. The guide adds one thing at a time, with the lines that change at each step, and every example on the site has an Open in CodePen button.
Then add what you need
Everything below is one more object attached to the chart, or one prop on an object.
| You want | You add |
|---|---|
| A crosshair and a tooltip | new D3PCursorIndicator({ bus }), new D3PTooltip({ bus }) |
| Pan and zoom | a D3PToolbox with a D3PPanTool and a D3PWheelZoomTool |
| A title, axis titles, a legend | D3PTitle, D3PAxisTitle, D3PLegendPanel |
| Several lines | one more D3PSeries and one more renderer |
| Bars, areas, candlesticks, a pie... | another renderer: D3PBarRenderer, D3PAreaRenderer, D3PCandlestickRenderer, D3PPieRenderer |
| A polar chart | position: 'angle' and position: 'radius' on the axes |
| Axes that fit the data | a D3PAutoDomain linked to the series |
| New data | series.data = [...series.data, [28, 15]], and the chart animates |
| Charts that move together | give their axes the same D3PAxisDomain |
| A file | chart.downloadSVG('chart.svg'), chart.downloadPNG('chart.png') |
Vue and React
In a component, useD3PContext() makes the objects with the bus of the app and destroys them when the component goes away. ctx.newSeries({ ... }) does what new D3PSeries({ bus, ... }) does, for every object.
<script setup lang="ts">
import { D3PVChart, useD3PContext } from 'd3pict/vue'
const ctx = useD3PContext()
const series = ctx.newSeries({ name: 'temperature', data: [[0, 12], [4, 9], [8, 14], [12, 21], [16, 24]] })
const x = ctx.newAxis({ name: 'hour', domain: ctx.newAxisDomain({ domain: [0, 16] }) })
const y = ctx.newAxis({ name: 'temperature', domain: ctx.newAxisDomain({ domain: [0, 30] }), position: 'left' })
const objects = [x, y, ctx.newLineRenderer({ series })]
</script>
<template>
<div style="height: 360px"><D3PVChart :objects="objects" /></div>
</template>import { createApp } from 'vue'
import { D3PVPlugin } from 'd3pict/vue'
createApp(App).use(D3PVPlugin) // the app gets a bus of its ownimport { useEffect, useState } from 'react'
import type { D3PChartObject, D3PContext } from 'd3pict'
import { D3PRChart, useD3PContext } from 'd3pict/react'
function build(ctx: D3PContext): D3PChartObject[] {
const series = ctx.newSeries({ name: 'temperature', data: [[0, 12], [4, 9], [8, 14], [12, 21], [16, 24]] })
const x = ctx.newAxis({ name: 'hour', domain: ctx.newAxisDomain({ domain: [0, 16] }) })
const y = ctx.newAxis({ name: 'temperature', domain: ctx.newAxisDomain({ domain: [0, 30] }), position: 'left' })
return [x, y, ctx.newLineRenderer({ series })]
}
export function Chart() {
const ctx = useD3PContext()
const [objects, setObjects] = useState<D3PChartObject[]>()
useEffect(() => setObjects(build(ctx)), [ctx]) // make the objects in an effect, not while rendering
return objects && <div style={{ height: 360 }}><D3PRChart objects={objects} /></div>
}The components D3PVTooltip and D3PRTooltip (and the legend, slot and anchor ones) put your own markup in the tooltip, the legend or anywhere on the chart. See Vue and React.
Chart types
Lines (curves, steps, gaps), areas (stacked, 100 %, streamgraph, ranges), bars (grouped, stacked, horizontal, lollipops, waterfalls, histograms), scatter and bubbles, candlesticks, box plots, dumbbells, heatmaps, slope charts, annotations, pies, donuts and semi-donuts, polar areas, radial bars, radars, gauges and funnels. Each has a page with its code in the examples, and the showcase puts most of them side by side.
Documentation
- Guide: one chart, built in eleven steps, with what changes at each step, for Core, Vue and React.
- Concepts: the mental model, notion by notion.
- Advanced: the bus, the services, the render loop, projections, the animation, writing your own components.
- API reference: generated from the source and its comments.
- Examples: live, with the code to copy.
For coding agents
The package ships an agent skill (skills/d3pict/SKILL.md): the model, a working chart, the recipes and the pitfalls, so that an agent writes D3Pict code without guessing. After installing, make it visible to your agent, for instance for Claude Code:
mkdir -p .claude/skills && cp -r node_modules/d3pict/skills/d3pict .claude/skills/(or ~/.claude/skills/ for all your projects). Any agent that reads SKILL.md files can use it from node_modules/d3pict/skills/d3pict/, and the typings in node_modules/d3pict/dist/*.d.ts give it the exact API.
Working on D3Pict itself:
pnpm install
pnpm test # the tests
pnpm typecheck
pnpm build # the library, in dist/
pnpm docs:dev # the documentation site, with the API reference generated first