@x-cmd/draw 0.0.0-stage → 0.1.0

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.
Files changed (105) hide show
  1. package/LICENSE +228 -0
  2. package/README.md +224 -2
  3. package/dist/lib.d.ts +25 -0
  4. package/dist/lib.js +50 -0
  5. package/dist/node/cli.d.ts +2 -0
  6. package/dist/node/cli.js +76 -0
  7. package/dist/render/area.d.ts +8 -0
  8. package/dist/render/area.js +414 -0
  9. package/dist/render/bar-grid.d.ts +3 -0
  10. package/dist/render/bar-grid.js +52 -0
  11. package/dist/render/bar-grouped.d.ts +3 -0
  12. package/dist/render/bar-grouped.js +131 -0
  13. package/dist/render/bar-stacked.d.ts +3 -0
  14. package/dist/render/bar-stacked.js +142 -0
  15. package/dist/render/bar.d.ts +3 -0
  16. package/dist/render/bar.js +74 -0
  17. package/dist/render/combo.d.ts +3 -0
  18. package/dist/render/combo.js +256 -0
  19. package/dist/render/dispatch.d.ts +3 -0
  20. package/dist/render/dispatch.js +69 -0
  21. package/dist/render/gauge.d.ts +3 -0
  22. package/dist/render/gauge.js +216 -0
  23. package/dist/render/hbar-spec.d.ts +7 -0
  24. package/dist/render/hbar-spec.js +265 -0
  25. package/dist/render/hbar.d.ts +3 -0
  26. package/dist/render/hbar.js +55 -0
  27. package/dist/render/line-multi.d.ts +3 -0
  28. package/dist/render/line-multi.js +89 -0
  29. package/dist/render/line.d.ts +11 -0
  30. package/dist/render/line.js +123 -0
  31. package/dist/render/measure.d.ts +60 -0
  32. package/dist/render/measure.js +75 -0
  33. package/dist/render/pie.d.ts +5 -0
  34. package/dist/render/pie.js +439 -0
  35. package/dist/render/qr.d.ts +8 -0
  36. package/dist/render/qr.js +52 -0
  37. package/dist/render/query.d.ts +14 -0
  38. package/dist/render/query.js +220 -0
  39. package/dist/render/radar.d.ts +5 -0
  40. package/dist/render/radar.js +221 -0
  41. package/dist/render/sankey.d.ts +3 -0
  42. package/dist/render/sankey.js +280 -0
  43. package/dist/render/scatter.d.ts +3 -0
  44. package/dist/render/scatter.js +98 -0
  45. package/dist/render/svg.d.ts +93 -0
  46. package/dist/render/svg.js +471 -0
  47. package/dist/specs/base.d.ts +34 -0
  48. package/dist/specs/base.js +159 -0
  49. package/dist/specs/combo.d.ts +15 -0
  50. package/dist/specs/combo.js +135 -0
  51. package/dist/specs/csv.d.ts +1 -0
  52. package/dist/specs/csv.js +40 -0
  53. package/dist/specs/gauge.d.ts +15 -0
  54. package/dist/specs/gauge.js +108 -0
  55. package/dist/specs/index.d.ts +8 -0
  56. package/dist/specs/index.js +9 -0
  57. package/dist/specs/input.d.ts +11 -0
  58. package/dist/specs/input.js +33 -0
  59. package/dist/specs/multi.d.ts +12 -0
  60. package/dist/specs/multi.js +112 -0
  61. package/dist/specs/sankey.d.ts +13 -0
  62. package/dist/specs/sankey.js +112 -0
  63. package/dist/specs/scatter.d.ts +20 -0
  64. package/dist/specs/scatter.js +121 -0
  65. package/dist/store.d.ts +15 -0
  66. package/dist/store.js +121 -0
  67. package/dist/styles/area.d.ts +2 -0
  68. package/dist/styles/area.js +106 -0
  69. package/dist/styles/bar.d.ts +2 -0
  70. package/dist/styles/bar.js +52 -0
  71. package/dist/styles/combo.d.ts +2 -0
  72. package/dist/styles/combo.js +37 -0
  73. package/dist/styles/common.d.ts +34 -0
  74. package/dist/styles/common.js +259 -0
  75. package/dist/styles/gauge.d.ts +2 -0
  76. package/dist/styles/gauge.js +51 -0
  77. package/dist/styles/handlee.d.ts +1 -0
  78. package/dist/styles/handlee.js +2 -0
  79. package/dist/styles/hbar.d.ts +2 -0
  80. package/dist/styles/hbar.js +66 -0
  81. package/dist/styles/index.d.ts +7 -0
  82. package/dist/styles/index.js +79 -0
  83. package/dist/styles/inter.d.ts +3 -0
  84. package/dist/styles/inter.js +10 -0
  85. package/dist/styles/line.d.ts +2 -0
  86. package/dist/styles/line.js +97 -0
  87. package/dist/styles/pie.d.ts +2 -0
  88. package/dist/styles/pie.js +163 -0
  89. package/dist/styles/qr.d.ts +2 -0
  90. package/dist/styles/qr.js +12 -0
  91. package/dist/styles/radar.d.ts +2 -0
  92. package/dist/styles/radar.js +50 -0
  93. package/dist/styles/sankey.d.ts +2 -0
  94. package/dist/styles/sankey.js +29 -0
  95. package/dist/styles/scatter.d.ts +2 -0
  96. package/dist/styles/scatter.js +21 -0
  97. package/dist/styles/types.d.ts +105 -0
  98. package/dist/styles/types.js +70 -0
  99. package/dist/theme.d.ts +3 -0
  100. package/dist/theme.js +11 -0
  101. package/dist/types.d.ts +17 -0
  102. package/dist/types.js +1 -0
  103. package/dist/version.d.ts +6 -0
  104. package/dist/version.js +16 -0
  105. package/package.json +47 -4
package/LICENSE ADDED
@@ -0,0 +1,228 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
203
+
204
+
205
+ APPENDIX: How to apply the Apache License to your work.
206
+
207
+ To apply the Apache License to your work, attach the following
208
+ boilerplate notice, with the fields enclosed by brackets "[]"
209
+ replaced with your own identifying information. (Don't include the
210
+ brackets!) The text should be enclosed in the appropriate comment
211
+ syntax for the file format. We recommend that a file or class name
212
+ and description of purpose be included on the same "printed page" as
213
+ the copyright notice for easier identification within third-party
214
+ archives.
215
+
216
+ Copyright 2026 x-cmd
217
+
218
+ Licensed under the Apache License, Version 2.0 (the "License");
219
+ you may not use this file except in compliance with the License.
220
+ You may obtain a copy of the License at
221
+
222
+ http://www.apache.org/licenses/LICENSE-2.0
223
+
224
+ Unless required by applicable law or agreed to in writing, software
225
+ distributed under the License is distributed on an "AS IS" BASIS,
226
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
227
+ See the License for the specific language governing permissions and
228
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,225 @@
1
- # Temporary Holding Version
1
+ # draw
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ AI 时代的 SVG 图表生成服务(狗粮原型)。单端点 `/draw?type=...`,数据在 URL 里、输出带机器可读 `<metadata>`、Cloudflare Workers + TypeScript 直生成 SVG。风格体系对齐设计师规范(13 张 SVG),每图种多风格变体。
4
+
5
+ ## 三种使用方式(同一份渲染核心)
6
+
7
+ **托管服务**(零安装):直接拼 URL — `https://x-cmd.com/draw?type=pie&labels=A,B&values=3,2`
8
+
9
+ **npm 库**(离线/嵌入,同步无网络,参数与 URL 完全一致):
10
+
11
+ ```sh
12
+ npm install @x-cmd/draw
13
+ ```
14
+ ```ts
15
+ import { draw } from '@x-cmd/draw';
16
+ const { svg } = draw({ type: 'pie', labels: 'A,B', values: '3,2', t: 'evil', scheme: 'dark' });
17
+ // 也接受查询串: draw('?type=bar&labels=Mon,Tue&values=3,5')
18
+ // 数组值 = 重复参数(labels 含逗号时): draw({ type: 'bar', labels: ['Q1, 2024', 'Q2'], values: [1, 2] })
19
+ ```
20
+
21
+ **CLI**(`npm i -g @x-cmd/draw` 后可用,或 `npx`):
22
+
23
+ ```sh
24
+ x-draw --type pie --labels "TypeScript,Python,Go" --values "4821,2312,980" --out pie.svg
25
+ x-draw "?type=hbar&labels=a,b,c&values=3,2,1&t=xcmd" # 裸查询串,与 URL 同参
26
+ ```
27
+
28
+ **自托管**:三种入口任选 —
29
+
30
+ ```sh
31
+ git clone https://github.com/x-cmd/draw && cd draw && npm install
32
+ npm run serve # Node 本地服务(默认 http://localhost:8787/draw,含演示页)
33
+ npm run dev # Cloudflare wrangler 本地开发
34
+ bash scripts/setup.sh # 一键部署到你自己的 Cloudflare 账号
35
+ ```
36
+
37
+ 环境变量(Node 服务):`PORT`(默认 8787)· `BASE_PATH` · `DATA_REPO`/`DATA_BRANCH`/`GITHUB_TOKEN`(配置后 POST 短链同样可用,未配置时与线上自部署一样优雅降级)。
38
+
39
+ **Deno**(原生 TS;渲染核心是纯函数 — 不碰网络/文件,零权限沙盒即安全边界):
40
+
41
+ ```ts
42
+ import { draw } from "jsr:@x-cmd/draw"; // JSR(Deno 原生注册表)
43
+ // 或 npm:import { draw } from "npm:@x-cmd/draw";
44
+ const { svg } = draw({ type: "pie", labels: "A,B", values: "3,2" });
45
+ ```
46
+
47
+ ```sh
48
+ deno add jsr:@x-cmd/draw
49
+ # 仓库内直跑源码(免构建):
50
+ deno task cli --type pie --labels A,B --values 3,2 --out pie.svg # 仅需 --allow-write
51
+ deno task serve # 本地服务(同 npm run serve)
52
+ # 渲染零权限可证:deno eval 默认无任何授权,直接 import 即渲染成功
53
+ ```
54
+
55
+ ## 端点
56
+
57
+ **`GET /draw?type=<pie|bar|hbar|line|area|radar|gauge|scatter|combo|sankey|qr>`** — 渲染图表
58
+
59
+ | type | 图表 | 限制 |
60
+ |---|---|---|
61
+ | `pie` | 环形图;`donut=0` 变实心饼 | ≤100 类(>9 自动合并 Other) |
62
+ | `bar` | 柱状图/分组并排;多系列加 `stack=1` 变堆叠柱 | ≤30 组 |
63
+ | `hbar` | 水平柱状图(排行场景,标签左值右) | ≤30 条,单系列(部分风格双系列) |
64
+ | `line` | 折线图(平滑+渐变面积)/多折线 | ≤60 点 |
65
+ | `area` | 面积图/多系列面积(渐变填充) | ≤60 点 |
66
+ | `radar` | 雷达图 | 3-12 维 |
67
+ | `gauge` | 仪表盘(240° 弧+中心数值) | `value`+`max`(默认100),value≤max;多环 ≤8 |
68
+ | `scatter` | 散点图(双数值轴,支持负值;默认不标数值) | `x=&y=`,≤200 点 |
69
+ | `combo` | 混合图:柱(`series=`)+线(`line=`)同图,`stack=1` 变堆叠柱 | ≤30 组,共 ≤6 系列 |
70
+ | `sankey` | 桑基图(流向带宽) | `labels=`+`links=A>B:30`,≤24 节点/60 链接 |
71
+ | `qr` | 二维码 | `d`=文本(≤1000 字符),`fg`/`bg` 配色,`title`/`unit` 标注 |
72
+
73
+ **`GET /draw`**(无参数)或 `?tab=pie` — 演示页(11 图种分类、全部风格变体、URL 双形态面板)
74
+ **`POST /draw?type=...`** — 提交 JSON/CSV body 创建永久短链
75
+ **`GET /draw/c/<hash>`** — 短链渲染(immutable)
76
+
77
+ 通用参数:
78
+
79
+ - `t` 风格家族(见下节,按图种取用;该图种未做时回退默认)
80
+ - `scheme` 明暗:`auto`(默认,跟随系统)/ `dark` / `light`
81
+ - `v` 钉住版本;`w`/`h` 尺寸(200–1600 / 140–1200,qr 为 120–1000 / 140–1000)
82
+ - `num` 数值标注:`all` / `peak`(仅最高)/ `none`。默认跟随风格(设计规范变体多为 `none`,`?num=all` 显式全标)。适用于 pie/bar/hbar/line/area/scatter/combo;radar/gauge/sankey/qr 忽略
83
+ - `subtitle` 编辑式副标题(仅 area 的编辑式标题风格 `t=opendata` 渲染)、`source` 数据来源脚注(仅 area 渲染,任意 `t`);其余图种解析但忽略
84
+
85
+ ## 风格(`?t=`)
86
+
87
+ 按图种提供变体,明暗双配色自动切换:
88
+
89
+ | type | 可用家族 |
90
+ |---|---|
91
+ | `pie` | `opendata`(默认,标签全外)、`evil`(圆角环+内标值)、`evil-mono`(实心饼+引线外标)、`market-mono`(灰阶+环心总计)、`progress`(点状进度环)、`revenue`(渐变扇区+虚线内圈) |
92
+ | `bar` | `evil`(默认,分组柱)、`base`(卡片柱)、`grid`(表格柱) |
93
+ | `hbar` | `base`(默认)、`opendata`(支持 `ref=`/`reflabel=` 参考线)、`xcmd`(像素条,值为 %)、`xcmd-issue`(双系列蝴蝶)、`rank`(双系列实底+斜纹) |
94
+ | `line` | `shadcn`(默认)、`evil`、`evil-glow`(发光)、`evil-mono`、`opendata`(线端标签) |
95
+ | `area` | `evil`(默认,双系列虚线面积)、`star`(手绘风)、`opendata`(浓渐变+编辑标题)、`pixel`、`polka` |
96
+ | `radar` | `base`(默认)、`evil`(多系列)、`xcmd` |
97
+ | `sankey` | `base`(默认)、`fund`、`pipeline` |
98
+ | `gauge` | `base`(默认)、`gradient`、`reliability`(分区环仪表) |
99
+ | `scatter` / `combo` / `qr` | `base`(combo 另有 `evil`) |
100
+
101
+ ## 传数据的两种方式
102
+
103
+ **扁平参数(URL 永远可读,推荐)**:
104
+
105
+ ```
106
+ /draw?type=pie&title=Stars by language&unit=stars&labels=TypeScript,Python,Go,Rust&values=4821,2312,980,645
107
+ /draw?type=bar&title=Weekly downloads&labels=Mon,Tue&values=1200,1800
108
+ ```
109
+
110
+ 标签含逗号时用重复参数:`labels=Q1, 2024&labels=Q2&values=1&values=2`
111
+
112
+ **多系列对比**(type=bar 分组/堆叠、type=line 多折线、type=area 多面积,≤6 系列):
113
+
114
+ ```
115
+ /draw?type=area&labels=Mon,Tue,Wed&series=Desktop:120,180,150&series=Mobile:80,90,85
116
+ ```
117
+
118
+ **JSON 形态(复杂数据)**:`?d=` + URL 编码的 JSON:
119
+
120
+ ```json
121
+ {"title":"Stars by language","unit":"stars","data":[{"label":"TypeScript","value":4821},{"label":"Python","value":2312}]}
122
+ ```
123
+
124
+ 多系列 JSON:`{"labels":["Mon","Tue"],"series":[{"name":"draw","values":[120,180]},{"name":"qc","values":[80,90]}]}`
125
+
126
+ 二维码:`/draw?type=qr&d=https://github.com/tzw&fg=%236366f1`
127
+
128
+ 仪表盘:`/draw?type=gauge&title=Build pass rate&value=87&max=100`
129
+
130
+ 散点图:`/draw?type=scatter&x=10,20,30&y=5,8,7`
131
+
132
+ 混合图:`/draw?type=combo&labels=Mon,Tue&series=dl:120,180&line=growth:5,8`(柱与线量级差 >5 倍时自动启用双 y 轴)
133
+
134
+ ## 短链接(数据存 GitHub 仓库,内容寻址、永不过期)
135
+
136
+ ```
137
+ POST /draw?type=pie body = 图表 spec JSON
138
+ POST /draw?type=bar&t=evil body = CSV(text/csv,首行表头)
139
+ → 201 {"hash":"a3f9c2d4e5f60718","url":"https://x-cmd.com/draw/c/a3f9c2d4e5f60718"}
140
+
141
+ GET /draw/c/<hash> → SVG,永久 immutable 缓存
142
+ ```
143
+
144
+ - 指纹 = 规范化数据(template+theme+spec,递归排序 key)的 SHA-256 前 16 位;相同数据 → 相同短链(自动去重、幂等);读取时复核指纹,存档被改即拒绝
145
+ - 多系列支持 bar/line/area/radar,以及 hbar(仅 `t=xcmd-issue`/`t=rank`,恰好 2 系列);其余 type 提交多系列会被 400 拒绝;POST 限 30 次/分钟/IP
146
+ - 服务端配置:`DATA_REPO`(公开仓,`owner/name`,wrangler.jsonc)、`GITHUB_TOKEN`(secret,fine-grained PAT 仅授该仓 **Contents 写**;读取走 raw 免 token);数据文件存于 `charts/<hash>.json`
147
+ - 可见性:数据仓是公开的,任何人可浏览目录——**请勿创建含敏感数据的短链**(私有存储方案规划中,将收费提供)
148
+ - 对比 quickchart:短链会过期(6个月/数天),我们的永不过期
149
+
150
+ ## 版本语义
151
+
152
+ - 不带 `v`:渲染**最新版**,`Cache-Control: max-age=300`(链接随升级变化)
153
+ - `?v=1`:钉住版本,`Cache-Control: max-age=86400`(1 天;渲染仍随部署演进,不承诺永久冻结)。`v` 取值 1..当前版本,超出报 400
154
+ - `immutable` 仅用于短链 `/c/<hash>`(内容寻址,输出真正永不变化;短链忽略 `v` 参数)
155
+
156
+ ## AI 可读
157
+
158
+ 每个 SVG 内嵌 `<metadata>`(转义 JSON):`{generator, template, version, spec}`,数据与参数可直接 round-trip。文字全部用 `<text>`(真文本)。
159
+
160
+ ## 项目结构
161
+
162
+ 一次请求的路径:**index.ts(路由)→ specs/(解析)→ styles/(选皮肤)→ render/(算几何出 SVG)**,四层各管一事。
163
+
164
+ ```
165
+ src/
166
+ ├── index.ts 路由入口:GET /draw?type= 分发、演示页、短链 /c/、错误处理
167
+ ├── dispatch.ts 短链存档 → 渲染器的模板分发
168
+ ├── demo.ts 演示页(图种 tab、风格变体、URL 双形态面板)
169
+ ├── theme.ts 皮肤 → CSS 变量(:root + 暗色媒体查询)
170
+ ├── store.ts 短链存储(GitHub repo 读写、指纹、幂等)
171
+ ├── version.ts 版本号与缓存语义
172
+ │
173
+ ├── specs/ 【解析层】URL 参数 → 强类型 spec(校验、截断、上限)
174
+ │ ├── base.ts ChartSpec + 扁平参数/JSON 双形态解析
175
+ │ ├── multi.ts 多系列(series=)解析
176
+ │ └── gauge/scatter/combo/csv.ts 图种专属解析
177
+ │
178
+ ├── render/ 【渲染层】spec + 皮肤 → SVG 字符串(纯计算,无布局引擎)
179
+ │ ├── svg.ts 公共构造:布局几何、刻度、网格、图例、标签防碰撞、文字测量
180
+ │ ├── pie/line/line-multi/area/… 每图种一个渲染文件(bar 拆 3、line 拆 2)
181
+ │ └── qr.ts 二维码
182
+ │
183
+ └── styles/ 【风格层】设计师规范 SVG 的规则化
184
+ ├── types.ts Rules 词汇表 + 默认值(方向/圆角/图例/虚线/标注…)
185
+ ├── pie/line/area/… 每图种变体定义(色板明暗双值 + 规则差异 + 规范画布尺寸)
186
+ ├── common.ts 规范色板常量
187
+ ├── handlee.ts 手绘字体 base64(star 风格)
188
+ └── index.ts resolveSkin:图种 + ?t= + 明暗 → 皮肤
189
+
190
+ test/render.test.ts 单测(解析 + 各渲染器行为断言)
191
+ wrangler.jsonc 部署配置(路由 x-cmd.com/draw*)
192
+ ```
193
+
194
+ 加新图种 = `specs/xxx.ts` + `render/xxx.ts` + `styles/xxx.ts` 各一个文件,再在 `dispatch.ts` 与 `index.ts` 各注册一行。
195
+
196
+ ## 自部署(一键)
197
+
198
+ 部署一套自己的 draw 到你的 Cloudflare 账号(免费计划即可,只需 CF 账号):
199
+
200
+ ```sh
201
+ curl -fsSL https://raw.githubusercontent.com/x-cmd-hub/draw/main/scripts/setup.sh | bash
202
+ ```
203
+
204
+ 脚本流程:克隆仓库 → wrangler 浏览器登录 → 部署 → 冒烟测试,结束打印你的 `*.workers.dev` 地址。GET 渲染全家桶(11 图种、全部风格)开箱即用。
205
+
206
+ - **短链(POST)第一阶段未默认启用**,调用会返回"未配置"提示。启用方法:建一个公开仓存数据 → 本地 wrangler.jsonc 的 vars 设 `DATA_REPO=owner/name` → `npx wrangler secret put GITHUB_TOKEN`(fine-grained PAT 仅授该仓 Contents 写)→ 重新部署
207
+ - 已 clone 仓库的,在仓库目录里跑 `npm run setup` 即可(跳过克隆)
208
+ - 路径前缀自动适配:workers.dev 根路径与 `/draw` 子路由都能用,无需改代码;也可用 `BASE_PATH` var 显式钉死
209
+ - 绑自有域名:dashboard → Workers → Settings → Domains & Routes 加 route(任意路径前缀)
210
+ - Windows 请用 WSL 或 Git Bash 运行
211
+
212
+ ## 开发
213
+
214
+ ```sh
215
+ npm install
216
+ npm run dev # wrangler dev 本地起服
217
+ npm test # vitest
218
+ npm run check # tsc --noEmit × 3(worker / node / build)
219
+ npm run build # 产 dist/(npm 库 + CLI)
220
+ npm run deploy # wrangler deploy
221
+ ```
222
+
223
+ ## 许可
224
+
225
+ 代码 [Apache-2.0](LICENSE)。内嵌字体遵循各自许可:Inter(OFL,全文见 `src/styles/inter-OFL.txt`)、Handlee(OFL)。
package/dist/lib.d.ts ADDED
@@ -0,0 +1,25 @@
1
+ export * from './specs/index.js';
2
+ export * from './styles/index.js';
3
+ export { CURRENT_VERSION, resolveVersion, type VersionInfo } from './version.js';
4
+ export { renderPie } from './render/pie.js';
5
+ export { renderBar } from './render/bar.js';
6
+ export { renderBarGrid } from './render/bar-grid.js';
7
+ export { renderBarMulti } from './render/bar-grouped.js';
8
+ export { renderBarStacked } from './render/bar-stacked.js';
9
+ export { renderLine } from './render/line.js';
10
+ export { renderLineMulti } from './render/line-multi.js';
11
+ export { renderAreaByMode } from './render/area.js';
12
+ export { renderRadarByMode } from './render/radar.js';
13
+ export { renderHbarByMode } from './render/hbar-spec.js';
14
+ export { renderGauge } from './render/gauge.js';
15
+ export { renderScatter } from './render/scatter.js';
16
+ export { renderCombo } from './render/combo.js';
17
+ export { renderQR } from './render/qr.js';
18
+ export { renderSankey } from './render/sankey.js';
19
+ export { renderStored } from './render/dispatch.js';
20
+ export type { RenderOpts } from './render/svg.js';
21
+ import { type DrawResult } from './render/query.js';
22
+ export { renderFromUrl, renderStoredFromUrl } from './render/query.js';
23
+ export type { DrawResult } from './render/query.js';
24
+ export { CHART_TEMPLATES, fingerprint, canonicalJson, type ChartTemplate, type StoredChart, } from './store.js';
25
+ export declare function draw(params: Record<string, string | string[]> | URLSearchParams | string): DrawResult;
package/dist/lib.js ADDED
@@ -0,0 +1,50 @@
1
+ // npm 库入口(@x-cmd/draw):
2
+ // import { draw } from '@x-cmd/draw';
3
+ // const { svg } = draw({ type: 'pie', labels: 'A,B', values: '3,2' });
4
+ // 高层 draw() 同步渲染,与 GET /draw?type=... 完全同参同输出,无需网络。
5
+ // 亦可直接使用下层解析/渲染/皮肤 API(与线上服务同一套代码)。
6
+ export * from './specs/index.js';
7
+ export * from './styles/index.js';
8
+ export { CURRENT_VERSION, resolveVersion } from './version.js';
9
+ export { renderPie } from './render/pie.js';
10
+ export { renderBar } from './render/bar.js';
11
+ export { renderBarGrid } from './render/bar-grid.js';
12
+ export { renderBarMulti } from './render/bar-grouped.js';
13
+ export { renderBarStacked } from './render/bar-stacked.js';
14
+ export { renderLine } from './render/line.js';
15
+ export { renderLineMulti } from './render/line-multi.js';
16
+ export { renderAreaByMode } from './render/area.js';
17
+ export { renderRadarByMode } from './render/radar.js';
18
+ export { renderHbarByMode } from './render/hbar-spec.js';
19
+ export { renderGauge } from './render/gauge.js';
20
+ export { renderScatter } from './render/scatter.js';
21
+ export { renderCombo } from './render/combo.js';
22
+ export { renderQR } from './render/qr.js';
23
+ export { renderSankey } from './render/sankey.js';
24
+ export { renderStored } from './render/dispatch.js';
25
+ import { renderFromUrl } from './render/query.js';
26
+ export { renderFromUrl, renderStoredFromUrl } from './render/query.js';
27
+ export { CHART_TEMPLATES, fingerprint, canonicalJson, } from './store.js';
28
+ // 对象/查询串/URLSearchParams → DrawResult(同步)。
29
+ // 对象值支持数组(重复参数,labels 含逗号时用):
30
+ // draw({ type: 'bar', labels: ['Q1, 2024', 'Q2'], values: [1, 2] })
31
+ function toSearchParams(o) {
32
+ const sp = new URLSearchParams();
33
+ for (const [k, v] of Object.entries(o)) {
34
+ if (Array.isArray(v))
35
+ for (const item of v)
36
+ sp.append(k, item);
37
+ else
38
+ sp.append(k, v);
39
+ }
40
+ return sp;
41
+ }
42
+ export function draw(params) {
43
+ const sp = typeof params === 'string'
44
+ ? new URLSearchParams(params.startsWith('?') ? params.slice(1) : params)
45
+ : params instanceof URLSearchParams
46
+ ? params
47
+ : toSearchParams(params);
48
+ // renderFromUrl 只读 searchParams;host 用占位符,不发起任何网络请求
49
+ return renderFromUrl(new URL(`https://draw.local/?${sp.toString()}`));
50
+ }
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env node
2
+ // CLI:把查询参数渲染成 SVG,写 stdout 或 --out 文件。
3
+ // x-draw --type pie --labels "A,B" --values "3,2" --out pie.svg
4
+ // x-draw "?type=bar&labels=Mon,Tue&values=3,5&scheme=dark" # 裸查询串(与 URL 同参)
5
+ // 未知 flag 透传为查询参数(--ref 5 → ref=5);重复 flag → 重复参数(labels 含逗号时用)。
6
+ // 参数语义与 GET /draw 完全一致,见 README 或 x-cmd.com/draw 演示页。
7
+ import { writeFileSync } from 'node:fs';
8
+ import { draw } from '../lib.js';
9
+ import { ApiError } from '../specs/index.js';
10
+ const HELP = `usage: x-draw --type <pie|bar|hbar|line|area|radar|gauge|scatter|combo|sankey|qr> [params] [--out <file.svg>]
11
+
12
+ params are the same as GET /draw query params:
13
+ --labels "A,B" --values "3,2" data (repeated flags = repeated params)
14
+ --t <family> --scheme <auto|dark|light> --w 560 --h 300 --num all
15
+ --title "Stars by language" --unit stars
16
+ or pass one bare query string:
17
+ x-draw "?type=pie&labels=A,B&values=3,2"
18
+
19
+ examples:
20
+ x-draw --type pie --labels "TypeScript,Python,Go" --values "4821,2312,980" --out pie.svg
21
+ x-draw "?type=hbar&labels=a,b,c&values=3,2,1&t=xcmd" | open -f -a Safari`;
22
+ function fail(msg) {
23
+ console.error(`x-draw: ${msg}\n\n${HELP}`);
24
+ process.exit(1);
25
+ }
26
+ const args = process.argv.slice(2);
27
+ if (args.length === 0 || args[0] === '--help' || args[0] === '-h') {
28
+ console.log(HELP);
29
+ process.exit(0);
30
+ }
31
+ const sp = new URLSearchParams();
32
+ let out;
33
+ for (let i = 0; i < args.length; i++) {
34
+ const a = args[i];
35
+ if (a === '--out' || a === '-o') {
36
+ out = args[++i];
37
+ if (out === undefined)
38
+ fail('--out requires a file path');
39
+ continue;
40
+ }
41
+ if (a.startsWith('--')) {
42
+ const key = a.slice(2);
43
+ const next = args[i + 1];
44
+ if (next === undefined || next.startsWith('--')) {
45
+ sp.set(key, '1'); // 无值 flag → 布尔(--stack → stack=1)
46
+ continue;
47
+ }
48
+ sp.append(key, next);
49
+ i++;
50
+ continue;
51
+ }
52
+ if (a.includes('=')) {
53
+ const qs = new URLSearchParams(a.startsWith('?') ? a.slice(1) : a);
54
+ for (const [k, v] of qs)
55
+ sp.append(k, v);
56
+ continue;
57
+ }
58
+ fail(`unexpected argument: ${a}`);
59
+ }
60
+ try {
61
+ const { svg } = draw(sp);
62
+ if (out !== undefined) {
63
+ writeFileSync(out, svg);
64
+ console.error(`wrote ${out} (${svg.length} bytes)`);
65
+ }
66
+ else {
67
+ process.stdout.write(svg);
68
+ }
69
+ }
70
+ catch (e) {
71
+ if (e instanceof ApiError) {
72
+ console.error(`x-draw: ${e.message}`);
73
+ process.exit(1);
74
+ }
75
+ throw e;
76
+ }
@@ -0,0 +1,8 @@
1
+ import { type ChartSpec, type MultiSpec } from '../specs/index.js';
2
+ import { type RenderOpts } from './svg.js';
3
+ export declare function renderArea(spec: ChartSpec, o: RenderOpts): string;
4
+ export declare function renderAreaMulti(spec: MultiSpec, o: RenderOpts): string;
5
+ export declare function renderAreaByMode(input: {
6
+ multi: boolean;
7
+ spec: ChartSpec | MultiSpec;
8
+ }, o: RenderOpts): string;