@x-cmd/draw 0.0.0-stage → 0.2.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.
- package/LICENSE +228 -0
- package/README.cn.md +235 -0
- package/README.md +235 -2
- package/dist/lib.d.ts +25 -0
- package/dist/lib.js +50 -0
- package/dist/node/cli.d.ts +2 -0
- package/dist/node/cli.js +79 -0
- package/dist/render/area.d.ts +8 -0
- package/dist/render/area.js +414 -0
- package/dist/render/bar-grid.d.ts +3 -0
- package/dist/render/bar-grid.js +52 -0
- package/dist/render/bar-grouped.d.ts +3 -0
- package/dist/render/bar-grouped.js +131 -0
- package/dist/render/bar-stacked.d.ts +3 -0
- package/dist/render/bar-stacked.js +142 -0
- package/dist/render/bar.d.ts +3 -0
- package/dist/render/bar.js +74 -0
- package/dist/render/combo.d.ts +3 -0
- package/dist/render/combo.js +256 -0
- package/dist/render/dispatch.d.ts +3 -0
- package/dist/render/dispatch.js +69 -0
- package/dist/render/gauge.d.ts +3 -0
- package/dist/render/gauge.js +216 -0
- package/dist/render/hbar-spec.d.ts +7 -0
- package/dist/render/hbar-spec.js +265 -0
- package/dist/render/hbar.d.ts +3 -0
- package/dist/render/hbar.js +55 -0
- package/dist/render/line-multi.d.ts +3 -0
- package/dist/render/line-multi.js +89 -0
- package/dist/render/line.d.ts +11 -0
- package/dist/render/line.js +123 -0
- package/dist/render/measure.d.ts +60 -0
- package/dist/render/measure.js +75 -0
- package/dist/render/pie.d.ts +5 -0
- package/dist/render/pie.js +439 -0
- package/dist/render/qr.d.ts +8 -0
- package/dist/render/qr.js +52 -0
- package/dist/render/query.d.ts +14 -0
- package/dist/render/query.js +220 -0
- package/dist/render/radar.d.ts +5 -0
- package/dist/render/radar.js +221 -0
- package/dist/render/sankey.d.ts +3 -0
- package/dist/render/sankey.js +280 -0
- package/dist/render/scatter.d.ts +3 -0
- package/dist/render/scatter.js +98 -0
- package/dist/render/svg.d.ts +93 -0
- package/dist/render/svg.js +471 -0
- package/dist/specs/base.d.ts +34 -0
- package/dist/specs/base.js +159 -0
- package/dist/specs/combo.d.ts +15 -0
- package/dist/specs/combo.js +135 -0
- package/dist/specs/csv.d.ts +1 -0
- package/dist/specs/csv.js +40 -0
- package/dist/specs/gauge.d.ts +15 -0
- package/dist/specs/gauge.js +108 -0
- package/dist/specs/index.d.ts +8 -0
- package/dist/specs/index.js +9 -0
- package/dist/specs/input.d.ts +11 -0
- package/dist/specs/input.js +33 -0
- package/dist/specs/multi.d.ts +12 -0
- package/dist/specs/multi.js +112 -0
- package/dist/specs/sankey.d.ts +13 -0
- package/dist/specs/sankey.js +112 -0
- package/dist/specs/scatter.d.ts +20 -0
- package/dist/specs/scatter.js +121 -0
- package/dist/store.d.ts +15 -0
- package/dist/store.js +121 -0
- package/dist/styles/area.d.ts +2 -0
- package/dist/styles/area.js +106 -0
- package/dist/styles/bar.d.ts +2 -0
- package/dist/styles/bar.js +52 -0
- package/dist/styles/combo.d.ts +2 -0
- package/dist/styles/combo.js +37 -0
- package/dist/styles/common.d.ts +34 -0
- package/dist/styles/common.js +259 -0
- package/dist/styles/gauge.d.ts +2 -0
- package/dist/styles/gauge.js +51 -0
- package/dist/styles/handlee.d.ts +1 -0
- package/dist/styles/handlee.js +2 -0
- package/dist/styles/hbar.d.ts +2 -0
- package/dist/styles/hbar.js +66 -0
- package/dist/styles/index.d.ts +7 -0
- package/dist/styles/index.js +79 -0
- package/dist/styles/inter.d.ts +3 -0
- package/dist/styles/inter.js +10 -0
- package/dist/styles/line.d.ts +2 -0
- package/dist/styles/line.js +97 -0
- package/dist/styles/pie.d.ts +2 -0
- package/dist/styles/pie.js +163 -0
- package/dist/styles/qr.d.ts +2 -0
- package/dist/styles/qr.js +12 -0
- package/dist/styles/radar.d.ts +2 -0
- package/dist/styles/radar.js +50 -0
- package/dist/styles/sankey.d.ts +2 -0
- package/dist/styles/sankey.js +29 -0
- package/dist/styles/scatter.d.ts +2 -0
- package/dist/styles/scatter.js +21 -0
- package/dist/styles/types.d.ts +105 -0
- package/dist/styles/types.js +70 -0
- package/dist/theme.d.ts +3 -0
- package/dist/theme.js +11 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.js +1 -0
- package/dist/version.d.ts +6 -0
- package/dist/version.js +16 -0
- package/package.json +53 -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.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)。
|