@blueprint-chart/lib

中文文档
2.15.0发布于 2026/11/08MITTypeScriptES Module构建来源已验证近 30 天下载 2713

DSL-driven charting library built on D3. Define charts as text, render them anywhere.

原始使用文档
文档目录10 节
  1. Install
  2. Quick start
  3. Node — write SVG and PNG to disk
  4. Browser — mount and step scenes
  5. DSL Grammar
  6. EBNF Overview
  7. Example
  8. AST Node Types
  9. Regenerating the Parser
  10. Color Palettes

@blueprint-chart/lib

Core library for Blueprint Chart — a DSL-driven charting engine built on D3.

Install

npm install @blueprint-chart/lib

Quick start

Node — write SVG and PNG to disk

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 }))

Browser — mount and step scenes

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

The simplest way to render a chart is the IIFE runtime, which auto-scans the document for embedded chart definitions and renders each one inline (inside a sandboxed iframe inserted before the script tag):

<script src="https://unpkg.com/@blueprint-chart/lib/dist/lib/lib.iife.js"></script>

<script type="application/blueprint-chart">
  chart MyChart {
    data { values = "[[0,1],[1,4],[2,2]]" }
    series "line" { color = "steelblue" }
  }
</script>

For programmatic use in an ES module project, the lib exposes the underlying primitives: parse (DSL → AST), validateChart (AST → ValidationResult with typed errors and warnings), buildChartOptions (AST → render options), createFrame/createCanvas, and the chart registry (registerChart, getChart). See src/index.ts for the full public API.

DSL Grammar

The chart DSL is defined by a PEG grammar in src/dsl/grammar.peggy and compiled to src/dsl/grammar.js using Peggy.

EBNF Overview

Chart       = "chart" Identifier "{" ChartMember* "}"
ChartMember = DataBlock | Colorize | AreaFill | Annotation | Series | Scene | Property

DataBlock   = "data" "{" Property* "}"
Colorize    = "colorize" String "{" Property* "}"
AreaFill    = "area-fill" String String "{" Property* "}"
Annotation  = "annotation" String "{" Property* "}"
Series      = "series" String "{" Property* "}"
Scene       = "scene" String "{" SceneMember* "}"
SceneMember = DataBlock | Colorize | AreaFill | Annotation | Property

Property    = PropertyKey "=" PropertyValue
PropertyKey = String | Identifier
PropertyValue = Number "%" | Number | String | Identifier

Identifier  = [a-zA-Z_#][a-zA-Z0-9_#-]*
Number      = [0-9]+("."[0-9]+)?
String      = '"' (EscapeSequence | [^"\\])* '"'

Example

chart horizontal-bar {
  title = "Revenue by Region"
  sort = descending

  data {
    "North America" = 75%
    "Europe" = 53.85%
    "Asia" = 44%
  }

  colorize "North America" {
    color = "#e53e3e"
    label = "Leader"
  }

  scene "Focus" {
    sort = ascending
    colorize "Asia" {
      color = "#38a169"
    }
  }
}

AST Node Types

NodeFields
ChartNodechartType, properties, data, colorizes, areaFills, annotations, series, scenes
PropertyNodekey, value, isPercentage
DataNodeentries (PropertyNode[])
ColorizeNodetarget, properties
AreaFillNodefrom, to, properties
AnnotationNodetarget, properties
SeriesNodename, properties
SceneNodename, properties, data, colorizes, areaFills, annotations

Regenerating the Parser

The generated parser (src/dsl/grammar.js) is checked into git so downstream packages can import lib source directly without a build step.

To regenerate after editing grammar.peggy:

pnpm --filter @blueprint-chart/lib run generate:parser

This also runs automatically before build and test via prebuild/pretest hooks.

Color Palettes

The built-in color palettes are curated from pypalettes by Joseph Music, a collection of 2700+ color palettes for data visualization. Licensed under MIT.

版本与发布记录

@blueprint-chart/lib 共发布 65 个版本,首次发布于 2026/05/15;过去 12 个月发布了 65 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.15.0latest2026/11/08今天
2.14.02026/11/08今天
2.13.02026/11/08今天
2.12.02026/11/08今天
2.11.02026/11/08今天
2.10.02026/11/071 天前

关于 @blueprint-chart/lib 的常见问题

@blueprint-chart/lib 是什么?

DSL-driven charting library built on D3. Define charts as text, render them anywhere.

如何安装 @blueprint-chart/lib?

在项目目录运行 npm install @blueprint-chart/lib,或使用 pnpm add / yarn add / bun add。

@blueprint-chart/lib 支持 TypeScript 吗?

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

@blueprint-chart/lib 支持 ESM 还是 CommonJS?

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

@blueprint-chart/lib 可以商用吗?

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

@blueprint-chart/lib 的下载量有多少?

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

@blueprint-chart/lib 还在维护吗?

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