GitHub - toddynho/chartkit

GitHub

8 min read Original article ↗

Charts an agent can write and a server can render.

A chart is one JSON spec. validate() and repair() check and fix it locally, with no model call and no hosted service. The same spec draws as SVG in React, Vue, Svelte, Solid, Qwik, Angular, Astro, plain DOM, or Node.

A bar chart of revenue by region, drawn by the example below.

import { render } from "@chartkitdev/core/all";

const data = [{ region: "NA", revenue: 42 }, { region: "EU", revenue: 31 }, { region: "APAC", revenue: 24 }];
const a11y = { alt: "North America leads Q3 revenue at $42M." };
const svg = render({ version: "1", type: "bar", data, encoding: { x: "region", y: "revenue" }, a11y });

svg is a string. It needs no browser, so the same call works in Node, in a build step, or in an edge worker.

npm install @chartkitdev/core

Why use it

  • It renders on the server, fast. A 48-bar chart takes 0.26 ms in Node. ECharts takes 5.45 ms, Observable Plot 2.26 ms and Vega-Lite 8.47 ms. No library we measured was faster on the same chart.
  • Agents write less to get a working chart. A spec is shorter than chart code, so it costs fewer output tokens on every model we tested. When a spec is wrong, repair() fixes it without asking the model again.
  • One spec, every stack. Nine adapters draw the same JSON, and each one prints the SVG on the server. Core has zero runtime dependencies.

A line chart is 32 KB gzip. uPlot is smaller at 24 KB. We publish the benchmarks we lose as well as the ones we win: BENCHMARKS.md.

Nothing to install: ChartKit Cloud does the same render over HTTP. Send a spec to /render and get an SVG or PNG back. Agents can call its remote MCP, which has three tools: render_chart, validate_chart and repair_chart. It goes live at cloud.chartkit.dev with v1. The service is in apps/cloud.

Docs | Gallery | Playground | Compare | Schema | Roadmap | llms.txt

Use it in your framework

Every adapter takes the same spec and adds no chart options of its own.

React

npm install @chartkitdev/core @chartkitdev/react
import { Chart } from "@chartkitdev/react";

function Panel({ spec }) {
  return <Chart spec={spec} />;
}

Plain JavaScript and Node

npm install @chartkitdev/core @chartkitdev/vanilla
import { mount, renderToString } from "@chartkitdev/vanilla";

const svg = renderToString(spec); // on the server

const chart = mount(document.querySelector("#chart"), spec); // in the browser
chart.update(nextSpec);
chart.destroy();

Vue

npm install @chartkitdev/core @chartkitdev/vue
<script setup>
import { Chart } from "@chartkitdev/vue";
</script>

<template>
  <Chart :spec="spec" />
</template>

Svelte

npm install @chartkitdev/core @chartkitdev/svelte
<script>
  import { Chart } from "@chartkitdev/svelte";
  export let spec;
</script>

<Chart {spec} />

The component compiles in Svelte 4 and Svelte 5.

More frameworks: Astro, Solid, Qwik, Angular and the <chart-kit> element

Astro (@chartkitdev/astro)

---
import { Chart } from "@chartkitdev/astro";
---

<Chart spec={spec} />

The chart is static SVG with no client script. The build fails on an invalid spec. Add the client prop for tooltips and selection.

Solid (@chartkitdev/solid)

import { Chart } from "@chartkitdev/solid";

export function Panel() {
  return <Chart spec={spec} />;
}

Qwik (@chartkitdev/qwik)

import { component$ } from "@builder.io/qwik";
import { Chart } from "@chartkitdev/qwik";

export const Panel = component$(() => <Chart spec={spec} />);

Angular (@chartkitdev/angular)

import { Component } from "@angular/core";
import { Chart } from "@chartkitdev/angular";

@Component({
  standalone: true,
  imports: [Chart],
  template: `<ck-chart [spec]="spec" />`,
})
export class Panel {
  spec = spec;
}

Custom element (@chartkitdev/element)

<script type="module">
  import "@chartkitdev/element";
</script>

<chart-kit>
  <script type="application/json">
    {"version":"1","type":"line","data":[{"week":"W1","baskets":12},{"week":"W4","baskets":22}],"encoding":{"x":"week","y":"baskets"},"a11y":{"alt":"Baskets rose."}}
  </script>
</chart-kit>

Each of these also prints the SVG during server rendering. Their READMEs are in packages/.

By default an adapter can draw every chart type. To ship one type, pass render from createRenderer([...]).

Validate and repair

import { repair, validate } from "@chartkitdev/core/validate";

const draft = {
  version: 1,
  type: "line",
  data: { values: [{ month: "Jan", sales: 3 }, { month: "Feb", sales: 5 }] },
  encoding: { x: "month", y: "sales" },
};

validate(draft).ok; // false
const fixed = repair(draft);
fixed.ok; // true
fixed.applied; // the JSON Patch operations it made

validate() returns coded diagnostics (CK1003 and so on), and each one carries a JSON Patch fix where a fix is safe. repair() applies those fixes. Both are deterministic and run in your process. repairAsync() from @chartkitdev/core/validate/convert also converts Chart.js, ECharts, Plotly, Vega-Lite and Recharts configs.

In our agent eval, a model with no ChartKit docs got 21 of 42 specs right on its first answer. Recharts, ECharts and Chart.js got all 42. Running repair() on ChartKit's answers made all 42 valid, with no second model call. The method and the full results are in BENCHMARKS.md.

For agents:

  • llms.txt is the short spec reference, with an example for most chart types.
  • npx -y @chartkitdev/mcp is a local MCP server with validate, repair and render tools. See packages/mcp.

The spec

{
  "version": "1",
  "type": "line",
  "data": [
    { "quarter": "Q1", "units": 40 },
    { "quarter": "Q2", "units": 55 }
  ],
  "encoding": { "x": "quarter", "y": "units" },
  "a11y": { "alt": "Units rose from Q1 to Q2." }
}
  • version is "1".
  • type is the chart type.
  • data is a plain array of rows.
  • encoding maps fields to x, y, color and the other channels.
  • a11y.alt is one sentence that says what the chart shows. It is required.

The site catalog has 41 types: the 39 ids in apps/web/src/data/chart-types.ts, plus choropleth and point maps from @chartkitdev/geo. packages/core/README.md has one working spec for each core type and the full rules. packages/core/SPEC.md is the reference.

An option either draws or returns a warning that says why it did not.

What it does not do

  • Millions of raw points. There is no WebGL renderer and none is planned. Lines are sampled to the pixel width, which is how a 1M-point line draws in about 24 ms. To draw and interact with every one of millions of points, use a GPU library such as SciChart or LightningChart.
  • The smallest bundle. uPlot is smaller for line, bar, scatter and mini-area charts. A single ChartKit chart type is 6.7 to 38 KB gzip, and every core chart together is 78 KB.
  • Every chart type. There is no network graph, chord, violin, parallel coordinates or 3D.

Interaction and accessibility

Tooltips, zoom, pan, brush, selection, a navigator, synced charts and drilldown run in the browser from @chartkitdev/core/interact. Server output is the static SVG.

Every chart requires alt text, and the validator enforces it. When an adapter mounts a chart in the browser, keyboard navigation and a hidden data table are on by default for line, bar, area, combo, scatter, candlestick, pie, donut and a few more types. Set a11y.keyboard or a11y.dataTable to false to turn either off. The other chart types have no keyboard model yet, and server-rendered SVG is unchanged.

Themes

The default theme is Soft. It follows the page's light or dark scheme and has a transparent background. refined, editorial and midnight are built in. More presets are opt-in imports from @chartkitdev/core/themes/<name>:

  • Dark: emerald, mono, slate, arctic, orchid, obsidian, neon, mocha, owl, retro, copper, rose
  • Light: sunset, silver, pearl, latte
  • Also: tape, kite, mustard, violet

For a brand colour, paletteFromBrand("#635bff") from @chartkitdev/core/themes/brand returns a series palette for theme.tokens.series.

Packages

Package What it is
@chartkitdev/core The spec, validate(), repair() and the SVG renderer. Zero runtime dependencies.
@chartkitdev/react, vue, svelte, astro, solid, qwik, angular Framework adapters.
@chartkitdev/vanilla mount() and renderToString() for plain DOM and Node.
@chartkitdev/element The <chart-kit> custom element.
@chartkitdev/geo Choropleth and point maps. Each atlas is its own chunk. There is no tile layer.
@chartkitdev/markdown remark and rehype plugins that turn chartkit fences into SVG.
@chartkitdev/mcp The local MCP server.

@chartkitdev/convert is private and powers the converter on the website.

Coming from v0

The older prop-based React components (LineChart, KpiCard and the rest) are frozen and live at @chartkitdev/react/legacy. See Upgrading from v0.

Benchmarks

BENCHMARKS.md has every table: bundle size, browser paint, updates, hover, server render and the agent eval, with the losses and the JSON files each number comes from. The raw results are in bench/results. npm run bench reproduces them.

Working on this repo

npm install
npm run build:lib
npm test
npm run dev

npm run dev starts the site in apps/web at http://localhost:4321. It needs Node 22.23.4 or newer. The libraries support Node 20. catalog/ holds llms.txt, the feature matrix and the published benchmark data. Releases are described in RELEASING.md, and merging does not deploy chartkit.dev.

Author

Created by @toddo, founder of BuySellAds.

License

MIT