@x-cmd/draw 0.2.0 → 0.2.2
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 +67 -55
- package/README.md +67 -55
- package/dist/handlee-OFL.txt +94 -0
- package/dist/inter-OFL.txt +92 -0
- package/dist/lib.d.ts +18 -0
- package/dist/lib.js +18 -0
- 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 +2 -2
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
|
}
|
package/dist/specs/combo.d.ts
CHANGED
|
@@ -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;
|
package/dist/specs/combo.js
CHANGED
|
@@ -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 !== '')
|
package/dist/specs/csv.d.ts
CHANGED
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)
|
package/dist/specs/gauge.d.ts
CHANGED
|
@@ -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;
|
package/dist/specs/gauge.js
CHANGED
|
@@ -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}`);
|
package/dist/specs/input.d.ts
CHANGED
|
@@ -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;
|
package/dist/specs/input.js
CHANGED
|
@@ -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')) {
|
package/dist/specs/multi.d.ts
CHANGED
|
@@ -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;
|