@x-cmd/draw 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/LICENSE +228 -0
  2. package/README.md +224 -2
  3. package/dist/lib.d.ts +25 -0
  4. package/dist/lib.js +50 -0
  5. package/dist/node/cli.d.ts +2 -0
  6. package/dist/node/cli.js +76 -0
  7. package/dist/render/area.d.ts +8 -0
  8. package/dist/render/area.js +414 -0
  9. package/dist/render/bar-grid.d.ts +3 -0
  10. package/dist/render/bar-grid.js +52 -0
  11. package/dist/render/bar-grouped.d.ts +3 -0
  12. package/dist/render/bar-grouped.js +131 -0
  13. package/dist/render/bar-stacked.d.ts +3 -0
  14. package/dist/render/bar-stacked.js +142 -0
  15. package/dist/render/bar.d.ts +3 -0
  16. package/dist/render/bar.js +74 -0
  17. package/dist/render/combo.d.ts +3 -0
  18. package/dist/render/combo.js +256 -0
  19. package/dist/render/dispatch.d.ts +3 -0
  20. package/dist/render/dispatch.js +69 -0
  21. package/dist/render/gauge.d.ts +3 -0
  22. package/dist/render/gauge.js +216 -0
  23. package/dist/render/hbar-spec.d.ts +7 -0
  24. package/dist/render/hbar-spec.js +265 -0
  25. package/dist/render/hbar.d.ts +3 -0
  26. package/dist/render/hbar.js +55 -0
  27. package/dist/render/line-multi.d.ts +3 -0
  28. package/dist/render/line-multi.js +89 -0
  29. package/dist/render/line.d.ts +11 -0
  30. package/dist/render/line.js +123 -0
  31. package/dist/render/measure.d.ts +60 -0
  32. package/dist/render/measure.js +75 -0
  33. package/dist/render/pie.d.ts +5 -0
  34. package/dist/render/pie.js +439 -0
  35. package/dist/render/qr.d.ts +8 -0
  36. package/dist/render/qr.js +52 -0
  37. package/dist/render/query.d.ts +14 -0
  38. package/dist/render/query.js +220 -0
  39. package/dist/render/radar.d.ts +5 -0
  40. package/dist/render/radar.js +221 -0
  41. package/dist/render/sankey.d.ts +3 -0
  42. package/dist/render/sankey.js +280 -0
  43. package/dist/render/scatter.d.ts +3 -0
  44. package/dist/render/scatter.js +98 -0
  45. package/dist/render/svg.d.ts +93 -0
  46. package/dist/render/svg.js +471 -0
  47. package/dist/specs/base.d.ts +34 -0
  48. package/dist/specs/base.js +159 -0
  49. package/dist/specs/combo.d.ts +15 -0
  50. package/dist/specs/combo.js +135 -0
  51. package/dist/specs/csv.d.ts +1 -0
  52. package/dist/specs/csv.js +40 -0
  53. package/dist/specs/gauge.d.ts +15 -0
  54. package/dist/specs/gauge.js +108 -0
  55. package/dist/specs/index.d.ts +8 -0
  56. package/dist/specs/index.js +9 -0
  57. package/dist/specs/input.d.ts +11 -0
  58. package/dist/specs/input.js +33 -0
  59. package/dist/specs/multi.d.ts +12 -0
  60. package/dist/specs/multi.js +112 -0
  61. package/dist/specs/sankey.d.ts +13 -0
  62. package/dist/specs/sankey.js +112 -0
  63. package/dist/specs/scatter.d.ts +20 -0
  64. package/dist/specs/scatter.js +121 -0
  65. package/dist/store.d.ts +15 -0
  66. package/dist/store.js +121 -0
  67. package/dist/styles/area.d.ts +2 -0
  68. package/dist/styles/area.js +106 -0
  69. package/dist/styles/bar.d.ts +2 -0
  70. package/dist/styles/bar.js +52 -0
  71. package/dist/styles/combo.d.ts +2 -0
  72. package/dist/styles/combo.js +37 -0
  73. package/dist/styles/common.d.ts +34 -0
  74. package/dist/styles/common.js +259 -0
  75. package/dist/styles/gauge.d.ts +2 -0
  76. package/dist/styles/gauge.js +51 -0
  77. package/dist/styles/handlee.d.ts +1 -0
  78. package/dist/styles/handlee.js +2 -0
  79. package/dist/styles/hbar.d.ts +2 -0
  80. package/dist/styles/hbar.js +66 -0
  81. package/dist/styles/index.d.ts +7 -0
  82. package/dist/styles/index.js +79 -0
  83. package/dist/styles/inter.d.ts +3 -0
  84. package/dist/styles/inter.js +10 -0
  85. package/dist/styles/line.d.ts +2 -0
  86. package/dist/styles/line.js +97 -0
  87. package/dist/styles/pie.d.ts +2 -0
  88. package/dist/styles/pie.js +163 -0
  89. package/dist/styles/qr.d.ts +2 -0
  90. package/dist/styles/qr.js +12 -0
  91. package/dist/styles/radar.d.ts +2 -0
  92. package/dist/styles/radar.js +50 -0
  93. package/dist/styles/sankey.d.ts +2 -0
  94. package/dist/styles/sankey.js +29 -0
  95. package/dist/styles/scatter.d.ts +2 -0
  96. package/dist/styles/scatter.js +21 -0
  97. package/dist/styles/types.d.ts +105 -0
  98. package/dist/styles/types.js +70 -0
  99. package/dist/theme.d.ts +3 -0
  100. package/dist/theme.js +11 -0
  101. package/dist/types.d.ts +17 -0
  102. package/dist/types.js +1 -0
  103. package/dist/version.d.ts +6 -0
  104. package/dist/version.js +16 -0
  105. package/package.json +47 -4
@@ -0,0 +1,135 @@
1
+ // 混合图(combo):柱系列+线系列同图,扁平 series=/line= 或 JSON(每系列可带 type)。
2
+ import { ApiError, LIMITS, clip, flatList } from './base.js';
3
+ const COMBO_EXAMPLE = 'labels=Mon,Tue&series=downloads:120,180&line=growth:5,8';
4
+ function parseSeriesEntries(rawEntries, labels, kind) {
5
+ return rawEntries.map((it, i) => {
6
+ if (typeof it !== 'object' || it === null || Array.isArray(it)) {
7
+ throw new ApiError(400, `series[${i}] must be an object with "name" and "values"`);
8
+ }
9
+ const s = it;
10
+ if (typeof s.name !== 'string' || s.name === '')
11
+ throw new ApiError(400, `series[${i}].name must be a non-empty string`);
12
+ if (!Array.isArray(s.values) || s.values.length !== labels.length) {
13
+ throw new ApiError(400, `series[${i}].values must have exactly ${labels.length} values (one per label)`);
14
+ }
15
+ const values = s.values.map((v, j) => {
16
+ if (typeof v !== 'number' || !Number.isFinite(v))
17
+ throw new ApiError(400, `series[${i}].values[${j}] must be a number`);
18
+ if (v < 0)
19
+ throw new ApiError(400, `series[${i}].values[${j}] must be >= 0, got ${v}`);
20
+ return v;
21
+ });
22
+ // 兼容存档回环:parseComboSpec 输出的是 kind(渲染器消费),短链渲染时会把存档
23
+ // 再喂回本解析器,若无 kind 分支,line 系列会被静默改成 bar(线整条消失)
24
+ const raw = s.type ?? s.kind;
25
+ const entryKind = raw === undefined ? kind : raw === 'bar' || raw === 'line' ? raw : null;
26
+ if (entryKind === null)
27
+ throw new ApiError(400, `series[${i}].type must be "bar" or "line"`);
28
+ return { name: clip(s.name, LIMITS.seriesName), values, kind: entryKind };
29
+ });
30
+ }
31
+ export function parseComboSpec(raw) {
32
+ if (raw === null || raw === '') {
33
+ throw new ApiError(400, `Missing data. Use ?type=combo&${COMBO_EXAMPLE}`);
34
+ }
35
+ let obj;
36
+ try {
37
+ obj = JSON.parse(raw);
38
+ }
39
+ catch {
40
+ throw new ApiError(400, `"d" is not valid JSON. Example: {"labels":["Mon","Tue"],"series":[{"name":"a","values":[1,2],"type":"bar"}]}`);
41
+ }
42
+ const o = obj;
43
+ if (!Array.isArray(o.labels) || o.labels.length === 0) {
44
+ throw new ApiError(400, `"labels" must be a non-empty array of x-axis labels`);
45
+ }
46
+ if (o.labels.length > LIMITS.maxBar) {
47
+ throw new ApiError(400, `"labels" has ${o.labels.length} points; combo supports at most ${LIMITS.maxBar}`);
48
+ }
49
+ const labels = o.labels.map((l, i) => {
50
+ if (typeof l === 'string')
51
+ return clip(l, LIMITS.label);
52
+ if (typeof l === 'number' && Number.isFinite(l))
53
+ return String(l);
54
+ throw new ApiError(400, `labels[${i}] must be a string`);
55
+ });
56
+ if (!Array.isArray(o.series) || o.series.length === 0) {
57
+ throw new ApiError(400, `"series" must be a non-empty array (each with optional "type": "bar"|"line")`);
58
+ }
59
+ const series = parseSeriesEntries(o.series, labels, 'bar');
60
+ if (series.length > LIMITS.maxSeries) {
61
+ throw new ApiError(400, `at most ${LIMITS.maxSeries} series are supported, got ${series.length}`);
62
+ }
63
+ if (!series.some((s) => s.values.some((v) => v > 0))) {
64
+ throw new ApiError(400, `all values are 0; series must sum to more than 0`);
65
+ }
66
+ let title;
67
+ if (o.title !== undefined) {
68
+ if (typeof o.title !== 'string')
69
+ throw new ApiError(400, `"title" must be a string`);
70
+ title = clip(o.title, LIMITS.title);
71
+ }
72
+ let unit;
73
+ if (o.unit !== undefined) {
74
+ if (typeof o.unit !== 'string')
75
+ throw new ApiError(400, `"unit" must be a string`);
76
+ unit = clip(o.unit, LIMITS.unit);
77
+ }
78
+ const stack = o.stack === true;
79
+ return { title, unit, labels, series, ...(stack ? { stack: true } : {}) };
80
+ }
81
+ export function comboSpecFromParams(params) {
82
+ const d = params.get('d');
83
+ if (d !== null && d !== '')
84
+ return d;
85
+ const hasBars = params.has('series');
86
+ const hasLines = params.has('line');
87
+ // values= 也接受:作为单个柱系列(图例名取 unit,缺省 'bar'),不与 series= 混用
88
+ const hasFlatValues = params.has('values');
89
+ if (!hasBars && !hasLines && !hasFlatValues)
90
+ return null;
91
+ const labels = flatList(params, 'labels');
92
+ if (labels.length === 0) {
93
+ throw new ApiError(400, `"labels" is required. Example: ?type=combo&${COMBO_EXAMPLE}`);
94
+ }
95
+ const series = [];
96
+ if (hasFlatValues && !hasBars) {
97
+ const values = flatList(params, 'values').map((v, i) => {
98
+ const n = Number(v);
99
+ if (!Number.isFinite(n))
100
+ throw new ApiError(400, `values[${i}] is not a number: ${JSON.stringify(v)}`);
101
+ return n;
102
+ });
103
+ series.push({ name: params.get('unit') || 'bar', values, type: 'bar' });
104
+ }
105
+ for (const [key, kind] of [['series', 'bar'], ['line', 'line']]) {
106
+ for (const e of params.getAll(key)) {
107
+ const idx = e.indexOf(':');
108
+ if (idx <= 0)
109
+ throw new ApiError(400, `${key} entry must look like "name:1,2,3", got: ${JSON.stringify(e)}`);
110
+ const name = e.slice(0, idx);
111
+ const values = e.slice(idx + 1).split(',').map((v) => {
112
+ const n = Number(v);
113
+ if (!Number.isFinite(n))
114
+ throw new ApiError(400, `${key} "${name}" has a non-numeric value: ${JSON.stringify(v)}`);
115
+ return n;
116
+ });
117
+ series.push({ name, values, type: kind });
118
+ }
119
+ }
120
+ const stackRaw = params.get('stack');
121
+ let stack = false;
122
+ if (stackRaw !== null && stackRaw !== '' && stackRaw !== '0' && stackRaw !== 'false') {
123
+ if (stackRaw !== '1' && stackRaw !== 'true') {
124
+ throw new ApiError(400, `"stack" must be 1/true (stacked bars) or 0/false, got: ${JSON.stringify(stackRaw)}`);
125
+ }
126
+ stack = true;
127
+ }
128
+ return JSON.stringify({
129
+ labels,
130
+ series,
131
+ ...(stack ? { stack: true } : {}),
132
+ ...(params.get('title') ? { title: params.get('title') } : {}),
133
+ ...(params.get('unit') ? { unit: params.get('unit') } : {}),
134
+ });
135
+ }
@@ -0,0 +1 @@
1
+ export declare function csvToSpecJson(csv: string): string;
@@ -0,0 +1,40 @@
1
+ // CSV → spec JSON:首行表头,两列=单系列,多列=多系列。
2
+ import { ApiError } from './base.js';
3
+ // CSV → spec JSON:首行为表头。两列(label,value)=单系列;多列(label,系列A,系列B)=多系列
4
+ export function csvToSpecJson(csv) {
5
+ const rows = csv.replace(/\r/g, '').split('\n').filter((l) => l.trim() !== '');
6
+ if (rows.length < 2)
7
+ throw new ApiError(400, 'CSV needs a header row and at least one data row');
8
+ const cells = rows.map((r) => r.split(',').map((c) => c.trim()));
9
+ const width = cells[0].length;
10
+ if (width < 2)
11
+ throw new ApiError(400, 'CSV needs at least 2 columns: label,value[,series2,...]');
12
+ for (let i = 1; i < cells.length; i++) {
13
+ if (cells[i].length !== width) {
14
+ throw new ApiError(400, `CSV row ${i + 1} has ${cells[i].length} cells, expected ${width}`);
15
+ }
16
+ }
17
+ const header = cells[0];
18
+ if (width === 2) {
19
+ return JSON.stringify({
20
+ data: cells.slice(1).map((r) => {
21
+ const v = Number(r[1]);
22
+ if (!Number.isFinite(v))
23
+ throw new ApiError(400, `CSV value "${r[1]}" (row with label "${r[0]}") is not a number`);
24
+ return { label: r[0], value: v };
25
+ }),
26
+ });
27
+ }
28
+ return JSON.stringify({
29
+ labels: cells.slice(1).map((r) => r[0]),
30
+ series: header.slice(1).map((name, si) => ({
31
+ name,
32
+ values: cells.slice(1).map((r) => {
33
+ const v = Number(r[si + 1]);
34
+ if (!Number.isFinite(v))
35
+ throw new ApiError(400, `CSV value "${r[si + 1]}" (series "${name}") is not a number`);
36
+ return v;
37
+ }),
38
+ })),
39
+ });
40
+ }
@@ -0,0 +1,15 @@
1
+ export interface GaugeDatum {
2
+ label: string;
3
+ value: number;
4
+ }
5
+ export interface GaugeSpec {
6
+ title?: string;
7
+ unit?: string;
8
+ /** 多环模式:每行一环(规范 gauge-1/2);与 value/max 二选一 */
9
+ data?: GaugeDatum[];
10
+ /** 单值模式(zones/reliability 或单环);data 存在时可为空 */
11
+ value?: number;
12
+ max: number;
13
+ }
14
+ export declare function gaugeSpecFromParams(params: URLSearchParams): string | null;
15
+ export declare function parseGaugeSpec(raw: string | null): GaugeSpec;
@@ -0,0 +1,108 @@
1
+ // 仪表盘(gauge):多环(labels+values,每行一环,值为百分比)或单值(value/max)。
2
+ import { ApiError, LIMITS, clip, flatList } from './base.js';
3
+ const GAUGE_EXAMPLE = 'value=87&max=100';
4
+ export function gaugeSpecFromParams(params) {
5
+ const d = params.get('d');
6
+ if (d !== null && d !== '')
7
+ return d;
8
+ // 多环:labels+values(值为百分比)
9
+ if (params.has('labels') && params.has('values')) {
10
+ const labels = flatList(params, 'labels');
11
+ const values = flatList(params, 'values');
12
+ if (labels.length !== values.length) {
13
+ throw new ApiError(400, `got ${labels.length} labels but ${values.length} values — they must match`);
14
+ }
15
+ const obj = { max: 100, data: labels.map((label, i) => ({ label, value: Number(values[i]) })) };
16
+ const title = params.get('title');
17
+ if (title)
18
+ obj.title = title;
19
+ return JSON.stringify(obj);
20
+ }
21
+ const valueRaw = params.get('value');
22
+ if (valueRaw === null || valueRaw === '')
23
+ return null;
24
+ const value = Number(valueRaw);
25
+ if (!Number.isFinite(value)) {
26
+ throw new ApiError(400, `"value" must be a number, got: ${JSON.stringify(valueRaw)}`);
27
+ }
28
+ const obj = { value };
29
+ const maxRaw = params.get('max');
30
+ if (maxRaw !== null && maxRaw !== '') {
31
+ const max = Number(maxRaw);
32
+ if (!Number.isFinite(max)) {
33
+ throw new ApiError(400, `"max" must be a number, got: ${JSON.stringify(maxRaw)}`);
34
+ }
35
+ obj.max = max;
36
+ }
37
+ const title = params.get('title');
38
+ if (title)
39
+ obj.title = title;
40
+ const unit = params.get('unit');
41
+ if (unit)
42
+ obj.unit = unit;
43
+ return JSON.stringify(obj);
44
+ }
45
+ export function parseGaugeSpec(raw) {
46
+ if (raw === null || raw === '') {
47
+ throw new ApiError(400, `Missing data. Use ?type=gauge&${GAUGE_EXAMPLE} — or ?d= with {"value":87,"max":100}`);
48
+ }
49
+ let obj;
50
+ try {
51
+ obj = JSON.parse(raw);
52
+ }
53
+ catch {
54
+ throw new ApiError(400, `"d" is not valid JSON. Example: {"value":87,"max":100}`);
55
+ }
56
+ if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
57
+ throw new ApiError(400, `gauge spec must be an object like {"value":87,"max":100} or {"max":100,"data":[{"label":"A","value":86}]}`);
58
+ }
59
+ const o = obj;
60
+ let title;
61
+ if (o.title !== undefined) {
62
+ if (typeof o.title !== 'string')
63
+ throw new ApiError(400, `"title" must be a string`);
64
+ title = clip(o.title, LIMITS.title);
65
+ }
66
+ let unit;
67
+ if (o.unit !== undefined) {
68
+ if (typeof o.unit !== 'string')
69
+ throw new ApiError(400, `"unit" must be a string`);
70
+ unit = clip(o.unit, LIMITS.unit);
71
+ }
72
+ // 多环形态:data 数组,每行一环,值为百分比
73
+ if (o.data !== undefined) {
74
+ if (!Array.isArray(o.data) || o.data.length === 0) {
75
+ throw new ApiError(400, `"data" must be a non-empty array like [{"label":"A","value":86}]`);
76
+ }
77
+ if (o.data.length > 8) {
78
+ throw new ApiError(400, `"data" has ${o.data.length} rings; gauge supports at most 8`);
79
+ }
80
+ const data = o.data.map((it, i) => {
81
+ if (typeof it !== 'object' || it === null)
82
+ throw new ApiError(400, `data[${i}] must be an object with "label" and "value"`);
83
+ const d = it;
84
+ if (typeof d.label !== 'string')
85
+ throw new ApiError(400, `data[${i}].label must be a string`);
86
+ if (typeof d.value !== 'number' || !Number.isFinite(d.value))
87
+ throw new ApiError(400, `data[${i}].value must be a number`);
88
+ if (d.value < 0)
89
+ throw new ApiError(400, `data[${i}].value must be >= 0 (ring percentage), got ${d.value}`);
90
+ return { label: clip(d.label, LIMITS.label), value: d.value };
91
+ });
92
+ return { title, unit, max: 100, data };
93
+ }
94
+ if (typeof o.value !== 'number' || !Number.isFinite(o.value) || o.value < 0) {
95
+ throw new ApiError(400, `"value" must be a number >= 0 (or use "data" for multi-ring), got ${JSON.stringify(o.value)}`);
96
+ }
97
+ let max = 100;
98
+ if (o.max !== undefined) {
99
+ if (typeof o.max !== 'number' || !Number.isFinite(o.max) || o.max <= 0) {
100
+ throw new ApiError(400, `"max" must be a number > 0, got ${JSON.stringify(o.max)}`);
101
+ }
102
+ max = o.max;
103
+ }
104
+ if (o.value > max) {
105
+ throw new ApiError(400, `"value" (${o.value}) must not exceed "max" (${max})`);
106
+ }
107
+ return { title, unit, value: o.value, max };
108
+ }
@@ -0,0 +1,8 @@
1
+ export * from './base.js';
2
+ export * from './multi.js';
3
+ export * from './gauge.js';
4
+ export * from './scatter.js';
5
+ export * from './combo.js';
6
+ export * from './csv.js';
7
+ export * from './input.js';
8
+ export * from './sankey.js';
@@ -0,0 +1,9 @@
1
+ // specs barrel:外部统一从 './specs' 导入,内部结构可自由调整。
2
+ export * from './base.js';
3
+ export * from './multi.js';
4
+ export * from './gauge.js';
5
+ export * from './scatter.js';
6
+ export * from './combo.js';
7
+ export * from './csv.js';
8
+ export * from './input.js';
9
+ export * from './sankey.js';
@@ -0,0 +1,11 @@
1
+ import { type ChartSpec } from './base.js';
2
+ import { type MultiSpec } from './multi.js';
3
+ export type ChartInput = {
4
+ multi: false;
5
+ spec: ChartSpec;
6
+ } | {
7
+ multi: true;
8
+ spec: MultiSpec;
9
+ };
10
+ export declare function looksMultiSeries(json: string): boolean;
11
+ export declare function parseInput(params: URLSearchParams, kind: 'pie' | 'bar' | 'hbar' | 'line' | 'area' | 'radar'): ChartInput;
@@ -0,0 +1,33 @@
1
+ // 统一输入入口:按参数决定单系列(ChartSpec)或多系列(MultiSpec)。
2
+ import { ApiError, parseChartSpec, specFromParams } from './base.js';
3
+ import { parseSeriesSpec, seriesSpecFromParams } from './multi.js';
4
+ // 结构判断(而非字符串嗅探):按解析后的对象是否含 "series" 键决定单/多系列,
5
+ // 避免标题里恰好含 "series" 字样时误路由
6
+ export function looksMultiSeries(json) {
7
+ try {
8
+ const obj = JSON.parse(json);
9
+ return typeof obj === 'object' && obj !== null && !Array.isArray(obj) && 'series' in obj;
10
+ }
11
+ catch {
12
+ return false; // 交给后续解析器给出具体报错
13
+ }
14
+ }
15
+ export function parseInput(params, kind) {
16
+ if (kind === 'pie') {
17
+ if (params.has('series')) {
18
+ throw new ApiError(400, `"series" (multi-series comparison) is not supported for /${kind}; use /bar or /line`);
19
+ }
20
+ return { multi: false, spec: parseChartSpec(specFromParams(params, kind), kind) };
21
+ }
22
+ const d = params.get('d');
23
+ if (d !== null && d !== '') {
24
+ // hbar 与 bar 同为 30 上限(line/area/radar 60);错误消息才不会张冠李戴
25
+ if (looksMultiSeries(d))
26
+ return { multi: true, spec: parseSeriesSpec(d, kind === 'line' || kind === 'area' || kind === 'radar' ? 'line' : 'bar') };
27
+ return { multi: false, spec: parseChartSpec(d, kind) };
28
+ }
29
+ const flatSeries = seriesSpecFromParams(params);
30
+ if (flatSeries !== null)
31
+ return { multi: true, spec: parseSeriesSpec(flatSeries, kind === 'line' || kind === 'area' || kind === 'radar' ? 'line' : 'bar') };
32
+ return { multi: false, spec: parseChartSpec(specFromParams(params, kind), kind) };
33
+ }
@@ -0,0 +1,12 @@
1
+ export interface SeriesEntry {
2
+ name: string;
3
+ values: number[];
4
+ }
5
+ export interface MultiSpec {
6
+ title?: string;
7
+ unit?: string;
8
+ labels: string[];
9
+ series: SeriesEntry[];
10
+ }
11
+ export declare function parseSeriesSpec(raw: string | null, kind: 'bar' | 'line'): MultiSpec;
12
+ export declare function seriesSpecFromParams(params: URLSearchParams): string | null;
@@ -0,0 +1,112 @@
1
+ // 多系列(bar 分组/堆叠、line 多曲线):labels + series[{name, values}]。
2
+ import { ApiError, LIMITS, MAX_BY_KIND, clip, flatList } from './base.js';
3
+ const SERIES_EXAMPLE_FLAT = 'labels=Mon,Tue&series=draw:120,180&series=qc:80,90';
4
+ export function parseSeriesSpec(raw, kind) {
5
+ if (raw === null || raw === '') {
6
+ throw new ApiError(400, `Missing data. Use ?${SERIES_EXAMPLE_FLAT} — or ?d= with {"labels":["A","B"],"series":[{"name":"s1","values":[1,2]}]}`);
7
+ }
8
+ let obj;
9
+ try {
10
+ obj = JSON.parse(raw);
11
+ }
12
+ catch {
13
+ throw new ApiError(400, `"d" is not valid JSON (remember to URL-encode it).`);
14
+ }
15
+ if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
16
+ throw new ApiError(400, `"d" must be an object with "labels" and "series"`);
17
+ }
18
+ const o = obj;
19
+ if (!Array.isArray(o.labels) || o.labels.length === 0) {
20
+ throw new ApiError(400, `"labels" must be a non-empty array of x-axis labels`);
21
+ }
22
+ const max = MAX_BY_KIND[kind];
23
+ if (o.labels.length > max) {
24
+ throw new ApiError(400, `"labels" has ${o.labels.length} points; ${kind} supports at most ${max}`);
25
+ }
26
+ const labels = o.labels.map((l, i) => {
27
+ if (typeof l === 'string')
28
+ return clip(l, LIMITS.label);
29
+ if (typeof l === 'number' && Number.isFinite(l))
30
+ return String(l);
31
+ throw new ApiError(400, `labels[${i}] must be a string, got ${typeof l}`);
32
+ });
33
+ if (!Array.isArray(o.series) || o.series.length === 0) {
34
+ throw new ApiError(400, `"series" must be a non-empty array of {"name":string,"values":number[]}`);
35
+ }
36
+ if (o.series.length > LIMITS.maxSeries) {
37
+ throw new ApiError(400, `"series" has ${o.series.length} entries; at most ${LIMITS.maxSeries} series are supported`);
38
+ }
39
+ let total = 0;
40
+ const series = o.series.map((it, i) => {
41
+ if (typeof it !== 'object' || it === null || Array.isArray(it)) {
42
+ throw new ApiError(400, `series[${i}] must be an object with "name" and "values"`);
43
+ }
44
+ const s = it;
45
+ if (typeof s.name !== 'string' || s.name === '') {
46
+ throw new ApiError(400, `series[${i}].name must be a non-empty string`);
47
+ }
48
+ if (!Array.isArray(s.values) || s.values.length !== labels.length) {
49
+ throw new ApiError(400, `series[${i}].values must have exactly ${labels.length} values (one per label)`);
50
+ }
51
+ const values = s.values.map((v, j) => {
52
+ if (typeof v !== 'number' || !Number.isFinite(v)) {
53
+ throw new ApiError(400, `series[${i}].values[${j}] must be a number`);
54
+ }
55
+ if (v < 0)
56
+ throw new ApiError(400, `series[${i}].values[${j}] must be >= 0, got ${v}`);
57
+ return v;
58
+ });
59
+ total += values.reduce((a, b) => a + b, 0);
60
+ return { name: clip(s.name, LIMITS.seriesName), values };
61
+ });
62
+ if (!(total > 0))
63
+ throw new ApiError(400, `all values are 0; series must sum to more than 0`);
64
+ let title;
65
+ if (o.title !== undefined) {
66
+ if (typeof o.title !== 'string')
67
+ throw new ApiError(400, `"title" must be a string`);
68
+ title = clip(o.title, LIMITS.title);
69
+ }
70
+ let unit;
71
+ if (o.unit !== undefined) {
72
+ if (typeof o.unit !== 'string')
73
+ throw new ApiError(400, `"unit" must be a string`);
74
+ unit = clip(o.unit, LIMITS.unit);
75
+ }
76
+ return { title, unit, labels, series };
77
+ }
78
+ // 扁平参数 → 多系列 JSON:series=名字:1,2,3 重复参数
79
+ export function seriesSpecFromParams(params) {
80
+ const entries = params.getAll('series');
81
+ if (entries.length === 0)
82
+ return null;
83
+ const labels = flatList(params, 'labels');
84
+ if (labels.length === 0) {
85
+ throw new ApiError(400, `"labels" is required together with "series". Example: ?${SERIES_EXAMPLE_FLAT}`);
86
+ }
87
+ const series = entries.map((e, i) => {
88
+ const idx = e.indexOf(':');
89
+ if (idx <= 0) {
90
+ throw new ApiError(400, `series[${i}] must look like "name:1,2,3" (colon after the name), got: ${JSON.stringify(e)}`);
91
+ }
92
+ const name = e.slice(0, idx);
93
+ if (name.includes(',')) {
94
+ throw new ApiError(400, `series[${i}] name must not contain commas (name: ${JSON.stringify(name)})`);
95
+ }
96
+ const values = e.slice(idx + 1).split(',').map((v) => {
97
+ const n = Number(v);
98
+ if (!Number.isFinite(n))
99
+ throw new ApiError(400, `series[${i}] has a non-numeric value: ${JSON.stringify(v)}`);
100
+ return n;
101
+ });
102
+ return { name, values };
103
+ });
104
+ const obj = { labels, series };
105
+ const title = params.get('title');
106
+ if (title)
107
+ obj.title = title;
108
+ const unit = params.get('unit');
109
+ if (unit)
110
+ obj.unit = unit;
111
+ return JSON.stringify(obj);
112
+ }
@@ -0,0 +1,13 @@
1
+ export interface SankeyLink {
2
+ from: string;
3
+ to: string;
4
+ value: number;
5
+ }
6
+ export interface SankeySpec {
7
+ title?: string;
8
+ unit?: string;
9
+ labels: string[];
10
+ links: SankeyLink[];
11
+ }
12
+ export declare function sankeySpecFromParams(params: URLSearchParams): string | null;
13
+ export declare function parseSankeySpec(raw: string | null): SankeySpec;
@@ -0,0 +1,112 @@
1
+ // 桑基图(sankey):labels + links(A>B:30),扁平或 JSON。
2
+ import { ApiError, LIMITS, clip, flatList } from './base.js';
3
+ const SANKEY_EXAMPLE = 'labels=Ads,Web,Signup,Buy&links=Ads>Signup:30,Web>Signup:20,Signup>Buy:40';
4
+ const MAX_NODES = 24;
5
+ const MAX_LINKS = 60;
6
+ export function sankeySpecFromParams(params) {
7
+ const d = params.get('d');
8
+ if (d !== null && d !== '')
9
+ return d;
10
+ if (!params.has('labels') && !params.has('links'))
11
+ return null;
12
+ const labels = flatList(params, 'labels');
13
+ if (labels.length === 0) {
14
+ throw new ApiError(400, `"labels" is required. Example: ?type=sankey&${SANKEY_EXAMPLE}`);
15
+ }
16
+ const links = params.getAll('links').flatMap((e) => e.split(';')).filter(Boolean).map((e) => {
17
+ const gt = e.indexOf('>');
18
+ const co = e.indexOf(':', gt + 1);
19
+ if (gt <= 0 || co <= gt + 1) {
20
+ throw new ApiError(400, `links entry must look like "A>B:30", got: ${JSON.stringify(e)}`);
21
+ }
22
+ return { from: e.slice(0, gt), to: e.slice(gt + 1, co), value: Number(e.slice(co + 1)) };
23
+ });
24
+ return JSON.stringify({
25
+ labels,
26
+ links,
27
+ ...(params.get('title') ? { title: params.get('title') } : {}),
28
+ ...(params.get('unit') ? { unit: params.get('unit') } : {}),
29
+ });
30
+ }
31
+ export function parseSankeySpec(raw) {
32
+ if (raw === null || raw === '') {
33
+ throw new ApiError(400, `Missing data. Use ?type=sankey&${SANKEY_EXAMPLE} — or ?d= with {"labels":["A","B"],"links":[{"from":"A","to":"B","value":30}]}`);
34
+ }
35
+ let obj;
36
+ try {
37
+ obj = JSON.parse(raw);
38
+ }
39
+ catch {
40
+ throw new ApiError(400, `"d" is not valid JSON. Example: {"labels":["A","B"],"links":[{"from":"A","to":"B","value":30}]}`);
41
+ }
42
+ const o = obj;
43
+ if (!Array.isArray(o.labels) || o.labels.length < 2) {
44
+ throw new ApiError(400, `"labels" must be an array with at least 2 node names`);
45
+ }
46
+ if (o.labels.length > MAX_NODES) {
47
+ throw new ApiError(400, `"labels" has ${o.labels.length} nodes; sankey supports at most ${MAX_NODES}`);
48
+ }
49
+ const seen = new Set();
50
+ const labels = o.labels.map((l, i) => {
51
+ if (typeof l !== 'string' || l === '')
52
+ throw new ApiError(400, `labels[${i}] must be a non-empty string`);
53
+ const name = clip(l, LIMITS.label);
54
+ if (seen.has(name))
55
+ throw new ApiError(400, `labels must be unique, duplicate: ${JSON.stringify(name)}`);
56
+ seen.add(name);
57
+ return name;
58
+ });
59
+ if (!Array.isArray(o.links) || o.links.length === 0) {
60
+ throw new ApiError(400, `"links" must be a non-empty array like [{"from":"A","to":"B","value":30}]`);
61
+ }
62
+ if (o.links.length > MAX_LINKS) {
63
+ throw new ApiError(400, `"links" has ${o.links.length} entries; sankey supports at most ${MAX_LINKS}`);
64
+ }
65
+ const links = o.links.map((it, i) => {
66
+ if (typeof it !== 'object' || it === null)
67
+ throw new ApiError(400, `links[${i}] must be an object with "from", "to", "value"`);
68
+ const l = it;
69
+ if (typeof l.from !== 'string' || typeof l.to !== 'string')
70
+ throw new ApiError(400, `links[${i}].from/.to must be node names`);
71
+ if (!seen.has(l.from))
72
+ throw new ApiError(400, `links[${i}].from references unknown node: ${JSON.stringify(l.from)}`);
73
+ if (!seen.has(l.to))
74
+ throw new ApiError(400, `links[${i}].to references unknown node: ${JSON.stringify(l.to)}`);
75
+ if (l.from === l.to)
76
+ throw new ApiError(400, `links[${i}] self-loop is not allowed (${JSON.stringify(l.from)})`);
77
+ if (typeof l.value !== 'number' || !Number.isFinite(l.value) || l.value <= 0) {
78
+ throw new ApiError(400, `links[${i}].value must be a number > 0`);
79
+ }
80
+ return { from: l.from, to: l.to, value: l.value };
81
+ });
82
+ let title;
83
+ if (o.title !== undefined) {
84
+ if (typeof o.title !== 'string')
85
+ throw new ApiError(400, `"title" must be a string`);
86
+ title = clip(o.title, LIMITS.title);
87
+ }
88
+ let unit;
89
+ if (o.unit !== undefined) {
90
+ if (typeof o.unit !== 'string')
91
+ throw new ApiError(400, `"unit" must be a string`);
92
+ unit = clip(o.unit, LIMITS.unit);
93
+ }
94
+ // DAG 校验(Kahn):环在解析期就报 400(直连与短链创建共用)
95
+ const deg = labels.map(() => 0);
96
+ for (const l of links)
97
+ deg[labels.indexOf(l.to)]++;
98
+ const q = labels.map((_, i) => i).filter((i) => deg[i] === 0);
99
+ let done = 0;
100
+ while (q.length) {
101
+ const i = q.shift();
102
+ done++;
103
+ for (const l of links) {
104
+ if (l.from === labels[i] && --deg[labels.indexOf(l.to)] === 0)
105
+ q.push(labels.indexOf(l.to));
106
+ }
107
+ }
108
+ if (done < labels.length) {
109
+ throw new ApiError(400, 'sankey links must form a DAG (no cycles); check links= for a loop');
110
+ }
111
+ return { title, unit, labels, links };
112
+ }
@@ -0,0 +1,20 @@
1
+ export interface ScatterSeries {
2
+ name: string;
3
+ points: {
4
+ x: number;
5
+ y: number;
6
+ }[];
7
+ }
8
+ export interface ScatterSpec {
9
+ title?: string;
10
+ unit?: string;
11
+ /** 单系列形态(无名,无图例) */
12
+ points?: {
13
+ x: number;
14
+ y: number;
15
+ }[];
16
+ /** 多系列形态(规范 scatter-1 双系列对比) */
17
+ series?: ScatterSeries[];
18
+ }
19
+ export declare function scatterSpecFromParams(params: URLSearchParams): string | null;
20
+ export declare function parseScatterSpec(raw: string | null): ScatterSpec;