@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.
- package/LICENSE +228 -0
- package/README.cn.md +235 -0
- package/README.md +235 -2
- package/dist/lib.d.ts +25 -0
- package/dist/lib.js +50 -0
- package/dist/node/cli.d.ts +2 -0
- package/dist/node/cli.js +79 -0
- package/dist/render/area.d.ts +8 -0
- package/dist/render/area.js +414 -0
- package/dist/render/bar-grid.d.ts +3 -0
- package/dist/render/bar-grid.js +52 -0
- package/dist/render/bar-grouped.d.ts +3 -0
- package/dist/render/bar-grouped.js +131 -0
- package/dist/render/bar-stacked.d.ts +3 -0
- package/dist/render/bar-stacked.js +142 -0
- package/dist/render/bar.d.ts +3 -0
- package/dist/render/bar.js +74 -0
- package/dist/render/combo.d.ts +3 -0
- package/dist/render/combo.js +256 -0
- package/dist/render/dispatch.d.ts +3 -0
- package/dist/render/dispatch.js +69 -0
- package/dist/render/gauge.d.ts +3 -0
- package/dist/render/gauge.js +216 -0
- package/dist/render/hbar-spec.d.ts +7 -0
- package/dist/render/hbar-spec.js +265 -0
- package/dist/render/hbar.d.ts +3 -0
- package/dist/render/hbar.js +55 -0
- package/dist/render/line-multi.d.ts +3 -0
- package/dist/render/line-multi.js +89 -0
- package/dist/render/line.d.ts +11 -0
- package/dist/render/line.js +123 -0
- package/dist/render/measure.d.ts +60 -0
- package/dist/render/measure.js +75 -0
- package/dist/render/pie.d.ts +5 -0
- package/dist/render/pie.js +439 -0
- package/dist/render/qr.d.ts +8 -0
- package/dist/render/qr.js +52 -0
- package/dist/render/query.d.ts +14 -0
- package/dist/render/query.js +220 -0
- package/dist/render/radar.d.ts +5 -0
- package/dist/render/radar.js +221 -0
- package/dist/render/sankey.d.ts +3 -0
- package/dist/render/sankey.js +280 -0
- package/dist/render/scatter.d.ts +3 -0
- package/dist/render/scatter.js +98 -0
- package/dist/render/svg.d.ts +93 -0
- package/dist/render/svg.js +471 -0
- package/dist/specs/base.d.ts +34 -0
- package/dist/specs/base.js +159 -0
- package/dist/specs/combo.d.ts +15 -0
- package/dist/specs/combo.js +135 -0
- package/dist/specs/csv.d.ts +1 -0
- package/dist/specs/csv.js +40 -0
- package/dist/specs/gauge.d.ts +15 -0
- package/dist/specs/gauge.js +108 -0
- package/dist/specs/index.d.ts +8 -0
- package/dist/specs/index.js +9 -0
- package/dist/specs/input.d.ts +11 -0
- package/dist/specs/input.js +33 -0
- package/dist/specs/multi.d.ts +12 -0
- package/dist/specs/multi.js +112 -0
- package/dist/specs/sankey.d.ts +13 -0
- package/dist/specs/sankey.js +112 -0
- package/dist/specs/scatter.d.ts +20 -0
- package/dist/specs/scatter.js +121 -0
- package/dist/store.d.ts +15 -0
- package/dist/store.js +121 -0
- package/dist/styles/area.d.ts +2 -0
- package/dist/styles/area.js +106 -0
- package/dist/styles/bar.d.ts +2 -0
- package/dist/styles/bar.js +52 -0
- package/dist/styles/combo.d.ts +2 -0
- package/dist/styles/combo.js +37 -0
- package/dist/styles/common.d.ts +34 -0
- package/dist/styles/common.js +259 -0
- package/dist/styles/gauge.d.ts +2 -0
- package/dist/styles/gauge.js +51 -0
- package/dist/styles/handlee.d.ts +1 -0
- package/dist/styles/handlee.js +2 -0
- package/dist/styles/hbar.d.ts +2 -0
- package/dist/styles/hbar.js +66 -0
- package/dist/styles/index.d.ts +7 -0
- package/dist/styles/index.js +79 -0
- package/dist/styles/inter.d.ts +3 -0
- package/dist/styles/inter.js +10 -0
- package/dist/styles/line.d.ts +2 -0
- package/dist/styles/line.js +97 -0
- package/dist/styles/pie.d.ts +2 -0
- package/dist/styles/pie.js +163 -0
- package/dist/styles/qr.d.ts +2 -0
- package/dist/styles/qr.js +12 -0
- package/dist/styles/radar.d.ts +2 -0
- package/dist/styles/radar.js +50 -0
- package/dist/styles/sankey.d.ts +2 -0
- package/dist/styles/sankey.js +29 -0
- package/dist/styles/scatter.d.ts +2 -0
- package/dist/styles/scatter.js +21 -0
- package/dist/styles/types.d.ts +105 -0
- package/dist/styles/types.js +70 -0
- package/dist/theme.d.ts +3 -0
- package/dist/theme.js +11 -0
- package/dist/types.d.ts +17 -0
- package/dist/types.js +1 -0
- package/dist/version.d.ts +6 -0
- package/dist/version.js +16 -0
- package/package.json +53 -4
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
import { card, TAB } from '../theme.js';
|
|
2
|
+
import { esc, fmtFull, fmtNum, r2, textW, trunc, svgDoc, editorialTitle, titleBlock, sVar, onVar, LabelPlacer, } from './svg.js';
|
|
3
|
+
import { radialFit } from './measure.js';
|
|
4
|
+
const MAX_SLICES = 9; // 8 个分类 + "Other"
|
|
5
|
+
function buildRows(data, seriesCount) {
|
|
6
|
+
// 按数值排名分配颜色:最大占比取最深/主色(色板不足时循环)
|
|
7
|
+
const rank = new Map(data.map((d) => d.value).sort((a, b) => b - a).map((v, i) => [v, i]));
|
|
8
|
+
const colorOf = (v, fallbackIdx) => (rank.get(v) ?? fallbackIdx) % Math.max(seriesCount, 1);
|
|
9
|
+
if (data.length <= MAX_SLICES) {
|
|
10
|
+
return data.map((d, i) => ({ ...d, colorIdx: colorOf(d.value, i) }));
|
|
11
|
+
}
|
|
12
|
+
const sorted = data.map((d, i) => ({ ...d, i })).sort((a, b) => b.value - a.value);
|
|
13
|
+
const keep = new Set(sorted.slice(0, MAX_SLICES - 1).map((x) => x.i));
|
|
14
|
+
const rows = data
|
|
15
|
+
.filter((_, i) => keep.has(i))
|
|
16
|
+
.map((d, i) => ({ ...d, colorIdx: colorOf(d.value, i) }));
|
|
17
|
+
const rest = data.filter((_, i) => !keep.has(i)).reduce((s, d) => s + d.value, 0);
|
|
18
|
+
rows.push({ label: 'Other', value: rest, colorIdx: -1 });
|
|
19
|
+
return rows;
|
|
20
|
+
}
|
|
21
|
+
const rowColor = (i) => (i === -1 ? 'var(--d-faint)' : sVar(i));
|
|
22
|
+
const rowOn = (i) => (i === -1 ? 'var(--d-ink)' : onVar(i));
|
|
23
|
+
// ECharts 极坐标(0=3 点钟,正角逆时针);圆角扇区恒 a0→a1 逆时针,
|
|
24
|
+
// 外弧两端切线 fillet(Evil 系签名),角半径受跨距/环宽约束。
|
|
25
|
+
export function pol(cx, cy, rad, a) {
|
|
26
|
+
return [cx + rad * Math.cos(a), cy - rad * Math.sin(a)];
|
|
27
|
+
}
|
|
28
|
+
const PT = (p) => `${r2(p[0])} ${r2(p[1])}`;
|
|
29
|
+
function filletArc(from, fc, to, c) {
|
|
30
|
+
const a0 = Math.atan2(from[1] - fc[1], from[0] - fc[0]);
|
|
31
|
+
const a1 = Math.atan2(to[1] - fc[1], to[0] - fc[0]);
|
|
32
|
+
let d = a1 - a0;
|
|
33
|
+
while (d > Math.PI)
|
|
34
|
+
d -= Math.PI * 2;
|
|
35
|
+
while (d < -Math.PI)
|
|
36
|
+
d += Math.PI * 2;
|
|
37
|
+
return `A${r2(c)} ${r2(c)} 0 ${Math.abs(d) > Math.PI ? 1 : 0} ${d > 0 ? 1 : 0} ${PT(to)}`;
|
|
38
|
+
}
|
|
39
|
+
export function segPath(cx, cy, R, r, a0, a1, corner) {
|
|
40
|
+
let sweep = a1 - a0;
|
|
41
|
+
if (sweep < 0)
|
|
42
|
+
sweep += Math.PI * 2;
|
|
43
|
+
if (sweep <= 0.0009)
|
|
44
|
+
return '';
|
|
45
|
+
const end = a0 + sweep;
|
|
46
|
+
if (r < 0.5)
|
|
47
|
+
r = 0;
|
|
48
|
+
const half = sweep / 2;
|
|
49
|
+
const maxCr = Math.min((R - r) / 2.2, (r > 0 ? r : R * 0.4) * Math.tan(Math.min(half, 1.2)) * 0.85, R * Math.tan(Math.min(half, 1.2)) * 0.85);
|
|
50
|
+
const c = Math.max(0, Math.min(corner, maxCr));
|
|
51
|
+
const large = sweep > Math.PI ? 1 : 0;
|
|
52
|
+
if (c < 0.45) {
|
|
53
|
+
if (!r)
|
|
54
|
+
return `M${PT(pol(cx, cy, R, a0))}A${r2(R)} ${r2(R)} 0 ${large} 0 ${PT(pol(cx, cy, R, end))}L${r2(cx)} ${r2(cy)}Z`;
|
|
55
|
+
return (`M${PT(pol(cx, cy, R, a0))}A${r2(R)} ${r2(R)} 0 ${large} 0 ${PT(pol(cx, cy, R, end))}` +
|
|
56
|
+
`L${PT(pol(cx, cy, r, end))}A${r2(r)} ${r2(r)} 0 ${large} 1 ${PT(pol(cx, cy, r, a0))}Z`);
|
|
57
|
+
}
|
|
58
|
+
const ao = Math.min(Math.asin(Math.min(0.999, c / Math.max(R - c, 0.001))), half * 0.9);
|
|
59
|
+
const rOJ = (R - c) * Math.cos(ao);
|
|
60
|
+
const osR = pol(cx, cy, rOJ, a0);
|
|
61
|
+
const osO = pol(cx, cy, R, a0 + ao);
|
|
62
|
+
const osC = pol(cx, cy, R - c, a0 + ao);
|
|
63
|
+
const oeO = pol(cx, cy, R, end - ao);
|
|
64
|
+
const oeR = pol(cx, cy, rOJ, end);
|
|
65
|
+
const oeC = pol(cx, cy, R - c, end - ao);
|
|
66
|
+
if (!r) {
|
|
67
|
+
return (`M${PT(osR)}${filletArc(osR, osC, osO, c)}A${r2(R)} ${r2(R)} 0 ${large} 0 ${PT(oeO)}` +
|
|
68
|
+
`${filletArc(oeO, oeC, oeR, c)}L${r2(cx)} ${r2(cy)}Z`);
|
|
69
|
+
}
|
|
70
|
+
const ai = Math.min(Math.asin(Math.min(0.999, c / (r + c))), half * 0.9);
|
|
71
|
+
const rIJ = (r + c) * Math.cos(ai);
|
|
72
|
+
const ieR = pol(cx, cy, rIJ, end);
|
|
73
|
+
const ieI = pol(cx, cy, r, end - ai);
|
|
74
|
+
const ieC = pol(cx, cy, r + c, end - ai);
|
|
75
|
+
const isI = pol(cx, cy, r, a0 + ai);
|
|
76
|
+
const isR = pol(cx, cy, rIJ, a0);
|
|
77
|
+
const isC = pol(cx, cy, r + c, a0 + ai);
|
|
78
|
+
return (`M${PT(osR)}${filletArc(osR, osC, osO, c)}A${r2(R)} ${r2(R)} 0 ${large} 0 ${PT(oeO)}` +
|
|
79
|
+
`${filletArc(oeO, oeC, oeR, c)}L${PT(ieR)}${filletArc(ieR, ieC, ieI, c)}` +
|
|
80
|
+
`A${r2(r)} ${r2(r)} 0 ${large} 1 ${PT(isI)}${filletArc(isI, isC, isR, c)}Z`);
|
|
81
|
+
}
|
|
82
|
+
const HALO_ATTRS = 'paint-order="stroke" stroke="var(--d-halo)" stroke-width="3" stroke-linejoin="round"';
|
|
83
|
+
export function renderPie(spec, o) {
|
|
84
|
+
const { width, height, version, skin } = o;
|
|
85
|
+
const rules = skin.rules;
|
|
86
|
+
const total = spec.data.reduce((s, d) => s + d.value, 0);
|
|
87
|
+
const rows = buildRows(spec.data, skin.series.length);
|
|
88
|
+
const mode = rules.pieLabels;
|
|
89
|
+
const editorial = rules.title === 'editorial' && !!spec.title;
|
|
90
|
+
const plain = rules.canvas === 'plain';
|
|
91
|
+
const pad = 24;
|
|
92
|
+
// 点状进度环(progress):每行数据一个环
|
|
93
|
+
if (mode === 'dots')
|
|
94
|
+
return renderDots(spec, o, rows, total, plain, pad);
|
|
95
|
+
// 标题区
|
|
96
|
+
let headSvg = '';
|
|
97
|
+
let headH = 0;
|
|
98
|
+
if (editorial) {
|
|
99
|
+
const t = editorialTitle(spec.title, width, pad);
|
|
100
|
+
headSvg = t.svg;
|
|
101
|
+
headH = t.height + 8;
|
|
102
|
+
}
|
|
103
|
+
else if (spec.title) {
|
|
104
|
+
headSvg = titleBlock(spec.title, pad);
|
|
105
|
+
headH = 26;
|
|
106
|
+
}
|
|
107
|
+
// 图例预留区
|
|
108
|
+
const legendBottom = rules.legend === 'bottom';
|
|
109
|
+
const legendRight = rules.legend === 'right';
|
|
110
|
+
const legendH = legendBottom ? (rules.legendCols > 1 ? Math.ceil(rows.length / rules.legendCols) * 26 + 26 : 36) : 0;
|
|
111
|
+
const legendW = legendRight ? 170 : 0;
|
|
112
|
+
// 环几何:规范实测 R/h 比例封顶(见 specR 表)
|
|
113
|
+
const specR = { onSlice: 140.6 / 500, insideValue: 168 / 520, outside: 148 / 520, insidePct: 168 / 520, legend: 188 / 520 };
|
|
114
|
+
const availW = width - pad * 2 - legendW;
|
|
115
|
+
const availH = height - pad * 2 - headH - legendH;
|
|
116
|
+
// outside 外标:走 radialFit 保图优先降级,穷尽则整层不标
|
|
117
|
+
const outLabelW = (cut) => Math.max(...rows.map((row) => textW(`${trunc(row.label, cut)} ${fmtFull(row.value)}`, 12)), 0);
|
|
118
|
+
const Rfit = Math.min(availW, availH) / 2 - (mode === 'onSlice' || mode === 'outside' ? 14 : 4);
|
|
119
|
+
const cx = legendRight ? pad + availW / 2 : width / 2;
|
|
120
|
+
const Rbase = plain && specR[mode] ? Math.min(Rfit, specR[mode] * height) : Rfit;
|
|
121
|
+
const fit = mode === 'outside'
|
|
122
|
+
? radialFit({ natural: Rbase, budget: Math.min(cx, width - cx) - pad, lead: 46, minLead: 26, labelW: outLabelW, maxCut: 12, minCut: 4, floorK: 0.55, overflow: 'drop' })
|
|
123
|
+
: null;
|
|
124
|
+
const outCut = fit?.cut ?? 12;
|
|
125
|
+
const lead = fit?.lead ?? 46; // 肘点 22 + 横段 16 + 间距 8
|
|
126
|
+
const outDrop = fit?.drop ?? false;
|
|
127
|
+
const R = fit && !fit.drop ? fit.R : Rbase;
|
|
128
|
+
const cy = pad + headH + availH / 2;
|
|
129
|
+
const donut = o.donut !== false; // donut=0 强制实心饼
|
|
130
|
+
const r = donut && rules.pieRadius > 0 ? R * rules.pieRadius : 0;
|
|
131
|
+
const parts = [card(width, height, plain)];
|
|
132
|
+
const gap = rules.pieSliceGap;
|
|
133
|
+
// 渐变扇区 defs(revenue)
|
|
134
|
+
if (rules.pieGradient) {
|
|
135
|
+
const defs = rows.map((row, i) => {
|
|
136
|
+
const a = row.colorIdx === -1 ? 0 : (row.colorIdx * 2) % skin.series.length;
|
|
137
|
+
const b = row.colorIdx === -1 ? 0 : (row.colorIdx * 2 + 1) % skin.series.length;
|
|
138
|
+
return (`<linearGradient id="dgs-${i}" x1="0" y1="0" x2="1" y2="1">` +
|
|
139
|
+
`<stop stop-color="${sVar(a)}"/><stop offset="1" stop-color="${sVar(b)}"/></linearGradient>`);
|
|
140
|
+
}).join('');
|
|
141
|
+
parts.push(`<defs>${defs}</defs>`);
|
|
142
|
+
}
|
|
143
|
+
const sliceFill = (i) => (rules.pieGradient ? `url(#dgs-${i})` : rowColor(rows[i].colorIdx));
|
|
144
|
+
// 扇区方向/起始角:规范 Evil 系 = 逆时针(ECharts 度),OpenData = 顺时针自 12 点钟
|
|
145
|
+
const startRad = (rules.pieStart * Math.PI) / 180;
|
|
146
|
+
const dirSign = rules.pieDir === 'ccw' ? 1 : -1;
|
|
147
|
+
const draw = rules.pieOrder === 'reverse' ? [...rows].reverse() : rows;
|
|
148
|
+
const midAngle = (row, before) => startRad + dirSign * (before + row.value / total / 2) * Math.PI * 2;
|
|
149
|
+
// 扇区
|
|
150
|
+
if (rows.length === 1) {
|
|
151
|
+
const stroke = gap > 0 ? ` stroke="var(--d-halo)" stroke-width="${gap}"` : '';
|
|
152
|
+
if (r > 0) {
|
|
153
|
+
parts.push(`<circle cx="${r2(cx)}" cy="${r2(cy)}" r="${r2((R + r) / 2)}" fill="none" stroke="${sliceFill(0)}" stroke-width="${r2(R - r - Math.max(gap, 0))}"/>`);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
parts.push(`<circle cx="${r2(cx)}" cy="${r2(cy)}" r="${r2(R)}" fill="${sliceFill(0)}"${stroke}/>`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
const twoPi = Math.PI * 2;
|
|
161
|
+
let cum = 0;
|
|
162
|
+
draw.forEach((row) => {
|
|
163
|
+
const frac = row.value / total;
|
|
164
|
+
// 起止角统一传 (小, 大):(大, 小) 会让 >180° 的扇区翻成小弧
|
|
165
|
+
const aA = startRad + dirSign * cum * twoPi;
|
|
166
|
+
const aB = startRad + dirSign * (cum + frac) * twoPi;
|
|
167
|
+
const lo = Math.min(aA, aB);
|
|
168
|
+
const hi = Math.max(aA, aB);
|
|
169
|
+
cum += frac;
|
|
170
|
+
const d = segPath(cx, cy, R, r, lo, hi, rules.pieCorner);
|
|
171
|
+
if (d) {
|
|
172
|
+
const fill = rules.pieGradient ? `url(#dgs-${rows.indexOf(row)})` : rowColor(row.colorIdx);
|
|
173
|
+
const stroke = gap > 0 ? ` stroke="var(--d-halo)" stroke-width="${gap}" stroke-linejoin="round" stroke-linecap="round"` : '';
|
|
174
|
+
parts.push(`<path d="${d}" fill="${fill}"${stroke}/>`);
|
|
175
|
+
}
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
// 标注
|
|
179
|
+
if (mode === 'onSlice') {
|
|
180
|
+
// OpenData:标签圈外;相撞时引线外标(斜出+横段)
|
|
181
|
+
let cum = 0;
|
|
182
|
+
const placedBoxes = [];
|
|
183
|
+
const out = [];
|
|
184
|
+
const h = 13;
|
|
185
|
+
// 就近找空位:先原位,再上下逐行向外;纵向钳制在画布内
|
|
186
|
+
const freeY = (x0, w, want) => {
|
|
187
|
+
const clash = (y) => placedBoxes.some((b) => x0 < b.x0 + b.w + 6 && x0 + w + 6 > b.x0 && y - h < b.y0 + b.h + 3 && y > b.y0 - 3);
|
|
188
|
+
const step = h + 4;
|
|
189
|
+
const yMin = pad + headH + 2;
|
|
190
|
+
const yMax = height - pad - legendH - h - 2;
|
|
191
|
+
for (let k = 0; k <= draw.length * 3; k++) {
|
|
192
|
+
const down = want + k * step;
|
|
193
|
+
const up = want - k * step;
|
|
194
|
+
if (down <= yMax && !clash(down))
|
|
195
|
+
return down;
|
|
196
|
+
if (up >= yMin && !clash(up))
|
|
197
|
+
return up;
|
|
198
|
+
}
|
|
199
|
+
return null; // 穷尽仍无空位:放弃该标签(绝不重叠优先于全可见)
|
|
200
|
+
};
|
|
201
|
+
for (const row of draw) {
|
|
202
|
+
const frac = row.value / total;
|
|
203
|
+
const a = midAngle(row, cum);
|
|
204
|
+
cum += frac;
|
|
205
|
+
const text = `${trunc(row.label, 12)} ${Math.round(frac * 100)}%`;
|
|
206
|
+
const w = textW(text, 11.5);
|
|
207
|
+
const [lx0, ly0] = pol(cx, cy, R + 10, a);
|
|
208
|
+
const anchor = Math.cos(a) > 0.25 ? 'start' : Math.cos(a) < -0.25 ? 'end' : 'middle';
|
|
209
|
+
const ax = anchor === 'end' ? lx0 - w : anchor === 'start' ? lx0 : lx0 - w / 2;
|
|
210
|
+
const lyNat = freeY(ax, w, ly0);
|
|
211
|
+
if (lyNat === null) {
|
|
212
|
+
placedBoxes.push({ x0: ax, y0: ly0 - h, w, h });
|
|
213
|
+
continue;
|
|
214
|
+
} // 放弃但占位,防后续再挤此区
|
|
215
|
+
const inCanvasX = ax >= pad - 2 && ax + w <= width - pad + 2;
|
|
216
|
+
if (Math.abs(lyNat - ly0) <= 6 && inCanvasX) {
|
|
217
|
+
// 自然位置可用:浮标签
|
|
218
|
+
placedBoxes.push({ x0: ax, y0: lyNat - h, w, h });
|
|
219
|
+
out.push(`<text x="${r2(lx0)}" y="${r2(lyNat + 4)}" text-anchor="${anchor}" font-size="11.5" font-weight="500" fill="var(--d-ink)" ${HALO_ATTRS}>${esc(text)}</text>`);
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
// 引线外标:径向外移,相撞时小幅垂直错开
|
|
223
|
+
const side = Math.cos(a) >= 0 ? 1 : -1;
|
|
224
|
+
const [sx, sy] = pol(cx, cy, R + 2, a);
|
|
225
|
+
const [ex, ey] = pol(cx, cy, R + 24, a); // 肘点:同角度外移
|
|
226
|
+
const endX = ex + side * 10; // 短横段
|
|
227
|
+
const tx = endX + side * 6;
|
|
228
|
+
const ax2 = side > 0 ? tx : tx - w;
|
|
229
|
+
const lyNear = freeY(ax2, w, ey - h / 2); // 基线取文字盒中线,就近搜索
|
|
230
|
+
if (lyNear === null || ax2 < pad - 2 || ax2 + w > width - pad + 2) {
|
|
231
|
+
placedBoxes.push({ x0: ax2, y0: ey - h / 2 - h, w, h });
|
|
232
|
+
continue;
|
|
233
|
+
} // 穷尽无空位/出画布:放弃该标签但占位
|
|
234
|
+
out.push(`<path d="M${r2(sx)} ${r2(sy)}L${r2(ex)} ${r2(lyNear)}H${r2(endX)}" fill="none" stroke="var(--d-muted)" stroke-opacity="0.45" stroke-width="1"/>`);
|
|
235
|
+
out.push(`<text x="${r2(tx)}" y="${r2(lyNear + 4)}" text-anchor="${side > 0 ? 'start' : 'end'}" font-size="11.5" font-weight="500" fill="var(--d-ink)" ${HALO_ATTRS}>${esc(text)}</text>`);
|
|
236
|
+
placedBoxes.push({ x0: ax2, y0: lyNear - h, w, h });
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
parts.push(...out);
|
|
240
|
+
}
|
|
241
|
+
else if (mode === 'insideValue' || mode === 'insidePct') {
|
|
242
|
+
// 数值/占比内标:白字,全有或全无
|
|
243
|
+
const labelable = draw.every((row) => row.value / total >= 0.05);
|
|
244
|
+
if (labelable) {
|
|
245
|
+
let cum = 0;
|
|
246
|
+
for (const row of draw) {
|
|
247
|
+
const frac = row.value / total;
|
|
248
|
+
const a = midAngle(row, cum);
|
|
249
|
+
cum += frac;
|
|
250
|
+
const [tx, ty] = pol(cx, cy, (R + r) / 2, a);
|
|
251
|
+
const text = mode === 'insideValue' ? fmtNum(row.value) : `${Math.round(frac * 100)}%`;
|
|
252
|
+
parts.push(`<text x="${r2(tx)}" y="${r2(ty + 4)}" text-anchor="middle" font-size="12" font-weight="500" fill="var(--d-card)" style="${TAB}">${esc(text)}</text>`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
else if (mode === 'outside') {
|
|
257
|
+
// Evil 外标:引线 + 名灰值墨,全有或全无
|
|
258
|
+
let cum = 0;
|
|
259
|
+
const placer = new LabelPlacer();
|
|
260
|
+
const out = [];
|
|
261
|
+
let allPlaced = draw.every((row) => row.value / total >= 0.02);
|
|
262
|
+
if (allPlaced && !outDrop) {
|
|
263
|
+
const elbowR = R + (lead === 46 ? 22 : 14);
|
|
264
|
+
const leadSeg = lead === 46 ? 16 : 8;
|
|
265
|
+
const leadGap = lead === 46 ? 8 : 4;
|
|
266
|
+
for (const row of draw) {
|
|
267
|
+
const frac = row.value / total;
|
|
268
|
+
const a = midAngle(row, cum);
|
|
269
|
+
cum += frac;
|
|
270
|
+
const [sx, sy] = pol(cx, cy, R + 2, a);
|
|
271
|
+
const [elbowX, elbowY] = pol(cx, cy, elbowR, a);
|
|
272
|
+
const side = Math.cos(a) >= 0 ? 1 : -1;
|
|
273
|
+
const endX = elbowX + side * leadSeg;
|
|
274
|
+
const anchor = side > 0 ? 'start' : 'end';
|
|
275
|
+
const name = trunc(row.label, outCut);
|
|
276
|
+
const valText = fmtFull(row.value);
|
|
277
|
+
const w = textW(name + ' ' + valText, 12);
|
|
278
|
+
const tx = endX + side * leadGap;
|
|
279
|
+
const ax = side > 0 ? tx : tx - w;
|
|
280
|
+
// 兜底:标签盒越出画布(含引线纵向)→ 整层不标(R 已收窄仍放不下说明画布过窄)
|
|
281
|
+
if (ax < pad - 2 || ax + w > width - pad + 2 || elbowY - 6 < pad - 2 || elbowY + 6 > height - pad + 2) {
|
|
282
|
+
allPlaced = false;
|
|
283
|
+
break;
|
|
284
|
+
}
|
|
285
|
+
if (!placer.fits(ax, elbowY - 6, w, 12)) {
|
|
286
|
+
allPlaced = false;
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
out.push(`<path d="M${r2(sx)} ${r2(sy)}L${r2(elbowX)} ${r2(elbowY)}H${r2(endX)}" fill="none" stroke="var(--d-muted)" stroke-opacity="0.45" stroke-width="1"/>`);
|
|
290
|
+
out.push(`<text x="${r2(tx)}" y="${r2(elbowY + 4)}" text-anchor="${anchor}" font-size="12" font-weight="500"><tspan fill="var(--d-muted)">${esc(name)} </tspan><tspan fill="var(--d-ink)" style="${TAB}">${esc(valText)}</tspan></text>`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (allPlaced)
|
|
294
|
+
parts.push(...out);
|
|
295
|
+
}
|
|
296
|
+
// 虚线内圈(revenue)
|
|
297
|
+
if (rules.pieDashedInner && r > 0) {
|
|
298
|
+
parts.push(`<circle cx="${r2(cx)}" cy="${r2(cy)}" r="${r2(r * 0.78)}" fill="none" stroke="var(--d-border)" stroke-width="1.5" stroke-dasharray="4 5"/>`);
|
|
299
|
+
}
|
|
300
|
+
// 环心(total:market-mono/revenue)
|
|
301
|
+
if (r > 0 && rules.pieCenter === 'total') {
|
|
302
|
+
const fs = Math.min(34, Math.max(18, r * 0.42));
|
|
303
|
+
parts.push(`<text x="${r2(cx)}" y="${r2(cy + fs * 0.26)}" text-anchor="middle" font-size="${r2(fs)}" font-weight="600" letter-spacing="-0.03em" fill="var(--d-ink)" style="${TAB}">${fmtFull(total)}</text>`);
|
|
304
|
+
if (spec.unit) {
|
|
305
|
+
parts.push(`<text x="${r2(cx)}" y="${r2(cy + fs * 0.26 + 18)}" text-anchor="middle" font-size="11" fill="var(--d-muted)">${esc(trunc(spec.unit, 18))}</text>`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
// 图例
|
|
309
|
+
if (legendBottom) {
|
|
310
|
+
const legendTop = height - pad - legendH + 12;
|
|
311
|
+
if (rules.legendCols > 1) {
|
|
312
|
+
// 多列网格(market-mono):顶部细分隔线,值右对齐
|
|
313
|
+
const cols = rules.legendCols;
|
|
314
|
+
parts.push(`<line x1="${pad}" y1="${r2(legendTop - 12)}" x2="${width - pad}" y2="${r2(legendTop - 12)}" stroke="var(--d-border)" stroke-width="1"/>`);
|
|
315
|
+
const colW = (width - pad * 2) / cols;
|
|
316
|
+
rows.forEach((row, i) => {
|
|
317
|
+
const x = pad + (i % cols) * colW;
|
|
318
|
+
const y = legendTop + Math.floor(i / cols) * 26;
|
|
319
|
+
const pct = Math.round((row.value / total) * 100);
|
|
320
|
+
parts.push(`<rect x="${r2(x)}" y="${r2(y - 8)}" width="9" height="9" rx="2" fill="${rowColor(row.colorIdx)}"/>`);
|
|
321
|
+
parts.push(`<text x="${r2(x + 15)}" y="${r2(y)}" font-size="12" font-weight="500" fill="var(--d-ink)">${esc(trunc(row.label, 14))}</text>`);
|
|
322
|
+
if (rules.legendValue) {
|
|
323
|
+
parts.push(`<text x="${r2(x + colW - 8)}" y="${r2(y)}" text-anchor="end" font-size="12" style="${TAB}">` +
|
|
324
|
+
`<tspan fill="var(--d-muted)">${fmtFull(row.value)}</tspan>` +
|
|
325
|
+
`<tspan fill="var(--d-faint)"> (${pct}%)</tspan></text>`);
|
|
326
|
+
}
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
// 单行横排(evil):居中,放不下换到第二行
|
|
331
|
+
const items = rows.map((row) => {
|
|
332
|
+
const name = trunc(row.label, 14);
|
|
333
|
+
return { row, name, w: 15 + textW(name, 12) + 24 };
|
|
334
|
+
});
|
|
335
|
+
const total = items.reduce((s, it) => s + it.w, 0);
|
|
336
|
+
let x = pad + Math.max(0, (width - pad * 2 - total) / 2);
|
|
337
|
+
let y = legendTop;
|
|
338
|
+
for (const it of items) {
|
|
339
|
+
if (x + it.w > width - pad && x > pad) {
|
|
340
|
+
x = pad;
|
|
341
|
+
y += 24;
|
|
342
|
+
}
|
|
343
|
+
parts.push(`<rect x="${r2(x)}" y="${r2(y - 8)}" width="9" height="9" rx="2" fill="${rowColor(it.row.colorIdx)}"/>`);
|
|
344
|
+
parts.push(`<text x="${r2(x + 15)}" y="${r2(y)}" font-size="12" font-weight="500" fill="var(--d-muted)">${esc(it.name)}</text>`);
|
|
345
|
+
x += it.w;
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
else if (legendRight) {
|
|
350
|
+
const lx = cx + R + 28;
|
|
351
|
+
const legendTop = pad + headH + 16;
|
|
352
|
+
const legendAvail = height - pad - 8 - legendTop;
|
|
353
|
+
const rowStep = rows.length > 1 ? Math.min(28, legendAvail / (rows.length - 1)) : 28;
|
|
354
|
+
const firstY = legendTop + (legendAvail - rowStep * (rows.length - 1)) / 2;
|
|
355
|
+
rows.forEach((row, i) => {
|
|
356
|
+
const y = firstY + i * rowStep;
|
|
357
|
+
parts.push(`<rect x="${r2(lx)}" y="${r2(y - 8)}" width="10" height="10" rx="2" fill="${rowColor(row.colorIdx)}"/>`);
|
|
358
|
+
parts.push(`<text x="${r2(lx + 16)}" y="${r2(y)}" font-size="12" font-weight="500" fill="var(--d-muted)">${esc(trunc(row.label, 14))}</text>`);
|
|
359
|
+
if (rules.legendValue) {
|
|
360
|
+
parts.push(`<text x="${r2(width - pad)}" y="${r2(y)}" text-anchor="end" font-size="12" font-weight="500" fill="var(--d-ink)" style="${TAB}">${fmtFull(row.value)}</text>`);
|
|
361
|
+
}
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
return svgDoc(width, height, 'pie', version, spec.title ?? 'pie chart', spec, skin, headSvg + parts.join(''));
|
|
365
|
+
}
|
|
366
|
+
// 点状进度环(饼图 5 · progress):每行数据一个环,点阵表示占比,环心大百分比+说明
|
|
367
|
+
function renderDots(spec, o, rows, total, plain, pad) {
|
|
368
|
+
const { width, height, version, skin } = o;
|
|
369
|
+
const n = rows.length;
|
|
370
|
+
const parts = [card(width, height, plain)];
|
|
371
|
+
// 标题:kicker(unit 大写)+ 18px 标题;右侧总数备注
|
|
372
|
+
let headH = 0;
|
|
373
|
+
if (spec.title) {
|
|
374
|
+
if (spec.unit) {
|
|
375
|
+
parts.push(`<text x="${pad}" y="${pad + 10}" font-size="10" letter-spacing="0.08em" fill="var(--d-muted)" style="text-transform:uppercase">${esc(spec.unit.toUpperCase())}</text>`);
|
|
376
|
+
parts.push(`<text x="${pad}" y="${pad + 32}" font-size="18" font-weight="500" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(spec.title)}</text>`);
|
|
377
|
+
parts.push(`<text x="${width - pad}" y="${pad + 10}" text-anchor="end" font-size="12" fill="var(--d-muted)" style="${TAB}">${fmtFull(total)} ${esc(spec.unit)}</text>`);
|
|
378
|
+
headH = 48;
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
parts.push(titleBlock(spec.title, pad));
|
|
382
|
+
headH = 30;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
// 环阵:横向排布;点阵环带 0.885R,点径 0.035R,中心盘 0.782R(规范 pie-5 比例)
|
|
386
|
+
const cellW = (width - pad * 2) / n;
|
|
387
|
+
const cellH = height - pad - headH - 8;
|
|
388
|
+
const R = Math.min(cellW, cellH) / 2 - 18;
|
|
389
|
+
const dotR = Math.max(2.2, R * 0.035);
|
|
390
|
+
const ringR = R * 0.885;
|
|
391
|
+
const discR = R * 0.782;
|
|
392
|
+
const DOTS = 40; // 圆周点数(规范:40)
|
|
393
|
+
rows.forEach((row, i) => {
|
|
394
|
+
const frac = row.value / total;
|
|
395
|
+
const cx = pad + cellW * i + cellW / 2;
|
|
396
|
+
const cy = headH + 8 + cellH / 2;
|
|
397
|
+
const lit = Math.round(frac * DOTS);
|
|
398
|
+
for (let d = 0; d < DOTS; d++) {
|
|
399
|
+
const a = Math.PI / 2 + ((d + 0.25) / DOTS) * Math.PI * 2; // 自 12 点钟逆时针点亮
|
|
400
|
+
const [dx, dy] = pol(cx, cy, ringR, a);
|
|
401
|
+
parts.push(`<circle cx="${r2(dx)}" cy="${r2(dy)}" r="${r2(dotR)}" fill="${d < lit ? 'var(--d-s1)' : 'var(--d-faint)'}"/>`);
|
|
402
|
+
}
|
|
403
|
+
parts.push(`<circle cx="${r2(cx)}" cy="${r2(cy)}" r="${r2(discR)}" fill="var(--d-grid)"/>`);
|
|
404
|
+
const fs = Math.min(44, Math.max(20, R * 0.3));
|
|
405
|
+
parts.push(`<text x="${r2(cx)}" y="${r2(cy - 4)}" text-anchor="middle" font-size="${r2(fs)}" font-weight="600" letter-spacing="-0.03em" fill="var(--d-ink)" style="${TAB}">${Math.round(frac * 100)}%</text>`);
|
|
406
|
+
// 说明:按词拆至多两行,放不下省略号
|
|
407
|
+
const maxW = discR * 1.72;
|
|
408
|
+
const capAtoms = row.label.match(/[A-Za-z0-9'’\-]+\s*|[^\s]/g) ?? [...row.label];
|
|
409
|
+
const capLines = [];
|
|
410
|
+
let capCur = '';
|
|
411
|
+
let capTrunc = false;
|
|
412
|
+
for (const atom of capAtoms) {
|
|
413
|
+
if (textW(capCur + atom, 12) <= maxW) {
|
|
414
|
+
capCur += atom;
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
if (capCur.trim())
|
|
418
|
+
capLines.push(capCur.trimEnd());
|
|
419
|
+
capCur = atom.trimStart();
|
|
420
|
+
if (capLines.length === 2) {
|
|
421
|
+
capTrunc = true;
|
|
422
|
+
capCur = '';
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (capCur.trim() && capLines.length < 2)
|
|
427
|
+
capLines.push(capCur.trimEnd());
|
|
428
|
+
if (capTrunc && capLines.length) {
|
|
429
|
+
let last = capLines[capLines.length - 1];
|
|
430
|
+
while (last.length > 1 && textW(last + '…', 12) > maxW)
|
|
431
|
+
last = last.slice(0, -1);
|
|
432
|
+
capLines[capLines.length - 1] = last + '…';
|
|
433
|
+
}
|
|
434
|
+
capLines.forEach((l, li) => {
|
|
435
|
+
parts.push(`<text x="${r2(cx)}" y="${r2(cy + 16 + li * 16)}" text-anchor="middle" font-size="12" fill="var(--d-muted)">${esc(l)}</text>`);
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
return svgDoc(width, height, 'pie', version, spec.title ?? 'progress rings', spec, skin, parts.join(''));
|
|
439
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Skin } from '../styles/types.js';
|
|
2
|
+
export declare function parseColor(raw: string | null, fallback: string, name: string): string;
|
|
3
|
+
export declare const QR_DEFAULTS: {
|
|
4
|
+
readonly fg: "var(--d-s1)";
|
|
5
|
+
readonly bg: "var(--d-card)";
|
|
6
|
+
};
|
|
7
|
+
export declare function renderQR(text: string, fg: string, bg: string, width: number, height: number, version: number, skin: Skin, caption?: string, subtitle?: string): string;
|
|
8
|
+
export declare function requireQRText(raw: string | null): string;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// 二维码(uqr 生成,规范 qr-1 卡片样式):白卡静区 rx18 + 圆角模块 + 标题/副标题。
|
|
2
|
+
import { encode } from 'uqr';
|
|
3
|
+
import { ApiError } from '../specs/index.js';
|
|
4
|
+
import { r2, esc, svgDoc } from './svg.js';
|
|
5
|
+
const HEX_RE = /^#?[0-9a-fA-F]{6}$/;
|
|
6
|
+
export function parseColor(raw, fallback, name) {
|
|
7
|
+
if (raw === null || raw === '')
|
|
8
|
+
return fallback;
|
|
9
|
+
if (!HEX_RE.test(raw)) {
|
|
10
|
+
throw new ApiError(400, `"${name}" must be a 6-digit hex color like %23123456 (note: "#" must be URL-encoded), got ${JSON.stringify(raw)}`);
|
|
11
|
+
}
|
|
12
|
+
return '#' + raw.replace('#', '').toLowerCase();
|
|
13
|
+
}
|
|
14
|
+
// 默认配色为 var 引用,随皮肤明暗自动切换;显式 fg/bg 参数则烘焙固定色
|
|
15
|
+
export const QR_DEFAULTS = { fg: 'var(--d-s1)', bg: 'var(--d-card)' };
|
|
16
|
+
const MAX_QR_TEXT = 1000;
|
|
17
|
+
export function renderQR(text, fg, bg, width, height, version, skin, caption, subtitle) {
|
|
18
|
+
const qr = encode(text, { ecc: 'M', border: 0 });
|
|
19
|
+
const n = qr.size;
|
|
20
|
+
// 几何(规范 920×490 实测):码区 ~0.71h 高,顶部起 0.086h,卡面四周留 24,卡下标题行
|
|
21
|
+
const ms = Math.min((height * 0.71) / n, (width * 0.42) / n);
|
|
22
|
+
const qrW = n * ms;
|
|
23
|
+
const x0 = width / 2 - qrW / 2;
|
|
24
|
+
const hasCaption = !!caption || !!subtitle;
|
|
25
|
+
const y0 = hasCaption ? height * 0.086 : (height - qrW) / 2;
|
|
26
|
+
const pad = Math.max(16, height * 0.049);
|
|
27
|
+
const parts = [`<rect x="${r2(x0 - pad)}" y="${r2(y0 - pad)}" width="${r2(qrW + pad * 2)}" height="${r2(qrW + pad * 2)}" rx="${r2(height * 0.0367)}" fill="${bg}"/>`];
|
|
28
|
+
for (let row = 0; row < n; row++) {
|
|
29
|
+
for (let col = 0; col < n; col++) {
|
|
30
|
+
if (qr.data[row][col]) {
|
|
31
|
+
parts.push(`<rect x="${r2(x0 + col * ms)}" y="${r2(y0 + row * ms)}" width="${r2(ms)}" height="${r2(ms)}" rx="${r2(ms * 0.125)}" fill="${fg}"/>`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// 卡下标题(粗 17px)+ 副标题(灰 12px)
|
|
36
|
+
if (caption) {
|
|
37
|
+
parts.push(`<text x="${r2(width / 2)}" y="${r2(height * 0.902)}" text-anchor="middle" font-size="${r2(height * 0.0347)}" font-weight="700" fill="var(--d-ink)">${esc(caption)}</text>`);
|
|
38
|
+
}
|
|
39
|
+
if (subtitle) {
|
|
40
|
+
parts.push(`<text x="${r2(width / 2)}" y="${r2(height * 0.951)}" text-anchor="middle" font-size="12" fill="var(--d-muted)">${esc(subtitle)}</text>`);
|
|
41
|
+
}
|
|
42
|
+
return svgDoc(width, height, 'qr', version, 'QR code', { text }, skin, parts.join(''));
|
|
43
|
+
}
|
|
44
|
+
export function requireQRText(raw) {
|
|
45
|
+
if (raw === null || raw === '') {
|
|
46
|
+
throw new ApiError(400, 'Missing required query parameter "d" (the text to encode). Example: /draw?type=qr&d=hello');
|
|
47
|
+
}
|
|
48
|
+
if (raw.length > MAX_QR_TEXT) {
|
|
49
|
+
throw new ApiError(400, `"d" for QR is limited to ${MAX_QR_TEXT} characters, got ${raw.length}`);
|
|
50
|
+
}
|
|
51
|
+
return raw;
|
|
52
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Scheme } from '../styles/index.js';
|
|
2
|
+
import { type StoredChart } from '../store.js';
|
|
3
|
+
export interface DrawResult {
|
|
4
|
+
svg: string;
|
|
5
|
+
version: number;
|
|
6
|
+
cacheControl: string;
|
|
7
|
+
}
|
|
8
|
+
export declare function valueLabelsParam(params: URLSearchParams): 'all' | 'peak' | 'none' | undefined;
|
|
9
|
+
export declare function clampInt(params: URLSearchParams, key: string, def: number, min: number, max: number): number;
|
|
10
|
+
export declare function stackParam(params: URLSearchParams): boolean;
|
|
11
|
+
export declare function schemeParam(params: URLSearchParams): Scheme;
|
|
12
|
+
export declare function boolParam(params: URLSearchParams, key: string): boolean | undefined;
|
|
13
|
+
export declare function renderFromUrl(url: URL): DrawResult;
|
|
14
|
+
export declare function renderStoredFromUrl(doc: StoredChart, url: URL): DrawResult;
|