@casoon/chartlet

中文文档
0.23.0发布于 2026/10/29MITTypeScriptES Module零依赖构建来源已验证近 30 天下载 2656

Build-time SVG and HTML charts for static websites

原始使用文档
文档目录6 节
  1. Render API
  2. Content Security Policy
  3. Interactive module
  4. Architecture builder
  5. Cloudflare Workers and Vite
  6. Development

@casoon/chartlet

Build static, accessible SVG or HTML charts during an Astro build. The component runs only on the server and ships no chart JavaScript to the browser.

The package carries the renderer compiled to WebAssembly, so it is all you install. It needs Node.js 22.12 or newer.

npm install @casoon/chartlet
---
import Chart from '@casoon/chartlet/astro';
import chart from '../data/monthly-revenue.json';
---

<Chart id="monthly-revenue" spec={chart} />

id must be stable and unique within the page. It prevents accessibility ID collisions when the same specification is embedded more than once.

Use format="svg" for the graphic alone or format="html" for the default figure, caption, source, and data-table output. The HTML table uses a native disclosure by default; set table="visible" to show it permanently.

Render API

import { renderChart } from '@casoon/chartlet';

const { content, warnings } = renderChart(spec, { format: 'svg', idPrefix: 'revenue' });

Options: format, table, idPrefix, variant (mobile renders the mobile variant alone as SVG, print the chart as SVG with literal colors for PDF and print renderers, social the chart on a 1200 × 630 canvas for Open Graph images), strict and manifest (true adds a provenance manifest to the result: chartlet version, SHA-256 of the canonical specification and of content, format, variant, ID prefix and warnings, without a timestamp). warnings holds the lines the CLI would write; an invalid specification throws with the CLI's message. The output is byte-identical to the chartlet CLI.

renderChartDetailed(spec, options) takes the same arguments but does not throw on an invalid specification: it returns { ok, error?, warnings } with { code, path, message } diagnostics, plus content, styleHashes and manifest when rendering succeeded. spec may also be JSON text.

inspectChart(spec, { disable }) runs the inspections of a model without rendering it, as chartlet inspect does: { ok: true, findings } with { code, severity, path, message, hint } per finding (severity is gap or hint), or { ok: false, error } for an invalid specification, a type without inspections or an unknown code in disable.

To render with an installed CLI instead of WebAssembly, set CHARTLET_BIN to its absolute path or pass binary.

Content Security Policy

A chart styles itself with inline <style> elements. styleHashes lists their CSP source expressions ('sha256-…'), one per distinct element, in order of first appearance; a strict style-src allows exactly those:

const { content, styleHashes } = renderChart(spec, { idPrefix: 'revenue' });

headers.set('Content-Security-Policy', `style-src 'self' ${styleHashes.join(' ')}`);

Astro takes the hashes without quotes: per page through Astro.csp?.insertStyleHash(hash.slice(1, -1)), or for all pages in security.csp.styleDirective.hashes. createRenderer returns the same field.

Interactive module

@casoon/chartlet/interactive is an optional browser module without dependencies for charts rendered with hooks: true: crosshair with values (pointer and keyboard), series toggles, scroll stations and playback. Import only the features a page uses:

import { enhance, crosshair, toggle } from '@casoon/chartlet/interactive';

enhance(document.querySelector('figure.chartlet-figure'), { crosshair, toggle });

It reads values from the data table and geometry from data-* hooks, needs no inline script and sets styles only through the CSSOM. See Interaction.

Architecture builder

@casoon/chartlet/builder assembles an architecture specification in code and returns it as a plain object for renderChart. Ids are explicit or joined with id(...); building blocks are plain functions; fanOut and fanIn connect one component with a list. Ids used twice and references to undeclared elements throw at the call.

import { architecture } from '@casoon/chartlet/builder';

const a = architecture();
const api = a.component('api', 'API');
const db = a.component('db', 'Orders', { kind: 'database' });
a.connect(api, db, { label: 'reads, writes' });
const spec = a.spec({ title: 'Orders' });

flow() builds a flow chart the same way, with lane, node, group, the same edge calls, mainPath and sameRank. See JavaScript runtimes.

Cloudflare Workers and Vite

Runtimes that cannot compile WebAssembly from bytes, such as workerd, import the module and pass it to createRenderer from @casoon/chartlet/wasm, which has no Node.js imports:

import wasm from '@casoon/chartlet/chartlet.wasm';
import { createRenderer } from '@casoon/chartlet/wasm';

const { renderChart } = createRenderer(wasm);

In a Vite browser build, compile it from the emitted URL:

import wasmUrl from '@casoon/chartlet/chartlet.wasm?url';
import { createRenderer } from '@casoon/chartlet/wasm';

const { renderChart } = createRenderer(
  await WebAssembly.compileStreaming(fetch(wasmUrl)),
);

Development

src/chartlet.wasm is not committed. npm run build:wasm builds it from the wasm/ crate of the repository (target wasm32-unknown-unknown, optimized with wasm-opt when it is installed); npm pack and npm publish run it first.

版本与发布记录

@casoon/chartlet 共发布 30 个版本,首次发布于 2026/09/11;过去 12 个月发布了 30 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.23.0latest2026/10/2911 天前
0.22.02026/10/2812 天前
0.21.02026/10/2416 天前
0.20.02026/10/2416 天前
0.19.02026/10/1228 天前
0.18.02026/10/1228 天前

其他发布通道:latest~ → 0.1.0-alpha.3,alpha → 0.1.0-alpha.7。

关于 @casoon/chartlet 的常见问题

@casoon/chartlet 是什么?

Build-time SVG and HTML charts for static websites

如何安装 @casoon/chartlet?

在项目目录运行 npm install @casoon/chartlet,或使用 pnpm add / yarn add / bun add。

@casoon/chartlet 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@casoon/chartlet 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@casoon/chartlet 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22.12.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@casoon/chartlet 可以商用吗?

@casoon/chartlet 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@casoon/chartlet 的下载量有多少?

根据 npm 官方下载统计,@casoon/chartlet 在 2026/10/09 – 2026/11/07 这 30 天内被下载 2,656 次,日均约 89 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@casoon/chartlet 还在维护吗?

最新版本 0.23.0 发布于 2026/10/29(11 天前);至今共发布 30 个版本,其中过去 12 个月发布了 30 个;首次发布于 2026/09/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。