@x-cmd/draw 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.cn.md +235 -0
- package/README.md +138 -127
- package/dist/lib.d.ts +18 -0
- package/dist/lib.js +18 -0
- package/dist/node/cli.js +14 -11
- package/dist/render/area.d.ts +3 -0
- package/dist/render/area.js +3 -0
- package/dist/render/bar-grid.d.ts +3 -0
- package/dist/render/bar-grid.js +3 -0
- package/dist/render/bar-grouped.d.ts +3 -0
- package/dist/render/bar-grouped.js +3 -0
- package/dist/render/bar-stacked.d.ts +3 -0
- package/dist/render/bar-stacked.js +3 -0
- package/dist/render/bar.d.ts +3 -0
- package/dist/render/bar.js +3 -0
- package/dist/render/combo.d.ts +3 -0
- package/dist/render/combo.js +3 -0
- package/dist/render/dispatch.d.ts +3 -0
- package/dist/render/dispatch.js +3 -0
- package/dist/render/gauge.d.ts +3 -0
- package/dist/render/gauge.js +3 -0
- package/dist/render/hbar-spec.d.ts +3 -0
- package/dist/render/hbar-spec.js +3 -0
- package/dist/render/line-multi.d.ts +3 -0
- package/dist/render/line-multi.js +3 -0
- package/dist/render/line.d.ts +3 -0
- package/dist/render/line.js +3 -0
- package/dist/render/pie.d.ts +3 -0
- package/dist/render/pie.js +3 -0
- package/dist/render/qr.d.ts +9 -0
- package/dist/render/qr.js +9 -0
- package/dist/render/query.d.ts +12 -0
- package/dist/render/query.js +9 -0
- package/dist/render/radar.d.ts +3 -0
- package/dist/render/radar.js +3 -0
- package/dist/render/sankey.d.ts +3 -0
- package/dist/render/sankey.js +3 -0
- package/dist/render/scatter.d.ts +3 -0
- package/dist/render/scatter.js +3 -0
- package/dist/render/svg.d.ts +3 -0
- package/dist/specs/base.d.ts +36 -0
- package/dist/specs/base.js +30 -0
- package/dist/specs/combo.d.ts +15 -0
- package/dist/specs/combo.js +9 -0
- package/dist/specs/csv.d.ts +4 -0
- package/dist/specs/csv.js +4 -0
- package/dist/specs/gauge.d.ts +15 -0
- package/dist/specs/gauge.js +9 -0
- package/dist/specs/input.d.ts +12 -0
- package/dist/specs/input.js +9 -0
- package/dist/specs/multi.d.ts +18 -0
- package/dist/specs/multi.js +11 -0
- package/dist/specs/sankey.d.ts +15 -0
- package/dist/specs/sankey.js +9 -0
- package/dist/specs/scatter.d.ts +15 -0
- package/dist/specs/scatter.js +9 -0
- package/dist/store.d.ts +15 -0
- package/dist/store.js +9 -0
- package/dist/styles/index.d.ts +10 -0
- package/dist/styles/index.js +10 -0
- package/dist/styles/types.d.ts +3 -0
- package/dist/version.d.ts +11 -0
- package/dist/version.js +8 -0
- package/package.json +8 -2
package/README.cn.md
ADDED
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
# draw
|
|
2
|
+
|
|
3
|
+
> [English](README.md) | 中文
|
|
4
|
+
|
|
5
|
+
AI 时代的 SVG 图表生成服务(狗粮原型)。单端点 `/draw?type=...`,数据在 URL 里、输出带机器可读 `<metadata>`、Cloudflare Workers + TypeScript 直生成 SVG。风格体系对齐设计师规范(13 张 SVG),每图种多风格变体。
|
|
6
|
+
|
|
7
|
+
## 三种使用方式(同一份渲染核心)
|
|
8
|
+
|
|
9
|
+
**托管服务**(零安装):直接拼 URL — `https://x-cmd.com/draw?type=pie&labels=A,B&values=3,2`
|
|
10
|
+
|
|
11
|
+
**npm 库**(离线/嵌入,同步无网络,参数与 URL 完全一致):
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm install @x-cmd/draw
|
|
15
|
+
```
|
|
16
|
+
```ts
|
|
17
|
+
import { draw } from '@x-cmd/draw';
|
|
18
|
+
const { svg } = draw({ type: 'pie', labels: 'A,B', values: '3,2', t: 'evil', scheme: 'dark' });
|
|
19
|
+
// 也接受查询串: draw('?type=bar&labels=Mon,Tue&values=3,5')
|
|
20
|
+
// 数组值 = 重复参数(labels 含逗号时): draw({ type: 'bar', labels: ['Q1, 2024', 'Q2'], values: [1, 2] })
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**CLI**(`npm i -g @x-cmd/draw` 后可用,或 `npx`):
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
draw --type pie --labels "TypeScript,Python,Go" --values "4821,2312,980" --out pie.svg
|
|
27
|
+
draw --type hbar --labels a,b,c --values 3,2,1 --theme xcmd # 长参数(--theme/--width/--height)
|
|
28
|
+
draw "?type=hbar&labels=a,b,c&values=3,2,1&t=xcmd" # 裸查询串,与 URL 同参
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
**自托管**:三种入口任选 —
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
git clone https://github.com/x-cmd/draw && cd draw && npm install
|
|
35
|
+
npm run serve # Node 本地服务(默认 http://localhost:8787/draw,含演示页)
|
|
36
|
+
npm run dev # Cloudflare wrangler 本地开发
|
|
37
|
+
bash scripts/setup.sh # 一键部署到你自己的 Cloudflare 账号
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
环境变量(Node 服务):`PORT`(默认 8787)· `BASE_PATH` · `DATA_REPO`/`DATA_BRANCH`/`GITHUB_TOKEN`(配置后 POST 短链同样可用,未配置时与线上自部署一样优雅降级)。
|
|
41
|
+
|
|
42
|
+
**Deno**(原生 TS;渲染核心是纯函数 — 不碰网络/文件,零权限沙盒即安全边界):
|
|
43
|
+
|
|
44
|
+
```ts
|
|
45
|
+
import { draw } from "jsr:@x-cmd/draw"; // JSR(Deno 原生注册表)
|
|
46
|
+
// 或 npm:import { draw } from "npm:@x-cmd/draw";
|
|
47
|
+
const { svg } = draw({ type: "pie", labels: "A,B", values: "3,2" });
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
```sh
|
|
51
|
+
deno add jsr:@x-cmd/draw
|
|
52
|
+
# 仓库内直跑源码(免构建):
|
|
53
|
+
deno task cli --type pie --labels A,B --values 3,2 --out pie.svg # 仅需 --allow-write
|
|
54
|
+
deno task serve # 本地服务(同 npm run serve)
|
|
55
|
+
# 渲染零权限可证:deno eval 默认无任何授权,直接 import 即渲染成功
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## 端点
|
|
59
|
+
|
|
60
|
+
**`GET /draw?type=<pie|bar|hbar|line|area|radar|gauge|scatter|combo|sankey|qr>`** — 渲染图表
|
|
61
|
+
|
|
62
|
+
| type | 图表 | 限制 |
|
|
63
|
+
|---|---|---|
|
|
64
|
+
| `pie` | 环形图;`donut=0` 变实心饼 | ≤100 类(>9 自动合并 Other) |
|
|
65
|
+
| `bar` | 柱状图/分组并排;多系列加 `stack=1` 变堆叠柱 | ≤30 组 |
|
|
66
|
+
| `hbar` | 水平柱状图(排行场景,标签左值右) | ≤30 条,单系列(部分风格双系列) |
|
|
67
|
+
| `line` | 折线图(平滑+渐变面积)/多折线 | ≤60 点 |
|
|
68
|
+
| `area` | 面积图/多系列面积(渐变填充) | ≤60 点 |
|
|
69
|
+
| `radar` | 雷达图 | 3-12 维 |
|
|
70
|
+
| `gauge` | 仪表盘(240° 弧+中心数值) | `value`+`max`(默认100),value≤max;多环 ≤8 |
|
|
71
|
+
| `scatter` | 散点图(双数值轴,支持负值;默认不标数值) | `x=&y=`,≤200 点 |
|
|
72
|
+
| `combo` | 混合图:柱(`series=`)+线(`line=`)同图,`stack=1` 变堆叠柱 | ≤30 组,共 ≤6 系列 |
|
|
73
|
+
| `sankey` | 桑基图(流向带宽) | `labels=`+`links=A>B:30`,≤24 节点/60 链接 |
|
|
74
|
+
| `qr` | 二维码 | `d`=文本(≤1000 字符),`fg`/`bg` 配色,`title`/`unit` 标注 |
|
|
75
|
+
|
|
76
|
+
**`GET /draw`**(无参数)或 `?tab=pie` — 演示页(11 图种分类、全部风格变体、URL 双形态面板)
|
|
77
|
+
**`POST /draw?type=...`** — 提交 JSON/CSV body 创建永久短链
|
|
78
|
+
**`GET /draw/c/<hash>`** — 短链渲染(immutable)
|
|
79
|
+
|
|
80
|
+
通用参数:
|
|
81
|
+
|
|
82
|
+
- `t` 风格家族(见下节,按图种取用;该图种未做时回退默认)
|
|
83
|
+
- `scheme` 明暗:`auto`(默认,跟随系统)/ `dark` / `light`
|
|
84
|
+
- `v` 钉住版本;`w`/`h` 尺寸(200–1600 / 140–1200,qr 为 120–1000 / 140–1000)
|
|
85
|
+
- `num` 数值标注:`all` / `peak`(仅最高)/ `none`。默认跟随风格(设计规范变体多为 `none`,`?num=all` 显式全标)。适用于 pie/bar/hbar/line/area/scatter/combo;radar/gauge/sankey/qr 忽略
|
|
86
|
+
- `subtitle` 编辑式副标题(仅 area 的编辑式标题风格 `t=opendata` 渲染)、`source` 数据来源脚注(仅 area 渲染,任意 `t`);其余图种解析但忽略
|
|
87
|
+
|
|
88
|
+
## 风格(`?t=`)
|
|
89
|
+
|
|
90
|
+
按图种提供变体,明暗双配色自动切换:
|
|
91
|
+
|
|
92
|
+
| type | 可用家族 |
|
|
93
|
+
|---|---|
|
|
94
|
+
| `pie` | `opendata`(默认,标签全外)、`evil`(圆角环+内标值)、`evil-mono`(实心饼+引线外标)、`market-mono`(灰阶+环心总计)、`progress`(点状进度环)、`revenue`(渐变扇区+虚线内圈) |
|
|
95
|
+
| `bar` | `evil`(默认,分组柱)、`base`(卡片柱)、`grid`(表格柱) |
|
|
96
|
+
| `hbar` | `base`(默认)、`opendata`(支持 `ref=`/`reflabel=` 参考线)、`xcmd`(像素条,值为 %)、`xcmd-issue`(双系列蝴蝶)、`rank`(双系列实底+斜纹) |
|
|
97
|
+
| `line` | `shadcn`(默认)、`evil`、`evil-glow`(发光)、`evil-mono`、`opendata`(线端标签) |
|
|
98
|
+
| `area` | `evil`(默认,双系列虚线面积)、`star`(手绘风)、`opendata`(浓渐变+编辑标题)、`pixel`、`polka` |
|
|
99
|
+
| `radar` | `base`(默认)、`evil`(多系列)、`xcmd` |
|
|
100
|
+
| `sankey` | `base`(默认)、`fund`、`pipeline` |
|
|
101
|
+
| `gauge` | `base`(默认)、`gradient`、`reliability`(分区环仪表) |
|
|
102
|
+
| `scatter` / `combo` / `qr` | `base`(combo 另有 `evil`) |
|
|
103
|
+
|
|
104
|
+
## 传数据的两种方式
|
|
105
|
+
|
|
106
|
+
**扁平参数(URL 永远可读,推荐)**:
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
/draw?type=pie&title=Stars by language&unit=stars&labels=TypeScript,Python,Go,Rust&values=4821,2312,980,645
|
|
110
|
+
/draw?type=bar&title=Weekly downloads&labels=Mon,Tue&values=1200,1800
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
标签含逗号时用重复参数:`labels=Q1, 2024&labels=Q2&values=1&values=2`
|
|
114
|
+
|
|
115
|
+
**多系列对比**(type=bar 分组/堆叠、type=line 多折线、type=area 多面积,≤6 系列):
|
|
116
|
+
|
|
117
|
+
```
|
|
118
|
+
/draw?type=area&labels=Mon,Tue,Wed&series=Desktop:120,180,150&series=Mobile:80,90,85
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
**JSON 形态(复杂数据)**:`?d=` + URL 编码的 JSON:
|
|
122
|
+
|
|
123
|
+
```json
|
|
124
|
+
{"title":"Stars by language","unit":"stars","data":[{"label":"TypeScript","value":4821},{"label":"Python","value":2312}]}
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
多系列 JSON:`{"labels":["Mon","Tue"],"series":[{"name":"draw","values":[120,180]},{"name":"qc","values":[80,90]}]}`
|
|
128
|
+
|
|
129
|
+
二维码:`/draw?type=qr&d=https://github.com/tzw&fg=%236366f1`
|
|
130
|
+
|
|
131
|
+
仪表盘:`/draw?type=gauge&title=Build pass rate&value=87&max=100`
|
|
132
|
+
|
|
133
|
+
散点图:`/draw?type=scatter&x=10,20,30&y=5,8,7`
|
|
134
|
+
|
|
135
|
+
混合图:`/draw?type=combo&labels=Mon,Tue&series=dl:120,180&line=growth:5,8`(柱与线量级差 >5 倍时自动启用双 y 轴)
|
|
136
|
+
|
|
137
|
+
## 短链接(数据存 GitHub 仓库,内容寻址、永不过期)
|
|
138
|
+
|
|
139
|
+
```
|
|
140
|
+
POST /draw?type=pie body = 图表 spec JSON
|
|
141
|
+
POST /draw?type=bar&t=evil body = CSV(text/csv,首行表头)
|
|
142
|
+
→ 201 {"hash":"a3f9c2d4e5f60718","url":"https://x-cmd.com/draw/c/a3f9c2d4e5f60718"}
|
|
143
|
+
|
|
144
|
+
GET /draw/c/<hash> → SVG,永久 immutable 缓存
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
- 指纹 = 规范化数据(template+theme+spec,递归排序 key)的 SHA-256 前 16 位;相同数据 → 相同短链(自动去重、幂等);读取时复核指纹,存档被改即拒绝
|
|
148
|
+
- 多系列支持 bar/line/area/radar,以及 hbar(仅 `t=xcmd-issue`/`t=rank`,恰好 2 系列);其余 type 提交多系列会被 400 拒绝;POST 限 30 次/分钟/IP
|
|
149
|
+
- 服务端配置:`DATA_REPO`(公开仓,`owner/name`,wrangler.jsonc)、`GITHUB_TOKEN`(secret,fine-grained PAT 仅授该仓 **Contents 写**;读取走 raw 免 token);数据文件存于 `charts/<hash>.json`
|
|
150
|
+
- 可见性:数据仓是公开的,任何人可浏览目录——**请勿创建含敏感数据的短链**(私有存储方案规划中,将收费提供)
|
|
151
|
+
- 对比 quickchart:短链会过期(6个月/数天),我们的永不过期
|
|
152
|
+
|
|
153
|
+
## 版本语义
|
|
154
|
+
|
|
155
|
+
- 不带 `v`:渲染**最新版**,`Cache-Control: max-age=300`(链接随升级变化)
|
|
156
|
+
- `?v=1`:钉住版本,`Cache-Control: max-age=86400`(1 天;渲染仍随部署演进,不承诺永久冻结)。`v` 取值 1..当前版本,超出报 400
|
|
157
|
+
- `immutable` 仅用于短链 `/c/<hash>`(内容寻址,输出真正永不变化;短链忽略 `v` 参数)
|
|
158
|
+
|
|
159
|
+
## AI 可读
|
|
160
|
+
|
|
161
|
+
每个 SVG 内嵌 `<metadata>`(转义 JSON):`{generator, template, version, spec}`,数据与参数可直接 round-trip。文字全部用 `<text>`(真文本)。
|
|
162
|
+
|
|
163
|
+
## 项目结构
|
|
164
|
+
|
|
165
|
+
一次请求的路径:**handler.ts(HTTP 路由)→ render/query.ts(参数→渲染核心)→ specs/(解析)→ styles/(选皮肤)→ render/(算几何出 SVG)**。渲染核心是纯函数,三个运行时共享:Cloudflare Worker(index.ts 薄壳)、Node 服务(node/server.ts)、npm/JSR 库与 CLI(lib.ts + node/cli.ts)。
|
|
166
|
+
|
|
167
|
+
```
|
|
168
|
+
src/
|
|
169
|
+
├── index.ts Cloudflare Workers 入口(薄壳:边缘缓存、限流 binding、waitUntil)
|
|
170
|
+
├── handler.ts 共享 HTTP 层:路由、POST 短链创建、短链渲染、演示页、错误包装
|
|
171
|
+
├── lib.ts npm/JSR 库入口(barrel + 高层 draw() 同步 API)
|
|
172
|
+
├── demo.ts 演示页(图种 tab、风格变体、URL 双形态面板)
|
|
173
|
+
├── theme.ts 皮肤 → CSS 变量(:root + 暗色媒体查询)
|
|
174
|
+
├── store.ts 短链存储(GitHub repo 读写、指纹、幂等)
|
|
175
|
+
├── version.ts 版本号与缓存语义
|
|
176
|
+
├── types.ts 跨运行时结构类型(DrawEnv/Platform)
|
|
177
|
+
│
|
|
178
|
+
├── specs/ 【解析层】URL 参数 → 强类型 spec(校验、截断、上限)
|
|
179
|
+
│ ├── base.ts ChartSpec + 扁平参数/JSON 双形态解析
|
|
180
|
+
│ ├── multi.ts 多系列(series=)解析
|
|
181
|
+
│ └── gauge/scatter/combo/csv.ts 图种专属解析
|
|
182
|
+
│
|
|
183
|
+
├── render/ 【渲染层】spec + 皮肤 → SVG 字符串(纯计算,无布局引擎)
|
|
184
|
+
│ ├── svg.ts 公共构造:布局几何、刻度、网格、图例、标签防碰撞、文字测量
|
|
185
|
+
│ ├── query.ts 参数→SVG 同步核心(drawHandler/短链渲染段,库与 HTTP 共用)
|
|
186
|
+
│ ├── dispatch.ts 短链存档 → 渲染器的模板分发
|
|
187
|
+
│ ├── pie/line/line-multi/area/… 每图种一个渲染文件(bar 拆 3、line 拆 2)
|
|
188
|
+
│ └── qr.ts 二维码
|
|
189
|
+
│
|
|
190
|
+
├── styles/ 【风格层】设计师规范 SVG 的规则化
|
|
191
|
+
│ ├── types.ts Rules 词汇表 + 默认值(方向/圆角/图例/虚线/标注…)
|
|
192
|
+
│ ├── pie/line/area/… 每图种变体定义(色板明暗双值 + 规则差异 + 规范画布尺寸)
|
|
193
|
+
│ ├── common.ts 规范色板常量
|
|
194
|
+
│ ├── handlee.ts 手绘字体 base64(star 风格)
|
|
195
|
+
│ └── index.ts resolveSkin:图种 + ?t= + 明暗 → 皮肤
|
|
196
|
+
│
|
|
197
|
+
└── node/ 【Node 入口】server.ts 本地服务 · cli.ts draw CLI
|
|
198
|
+
|
|
199
|
+
test/ vitest(解析 + 渲染 + 共享 HTTP 层 + 库 API)
|
|
200
|
+
wrangler.jsonc 部署配置(路由 x-cmd.com/draw*)
|
|
201
|
+
deno.json JSR 发布 + Deno 任务
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
加新图种 = `specs/xxx.ts` + `render/xxx.ts` + `styles/xxx.ts` 各一个文件,再在 `render/dispatch.ts` 与 `render/query.ts` 各注册一行。
|
|
205
|
+
|
|
206
|
+
## 自部署(一键)
|
|
207
|
+
|
|
208
|
+
部署一套自己的 draw 到你的 Cloudflare 账号(免费计划即可,只需 CF 账号):
|
|
209
|
+
|
|
210
|
+
```sh
|
|
211
|
+
curl -fsSL https://raw.githubusercontent.com/x-cmd/draw/main/scripts/setup.sh | bash
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
脚本流程:克隆仓库 → wrangler 浏览器登录 → 部署 → 冒烟测试,结束打印你的 `*.workers.dev` 地址。GET 渲染全家桶(11 图种、全部风格)开箱即用。
|
|
215
|
+
|
|
216
|
+
- **短链(POST)第一阶段未默认启用**,调用会返回"未配置"提示。启用方法:建一个公开仓存数据 → 本地 wrangler.jsonc 的 vars 设 `DATA_REPO=owner/name` → `npx wrangler secret put GITHUB_TOKEN`(fine-grained PAT 仅授该仓 Contents 写)→ 重新部署
|
|
217
|
+
- 已 clone 仓库的,在仓库目录里跑 `npm run setup` 即可(跳过克隆)
|
|
218
|
+
- 路径前缀自动适配:workers.dev 根路径与 `/draw` 子路由都能用,无需改代码;也可用 `BASE_PATH` var 显式钉死
|
|
219
|
+
- 绑自有域名:dashboard → Workers → Settings → Domains & Routes 加 route(任意路径前缀)
|
|
220
|
+
- Windows 请用 WSL 或 Git Bash 运行
|
|
221
|
+
|
|
222
|
+
## 开发
|
|
223
|
+
|
|
224
|
+
```sh
|
|
225
|
+
npm install
|
|
226
|
+
npm run dev # wrangler dev 本地起服
|
|
227
|
+
npm test # vitest
|
|
228
|
+
npm run check # tsc --noEmit × 3(worker / node / build)
|
|
229
|
+
npm run build # 产 dist/(npm 库 + CLI)
|
|
230
|
+
npm run deploy # wrangler deploy
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
## 许可
|
|
234
|
+
|
|
235
|
+
代码 [Apache-2.0](LICENSE)。内嵌字体遵循各自许可:Inter(OFL,全文见 `src/styles/inter-OFL.txt`)、Handlee(OFL)。
|
package/README.md
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# draw
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> English | [中文](README.cn.md)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
SVG chart generation for the AI era. One endpoint — `/draw?type=...` — with the data in the URL, machine-readable `<metadata>` embedded in every output, and SVG rendered directly by TypeScript. The style system mirrors a designer spec (13 SVGs), with multiple style variants per chart type.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
## Three ways to use it (one render core)
|
|
8
8
|
|
|
9
|
-
**
|
|
9
|
+
**Hosted service** (zero install): just build a URL — `https://x-cmd.com/draw?type=pie&labels=A,B&values=3,2`
|
|
10
|
+
|
|
11
|
+
**npm library** (offline/embedded, synchronous, no network; params identical to the URL):
|
|
10
12
|
|
|
11
13
|
```sh
|
|
12
14
|
npm install @x-cmd/draw
|
|
@@ -14,212 +16,221 @@ npm install @x-cmd/draw
|
|
|
14
16
|
```ts
|
|
15
17
|
import { draw } from '@x-cmd/draw';
|
|
16
18
|
const { svg } = draw({ type: 'pie', labels: 'A,B', values: '3,2', t: 'evil', scheme: 'dark' });
|
|
17
|
-
//
|
|
18
|
-
//
|
|
19
|
+
// also accepts a query string: draw('?type=bar&labels=Mon,Tue&values=3,5')
|
|
20
|
+
// array values = repeated params (when labels contain commas):
|
|
21
|
+
draw({ type: 'bar', labels: ['Q1, 2024', 'Q2'], values: [1, 2] })
|
|
19
22
|
```
|
|
20
23
|
|
|
21
|
-
**CLI**(`npm i -g @x-cmd/draw
|
|
24
|
+
**CLI** (after `npm i -g @x-cmd/draw`, or via `npx`):
|
|
22
25
|
|
|
23
26
|
```sh
|
|
24
|
-
|
|
25
|
-
|
|
27
|
+
draw --type pie --labels "TypeScript,Python,Go" --values "4821,2312,980" --out pie.svg
|
|
28
|
+
draw --type hbar --labels a,b,c --values 3,2,1 --theme xcmd # long flags (--theme/--width/--height)
|
|
29
|
+
draw "?type=hbar&labels=a,b,c&values=3,2,1&t=xcmd" # bare query string, same params as the URL
|
|
26
30
|
```
|
|
27
31
|
|
|
28
|
-
|
|
32
|
+
**Self-host** — pick any entry point:
|
|
29
33
|
|
|
30
34
|
```sh
|
|
31
35
|
git clone https://github.com/x-cmd/draw && cd draw && npm install
|
|
32
|
-
npm run serve # Node
|
|
33
|
-
npm run dev # Cloudflare wrangler
|
|
34
|
-
bash scripts/setup.sh #
|
|
36
|
+
npm run serve # local Node server (http://localhost:8787/draw, demo page included)
|
|
37
|
+
npm run dev # Cloudflare wrangler local dev
|
|
38
|
+
bash scripts/setup.sh # one-shot deploy to your own Cloudflare account
|
|
35
39
|
```
|
|
36
40
|
|
|
37
|
-
|
|
41
|
+
Environment variables (Node server): `PORT` (default 8787) · `BASE_PATH` · `DATA_REPO`/`DATA_BRANCH`/`GITHUB_TOKEN` (set these to enable POST short links; unset degrades gracefully, exactly like self-deployed workers).
|
|
38
42
|
|
|
39
|
-
**Deno**(
|
|
43
|
+
**Deno** (native TS; the render core is pure — no network, no files — so the zero-permission sandbox is the security boundary):
|
|
40
44
|
|
|
41
45
|
```ts
|
|
42
|
-
import { draw } from "jsr:@x-cmd/draw"; // JSR(Deno
|
|
43
|
-
//
|
|
46
|
+
import { draw } from "jsr:@x-cmd/draw"; // JSR (Deno's native registry)
|
|
47
|
+
// or npm: import { draw } from "npm:@x-cmd/draw";
|
|
44
48
|
const { svg } = draw({ type: "pie", labels: "A,B", values: "3,2" });
|
|
45
49
|
```
|
|
46
50
|
|
|
47
51
|
```sh
|
|
48
52
|
deno add jsr:@x-cmd/draw
|
|
49
|
-
#
|
|
50
|
-
deno task cli --type pie --labels A,B --values 3,2 --out pie.svg #
|
|
51
|
-
deno task serve #
|
|
52
|
-
#
|
|
53
|
+
# run straight from source inside the repo (no build):
|
|
54
|
+
deno task cli --type pie --labels A,B --values 3,2 --out pie.svg # only needs --allow-write
|
|
55
|
+
deno task serve # local server (same as npm run serve)
|
|
56
|
+
# proof of zero permissions: deno eval defaults to no grants — importing and rendering just works
|
|
53
57
|
```
|
|
54
58
|
|
|
55
|
-
##
|
|
59
|
+
## Endpoints
|
|
56
60
|
|
|
57
|
-
**`GET /draw?type=<pie|bar|hbar|line|area|radar|gauge|scatter|combo|sankey|qr>`** —
|
|
61
|
+
**`GET /draw?type=<pie|bar|hbar|line|area|radar|gauge|scatter|combo|sankey|qr>`** — render a chart
|
|
58
62
|
|
|
59
|
-
| type |
|
|
63
|
+
| type | chart | limits |
|
|
60
64
|
|---|---|---|
|
|
61
|
-
| `pie` |
|
|
62
|
-
| `bar` |
|
|
63
|
-
| `hbar` |
|
|
64
|
-
| `line` |
|
|
65
|
-
| `area` |
|
|
66
|
-
| `radar` |
|
|
67
|
-
| `gauge` |
|
|
68
|
-
| `scatter` |
|
|
69
|
-
| `combo` |
|
|
70
|
-
| `sankey` |
|
|
71
|
-
| `qr` |
|
|
72
|
-
|
|
73
|
-
**`GET /draw`**(
|
|
74
|
-
**`POST /draw?type=...`** —
|
|
75
|
-
**`GET /draw/c/<hash>`** —
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
- `t`
|
|
80
|
-
- `scheme`
|
|
81
|
-
- `v`
|
|
82
|
-
- `num`
|
|
83
|
-
- `subtitle`
|
|
84
|
-
|
|
85
|
-
##
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
| type |
|
|
65
|
+
| `pie` | donut; `donut=0` for a solid pie | ≤100 categories (>9 auto-merge into Other) |
|
|
66
|
+
| `bar` | grouped columns; multi-series with `stack=1` for stacked | ≤30 groups |
|
|
67
|
+
| `hbar` | horizontal bars (rankings; labels left, values right) | ≤30 bars, single series (some styles take two) |
|
|
68
|
+
| `line` | smooth line + gradient area / multi-line | ≤60 points |
|
|
69
|
+
| `area` | area / multi-series area (gradient fill) | ≤60 points |
|
|
70
|
+
| `radar` | radar chart | 3–12 axes |
|
|
71
|
+
| `gauge` | gauge (240° arc + center value) | `value`+`max` (default 100), value ≤ max; multi-ring ≤8 |
|
|
72
|
+
| `scatter` | scatter (two numeric axes, negatives supported; no value labels by default) | `x=&y=`, ≤200 points |
|
|
73
|
+
| `combo` | bars (`series=`) + line (`line=`) in one chart; `stack=1` to stack the bars | ≤30 groups, ≤6 series total |
|
|
74
|
+
| `sankey` | flow diagram with weighted bands | `labels=`+`links=A>B:30`, ≤24 nodes/60 links |
|
|
75
|
+
| `qr` | QR code | `d`=text (≤1000 chars), `fg`/`bg` colors, `title`/`unit` captions |
|
|
76
|
+
|
|
77
|
+
**`GET /draw`** (no params) or `?tab=pie` — the demo page (11 chart types, all style variants, dual URL forms)
|
|
78
|
+
**`POST /draw?type=...`** — submit a JSON/CSV body to create a permanent short link
|
|
79
|
+
**`GET /draw/c/<hash>`** — short-link render (immutable)
|
|
80
|
+
|
|
81
|
+
Common parameters:
|
|
82
|
+
|
|
83
|
+
- `t` style family (per chart type, see below; falls back to default when not built for that type)
|
|
84
|
+
- `scheme` `auto` (default, follows system) / `dark` / `light`
|
|
85
|
+
- `v` pin a version; `w`/`h` size (200–1600 / 140–1200; qr is 120–1000 / 140–1000)
|
|
86
|
+
- `num` value labels: `all` / `peak` (highest only) / `none`. Defaults follow the style; applies to pie/bar/hbar/line/area/scatter/combo (radar/gauge/sankey/qr ignore it)
|
|
87
|
+
- `subtitle` editorial subtitle (only rendered by area's editorial style `t=opendata`), `source` data-source footnote (area only, any `t`); other types parse but ignore these
|
|
88
|
+
|
|
89
|
+
## Styles (`?t=`)
|
|
90
|
+
|
|
91
|
+
Per chart type, with automatic light/dark palettes:
|
|
92
|
+
|
|
93
|
+
| type | available families |
|
|
90
94
|
|---|---|
|
|
91
|
-
| `pie` | `opendata`(
|
|
92
|
-
| `bar` | `evil`(
|
|
93
|
-
| `hbar` | `base`(
|
|
94
|
-
| `line` | `shadcn`(
|
|
95
|
-
| `area` | `evil`(
|
|
96
|
-
| `radar` | `base`(
|
|
97
|
-
| `sankey` | `base`(
|
|
98
|
-
| `gauge` | `base`(
|
|
99
|
-
| `scatter` / `combo` / `qr` | `base`(combo
|
|
95
|
+
| `pie` | `opendata` (default, all labels outside), `evil` (rounded ring + inner values), `evil-mono` (solid pie + leader lines), `market-mono` (grayscale + ring total), `progress` (dotted progress ring), `revenue` (gradient wedges + dashed inner ring) |
|
|
96
|
+
| `bar` | `evil` (default, grouped), `base` (card), `grid` (table bars) |
|
|
97
|
+
| `hbar` | `base` (default), `opendata` (supports `ref=`/`reflabel=` reference line), `xcmd` (pixel bars, %), `xcmd-issue` (two-series butterfly), `rank` (two-series solid + hatching) |
|
|
98
|
+
| `line` | `shadcn` (default), `evil`, `evil-glow` (glow), `evil-mono`, `opendata` (line-end labels) |
|
|
99
|
+
| `area` | `evil` (default, two-series dashed areas), `star` (hand-drawn), `opendata` (dense gradient + editorial title), `pixel`, `polka` |
|
|
100
|
+
| `radar` | `base` (default), `evil` (multi-series), `xcmd` |
|
|
101
|
+
| `sankey` | `base` (default), `fund`, `pipeline` |
|
|
102
|
+
| `gauge` | `base` (default), `gradient`, `reliability` (zoned ring gauge) |
|
|
103
|
+
| `scatter` / `combo` / `qr` | `base` (combo also has `evil`) |
|
|
100
104
|
|
|
101
|
-
##
|
|
105
|
+
## Two ways to pass data
|
|
102
106
|
|
|
103
|
-
|
|
107
|
+
**Flat params** (URL stays readable — recommended):
|
|
104
108
|
|
|
105
109
|
```
|
|
106
110
|
/draw?type=pie&title=Stars by language&unit=stars&labels=TypeScript,Python,Go,Rust&values=4821,2312,980,645
|
|
107
111
|
/draw?type=bar&title=Weekly downloads&labels=Mon,Tue&values=1200,1800
|
|
108
112
|
```
|
|
109
113
|
|
|
110
|
-
|
|
114
|
+
When labels contain commas, use repeated params: `labels=Q1, 2024&labels=Q2&values=1&values=2`
|
|
111
115
|
|
|
112
|
-
|
|
116
|
+
**Multi-series comparison** (type=bar grouped/stacked, type=line, type=area, ≤6 series):
|
|
113
117
|
|
|
114
118
|
```
|
|
115
119
|
/draw?type=area&labels=Mon,Tue,Wed&series=Desktop:120,180,150&series=Mobile:80,90,85
|
|
116
120
|
```
|
|
117
121
|
|
|
118
|
-
**JSON
|
|
122
|
+
**JSON form** (complex data): `?d=` + URL-encoded JSON:
|
|
119
123
|
|
|
120
124
|
```json
|
|
121
125
|
{"title":"Stars by language","unit":"stars","data":[{"label":"TypeScript","value":4821},{"label":"Python","value":2312}]}
|
|
122
126
|
```
|
|
123
127
|
|
|
124
|
-
|
|
128
|
+
Multi-series JSON: `{"labels":["Mon","Tue"],"series":[{"name":"draw","values":[120,180]},{"name":"qc","values":[80,90]}]}`
|
|
125
129
|
|
|
126
|
-
|
|
130
|
+
QR code: `/draw?type=qr&d=https://github.com/tzw&fg=%236366f1`
|
|
127
131
|
|
|
128
|
-
|
|
132
|
+
Gauge: `/draw?type=gauge&title=Build pass rate&value=87&max=100`
|
|
129
133
|
|
|
130
|
-
|
|
134
|
+
Scatter: `/draw?type=scatter&x=10,20,30&y=5,8,7`
|
|
131
135
|
|
|
132
|
-
|
|
136
|
+
Combo: `/draw?type=combo&labels=Mon,Tue&series=dl:120,180&line=growth:5,8` (dual y-axes kick in automatically when bars and lines differ by >5×)
|
|
133
137
|
|
|
134
|
-
##
|
|
138
|
+
## Short links (data stored in a GitHub repo; content-addressed, never expires)
|
|
135
139
|
|
|
136
140
|
```
|
|
137
|
-
POST /draw?type=pie body =
|
|
138
|
-
POST /draw?type=bar&t=evil body = CSV(text/csv
|
|
141
|
+
POST /draw?type=pie body = chart spec JSON
|
|
142
|
+
POST /draw?type=bar&t=evil body = CSV (text/csv, header row)
|
|
139
143
|
→ 201 {"hash":"a3f9c2d4e5f60718","url":"https://x-cmd.com/draw/c/a3f9c2d4e5f60718"}
|
|
140
144
|
|
|
141
|
-
GET /draw/c/<hash> → SVG
|
|
145
|
+
GET /draw/c/<hash> → SVG, cached immutable forever
|
|
142
146
|
```
|
|
143
147
|
|
|
144
|
-
-
|
|
145
|
-
-
|
|
146
|
-
-
|
|
147
|
-
-
|
|
148
|
-
-
|
|
148
|
+
- The fingerprint is the first 16 hex chars of SHA-256 over canonicalized data (template+theme+spec, keys recursively sorted); identical data → identical short link (dedup, idempotent). On read the fingerprint is re-verified — tampered archives are rejected
|
|
149
|
+
- Multi-series is supported for bar/line/area/radar, plus hbar (only `t=xcmd-issue`/`t=rank`, exactly 2 series); other types reject multi-series bodies with 400. POST is rate-limited to 30/min/IP
|
|
150
|
+
- Server-side config: `DATA_REPO` (public repo, `owner/name`, in wrangler.jsonc) and `GITHUB_TOKEN` (secret; fine-grained PAT with **Contents: write** on that repo only — reads go through raw, tokenless). Data files live at `charts/<hash>.json`
|
|
151
|
+
- Visibility: the data repo is public and browsable — **do not create short links with sensitive data** (private storage is planned, as a paid feature)
|
|
152
|
+
- Compared to quickchart: their short links expire (6 months / days); ours never do
|
|
149
153
|
|
|
150
|
-
##
|
|
154
|
+
## Version semantics
|
|
151
155
|
|
|
152
|
-
-
|
|
153
|
-
- `?v=1
|
|
154
|
-
- `immutable`
|
|
156
|
+
- No `v`: renders the **latest** version, `Cache-Control: max-age=300` (output changes as the service upgrades)
|
|
157
|
+
- `?v=1`: pins a version, `Cache-Control: max-age=86400` (1 day; rendering still evolves with deploys — not frozen forever). `v` accepts 1..current; out of range → 400
|
|
158
|
+
- `immutable` is reserved for short links `/c/<hash>` (content-addressed, truly unchanging; `v` is ignored there)
|
|
155
159
|
|
|
156
|
-
## AI
|
|
160
|
+
## AI-friendly
|
|
157
161
|
|
|
158
|
-
|
|
162
|
+
Every SVG embeds `<metadata>` (escaped JSON): `{generator, template, version, spec}` — data and params round-trip directly. All text is real `<text>` elements.
|
|
159
163
|
|
|
160
|
-
##
|
|
164
|
+
## Project structure
|
|
161
165
|
|
|
162
|
-
|
|
166
|
+
A request flows **handler.ts (HTTP routing) → render/query.ts (params → render core) → specs/ (parse) → styles/ (pick skin) → render/ (geometry → SVG)**. The render core is pure functions shared by three runtimes: the Cloudflare Worker (index.ts thin shell), the Node server (node/server.ts), and the npm/JSR library + CLI (lib.ts + node/cli.ts).
|
|
163
167
|
|
|
164
168
|
```
|
|
165
169
|
src/
|
|
166
|
-
├── index.ts
|
|
167
|
-
├──
|
|
168
|
-
├──
|
|
169
|
-
├──
|
|
170
|
-
├──
|
|
171
|
-
├──
|
|
170
|
+
├── index.ts Cloudflare Workers entry (thin shell: edge cache, rate-limit binding, waitUntil)
|
|
171
|
+
├── handler.ts shared HTTP layer: routing, POST short-link creation, short-link render, demo page, error wrapper
|
|
172
|
+
├── lib.ts npm/JSR library entry (barrel + high-level synchronous draw())
|
|
173
|
+
├── demo.ts demo page (type tabs, style variants, dual URL forms)
|
|
174
|
+
├── theme.ts skin → CSS variables (:root + dark media query)
|
|
175
|
+
├── store.ts short-link storage (GitHub repo read/write, fingerprint, idempotency)
|
|
176
|
+
├── version.ts version numbers and cache semantics
|
|
177
|
+
├── types.ts cross-runtime structural types (DrawEnv/Platform)
|
|
178
|
+
│
|
|
179
|
+
├── specs/ [parse] URL params → strongly-typed spec (validation, clipping, limits)
|
|
180
|
+
│ ├── base.ts ChartSpec + both flat-params and JSON forms
|
|
181
|
+
│ ├── multi.ts multi-series (series=) parsing
|
|
182
|
+
│ └── gauge/scatter/combo/csv.ts per-type parsers
|
|
172
183
|
│
|
|
173
|
-
├──
|
|
174
|
-
│ ├──
|
|
175
|
-
│ ├──
|
|
176
|
-
│
|
|
184
|
+
├── render/ [render] spec + skin → SVG string (pure computation, no layout engine)
|
|
185
|
+
│ ├── svg.ts shared helpers: layout geometry, scales, grid, legend, label collision, text measurement
|
|
186
|
+
│ ├── query.ts params→SVG synchronous core (GET dispatch + short-link segment; shared by lib and HTTP)
|
|
187
|
+
│ ├── dispatch.ts stored chart → renderer template dispatch
|
|
188
|
+
│ ├── pie/line/line-multi/area/… one file per chart type (bar ×3, line ×2)
|
|
189
|
+
│ └── qr.ts QR codes
|
|
177
190
|
│
|
|
178
|
-
├──
|
|
179
|
-
│ ├──
|
|
180
|
-
│ ├── pie/line/
|
|
181
|
-
│
|
|
191
|
+
├── styles/ [style] designer-spec SVGs turned into rules
|
|
192
|
+
│ ├── types.ts Rules vocabulary + defaults (orientation/radius/legend/dash/labels…)
|
|
193
|
+
│ ├── pie/line/area/… per-type variants (light+dark palettes + rule deltas + spec canvas size)
|
|
194
|
+
│ ├── common.ts spec palette constants
|
|
195
|
+
│ ├── handlee.ts hand-drawn font, base64 (star style)
|
|
196
|
+
│ └── index.ts resolveSkin: type + ?t= + scheme → skin
|
|
182
197
|
│
|
|
183
|
-
└──
|
|
184
|
-
├── types.ts Rules 词汇表 + 默认值(方向/圆角/图例/虚线/标注…)
|
|
185
|
-
├── pie/line/area/… 每图种变体定义(色板明暗双值 + 规则差异 + 规范画布尺寸)
|
|
186
|
-
├── common.ts 规范色板常量
|
|
187
|
-
├── handlee.ts 手绘字体 base64(star 风格)
|
|
188
|
-
└── index.ts resolveSkin:图种 + ?t= + 明暗 → 皮肤
|
|
198
|
+
└── node/ [Node] server.ts local server · cli.ts draw CLI
|
|
189
199
|
|
|
190
|
-
test/
|
|
191
|
-
wrangler.jsonc
|
|
200
|
+
test/ vitest (parsing + renderers + shared HTTP layer + library API)
|
|
201
|
+
wrangler.jsonc deploy config (route x-cmd.com/draw*)
|
|
202
|
+
deno.json JSR publishing + Deno tasks
|
|
192
203
|
```
|
|
193
204
|
|
|
194
|
-
|
|
205
|
+
Adding a chart type = one file each in `specs/xxx.ts` + `render/xxx.ts` + `styles/xxx.ts`, plus one registration line each in `render/dispatch.ts` and `render/query.ts`.
|
|
195
206
|
|
|
196
|
-
##
|
|
207
|
+
## One-shot self-deploy
|
|
197
208
|
|
|
198
|
-
|
|
209
|
+
Deploy your own draw to your Cloudflare account (free tier works; you just need a CF account):
|
|
199
210
|
|
|
200
211
|
```sh
|
|
201
|
-
curl -fsSL https://raw.githubusercontent.com/x-cmd
|
|
212
|
+
curl -fsSL https://raw.githubusercontent.com/x-cmd/draw/main/scripts/setup.sh | bash
|
|
202
213
|
```
|
|
203
214
|
|
|
204
|
-
|
|
215
|
+
The script: clones the repo → browser login for wrangler → deploys → smoke test → prints your `*.workers.dev` URL. All GET rendering (11 chart types, every style) works out of the box.
|
|
205
216
|
|
|
206
|
-
-
|
|
207
|
-
-
|
|
208
|
-
-
|
|
209
|
-
-
|
|
210
|
-
- Windows
|
|
217
|
+
- **Short links (POST) are not enabled by default** — calls return a "not configured" message. To enable: create a public data repo → set `DATA_REPO=owner/name` in the local wrangler.jsonc vars → `npx wrangler secret put GITHUB_TOKEN` (fine-grained PAT, Contents: write on that repo only) → redeploy
|
|
218
|
+
- Already cloned the repo? Run `npm run setup` inside it (skips the clone)
|
|
219
|
+
- Path prefix auto-adapts: both the workers.dev root and a `/draw` sub-route work with zero code changes; or pin it explicitly with the `BASE_PATH` var
|
|
220
|
+
- Custom domain: dashboard → Workers → Settings → Domains & Routes, add a route (any path prefix)
|
|
221
|
+
- On Windows, run under WSL or Git Bash
|
|
211
222
|
|
|
212
|
-
##
|
|
223
|
+
## Development
|
|
213
224
|
|
|
214
225
|
```sh
|
|
215
226
|
npm install
|
|
216
|
-
npm run dev # wrangler dev
|
|
227
|
+
npm run dev # wrangler dev, local server
|
|
217
228
|
npm test # vitest
|
|
218
|
-
npm run check # tsc --noEmit × 3(worker / node / build)
|
|
219
|
-
npm run build #
|
|
229
|
+
npm run check # tsc --noEmit × 3 (worker / node / build)
|
|
230
|
+
npm run build # emit dist/ (npm library + CLI)
|
|
220
231
|
npm run deploy # wrangler deploy
|
|
221
232
|
```
|
|
222
233
|
|
|
223
|
-
##
|
|
234
|
+
## License
|
|
224
235
|
|
|
225
|
-
|
|
236
|
+
Code is [Apache-2.0](LICENSE). Bundled fonts keep their own licenses: Inter (OFL, full text in `src/styles/inter-OFL.txt`) and Handlee (OFL).
|