@x-cmd/draw 0.2.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.
Files changed (61) hide show
  1. package/dist/lib.d.ts +18 -0
  2. package/dist/lib.js +18 -0
  3. package/dist/render/area.d.ts +3 -0
  4. package/dist/render/area.js +3 -0
  5. package/dist/render/bar-grid.d.ts +3 -0
  6. package/dist/render/bar-grid.js +3 -0
  7. package/dist/render/bar-grouped.d.ts +3 -0
  8. package/dist/render/bar-grouped.js +3 -0
  9. package/dist/render/bar-stacked.d.ts +3 -0
  10. package/dist/render/bar-stacked.js +3 -0
  11. package/dist/render/bar.d.ts +3 -0
  12. package/dist/render/bar.js +3 -0
  13. package/dist/render/combo.d.ts +3 -0
  14. package/dist/render/combo.js +3 -0
  15. package/dist/render/dispatch.d.ts +3 -0
  16. package/dist/render/dispatch.js +3 -0
  17. package/dist/render/gauge.d.ts +3 -0
  18. package/dist/render/gauge.js +3 -0
  19. package/dist/render/hbar-spec.d.ts +3 -0
  20. package/dist/render/hbar-spec.js +3 -0
  21. package/dist/render/line-multi.d.ts +3 -0
  22. package/dist/render/line-multi.js +3 -0
  23. package/dist/render/line.d.ts +3 -0
  24. package/dist/render/line.js +3 -0
  25. package/dist/render/pie.d.ts +3 -0
  26. package/dist/render/pie.js +3 -0
  27. package/dist/render/qr.d.ts +9 -0
  28. package/dist/render/qr.js +9 -0
  29. package/dist/render/query.d.ts +12 -0
  30. package/dist/render/query.js +9 -0
  31. package/dist/render/radar.d.ts +3 -0
  32. package/dist/render/radar.js +3 -0
  33. package/dist/render/sankey.d.ts +3 -0
  34. package/dist/render/sankey.js +3 -0
  35. package/dist/render/scatter.d.ts +3 -0
  36. package/dist/render/scatter.js +3 -0
  37. package/dist/render/svg.d.ts +3 -0
  38. package/dist/specs/base.d.ts +36 -0
  39. package/dist/specs/base.js +30 -0
  40. package/dist/specs/combo.d.ts +15 -0
  41. package/dist/specs/combo.js +9 -0
  42. package/dist/specs/csv.d.ts +4 -0
  43. package/dist/specs/csv.js +4 -0
  44. package/dist/specs/gauge.d.ts +15 -0
  45. package/dist/specs/gauge.js +9 -0
  46. package/dist/specs/input.d.ts +12 -0
  47. package/dist/specs/input.js +9 -0
  48. package/dist/specs/multi.d.ts +18 -0
  49. package/dist/specs/multi.js +11 -0
  50. package/dist/specs/sankey.d.ts +15 -0
  51. package/dist/specs/sankey.js +9 -0
  52. package/dist/specs/scatter.d.ts +15 -0
  53. package/dist/specs/scatter.js +9 -0
  54. package/dist/store.d.ts +15 -0
  55. package/dist/store.js +9 -0
  56. package/dist/styles/index.d.ts +10 -0
  57. package/dist/styles/index.js +10 -0
  58. package/dist/styles/types.d.ts +3 -0
  59. package/dist/version.d.ts +11 -0
  60. package/dist/version.js +8 -0
  61. package/package.json +1 -1
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)
@@ -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;
@@ -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') {
@@ -1,3 +1,6 @@
1
1
  import type { ChartSpec } from '../specs/index.js';
2
2
  import { type RenderOpts } from './svg.js';
3
+ /**
4
+ * Table-style bar chart (`t=grid`): rows of label + inline bar + value.
5
+ */
3
6
  export declare function renderBarGrid(spec: ChartSpec, o: RenderOpts): string;
@@ -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
+ * Grouped (side-by-side) multi-series bars.
5
+ */
3
6
  export declare function renderBarMulti(spec: MultiSpec, o: RenderOpts): string;
@@ -13,6 +13,9 @@ function barGeo(w, h, hasTitle, m = {}) {
13
13
  legendY: Math.round(h * 0.0923),
14
14
  };
15
15
  }
16
+ /**
17
+ * Grouped (side-by-side) multi-series bars.
18
+ */
16
19
  export function renderBarMulti(spec, o) {
17
20
  const height = o.height;
18
21
  const { version, skin } = o;
@@ -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;
@@ -1,3 +1,6 @@
1
1
  import type { ChartSpec } from '../specs/index.js';
2
2
  import { type RenderOpts } from './svg.js';
3
+ /**
4
+ * Single-series bar chart.
5
+ */
3
6
  export declare function renderBar(spec: ChartSpec, o: RenderOpts): string;
@@ -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;
@@ -1,3 +1,6 @@
1
1
  import type { ComboSpec } from '../specs/index.js';
2
2
  import { type RenderOpts } from './svg.js';
3
+ /**
4
+ * Bars + line(s) in one chart; dual y-axes on >5× magnitude spread.
5
+ */
3
6
  export declare function renderCombo(spec: ComboSpec, o: RenderOpts): string;
@@ -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;
@@ -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)
@@ -1,3 +1,6 @@
1
1
  import { type GaugeSpec } from '../specs/index.js';
2
2
  import { type RenderOpts } from './svg.js';
3
+ /**
4
+ * 240° gauge with a center value (single) or concentric rings (multi).
5
+ */
3
6
  export declare function renderGauge(spec: GaugeSpec, o: RenderOpts): string;
@@ -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;
@@ -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 = [];
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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;
@@ -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];
@@ -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;
@@ -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') {
@@ -1,3 +1,6 @@
1
1
  import type { SankeySpec } from '../specs/index.js';
2
2
  import { type RenderOpts } from './svg.js';
3
+ /**
4
+ * Sankey flow diagram with weighted bands.
5
+ */
3
6
  export declare function renderSankey(spec: SankeySpec, o: RenderOpts): string;
@@ -135,6 +135,9 @@ function labelStyles(nodes, cols) {
135
135
  }
136
136
  return m;
137
137
  }
138
+ /**
139
+ * Sankey flow diagram with weighted bands.
140
+ */
138
141
  export function renderSankey(spec, o) {
139
142
  const mode = o.skin.rules.sankeyMode;
140
143
  if (mode === 'fund')
@@ -1,3 +1,6 @@
1
1
  import type { ScatterSpec } from '../specs/index.js';
2
2
  import { type RenderOpts } from './svg.js';
3
+ /**
4
+ * Scatter plot with two numeric axes; negative values allowed.
5
+ */
3
6
  export declare function renderScatter(spec: ScatterSpec, o: RenderOpts): string;
@@ -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
@@ -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;
@@ -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;
@@ -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
  }
@@ -1,8 +1,14 @@
1
+ /**
2
+ * One combo series: bars by default, or the line overlay with `type: 'line'`.
3
+ */
1
4
  export interface ComboSeries {
2
5
  name: string;
3
6
  values: number[];
4
7
  kind: 'bar' | 'line';
5
8
  }
9
+ /**
10
+ * Combo data: bar series + line series on shared labels.
11
+ */
6
12
  export interface ComboSpec {
7
13
  title?: string;
8
14
  unit?: string;
@@ -11,5 +17,14 @@ export interface ComboSpec {
11
17
  /** 柱系列堆叠(而非并排),折线仍叠加在上 */
12
18
  stack?: boolean;
13
19
  }
20
+ /**
21
+ * Parse and validate combo JSON.
22
+ *
23
+ * @throws ApiError(400) with a corrective example on bad input
24
+ */
14
25
  export declare function parseComboSpec(raw: string | null): ComboSpec;
26
+ /**
27
+ * Build combo spec JSON from `?series=` (bars) and `?line=` (lines).
28
+ * Returns null when neither is present.
29
+ */
15
30
  export declare function comboSpecFromParams(params: URLSearchParams): string | null;
@@ -28,6 +28,11 @@ function parseSeriesEntries(rawEntries, labels, kind) {
28
28
  return { name: clip(s.name, LIMITS.seriesName), values, kind: entryKind };
29
29
  });
30
30
  }
31
+ /**
32
+ * Parse and validate combo JSON.
33
+ *
34
+ * @throws ApiError(400) with a corrective example on bad input
35
+ */
31
36
  export function parseComboSpec(raw) {
32
37
  if (raw === null || raw === '') {
33
38
  throw new ApiError(400, `Missing data. Use ?type=combo&${COMBO_EXAMPLE}`);
@@ -78,6 +83,10 @@ export function parseComboSpec(raw) {
78
83
  const stack = o.stack === true;
79
84
  return { title, unit, labels, series, ...(stack ? { stack: true } : {}) };
80
85
  }
86
+ /**
87
+ * Build combo spec JSON from `?series=` (bars) and `?line=` (lines).
88
+ * Returns null when neither is present.
89
+ */
81
90
  export function comboSpecFromParams(params) {
82
91
  const d = params.get('d');
83
92
  if (d !== null && d !== '')
@@ -1 +1,5 @@
1
+ /**
2
+ * Convert CSV (first row = header) into spec JSON: one numeric column
3
+ * → single-series spec; several → multi-series.
4
+ */
1
5
  export declare function csvToSpecJson(csv: string): string;
package/dist/specs/csv.js CHANGED
@@ -1,6 +1,10 @@
1
1
  // CSV → spec JSON:首行表头,两列=单系列,多列=多系列。
2
2
  import { ApiError } from './base.js';
3
3
  // CSV → spec JSON:首行为表头。两列(label,value)=单系列;多列(label,系列A,系列B)=多系列
4
+ /**
5
+ * Convert CSV (first row = header) into spec JSON: one numeric column
6
+ * → single-series spec; several → multi-series.
7
+ */
4
8
  export function csvToSpecJson(csv) {
5
9
  const rows = csv.replace(/\r/g, '').split('\n').filter((l) => l.trim() !== '');
6
10
  if (rows.length < 2)
@@ -1,7 +1,13 @@
1
+ /**
2
+ * One ring of a multi-ring gauge; `value` is a percentage.
3
+ */
1
4
  export interface GaugeDatum {
2
5
  label: string;
3
6
  value: number;
4
7
  }
8
+ /**
9
+ * Gauge data: single value (`{value,max}`) or multi-ring (`{max,data}`).
10
+ */
5
11
  export interface GaugeSpec {
6
12
  title?: string;
7
13
  unit?: string;
@@ -11,5 +17,14 @@ export interface GaugeSpec {
11
17
  value?: number;
12
18
  max: number;
13
19
  }
20
+ /**
21
+ * Build gauge spec JSON from `?value=&max=` (single) or `?labels=&values=` (multi-ring).
22
+ * Returns null when neither form is present.
23
+ */
14
24
  export declare function gaugeSpecFromParams(params: URLSearchParams): string | null;
25
+ /**
26
+ * Parse and validate gauge JSON.
27
+ *
28
+ * @throws ApiError(400) with a corrective example on bad input
29
+ */
15
30
  export declare function parseGaugeSpec(raw: string | null): GaugeSpec;
@@ -1,6 +1,10 @@
1
1
  // 仪表盘(gauge):多环(labels+values,每行一环,值为百分比)或单值(value/max)。
2
2
  import { ApiError, LIMITS, clip, flatList } from './base.js';
3
3
  const GAUGE_EXAMPLE = 'value=87&max=100';
4
+ /**
5
+ * Build gauge spec JSON from `?value=&max=` (single) or `?labels=&values=` (multi-ring).
6
+ * Returns null when neither form is present.
7
+ */
4
8
  export function gaugeSpecFromParams(params) {
5
9
  const d = params.get('d');
6
10
  if (d !== null && d !== '')
@@ -42,6 +46,11 @@ export function gaugeSpecFromParams(params) {
42
46
  obj.unit = unit;
43
47
  return JSON.stringify(obj);
44
48
  }
49
+ /**
50
+ * Parse and validate gauge JSON.
51
+ *
52
+ * @throws ApiError(400) with a corrective example on bad input
53
+ */
45
54
  export function parseGaugeSpec(raw) {
46
55
  if (raw === null || raw === '') {
47
56
  throw new ApiError(400, `Missing data. Use ?type=gauge&${GAUGE_EXAMPLE} — or ?d= with {"value":87,"max":100}`);
@@ -1,5 +1,8 @@
1
1
  import { type ChartSpec } from './base.js';
2
2
  import { type MultiSpec } from './multi.js';
3
+ /**
4
+ * A parsed chart input, tagged single- (`multi: false`) or multi-series.
5
+ */
3
6
  export type ChartInput = {
4
7
  multi: false;
5
8
  spec: ChartSpec;
@@ -7,5 +10,14 @@ export type ChartInput = {
7
10
  multi: true;
8
11
  spec: MultiSpec;
9
12
  };
13
+ /**
14
+ * Structural check: does this JSON payload carry a `series` array?
15
+ */
10
16
  export declare function looksMultiSeries(json: string): boolean;
17
+ /**
18
+ * Unified parser for the six basic kinds: flat params or `?d=` JSON
19
+ * → a tagged ChartInput.
20
+ *
21
+ * @throws ApiError(400) with a corrective example on bad input
22
+ */
11
23
  export declare function parseInput(params: URLSearchParams, kind: 'pie' | 'bar' | 'hbar' | 'line' | 'area' | 'radar'): ChartInput;
@@ -3,6 +3,9 @@ import { ApiError, parseChartSpec, specFromParams } from './base.js';
3
3
  import { parseSeriesSpec, seriesSpecFromParams } from './multi.js';
4
4
  // 结构判断(而非字符串嗅探):按解析后的对象是否含 "series" 键决定单/多系列,
5
5
  // 避免标题里恰好含 "series" 字样时误路由
6
+ /**
7
+ * Structural check: does this JSON payload carry a `series` array?
8
+ */
6
9
  export function looksMultiSeries(json) {
7
10
  try {
8
11
  const obj = JSON.parse(json);
@@ -12,6 +15,12 @@ export function looksMultiSeries(json) {
12
15
  return false; // 交给后续解析器给出具体报错
13
16
  }
14
17
  }
18
+ /**
19
+ * Unified parser for the six basic kinds: flat params or `?d=` JSON
20
+ * → a tagged ChartInput.
21
+ *
22
+ * @throws ApiError(400) with a corrective example on bad input
23
+ */
15
24
  export function parseInput(params, kind) {
16
25
  if (kind === 'pie') {
17
26
  if (params.has('series')) {
@@ -1,12 +1,30 @@
1
+ /**
2
+ * One named series of a multi-series chart.
3
+ */
1
4
  export interface SeriesEntry {
2
5
  name: string;
3
6
  values: number[];
4
7
  }
8
+ /**
9
+ * Multi-series chart data: shared labels + named series
10
+ * (grouped/stacked bars, multi-line, multi-area, multi-radar).
11
+ */
5
12
  export interface MultiSpec {
6
13
  title?: string;
7
14
  unit?: string;
8
15
  labels: string[];
9
16
  series: SeriesEntry[];
10
17
  }
18
+ /**
19
+ * Parse and validate a multi-series spec from a JSON string.
20
+ *
21
+ * @param kind `'bar'` or `'line'` — selects the applicable limits
22
+ * @throws ApiError(400) with a corrective example on bad input
23
+ */
11
24
  export declare function parseSeriesSpec(raw: string | null, kind: 'bar' | 'line'): MultiSpec;
25
+ /**
26
+ * Build multi-series spec JSON from flat params
27
+ * (`labels=Mon,Tue&series=a:1,2&series=b:3,4`).
28
+ * Returns null when no series params are present.
29
+ */
12
30
  export declare function seriesSpecFromParams(params: URLSearchParams): string | null;
@@ -1,6 +1,12 @@
1
1
  // 多系列(bar 分组/堆叠、line 多曲线):labels + series[{name, values}]。
2
2
  import { ApiError, LIMITS, MAX_BY_KIND, clip, flatList } from './base.js';
3
3
  const SERIES_EXAMPLE_FLAT = 'labels=Mon,Tue&series=draw:120,180&series=qc:80,90';
4
+ /**
5
+ * Parse and validate a multi-series spec from a JSON string.
6
+ *
7
+ * @param kind `'bar'` or `'line'` — selects the applicable limits
8
+ * @throws ApiError(400) with a corrective example on bad input
9
+ */
4
10
  export function parseSeriesSpec(raw, kind) {
5
11
  if (raw === null || raw === '') {
6
12
  throw new ApiError(400, `Missing data. Use ?${SERIES_EXAMPLE_FLAT} — or ?d= with {"labels":["A","B"],"series":[{"name":"s1","values":[1,2]}]}`);
@@ -76,6 +82,11 @@ export function parseSeriesSpec(raw, kind) {
76
82
  return { title, unit, labels, series };
77
83
  }
78
84
  // 扁平参数 → 多系列 JSON:series=名字:1,2,3 重复参数
85
+ /**
86
+ * Build multi-series spec JSON from flat params
87
+ * (`labels=Mon,Tue&series=a:1,2&series=b:3,4`).
88
+ * Returns null when no series params are present.
89
+ */
79
90
  export function seriesSpecFromParams(params) {
80
91
  const entries = params.getAll('series');
81
92
  if (entries.length === 0)
@@ -1,13 +1,28 @@
1
+ /**
2
+ * One flow: node `from` → node `to` with positive weight `value`.
3
+ */
1
4
  export interface SankeyLink {
2
5
  from: string;
3
6
  to: string;
4
7
  value: number;
5
8
  }
9
+ /**
10
+ * Sankey data: node labels + weighted links (must form a DAG).
11
+ */
6
12
  export interface SankeySpec {
7
13
  title?: string;
8
14
  unit?: string;
9
15
  labels: string[];
10
16
  links: SankeyLink[];
11
17
  }
18
+ /**
19
+ * Build sankey spec JSON from `?labels=` + `?links=A>B:3;C>B:2`.
20
+ * Returns null when absent.
21
+ */
12
22
  export declare function sankeySpecFromParams(params: URLSearchParams): string | null;
23
+ /**
24
+ * Parse and validate sankey JSON (includes the cycle check).
25
+ *
26
+ * @throws ApiError(400) with a corrective example on bad input
27
+ */
13
28
  export declare function parseSankeySpec(raw: string | null): SankeySpec;
@@ -3,6 +3,10 @@ import { ApiError, LIMITS, clip, flatList } from './base.js';
3
3
  const SANKEY_EXAMPLE = 'labels=Ads,Web,Signup,Buy&links=Ads>Signup:30,Web>Signup:20,Signup>Buy:40';
4
4
  const MAX_NODES = 24;
5
5
  const MAX_LINKS = 60;
6
+ /**
7
+ * Build sankey spec JSON from `?labels=` + `?links=A>B:3;C>B:2`.
8
+ * Returns null when absent.
9
+ */
6
10
  export function sankeySpecFromParams(params) {
7
11
  const d = params.get('d');
8
12
  if (d !== null && d !== '')
@@ -28,6 +32,11 @@ export function sankeySpecFromParams(params) {
28
32
  ...(params.get('unit') ? { unit: params.get('unit') } : {}),
29
33
  });
30
34
  }
35
+ /**
36
+ * Parse and validate sankey JSON (includes the cycle check).
37
+ *
38
+ * @throws ApiError(400) with a corrective example on bad input
39
+ */
31
40
  export function parseSankeySpec(raw) {
32
41
  if (raw === null || raw === '') {
33
42
  throw new ApiError(400, `Missing data. Use ?type=sankey&${SANKEY_EXAMPLE} — or ?d= with {"labels":["A","B"],"links":[{"from":"A","to":"B","value":30}]}`);
@@ -1,3 +1,6 @@
1
+ /**
2
+ * One scatter series: a name plus `[x,y]` points.
3
+ */
1
4
  export interface ScatterSeries {
2
5
  name: string;
3
6
  points: {
@@ -5,6 +8,9 @@ export interface ScatterSeries {
5
8
  y: number;
6
9
  }[];
7
10
  }
11
+ /**
12
+ * Scatter data — one or more series; negative coordinates allowed.
13
+ */
8
14
  export interface ScatterSpec {
9
15
  title?: string;
10
16
  unit?: string;
@@ -16,5 +22,14 @@ export interface ScatterSpec {
16
22
  /** 多系列形态(规范 scatter-1 双系列对比) */
17
23
  series?: ScatterSeries[];
18
24
  }
25
+ /**
26
+ * Build scatter spec JSON from `?x=&y=` (repeated pairs form multiple series).
27
+ * Returns null when absent.
28
+ */
19
29
  export declare function scatterSpecFromParams(params: URLSearchParams): string | null;
30
+ /**
31
+ * Parse and validate scatter JSON (`{"points":[[1,4]]}` or `{"series":[...]}`).
32
+ *
33
+ * @throws ApiError(400) with a corrective example on bad input
34
+ */
20
35
  export declare function parseScatterSpec(raw: string | null): ScatterSpec;
@@ -1,6 +1,10 @@
1
1
  // 散点图(scatter):x,y 点对;单系列扁平 x=/y=(或 JSON points),多系列重复 x=/y= 参数(或 JSON series)。
2
2
  import { ApiError, LIMITS, clip } from './base.js';
3
3
  const SCATTER_EXAMPLE = 'x=1,2,3&y=4,5,6';
4
+ /**
5
+ * Build scatter spec JSON from `?x=&y=` (repeated pairs form multiple series).
6
+ * Returns null when absent.
7
+ */
4
8
  export function scatterSpecFromParams(params) {
5
9
  const d = params.get('d');
6
10
  if (d !== null && d !== '')
@@ -69,6 +73,11 @@ function parsePoints(rawPoints, at) {
69
73
  return { x, y };
70
74
  });
71
75
  }
76
+ /**
77
+ * Parse and validate scatter JSON (`{"points":[[1,4]]}` or `{"series":[...]}`).
78
+ *
79
+ * @throws ApiError(400) with a corrective example on bad input
80
+ */
72
81
  export function parseScatterSpec(raw) {
73
82
  if (raw === null || raw === '') {
74
83
  throw new ApiError(400, `Missing data. Use ?type=scatter&${SCATTER_EXAMPLE} — or ?d= with {"points":[[1,4],[2,5]]}`);
package/dist/store.d.ts CHANGED
@@ -1,6 +1,15 @@
1
1
  import type { DrawEnv } from './types.js';
2
+ /**
3
+ * All supported chart template ids — the valid `?type=` values.
4
+ */
2
5
  export declare const CHART_TEMPLATES: readonly ["pie", "bar", "hbar", "line", "area", "radar", "gauge", "scatter", "combo", "sankey", "qr"];
6
+ /**
7
+ * A chart template id; one of {@link CHART_TEMPLATES}.
8
+ */
3
9
  export type ChartTemplate = (typeof CHART_TEMPLATES)[number];
10
+ /**
11
+ * A stored (short-linked) chart: template + style family + normalized spec.
12
+ */
4
13
  export interface StoredChart {
5
14
  template: ChartTemplate;
6
15
  theme: string;
@@ -8,7 +17,13 @@ export interface StoredChart {
8
17
  spec: unknown;
9
18
  }
10
19
  export declare function assertHash(hash: string): void;
20
+ /**
21
+ * Canonical JSON with keys recursively sorted — equal data, equal text.
22
+ */
11
23
  export declare function canonicalJson(v: unknown): string;
24
+ /**
25
+ * Content hash (SHA-256, first 16 hex chars) of a stored chart — the short-link id.
26
+ */
12
27
  export declare function fingerprint(doc: StoredChart): Promise<string>;
13
28
  export declare function putChart(env: DrawEnv, hash: string, doc: StoredChart): Promise<void>;
14
29
  export declare function getChart(env: DrawEnv, hash: string): Promise<StoredChart>;
package/dist/store.js CHANGED
@@ -2,6 +2,9 @@
2
2
  // 读取走 raw(免 token);写入需 GITHUB_TOKEN(secret,fine-grained PAT 仅授本仓 Contents 写)。
3
3
  // 可见性:公开仓任何人都可浏览目录,请勿存放敏感数据(私有方案规划中,将收费提供)。
4
4
  import { ApiError } from './specs/index.js';
5
+ /**
6
+ * All supported chart template ids — the valid `?type=` values.
7
+ */
5
8
  export const CHART_TEMPLATES = ['pie', 'bar', 'hbar', 'line', 'area', 'radar', 'gauge', 'scatter', 'combo', 'sankey', 'qr'];
6
9
  const HASH_RE = /^[0-9a-f]{16}$/;
7
10
  const MAX_BODY = 64 * 1024;
@@ -11,6 +14,9 @@ export function assertHash(hash) {
11
14
  }
12
15
  }
13
16
  // 规范化 JSON(递归排序 key),让"相同数据"得到相同指纹
17
+ /**
18
+ * Canonical JSON with keys recursively sorted — equal data, equal text.
19
+ */
14
20
  export function canonicalJson(v) {
15
21
  const walk = (x) => {
16
22
  if (Array.isArray(x))
@@ -26,6 +32,9 @@ export function canonicalJson(v) {
26
32
  };
27
33
  return JSON.stringify(walk(v));
28
34
  }
35
+ /**
36
+ * Content hash (SHA-256, first 16 hex chars) of a stored chart — the short-link id.
37
+ */
29
38
  export async function fingerprint(doc) {
30
39
  const data = new TextEncoder().encode(canonicalJson(doc));
31
40
  const digest = await crypto.subtle.digest('SHA-256', data);
@@ -1,7 +1,17 @@
1
1
  import type { ChartTemplate } from '../store.js';
2
2
  import { type Scheme, type Skin, type Variant } from './types.js';
3
+ /**
4
+ * Style registry: available variants per chart type (index 0 = default family).
5
+ */
3
6
  export declare const VARIANTS: Record<ChartTemplate, Variant[]>;
4
7
  /** 全部有效风格家族(错误提示/演示页用) */
5
8
  export declare const FAMILIES: string[];
9
+ /**
10
+ * Resolve the render-ready skin for a chart.
11
+ *
12
+ * @param family the `?t=` style family; null uses the type default.
13
+ * An unknown family throws; a family not built for this type falls back to the default.
14
+ * @param scheme `auto` emits both palettes plus a `prefers-color-scheme` media query.
15
+ */
6
16
  export declare function resolveSkin(type: ChartTemplate, family: string | null, scheme: Scheme): Skin;
7
17
  export { type Skin, type Scheme, type Variant } from './types.js';
@@ -13,6 +13,9 @@ import { GAUGE_VARIANTS } from './gauge.js';
13
13
  import { SCATTER_VARIANTS } from './scatter.js';
14
14
  import { COMBO_VARIANTS } from './combo.js';
15
15
  import { QR_VARIANTS } from './qr.js';
16
+ /**
17
+ * Style registry: available variants per chart type (index 0 = default family).
18
+ */
16
19
  export const VARIANTS = {
17
20
  pie: PIE_VARIANTS,
18
21
  bar: BAR_VARIANTS,
@@ -58,6 +61,13 @@ function padSeries(series) {
58
61
  }
59
62
  return out;
60
63
  }
64
+ /**
65
+ * Resolve the render-ready skin for a chart.
66
+ *
67
+ * @param family the `?t=` style family; null uses the type default.
68
+ * An unknown family throws; a family not built for this type falls back to the default.
69
+ * @param scheme `auto` emits both palettes plus a `prefers-color-scheme` media query.
70
+ */
61
71
  export function resolveSkin(type, family, scheme) {
62
72
  const variants = VARIANTS[type];
63
73
  let variant = variants[0]; // 默认 = 第一个变体
@@ -94,6 +94,9 @@ export interface Variant {
94
94
  export declare function makeVariant(v: VariantInput): Variant;
95
95
  /** 渲染期皮肤:变体 + 明暗模式 → 渲染器直接消费 */
96
96
  export type Scheme = 'auto' | 'dark' | 'light';
97
+ /**
98
+ * A resolved, render-ready skin: palette, rules, and optional design canvas size.
99
+ */
97
100
  export interface Skin {
98
101
  scheme: Scheme;
99
102
  series: Duo[];
package/dist/version.d.ts CHANGED
@@ -1,6 +1,17 @@
1
+ /**
2
+ * The current render version. Bump when a deploy changes rendered output.
3
+ */
1
4
  export declare const CURRENT_VERSION = 1;
5
+ /**
6
+ * A resolved version plus the Cache-Control policy it implies.
7
+ */
2
8
  export interface VersionInfo {
3
9
  version: number;
4
10
  cacheControl: string;
5
11
  }
12
+ /**
13
+ * Resolve `?v=`: omitted → latest (300s cache); `v=N` → pinned (1-day cache).
14
+ *
15
+ * @throws ApiError(400) for values outside 1..CURRENT_VERSION
16
+ */
6
17
  export declare function resolveVersion(vParam: string | null): VersionInfo;
package/dist/version.js CHANGED
@@ -2,7 +2,15 @@
2
2
  // 缺省 = 最新版(短 TTL 300s)。?v=N = 钉住版本,长缓存 1 天;
3
3
  // immutable 仅用于内容寻址短链 /c/<hash>(渲染代码随部署变化,/draw 的输出做不到真正冻结)。
4
4
  import { ApiError } from './specs/index.js';
5
+ /**
6
+ * The current render version. Bump when a deploy changes rendered output.
7
+ */
5
8
  export const CURRENT_VERSION = 1;
9
+ /**
10
+ * Resolve `?v=`: omitted → latest (300s cache); `v=N` → pinned (1-day cache).
11
+ *
12
+ * @throws ApiError(400) for values outside 1..CURRENT_VERSION
13
+ */
6
14
  export function resolveVersion(vParam) {
7
15
  if (vParam === null) {
8
16
  return { version: CURRENT_VERSION, cacheControl: 'public, max-age=300' };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-cmd/draw",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "SVG chart rendering as a service, a library, and a CLI — pie/bar/hbar/line/area/radar/gauge/scatter/combo/sankey/qr, same params everywhere",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",