@x-cmd/draw 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/LICENSE +228 -0
  2. package/README.cn.md +235 -0
  3. package/README.md +235 -2
  4. package/dist/lib.d.ts +25 -0
  5. package/dist/lib.js +50 -0
  6. package/dist/node/cli.d.ts +2 -0
  7. package/dist/node/cli.js +79 -0
  8. package/dist/render/area.d.ts +8 -0
  9. package/dist/render/area.js +414 -0
  10. package/dist/render/bar-grid.d.ts +3 -0
  11. package/dist/render/bar-grid.js +52 -0
  12. package/dist/render/bar-grouped.d.ts +3 -0
  13. package/dist/render/bar-grouped.js +131 -0
  14. package/dist/render/bar-stacked.d.ts +3 -0
  15. package/dist/render/bar-stacked.js +142 -0
  16. package/dist/render/bar.d.ts +3 -0
  17. package/dist/render/bar.js +74 -0
  18. package/dist/render/combo.d.ts +3 -0
  19. package/dist/render/combo.js +256 -0
  20. package/dist/render/dispatch.d.ts +3 -0
  21. package/dist/render/dispatch.js +69 -0
  22. package/dist/render/gauge.d.ts +3 -0
  23. package/dist/render/gauge.js +216 -0
  24. package/dist/render/hbar-spec.d.ts +7 -0
  25. package/dist/render/hbar-spec.js +265 -0
  26. package/dist/render/hbar.d.ts +3 -0
  27. package/dist/render/hbar.js +55 -0
  28. package/dist/render/line-multi.d.ts +3 -0
  29. package/dist/render/line-multi.js +89 -0
  30. package/dist/render/line.d.ts +11 -0
  31. package/dist/render/line.js +123 -0
  32. package/dist/render/measure.d.ts +60 -0
  33. package/dist/render/measure.js +75 -0
  34. package/dist/render/pie.d.ts +5 -0
  35. package/dist/render/pie.js +439 -0
  36. package/dist/render/qr.d.ts +8 -0
  37. package/dist/render/qr.js +52 -0
  38. package/dist/render/query.d.ts +14 -0
  39. package/dist/render/query.js +220 -0
  40. package/dist/render/radar.d.ts +5 -0
  41. package/dist/render/radar.js +221 -0
  42. package/dist/render/sankey.d.ts +3 -0
  43. package/dist/render/sankey.js +280 -0
  44. package/dist/render/scatter.d.ts +3 -0
  45. package/dist/render/scatter.js +98 -0
  46. package/dist/render/svg.d.ts +93 -0
  47. package/dist/render/svg.js +471 -0
  48. package/dist/specs/base.d.ts +34 -0
  49. package/dist/specs/base.js +159 -0
  50. package/dist/specs/combo.d.ts +15 -0
  51. package/dist/specs/combo.js +135 -0
  52. package/dist/specs/csv.d.ts +1 -0
  53. package/dist/specs/csv.js +40 -0
  54. package/dist/specs/gauge.d.ts +15 -0
  55. package/dist/specs/gauge.js +108 -0
  56. package/dist/specs/index.d.ts +8 -0
  57. package/dist/specs/index.js +9 -0
  58. package/dist/specs/input.d.ts +11 -0
  59. package/dist/specs/input.js +33 -0
  60. package/dist/specs/multi.d.ts +12 -0
  61. package/dist/specs/multi.js +112 -0
  62. package/dist/specs/sankey.d.ts +13 -0
  63. package/dist/specs/sankey.js +112 -0
  64. package/dist/specs/scatter.d.ts +20 -0
  65. package/dist/specs/scatter.js +121 -0
  66. package/dist/store.d.ts +15 -0
  67. package/dist/store.js +121 -0
  68. package/dist/styles/area.d.ts +2 -0
  69. package/dist/styles/area.js +106 -0
  70. package/dist/styles/bar.d.ts +2 -0
  71. package/dist/styles/bar.js +52 -0
  72. package/dist/styles/combo.d.ts +2 -0
  73. package/dist/styles/combo.js +37 -0
  74. package/dist/styles/common.d.ts +34 -0
  75. package/dist/styles/common.js +259 -0
  76. package/dist/styles/gauge.d.ts +2 -0
  77. package/dist/styles/gauge.js +51 -0
  78. package/dist/styles/handlee.d.ts +1 -0
  79. package/dist/styles/handlee.js +2 -0
  80. package/dist/styles/hbar.d.ts +2 -0
  81. package/dist/styles/hbar.js +66 -0
  82. package/dist/styles/index.d.ts +7 -0
  83. package/dist/styles/index.js +79 -0
  84. package/dist/styles/inter.d.ts +3 -0
  85. package/dist/styles/inter.js +10 -0
  86. package/dist/styles/line.d.ts +2 -0
  87. package/dist/styles/line.js +97 -0
  88. package/dist/styles/pie.d.ts +2 -0
  89. package/dist/styles/pie.js +163 -0
  90. package/dist/styles/qr.d.ts +2 -0
  91. package/dist/styles/qr.js +12 -0
  92. package/dist/styles/radar.d.ts +2 -0
  93. package/dist/styles/radar.js +50 -0
  94. package/dist/styles/sankey.d.ts +2 -0
  95. package/dist/styles/sankey.js +29 -0
  96. package/dist/styles/scatter.d.ts +2 -0
  97. package/dist/styles/scatter.js +21 -0
  98. package/dist/styles/types.d.ts +105 -0
  99. package/dist/styles/types.js +70 -0
  100. package/dist/theme.d.ts +3 -0
  101. package/dist/theme.js +11 -0
  102. package/dist/types.d.ts +17 -0
  103. package/dist/types.js +1 -0
  104. package/dist/version.d.ts +6 -0
  105. package/dist/version.js +16 -0
  106. package/package.json +53 -4
@@ -0,0 +1,11 @@
1
+ import type { ChartSpec, MultiSpec } from '../specs/index.js';
2
+ import { type RenderOpts } from './svg.js';
3
+ export declare function renderLine(spec: ChartSpec, o: RenderOpts): string;
4
+ export declare function lineLayout(spec: MultiSpec, o: RenderOpts): {
5
+ left: number;
6
+ right: number;
7
+ bottom: number;
8
+ contentTop: number;
9
+ xLabelY: number;
10
+ legendY: number;
11
+ };
@@ -0,0 +1,123 @@
1
+ import { card } from '../theme.js';
2
+ import { esc, fmtNum, niceTicks, r2, trunc, LabelPlacer, pushLineLabelsAllOrNothing, pushPoints, labelStride, toSegments, reserveBands, smoothPath, svgDoc, titleBlock, editorialTitle, yAxisGrid, axisTitleY } from './svg.js';
3
+ import { plainGeo } from './measure.js';
4
+ export function renderLine(spec, o) {
5
+ const { width, height, version, skin } = o;
6
+ const rules = skin.rules;
7
+ const values = spec.data.map((d) => d.value);
8
+ const max = Math.max(...values);
9
+ const peakIdx = values.indexOf(max);
10
+ const plain = rules.canvas === 'plain';
11
+ // 标题区(编辑式大标题占两行)
12
+ const pad = 24;
13
+ let headSvg = '';
14
+ let headH = 0;
15
+ if (rules.title === 'editorial' && spec.title) {
16
+ const t = editorialTitle(spec.title, width, pad);
17
+ headSvg = t.svg;
18
+ headH = t.height + 6;
19
+ }
20
+ else {
21
+ headSvg = titleBlock(spec.title, pad);
22
+ if (spec.title)
23
+ headH = 24;
24
+ }
25
+ const ticks = niceTicks(max, 4);
26
+ // 测量驱动留白:inline 轴刻度画在绘图区内,不占左列
27
+ const measure = {
28
+ xLabels: spec.data.map((d) => d.label),
29
+ ...(rules.axisInline ? {} : { ticks }),
30
+ };
31
+ const geo = plain ? plainGeo(width, height, !!spec.title, measure) : null;
32
+ const contentTop = geo ? Math.max(geo.top, pad + headH + 14) : pad + headH + 20;
33
+ const left = geo ? geo.left : pad + (rules.axisInline ? 42 : 48);
34
+ const right = geo ? geo.right : width - pad;
35
+ const bottom = geo ? geo.bottom : height - pad - 30;
36
+ const xLabelY = geo ? geo.xLabelY : bottom + (rules.baseline === 'dots' ? 34 : 20);
37
+ const yMax = ticks[ticks.length - 1];
38
+ const y = (v) => bottom - (v / yMax) * (bottom - contentTop);
39
+ const n = spec.data.length;
40
+ const xs = (i) => (n === 1 ? (left + right) / 2 : left + ((right - left) * i) / (n - 1));
41
+ const pts = spec.data.map((d, i) => [xs(i), y(d.value)]);
42
+ const path = rules.smooth ? smoothPath(pts) : `M${pts.map((p) => `${r2(p[0])} ${r2(p[1])}`).join('L')}`;
43
+ const parts = [card(width, height, rules.canvas === 'plain')];
44
+ if (rules.areaGradient) {
45
+ parts.push(`<defs><linearGradient id="dg-area" x1="0" y1="0" x2="0" y2="1">` +
46
+ `<stop offset="0" stop-color="var(--d-s1)" stop-opacity="${rules.areaTop}"/>` +
47
+ `<stop offset="1" stop-color="var(--d-s1)" stop-opacity="${rules.areaBottom}"/></linearGradient></defs>`);
48
+ }
49
+ yAxisGrid(parts, ticks, y, left, right, bottom, rules.grid, rules.gridDash, rules.axisInline, rules.baseline, pts.map((p) => p[0]));
50
+ if (rules.axisTitle)
51
+ parts.push(axisTitleY(spec.unit, (contentTop + bottom) / 2, left - (rules.axisInline ? 17 : 26)));
52
+ if (rules.glow) {
53
+ // Glow 风:模糊底层 + 渐变描边(规范 line-3:宽14/透明.32/模糊7)
54
+ parts.push(`<defs><filter id="d-glow" x="-20%" y="-60%" width="140%" height="220%"><feGaussianBlur stdDeviation="7"/></filter>` +
55
+ `<linearGradient id="dg-glow-0" x1="0" y1="0" x2="1" y2="0">` +
56
+ `<stop stop-color="var(--d-s1)"/><stop offset="1" stop-color="var(--d-s2)"/></linearGradient></defs>`);
57
+ parts.push(`<path d="${path}" fill="none" stroke="var(--d-s1)" stroke-width="14" stroke-opacity="0.32" stroke-linecap="round" filter="url(#d-glow)"/>`);
58
+ parts.push(`<path d="${path}" fill="none" stroke="url(#dg-glow-0)" stroke-width="${rules.lineWidth}" stroke-linecap="round" stroke-linejoin="round"/>`);
59
+ }
60
+ else {
61
+ if (rules.areaGradient) {
62
+ parts.push(`<path d="${path}L${r2(pts[n - 1][0])} ${r2(bottom)}L${r2(pts[0][0])} ${r2(bottom)}Z" fill="url(#dg-area)"/>`);
63
+ }
64
+ const dash = rules.lineDash ? ` stroke-dasharray="${rules.lineDash}"` : '';
65
+ parts.push(`<path d="${path}" fill="none" stroke="var(--d-s1)" stroke-width="${rules.lineWidth}" stroke-linecap="round" stroke-linejoin="round"${dash}/>`);
66
+ }
67
+ pushPoints(parts, pts, 'var(--d-s1)', rules);
68
+ const xStride = labelStride(spec.data.map((d) => d.label), (right - left) / Math.max(n - 1, 1));
69
+ spec.data.forEach((d, i) => {
70
+ if (i % xStride === 0)
71
+ parts.push(`<text x="${r2(xs(i))}" y="${r2(xLabelY)}" text-anchor="middle" font-size="12" fill="var(--d-muted)">${esc(trunc(d.label, 12))}</text>`);
72
+ });
73
+ // 数值标签:全有或全无(任一位置放不下则整层不标);规范变体默认不标
74
+ const num = o.valueLabels ?? rules.num;
75
+ if (num !== 'none') {
76
+ const items = pts
77
+ .map((p, i) => ({ px: p[0], py: p[1], text: fmtNum(spec.data[i].value), isPeak: i === peakIdx }))
78
+ .filter((_, i) => num === 'all' || i === peakIdx);
79
+ const placer = new LabelPlacer();
80
+ // 预留 Y 刻度带与 X 标签带:数值标签不得压刻度
81
+ reserveBands(placer, left, right, contentTop, bottom, xLabelY, [], 0, 0);
82
+ pushLineLabelsAllOrNothing(parts, placer, items, toSegments(pts));
83
+ }
84
+ return svgDoc(width, height, 'line', version, spec.title ?? 'line chart', spec, skin, headSvg + parts.join(''));
85
+ }
86
+ // 多系列共享的头部布局计算(line-multi 使用)
87
+ export function lineLayout(spec, o) {
88
+ const rules = o.skin.rules;
89
+ const pad = 24;
90
+ let headH = 0;
91
+ if (rules.title === 'editorial' && spec.title) {
92
+ headH = editorialTitle(spec.title, o.width, pad).height + 6;
93
+ }
94
+ else if (spec.title) {
95
+ headH = 24;
96
+ }
97
+ const hasLegendRow = rules.legend.startsWith('top');
98
+ if (rules.canvas === 'plain') {
99
+ // 预览刻度与标签宽度参与留白测量(与渲染时的 niceTicks(gMax, 4) 同源)
100
+ const m = {
101
+ xLabels: spec.labels,
102
+ ...(rules.axisInline ? {} : { ticks: niceTicks(Math.max(...spec.series.flatMap((s) => s.values)), 4) }),
103
+ };
104
+ const geo = plainGeo(o.width, o.height, !!spec.title, m);
105
+ return {
106
+ left: geo.left,
107
+ right: geo.right,
108
+ bottom: geo.bottom,
109
+ contentTop: Math.max(geo.top, pad + headH + (hasLegendRow ? 26 : 14) + 10),
110
+ xLabelY: geo.xLabelY,
111
+ legendY: Math.max(geo.legendY, pad + headH + 16), // 标题存在时图例下移,避免压标题
112
+ };
113
+ }
114
+ const bottom = o.height - pad - 30;
115
+ return {
116
+ left: pad + (rules.axisInline ? 42 : 48),
117
+ right: o.width - pad,
118
+ bottom,
119
+ contentTop: pad + headH + (hasLegendRow ? 26 : 14) + 10,
120
+ xLabelY: bottom + (rules.baseline === 'dots' ? 34 : 20),
121
+ legendY: pad + headH + 16,
122
+ };
123
+ }
@@ -0,0 +1,60 @@
1
+ /** 四边留白 */
2
+ export interface Insets {
3
+ left: number;
4
+ right: number;
5
+ top: number;
6
+ bottom: number;
7
+ }
8
+ /** Y 轴刻度列宽:最宽刻度文字(可带后缀,如堆叠百分比模式的 "%")+ 与绘图区的间距 */
9
+ export declare function tickColW(ticks: readonly number[], fs?: number, gap?: number, suffix?: string): number;
10
+ /** 径向"保图优先"预算结果 */
11
+ export interface RadialFitResult {
12
+ R: number;
13
+ /** 标签截短长度(= maxCut 表示未截) */
14
+ cut: number;
15
+ /** 引线段/标签间距(压缩后) */
16
+ lead: number;
17
+ /** true = 仍放不下 → 整层放弃标签,R 保持自然值 */
18
+ drop: boolean;
19
+ }
20
+ /** 径向标注预算:R + lead + labelW ≤ budget。放不下按序降级(压引线/截名/R 下限),
21
+ * 顺序由 order 决定;overflow='drop' 穷尽后放弃整层 */
22
+ export declare function radialFit(o: {
23
+ natural: number;
24
+ budget: number;
25
+ lead: number;
26
+ minLead: number;
27
+ labelW: (cut: number) => number;
28
+ maxCut: number;
29
+ minCut: number;
30
+ cutStep?: number;
31
+ floorK: number;
32
+ floorAbs?: number;
33
+ order?: 'lead-first' | 'cut-first';
34
+ overflow: 'drop' | 'floor';
35
+ }): RadialFitResult;
36
+ /** 规范几何函数接入测量的输入 */
37
+ export interface GeoMeasure {
38
+ /** Y 轴刻度(非 inline 轴才有左列;yAxisGrid 默认 fs=12 / gap=8) */
39
+ ticks?: readonly number[];
40
+ tickFs?: number;
41
+ tickGap?: number;
42
+ /** X 轴标签(anchor=middle:首末标签半宽会外伸左右缘) */
43
+ xLabels?: readonly string[];
44
+ xFs?: number;
45
+ }
46
+ /** 首末 X 标签半宽外伸(按渲染同款 trunc 12 截断后取最宽的一半 + 间距) */
47
+ export declare function xOverhang(m: GeoMeasure): number;
48
+ /** Y 刻度列需要的最小左内边距 */
49
+ export declare function yTickInset(m: GeoMeasure): number;
50
+ /** 预算内最大截断长度:按此截断后最宽标签 ≤ budget(放不下先截短不重叠) */
51
+ export declare function fitCut(labels: readonly string[], maxCut: number, budget: number, fs: number): number;
52
+ export interface PlainGeo {
53
+ left: number;
54
+ right: number;
55
+ top: number;
56
+ bottom: number;
57
+ xLabelY: number;
58
+ legendY: number;
59
+ }
60
+ export declare function plainGeo(w: number, h: number, hasTitle: boolean, m?: GeoMeasure): PlainGeo;
@@ -0,0 +1,75 @@
1
+ // 测量层:量出固定文字的真实占位,各图种按 留白 = max(规范, 实测) 反推绘图区。
2
+ // 默认数据零漂移(实测 < 规范用规范值);防碰撞是 svg.ts 的 LabelPlacer,与本层互补。
3
+ // 精度来源:inter.ts 字宽表与渲染字体同源。
4
+ import { textW, fmtNum, trunc } from './svg.js';
5
+ /** Y 轴刻度列宽:最宽刻度文字(可带后缀,如堆叠百分比模式的 "%")+ 与绘图区的间距 */
6
+ export function tickColW(ticks, fs = 12, gap = 8, suffix = '') {
7
+ const w = Math.max(...ticks.map((t) => textW(fmtNum(t) + suffix, fs)), 0);
8
+ return w + gap;
9
+ }
10
+ /** 径向标注预算:R + lead + labelW ≤ budget。放不下按序降级(压引线/截名/R 下限),
11
+ * 顺序由 order 决定;overflow='drop' 穷尽后放弃整层 */
12
+ export function radialFit(o) {
13
+ if (o.natural + o.lead + o.labelW(o.maxCut) <= o.budget) {
14
+ return { R: Math.max(Math.min(o.natural, o.budget - o.lead - o.labelW(o.maxCut)), o.floorAbs ?? 0), cut: o.maxCut, lead: o.lead, drop: false };
15
+ }
16
+ const cutFirst = o.order === 'cut-first';
17
+ const lead = cutFirst ? o.lead : o.minLead;
18
+ const step = o.cutStep ?? 2;
19
+ let cut = o.maxCut;
20
+ while (cut > o.minCut && o.floorK * o.natural + lead + o.labelW(cut) > o.budget) {
21
+ cut = Math.max(o.minCut, cut - step);
22
+ }
23
+ if (cutFirst) {
24
+ const r1 = Math.min(o.natural, o.budget - o.lead - o.labelW(cut));
25
+ if (r1 >= o.floorK * o.natural) {
26
+ return { R: Math.max(r1, o.floorAbs ?? 0), cut, lead: o.lead, drop: false };
27
+ }
28
+ const lead2 = Math.max(o.minLead, Math.floor(o.budget - o.labelW(cut) - o.floorK * o.natural));
29
+ // 不钳 floorK:钳了会让标签溢出画布(文字不裁切优先)
30
+ return { R: Math.max(Math.min(o.natural, o.budget - lead2 - o.labelW(cut)), o.floorAbs ?? 0), cut, lead: lead2, drop: false };
31
+ }
32
+ if (o.floorK * o.natural + lead + o.labelW(cut) <= o.budget) {
33
+ return { R: Math.max(Math.min(o.natural, o.budget - lead - o.labelW(cut)), o.floorK * o.natural, o.floorAbs ?? 0), cut, lead, drop: false };
34
+ }
35
+ if (o.overflow === 'drop') {
36
+ return { R: o.natural, cut, lead, drop: true };
37
+ }
38
+ const lead2 = Math.max(o.minLead, Math.floor(o.budget - o.labelW(cut) - o.floorK * o.natural));
39
+ return { R: Math.max(Math.min(o.natural, o.budget - lead2 - o.labelW(cut)), o.floorAbs ?? 0), cut, lead: lead2, drop: false };
40
+ }
41
+ /** 首末 X 标签半宽外伸(按渲染同款 trunc 12 截断后取最宽的一半 + 间距) */
42
+ export function xOverhang(m) {
43
+ if (!m.xLabels?.length)
44
+ return 0;
45
+ const fs = m.xFs ?? 12;
46
+ return Math.round(Math.max(...m.xLabels.map((l) => textW(trunc(l, 12), fs))) / 2 + 6);
47
+ }
48
+ /** Y 刻度列需要的最小左内边距 */
49
+ export function yTickInset(m) {
50
+ return m.ticks ? tickColW(m.ticks, m.tickFs ?? 12, m.tickGap ?? 8) + 2 : 0;
51
+ }
52
+ /** 预算内最大截断长度:按此截断后最宽标签 ≤ budget(放不下先截短不重叠) */
53
+ export function fitCut(labels, maxCut, budget, fs) {
54
+ const widest = (c) => Math.max(...labels.map((l) => textW(trunc(l, c), fs)), 0);
55
+ if (budget <= 0 || labels.length === 0)
56
+ return maxCut;
57
+ if (widest(maxCut) <= budget)
58
+ return maxCut;
59
+ for (let c = maxCut - 1; c >= 2; c--) {
60
+ if (widest(c) <= budget)
61
+ return c;
62
+ }
63
+ return 2;
64
+ }
65
+ export function plainGeo(w, h, hasTitle, m = {}) {
66
+ const halfX = xOverhang(m);
67
+ return {
68
+ left: Math.round(Math.max(Math.max(44, w * 0.082), halfX, yTickInset(m))),
69
+ right: Math.round(w - Math.max(Math.max(28, w * 0.052), halfX)),
70
+ top: Math.round(h * (hasTitle ? 0.185 : 0.163)),
71
+ bottom: Math.round(h * 0.80),
72
+ xLabelY: Math.round(h * 0.896),
73
+ legendY: Math.round(h * (hasTitle ? 0.096 : 0.077)),
74
+ };
75
+ }
@@ -0,0 +1,5 @@
1
+ import type { ChartSpec } from '../specs/index.js';
2
+ import { type RenderOpts } from './svg.js';
3
+ export declare function pol(cx: number, cy: number, rad: number, a: number): [number, number];
4
+ export declare function segPath(cx: number, cy: number, R: number, r: number, a0: number, a1: number, corner: number): string;
5
+ export declare function renderPie(spec: ChartSpec, o: RenderOpts): string;