@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/dist/lib.d.ts
CHANGED
|
@@ -22,4 +22,22 @@ import { type DrawResult } from './render/query.js';
|
|
|
22
22
|
export { renderFromUrl, renderStoredFromUrl } from './render/query.js';
|
|
23
23
|
export type { DrawResult } from './render/query.js';
|
|
24
24
|
export { CHART_TEMPLATES, fingerprint, canonicalJson, type ChartTemplate, type StoredChart, } from './store.js';
|
|
25
|
+
/**
|
|
26
|
+
* Render a chart to an SVG string — synchronously, with no network access.
|
|
27
|
+
*
|
|
28
|
+
* Params mirror the `GET /draw` query string exactly (`type` required):
|
|
29
|
+
* `labels`/`values` (or `series=`), `t` style family, `scheme`, `w`/`h`, `num`,
|
|
30
|
+
* `title`, `unit`, `v`, and the chart-specific params from the README.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { draw } from "@x-cmd/draw";
|
|
35
|
+
*
|
|
36
|
+
* const { svg, version } = draw({ type: "pie", labels: "A,B", values: "3,2" });
|
|
37
|
+
* draw("?type=bar&labels=Mon,Tue&values=3,5"); // query string form
|
|
38
|
+
* draw({ type: "bar", labels: ["Q1, 2024", "Q2"], values: [1, 2] }); // arrays = repeated params
|
|
39
|
+
* ```
|
|
40
|
+
*
|
|
41
|
+
* @throws ApiError(400) with a corrective message on bad params
|
|
42
|
+
*/
|
|
25
43
|
export declare function draw(params: Record<string, string | string[]> | URLSearchParams | string): DrawResult;
|
package/dist/lib.js
CHANGED
|
@@ -39,6 +39,24 @@ function toSearchParams(o) {
|
|
|
39
39
|
}
|
|
40
40
|
return sp;
|
|
41
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* Render a chart to an SVG string — synchronously, with no network access.
|
|
44
|
+
*
|
|
45
|
+
* Params mirror the `GET /draw` query string exactly (`type` required):
|
|
46
|
+
* `labels`/`values` (or `series=`), `t` style family, `scheme`, `w`/`h`, `num`,
|
|
47
|
+
* `title`, `unit`, `v`, and the chart-specific params from the README.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { draw } from "@x-cmd/draw";
|
|
52
|
+
*
|
|
53
|
+
* const { svg, version } = draw({ type: "pie", labels: "A,B", values: "3,2" });
|
|
54
|
+
* draw("?type=bar&labels=Mon,Tue&values=3,5"); // query string form
|
|
55
|
+
* draw({ type: "bar", labels: ["Q1, 2024", "Q2"], values: [1, 2] }); // arrays = repeated params
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @throws ApiError(400) with a corrective message on bad params
|
|
59
|
+
*/
|
|
42
60
|
export function draw(params) {
|
|
43
61
|
const sp = typeof params === 'string'
|
|
44
62
|
? new URLSearchParams(params.startsWith('?') ? params.slice(1) : params)
|
package/dist/node/cli.js
CHANGED
|
@@ -1,26 +1,29 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// CLI:把查询参数渲染成 SVG,写 stdout 或 --out 文件。
|
|
3
|
-
//
|
|
4
|
-
//
|
|
3
|
+
// draw --type pie --labels "A,B" --values "3,2" --out pie.svg
|
|
4
|
+
// draw "?type=bar&labels=Mon,Tue&values=3,5&scheme=dark" # 裸查询串(与 URL 同参)
|
|
5
5
|
// 未知 flag 透传为查询参数(--ref 5 → ref=5);重复 flag → 重复参数(labels 含逗号时用)。
|
|
6
6
|
// 参数语义与 GET /draw 完全一致,见 README 或 x-cmd.com/draw 演示页。
|
|
7
7
|
import { writeFileSync } from 'node:fs';
|
|
8
8
|
import { draw } from '../lib.js';
|
|
9
9
|
import { ApiError } from '../specs/index.js';
|
|
10
|
-
|
|
10
|
+
// 长 flag → 查询参数名(单字母直写也依然有效,经透传)
|
|
11
|
+
const ALIASES = { theme: 't', width: 'w', height: 'h' };
|
|
12
|
+
const HELP = `usage: draw --type <pie|bar|hbar|line|area|radar|gauge|scatter|combo|sankey|qr> [params] [--out <file.svg>]
|
|
11
13
|
|
|
12
14
|
params are the same as GET /draw query params:
|
|
13
|
-
--labels "A,B" --values "3,2"
|
|
14
|
-
--
|
|
15
|
+
--labels "A,B" --values "3,2" data (repeated flags = repeated params)
|
|
16
|
+
--theme <family> --scheme <auto|dark|light> --width 560 --height 300 --num all
|
|
15
17
|
--title "Stars by language" --unit stars
|
|
18
|
+
(single-letter query names also pass through: --t evil --w 560)
|
|
16
19
|
or pass one bare query string:
|
|
17
|
-
|
|
20
|
+
draw "?type=pie&labels=A,B&values=3,2"
|
|
18
21
|
|
|
19
22
|
examples:
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
draw --type pie --labels "TypeScript,Python,Go" --values "4821,2312,980" --out pie.svg
|
|
24
|
+
draw "?type=hbar&labels=a,b,c&values=3,2,1&t=xcmd" | open -f -a Safari`;
|
|
22
25
|
function fail(msg) {
|
|
23
|
-
console.error(`
|
|
26
|
+
console.error(`draw: ${msg}\n\n${HELP}`);
|
|
24
27
|
process.exit(1);
|
|
25
28
|
}
|
|
26
29
|
const args = process.argv.slice(2);
|
|
@@ -39,7 +42,7 @@ for (let i = 0; i < args.length; i++) {
|
|
|
39
42
|
continue;
|
|
40
43
|
}
|
|
41
44
|
if (a.startsWith('--')) {
|
|
42
|
-
const key = a.slice(2);
|
|
45
|
+
const key = ALIASES[a.slice(2)] ?? a.slice(2);
|
|
43
46
|
const next = args[i + 1];
|
|
44
47
|
if (next === undefined || next.startsWith('--')) {
|
|
45
48
|
sp.set(key, '1'); // 无值 flag → 布尔(--stack → stack=1)
|
|
@@ -69,7 +72,7 @@ try {
|
|
|
69
72
|
}
|
|
70
73
|
catch (e) {
|
|
71
74
|
if (e instanceof ApiError) {
|
|
72
|
-
console.error(`
|
|
75
|
+
console.error(`draw: ${e.message}`);
|
|
73
76
|
process.exit(1);
|
|
74
77
|
}
|
|
75
78
|
throw e;
|
package/dist/render/area.d.ts
CHANGED
|
@@ -2,6 +2,9 @@ import { type ChartSpec, type MultiSpec } from '../specs/index.js';
|
|
|
2
2
|
import { type RenderOpts } from './svg.js';
|
|
3
3
|
export declare function renderArea(spec: ChartSpec, o: RenderOpts): string;
|
|
4
4
|
export declare function renderAreaMulti(spec: MultiSpec, o: RenderOpts): string;
|
|
5
|
+
/**
|
|
6
|
+
* Area chart — single or multi-series, dispatched by the input tag.
|
|
7
|
+
*/
|
|
5
8
|
export declare function renderAreaByMode(input: {
|
|
6
9
|
multi: boolean;
|
|
7
10
|
spec: ChartSpec | MultiSpec;
|
package/dist/render/area.js
CHANGED
|
@@ -397,6 +397,9 @@ function renderPolkaArea(spec, o) {
|
|
|
397
397
|
return svgDoc(width, height, 'area', version, spec.title ?? 'star history', spec, skin, parts.join(''));
|
|
398
398
|
}
|
|
399
399
|
// 面积模式分发:auto=常规;pixel=单系列像素;polka=阶梯波点(单/多系列)
|
|
400
|
+
/**
|
|
401
|
+
* Area chart — single or multi-series, dispatched by the input tag.
|
|
402
|
+
*/
|
|
400
403
|
export function renderAreaByMode(input, o) {
|
|
401
404
|
const mode = o.skin.rules.areaMode;
|
|
402
405
|
if (mode === 'pixel') {
|
package/dist/render/bar-grid.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { card } from '../theme.js';
|
|
2
2
|
import { esc, fmtFull, r2, trunc, svgDoc, sVar } from './svg.js';
|
|
3
3
|
const MONO = "ui-monospace,SFMono-Regular,Menlo,Consolas,monospace";
|
|
4
|
+
/**
|
|
5
|
+
* Table-style bar chart (`t=grid`): rows of label + inline bar + value.
|
|
6
|
+
*/
|
|
4
7
|
export function renderBarGrid(spec, o) {
|
|
5
8
|
const { width, height, version, skin } = o;
|
|
6
9
|
const plain = skin.rules.canvas === 'plain';
|
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
import type { MultiSpec } from '../specs/index.js';
|
|
2
2
|
import { type RenderOpts } from './svg.js';
|
|
3
|
+
/**
|
|
4
|
+
* Stacked multi-series bars (auto-switches to 100% stacking on extreme magnitudes).
|
|
5
|
+
*/
|
|
3
6
|
export declare function renderBarStacked(spec: MultiSpec, o: RenderOpts): string;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { TAB, card } from '../theme.js';
|
|
2
2
|
import { esc, fmtNum, niceTicks, r2, textW, topLegend, trunc, svgDoc, titleBlock, yAxisGrid, findPeak, onVar, sVar, pushBarLabelsAllOrNothing, labelStride, LabelPlacer, reserveBands } from './svg.js';
|
|
3
3
|
import { yTickInset, tickColW } from './measure.js';
|
|
4
|
+
/**
|
|
5
|
+
* Stacked multi-series bars (auto-switches to 100% stacking on extreme magnitudes).
|
|
6
|
+
*/
|
|
4
7
|
export function renderBarStacked(spec, o) {
|
|
5
8
|
let width = o.width;
|
|
6
9
|
let height = o.height;
|
package/dist/render/bar.d.ts
CHANGED
package/dist/render/bar.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { card } from '../theme.js';
|
|
2
2
|
import { esc, fmtNum, niceTicks, r2, trunc, svgDoc, titleBlock, yAxisGrid, pushBarLabelsAllOrNothing, labelStride, textW, LabelPlacer, reserveBands } from './svg.js';
|
|
3
3
|
import { yTickInset } from './measure.js';
|
|
4
|
+
/**
|
|
5
|
+
* Single-series bar chart.
|
|
6
|
+
*/
|
|
4
7
|
export function renderBar(spec, o) {
|
|
5
8
|
const { height, version, skin } = o;
|
|
6
9
|
let width = o.width;
|
package/dist/render/combo.d.ts
CHANGED
package/dist/render/combo.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { TAB, card } from '../theme.js';
|
|
2
2
|
import { esc, fmtNum, niceTicks, r2, topLegend, trunc, textW, LabelPlacer, pushLineLabel, pushBarLabelsAllOrNothing, smoothPath, svgDoc, titleBlock, findPeak, onVar, sVar, labelStride, reserveBands, reserveEdges, toSegments } from './svg.js';
|
|
3
3
|
import { yTickInset, tickColW } from './measure.js';
|
|
4
|
+
/**
|
|
5
|
+
* Bars + line(s) in one chart; dual y-axes on >5× magnitude spread.
|
|
6
|
+
*/
|
|
4
7
|
export function renderCombo(spec, o) {
|
|
5
8
|
if (o.skin.rules.comboMode === 'evil')
|
|
6
9
|
return renderComboEvil(spec, o);
|
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
import type { RenderOpts } from './svg.js';
|
|
2
2
|
import type { StoredChart } from '../store.js';
|
|
3
|
+
/**
|
|
4
|
+
* Render a stored chart document under a fully resolved skin — the short-link render path.
|
|
5
|
+
*/
|
|
3
6
|
export declare function renderStored(doc: StoredChart, o: RenderOpts): string;
|
package/dist/render/dispatch.js
CHANGED
|
@@ -44,6 +44,9 @@ function dispatch(template, input, o, stack = false) {
|
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
// 短链存档 → SVG(结构校验后分发)
|
|
47
|
+
/**
|
|
48
|
+
* Render a stored chart document under a fully resolved skin — the short-link render path.
|
|
49
|
+
*/
|
|
47
50
|
export function renderStored(doc, o) {
|
|
48
51
|
if (doc.template === 'qr') {
|
|
49
52
|
// 存档可能早于校验规则改动:渲染前按 create 同一规则复验(文本长度/颜色必为 hex)
|
package/dist/render/gauge.d.ts
CHANGED
package/dist/render/gauge.js
CHANGED
|
@@ -28,6 +28,9 @@ import { TAB, card } from '../theme.js';
|
|
|
28
28
|
import { esc, fmtFull, fmtNum, r2, textW, trunc, svgDoc, titleBlock, sVar } from './svg.js';
|
|
29
29
|
import { segPath, pol } from './pie.js';
|
|
30
30
|
import { C_gaugeGradient } from '../styles/common.js';
|
|
31
|
+
/**
|
|
32
|
+
* 240° gauge with a center value (single) or concentric rings (multi).
|
|
33
|
+
*/
|
|
31
34
|
export function renderGauge(spec, o) {
|
|
32
35
|
const { width, height, version, skin } = o;
|
|
33
36
|
if (skin.rules.gauge === 'zones')
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { ChartSpec, MultiSpec, ChartInput } from '../specs/index.js';
|
|
2
2
|
import { type RenderOpts } from './svg.js';
|
|
3
|
+
/**
|
|
4
|
+
* Horizontal bars, dispatched by skin family (base / opendata / xcmd / butterfly / rank).
|
|
5
|
+
*/
|
|
3
6
|
export declare function renderHbarByMode(input: ChartInput, o: RenderOpts): string;
|
|
4
7
|
export declare function renderHbarOpendata(spec: ChartSpec, o: RenderOpts): string;
|
|
5
8
|
export declare function renderHbarCells(spec: ChartSpec, o: RenderOpts): string;
|
package/dist/render/hbar-spec.js
CHANGED
|
@@ -5,6 +5,9 @@ import { TAB, card } from '../theme.js';
|
|
|
5
5
|
import { esc, fmtFull, fmtNum, niceTicks, r2, sVar, textW, trunc, svgDoc } from './svg.js';
|
|
6
6
|
import { fitCut } from './measure.js';
|
|
7
7
|
import { renderHBar } from './hbar.js';
|
|
8
|
+
/**
|
|
9
|
+
* Horizontal bars, dispatched by skin family (base / opendata / xcmd / butterfly / rank).
|
|
10
|
+
*/
|
|
8
11
|
export function renderHbarByMode(input, o) {
|
|
9
12
|
const mode = o.skin.rules.hbarMode;
|
|
10
13
|
if (mode === 'diverging' || mode === 'lift') {
|
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
import type { MultiSpec } from '../specs/index.js';
|
|
2
2
|
import { type RenderOpts } from './svg.js';
|
|
3
|
+
/**
|
|
4
|
+
* Multi-series lines; only each line peak is value-labeled to avoid clutter.
|
|
5
|
+
*/
|
|
3
6
|
export declare function renderLineMulti(spec: MultiSpec, o: RenderOpts): string;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { card } from '../theme.js';
|
|
2
2
|
import { esc, fmtNum, niceTicks, r2, smoothPath, textW, topLegend, trunc, LabelPlacer, pushLineLabelsAllOrNothing, pushPoints, labelStride, reserveBands, toSegments, svgDoc, titleBlock, editorialTitle, yAxisGrid, axisTitleY, findPeak, sVar } from './svg.js';
|
|
3
3
|
import { lineLayout } from './line.js';
|
|
4
|
+
/**
|
|
5
|
+
* Multi-series lines; only each line peak is value-labeled to avoid clutter.
|
|
6
|
+
*/
|
|
4
7
|
export function renderLineMulti(spec, o) {
|
|
5
8
|
const { width, height, version, skin } = o;
|
|
6
9
|
const allSegs = [];
|
package/dist/render/line.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { ChartSpec, MultiSpec } from '../specs/index.js';
|
|
2
2
|
import { type RenderOpts } from './svg.js';
|
|
3
|
+
/**
|
|
4
|
+
* Single smooth line with gradient area fill.
|
|
5
|
+
*/
|
|
3
6
|
export declare function renderLine(spec: ChartSpec, o: RenderOpts): string;
|
|
4
7
|
export declare function lineLayout(spec: MultiSpec, o: RenderOpts): {
|
|
5
8
|
left: number;
|
package/dist/render/line.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { card } from '../theme.js';
|
|
2
2
|
import { esc, fmtNum, niceTicks, r2, trunc, LabelPlacer, pushLineLabelsAllOrNothing, pushPoints, labelStride, toSegments, reserveBands, smoothPath, svgDoc, titleBlock, editorialTitle, yAxisGrid, axisTitleY } from './svg.js';
|
|
3
3
|
import { plainGeo } from './measure.js';
|
|
4
|
+
/**
|
|
5
|
+
* Single smooth line with gradient area fill.
|
|
6
|
+
*/
|
|
4
7
|
export function renderLine(spec, o) {
|
|
5
8
|
const { width, height, version, skin } = o;
|
|
6
9
|
const rules = skin.rules;
|
package/dist/render/pie.d.ts
CHANGED
|
@@ -2,4 +2,7 @@ import type { ChartSpec } from '../specs/index.js';
|
|
|
2
2
|
import { type RenderOpts } from './svg.js';
|
|
3
3
|
export declare function pol(cx: number, cy: number, rad: number, a: number): [number, number];
|
|
4
4
|
export declare function segPath(cx: number, cy: number, R: number, r: number, a0: number, a1: number, corner: number): string;
|
|
5
|
+
/**
|
|
6
|
+
* Donut chart (solid pie with `o.donut = false`); label placement follows the skin.
|
|
7
|
+
*/
|
|
5
8
|
export declare function renderPie(spec: ChartSpec, o: RenderOpts): string;
|
package/dist/render/pie.js
CHANGED
|
@@ -80,6 +80,9 @@ export function segPath(cx, cy, R, r, a0, a1, corner) {
|
|
|
80
80
|
`A${r2(r)} ${r2(r)} 0 ${large} 1 ${PT(isI)}${filletArc(isI, isC, isR, c)}Z`);
|
|
81
81
|
}
|
|
82
82
|
const HALO_ATTRS = 'paint-order="stroke" stroke="var(--d-halo)" stroke-width="3" stroke-linejoin="round"';
|
|
83
|
+
/**
|
|
84
|
+
* Donut chart (solid pie with `o.donut = false`); label placement follows the skin.
|
|
85
|
+
*/
|
|
83
86
|
export function renderPie(spec, o) {
|
|
84
87
|
const { width, height, version, skin } = o;
|
|
85
88
|
const rules = skin.rules;
|
package/dist/render/qr.d.ts
CHANGED
|
@@ -4,5 +4,14 @@ export declare const QR_DEFAULTS: {
|
|
|
4
4
|
readonly fg: "var(--d-s1)";
|
|
5
5
|
readonly bg: "var(--d-card)";
|
|
6
6
|
};
|
|
7
|
+
/**
|
|
8
|
+
* Render a QR code with optional caption and subtitle.
|
|
9
|
+
*
|
|
10
|
+
* @param text payload, at most 1000 characters
|
|
11
|
+
* @param fg module color, 6-digit hex
|
|
12
|
+
* @param bg background color, 6-digit hex
|
|
13
|
+
* @param caption short title line under the code
|
|
14
|
+
* @param subtitle smaller unit/description line
|
|
15
|
+
*/
|
|
7
16
|
export declare function renderQR(text: string, fg: string, bg: string, width: number, height: number, version: number, skin: Skin, caption?: string, subtitle?: string): string;
|
|
8
17
|
export declare function requireQRText(raw: string | null): string;
|
package/dist/render/qr.js
CHANGED
|
@@ -14,6 +14,15 @@ export function parseColor(raw, fallback, name) {
|
|
|
14
14
|
// 默认配色为 var 引用,随皮肤明暗自动切换;显式 fg/bg 参数则烘焙固定色
|
|
15
15
|
export const QR_DEFAULTS = { fg: 'var(--d-s1)', bg: 'var(--d-card)' };
|
|
16
16
|
const MAX_QR_TEXT = 1000;
|
|
17
|
+
/**
|
|
18
|
+
* Render a QR code with optional caption and subtitle.
|
|
19
|
+
*
|
|
20
|
+
* @param text payload, at most 1000 characters
|
|
21
|
+
* @param fg module color, 6-digit hex
|
|
22
|
+
* @param bg background color, 6-digit hex
|
|
23
|
+
* @param caption short title line under the code
|
|
24
|
+
* @param subtitle smaller unit/description line
|
|
25
|
+
*/
|
|
17
26
|
export function renderQR(text, fg, bg, width, height, version, skin, caption, subtitle) {
|
|
18
27
|
const qr = encode(text, { ecc: 'M', border: 0 });
|
|
19
28
|
const n = qr.size;
|
package/dist/render/query.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { type Scheme } from '../styles/index.js';
|
|
2
2
|
import { type StoredChart } from '../store.js';
|
|
3
|
+
/**
|
|
4
|
+
* A render result: the SVG plus the version and Cache-Control it should carry.
|
|
5
|
+
*/
|
|
3
6
|
export interface DrawResult {
|
|
4
7
|
svg: string;
|
|
5
8
|
version: number;
|
|
@@ -10,5 +13,14 @@ export declare function clampInt(params: URLSearchParams, key: string, def: numb
|
|
|
10
13
|
export declare function stackParam(params: URLSearchParams): boolean;
|
|
11
14
|
export declare function schemeParam(params: URLSearchParams): Scheme;
|
|
12
15
|
export declare function boolParam(params: URLSearchParams, key: string): boolean | undefined;
|
|
16
|
+
/**
|
|
17
|
+
* Render a chart synchronously from a URL's query params — exactly the
|
|
18
|
+
* GET /draw semantics (`type` required; all params documented in the README).
|
|
19
|
+
*
|
|
20
|
+
* @throws ApiError(400) with a corrective example on bad params
|
|
21
|
+
*/
|
|
13
22
|
export declare function renderFromUrl(url: URL): DrawResult;
|
|
23
|
+
/**
|
|
24
|
+
* Render a stored (short-linked) chart, applying `w`/`h`/`num`/`scheme` overrides from the URL.
|
|
25
|
+
*/
|
|
14
26
|
export declare function renderStoredFromUrl(doc: StoredChart, url: URL): DrawResult;
|
package/dist/render/query.js
CHANGED
|
@@ -122,6 +122,12 @@ function qrRender(request) {
|
|
|
122
122
|
return svgResult(renderQR(text, fg, bg, width, height, v.version, skin, caption, subtitle), v);
|
|
123
123
|
}
|
|
124
124
|
// GET ?type=pie|bar|hbar|line|radar|gauge|scatter|combo|sankey|qr&...
|
|
125
|
+
/**
|
|
126
|
+
* Render a chart synchronously from a URL's query params — exactly the
|
|
127
|
+
* GET /draw semantics (`type` required; all params documented in the README).
|
|
128
|
+
*
|
|
129
|
+
* @throws ApiError(400) with a corrective example on bad params
|
|
130
|
+
*/
|
|
125
131
|
export function renderFromUrl(url) {
|
|
126
132
|
const typeParam = url.searchParams.get('type');
|
|
127
133
|
if (!typeParam || !CHART_TEMPLATES.includes(typeParam)) {
|
|
@@ -201,6 +207,9 @@ export function renderFromUrl(url) {
|
|
|
201
207
|
return chartRender(url, 'line', (i, o) => (i.multi ? renderLineMulti(i.spec, o) : renderLine(i.spec, o)), SIZE.line[0], SIZE.line[1]);
|
|
202
208
|
}
|
|
203
209
|
// 短链渲染段:?w/?h/?num/?scheme 应用到存档 doc(immutable,版本恒为当前)
|
|
210
|
+
/**
|
|
211
|
+
* Render a stored (short-linked) chart, applying `w`/`h`/`num`/`scheme` overrides from the URL.
|
|
212
|
+
*/
|
|
204
213
|
export function renderStoredFromUrl(doc, url) {
|
|
205
214
|
const skin = resolveSkin(doc.template, doc.theme, schemeParam(url.searchParams));
|
|
206
215
|
const [dw, dh] = skin.size ?? SIZE[doc.template];
|
package/dist/render/radar.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import type { ChartSpec, ChartInput } from '../specs/index.js';
|
|
2
2
|
import { type RenderOpts } from './svg.js';
|
|
3
|
+
/**
|
|
4
|
+
* Radar chart — single or multi-series, dispatched by the input tag.
|
|
5
|
+
*/
|
|
3
6
|
export declare function renderRadarByMode(input: ChartInput, o: RenderOpts): string;
|
|
4
7
|
export declare function assertDims(n: number): void;
|
|
5
8
|
export declare function renderRadar(spec: ChartSpec, o: RenderOpts): string;
|
package/dist/render/radar.js
CHANGED
|
@@ -3,6 +3,9 @@ import { card, TAB } from '../theme.js';
|
|
|
3
3
|
import { esc, fmtNum, r2, textW, trunc, sVar, svgDoc, titleBlock } from './svg.js';
|
|
4
4
|
import { radialFit } from './measure.js';
|
|
5
5
|
const HAND_FONT = "Handlee,'Comic Sans MS','Chalkboard SE','Comic Neue','Segoe Print',cursive";
|
|
6
|
+
/**
|
|
7
|
+
* Radar chart — single or multi-series, dispatched by the input tag.
|
|
8
|
+
*/
|
|
6
9
|
export function renderRadarByMode(input, o) {
|
|
7
10
|
const mode = o.skin.rules.radarMode;
|
|
8
11
|
if (mode === 'evil') {
|
package/dist/render/sankey.d.ts
CHANGED
package/dist/render/sankey.js
CHANGED
package/dist/render/scatter.d.ts
CHANGED
package/dist/render/scatter.js
CHANGED
|
@@ -11,6 +11,9 @@ function spanTicks(min, max, count) {
|
|
|
11
11
|
out.push(snap(v, step));
|
|
12
12
|
return out;
|
|
13
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Scatter plot with two numeric axes; negative values allowed.
|
|
16
|
+
*/
|
|
14
17
|
export function renderScatter(spec, o) {
|
|
15
18
|
const { width, height, version, skin } = o;
|
|
16
19
|
const series = spec.series?.length
|
package/dist/render/svg.d.ts
CHANGED
|
@@ -25,6 +25,9 @@ export declare function axisTitleY(unit: string | undefined, midY: number, x?: n
|
|
|
25
25
|
export declare function findPeak(series: readonly {
|
|
26
26
|
values: readonly number[];
|
|
27
27
|
}[]): [number, number];
|
|
28
|
+
/**
|
|
29
|
+
* Options shared by every renderer: canvas, version, skin, and feature flags.
|
|
30
|
+
*/
|
|
28
31
|
export interface RenderOpts {
|
|
29
32
|
width: number;
|
|
30
33
|
height: number;
|
package/dist/specs/base.d.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* API error with an HTTP status. Parsers and renderers throw these;
|
|
3
|
+
* the HTTP layer maps them to `status` + a plain-text body.
|
|
4
|
+
*/
|
|
1
5
|
export declare class ApiError extends Error {
|
|
2
6
|
status: number;
|
|
3
7
|
constructor(status: number, message: string);
|
|
4
8
|
}
|
|
9
|
+
/**
|
|
10
|
+
* One labeled data point: a category and its value.
|
|
11
|
+
*/
|
|
5
12
|
export interface Datum {
|
|
6
13
|
label: string;
|
|
7
14
|
value: number;
|
|
8
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Single-series chart data (pie / bar / hbar / line / area / radar).
|
|
18
|
+
*/
|
|
9
19
|
export interface ChartSpec {
|
|
10
20
|
title?: string;
|
|
11
21
|
subtitle?: string;
|
|
@@ -13,6 +23,10 @@ export interface ChartSpec {
|
|
|
13
23
|
unit?: string;
|
|
14
24
|
data: Datum[];
|
|
15
25
|
}
|
|
26
|
+
/**
|
|
27
|
+
* Global input limits shared by all parsers: text caps and max
|
|
28
|
+
* categories / points / axes / series.
|
|
29
|
+
*/
|
|
16
30
|
export declare const LIMITS: {
|
|
17
31
|
readonly title: 80;
|
|
18
32
|
readonly subtitle: 96;
|
|
@@ -27,8 +41,30 @@ export declare const LIMITS: {
|
|
|
27
41
|
readonly seriesName: 24;
|
|
28
42
|
readonly maxScatter: 200;
|
|
29
43
|
};
|
|
44
|
+
/**
|
|
45
|
+
* Cap on the main data axis per chart kind (categories, points or axes).
|
|
46
|
+
*/
|
|
30
47
|
export declare const MAX_BY_KIND: Record<string, number>;
|
|
48
|
+
/**
|
|
49
|
+
* Parse and validate a single-series spec from a JSON string (the `?d=` payload).
|
|
50
|
+
*
|
|
51
|
+
* @param raw JSON string; null or empty triggers the corrective error
|
|
52
|
+
* @param kind chart kind — selects the applicable limits
|
|
53
|
+
* @returns the validated ChartSpec
|
|
54
|
+
* @throws ApiError(400) with a corrective example on bad input
|
|
55
|
+
*/
|
|
31
56
|
export declare function parseChartSpec(raw: string | null, kind: 'pie' | 'bar' | 'hbar' | 'line' | 'area' | 'radar'): ChartSpec;
|
|
57
|
+
/**
|
|
58
|
+
* Build single-series spec JSON from flat params (`labels=A,B&values=1,2`;
|
|
59
|
+
* repeated params support comma-containing labels).
|
|
60
|
+
* Returns null when no data params are present.
|
|
61
|
+
*/
|
|
32
62
|
export declare function specFromParams(params: URLSearchParams, kind: 'pie' | 'bar' | 'hbar' | 'line' | 'area' | 'radar'): string | null;
|
|
63
|
+
/**
|
|
64
|
+
* All values of a repeated query param: `labels=a&labels=b` → `['a','b']`.
|
|
65
|
+
*/
|
|
33
66
|
export declare function flatList(params: URLSearchParams, key: string): string[];
|
|
67
|
+
/**
|
|
68
|
+
* Truncate a string to at most `n` characters (input hardening for titles/labels).
|
|
69
|
+
*/
|
|
34
70
|
export declare function clip(s: string, n: number): string;
|
package/dist/specs/base.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
// 公共解析基础:ApiError、LIMITS、单系列 ChartSpec(扁平参数/JSON)、统一入口 parseInput。
|
|
2
2
|
// 各图表专属解析器在本目录独立文件;新增图表 = 新增 specs/xxx.ts + render/xxx.ts。
|
|
3
|
+
/**
|
|
4
|
+
* API error with an HTTP status. Parsers and renderers throw these;
|
|
5
|
+
* the HTTP layer maps them to `status` + a plain-text body.
|
|
6
|
+
*/
|
|
3
7
|
export class ApiError extends Error {
|
|
4
8
|
status;
|
|
5
9
|
constructor(status, message) {
|
|
@@ -7,6 +11,10 @@ export class ApiError extends Error {
|
|
|
7
11
|
this.status = status;
|
|
8
12
|
}
|
|
9
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Global input limits shared by all parsers: text caps and max
|
|
16
|
+
* categories / points / axes / series.
|
|
17
|
+
*/
|
|
10
18
|
export const LIMITS = {
|
|
11
19
|
title: 80,
|
|
12
20
|
subtitle: 96,
|
|
@@ -21,6 +29,9 @@ export const LIMITS = {
|
|
|
21
29
|
seriesName: 24,
|
|
22
30
|
maxScatter: 200,
|
|
23
31
|
};
|
|
32
|
+
/**
|
|
33
|
+
* Cap on the main data axis per chart kind (categories, points or axes).
|
|
34
|
+
*/
|
|
24
35
|
export const MAX_BY_KIND = {
|
|
25
36
|
pie: LIMITS.maxPie,
|
|
26
37
|
bar: LIMITS.maxBar,
|
|
@@ -31,6 +42,14 @@ export const MAX_BY_KIND = {
|
|
|
31
42
|
};
|
|
32
43
|
const EXAMPLE = encodeURIComponent(JSON.stringify({ title: 'Demo', data: [{ label: 'A', value: 1 }, { label: 'B', value: 2 }] }));
|
|
33
44
|
const EXAMPLE_FLAT = 'title=Demo&labels=A,B&values=1,2';
|
|
45
|
+
/**
|
|
46
|
+
* Parse and validate a single-series spec from a JSON string (the `?d=` payload).
|
|
47
|
+
*
|
|
48
|
+
* @param raw JSON string; null or empty triggers the corrective error
|
|
49
|
+
* @param kind chart kind — selects the applicable limits
|
|
50
|
+
* @returns the validated ChartSpec
|
|
51
|
+
* @throws ApiError(400) with a corrective example on bad input
|
|
52
|
+
*/
|
|
34
53
|
export function parseChartSpec(raw, kind) {
|
|
35
54
|
if (raw === null || raw === '') {
|
|
36
55
|
throw new ApiError(400, `Missing data. Use flat params: ?${EXAMPLE_FLAT} — or JSON: ?d=${EXAMPLE}`);
|
|
@@ -105,6 +124,11 @@ export function parseChartSpec(raw, kind) {
|
|
|
105
124
|
}
|
|
106
125
|
// 扁平参数 → JSON 字符串:让 URL 永远可读,无需百分号编码。
|
|
107
126
|
// 支持两种形态:?labels=A,B&values=1,2(逗号)或 ?labels=A&labels=B&values=1&values=2(重复参数,标签含逗号时用)
|
|
127
|
+
/**
|
|
128
|
+
* Build single-series spec JSON from flat params (`labels=A,B&values=1,2`;
|
|
129
|
+
* repeated params support comma-containing labels).
|
|
130
|
+
* Returns null when no data params are present.
|
|
131
|
+
*/
|
|
108
132
|
export function specFromParams(params, kind) {
|
|
109
133
|
const d = params.get('d');
|
|
110
134
|
if (d !== null && d !== '')
|
|
@@ -141,6 +165,9 @@ export function specFromParams(params, kind) {
|
|
|
141
165
|
obj.unit = unit;
|
|
142
166
|
return JSON.stringify(obj);
|
|
143
167
|
}
|
|
168
|
+
/**
|
|
169
|
+
* All values of a repeated query param: `labels=a&labels=b` → `['a','b']`.
|
|
170
|
+
*/
|
|
144
171
|
export function flatList(params, key) {
|
|
145
172
|
const all = params.getAll(key);
|
|
146
173
|
if (all.length > 1)
|
|
@@ -154,6 +181,9 @@ function jsonType(v) {
|
|
|
154
181
|
return 'an array';
|
|
155
182
|
return typeof v;
|
|
156
183
|
}
|
|
184
|
+
/**
|
|
185
|
+
* Truncate a string to at most `n` characters (input hardening for titles/labels).
|
|
186
|
+
*/
|
|
157
187
|
export function clip(s, n) {
|
|
158
188
|
return s.length > n ? s.slice(0, n - 1) + '…' : s;
|
|
159
189
|
}
|