@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.
- package/LICENSE +228 -0
- package/README.md +224 -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 +76 -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 +47 -4
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
// 底部居中图例行:单行放不下自动换行(每行独立居中),窄画布多环不溢出
|
|
2
|
+
function pushLegendRow(parts, items, cx, ly0, width, advance, render) {
|
|
3
|
+
const avail = width - 24;
|
|
4
|
+
const rows = [[]];
|
|
5
|
+
let acc = -26;
|
|
6
|
+
for (const it of items) {
|
|
7
|
+
const adv = advance(it);
|
|
8
|
+
if (acc + adv > avail && rows[rows.length - 1].length > 0) {
|
|
9
|
+
rows.push([]);
|
|
10
|
+
acc = -26;
|
|
11
|
+
}
|
|
12
|
+
rows[rows.length - 1].push(it);
|
|
13
|
+
acc += adv;
|
|
14
|
+
}
|
|
15
|
+
rows.forEach((row, ri) => {
|
|
16
|
+
const rowW = row.reduce((sm, it) => sm + advance(it), -26);
|
|
17
|
+
let lx = cx - rowW / 2;
|
|
18
|
+
const ly = ly0 + ri * 18;
|
|
19
|
+
for (const it of row) {
|
|
20
|
+
render(lx, ly, it);
|
|
21
|
+
lx += advance(it);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
// 仪表盘:规范 gauge-1 半圆多环(默认)/ gauge-2 渐变整圆多环 / reliability 分区环。
|
|
26
|
+
import { ApiError } from '../specs/index.js';
|
|
27
|
+
import { TAB, card } from '../theme.js';
|
|
28
|
+
import { esc, fmtFull, fmtNum, r2, textW, trunc, svgDoc, titleBlock, sVar } from './svg.js';
|
|
29
|
+
import { segPath, pol } from './pie.js';
|
|
30
|
+
import { C_gaugeGradient } from '../styles/common.js';
|
|
31
|
+
export function renderGauge(spec, o) {
|
|
32
|
+
const { width, height, version, skin } = o;
|
|
33
|
+
if (skin.rules.gauge === 'zones')
|
|
34
|
+
return renderZoneGauge(spec, o);
|
|
35
|
+
if (skin.rules.gauge === 'rings')
|
|
36
|
+
return renderGaugeRings(spec, o);
|
|
37
|
+
return renderGaugeSemi(spec, o);
|
|
38
|
+
}
|
|
39
|
+
// 环数据:data 多行,或单值 value/max 折算为一环
|
|
40
|
+
function gaugeRows(spec) {
|
|
41
|
+
if (spec.data?.length)
|
|
42
|
+
return spec.data.map((d) => ({ label: d.label, pct: Math.max(0, Math.min(100, d.value)) }));
|
|
43
|
+
const pct = spec.max > 0 && spec.value !== undefined ? (spec.value / spec.max) * 100 : 0;
|
|
44
|
+
return [{ label: undefined, pct: Math.max(0, Math.min(100, pct)) }];
|
|
45
|
+
}
|
|
46
|
+
// 仪表盘 1 · 半圆多环(规范 gauge-1,860×480):
|
|
47
|
+
// 同心半圆弧(自左向右扫过上半圆),每行数据一环,轨道浅色+值弧系列色,底部图例
|
|
48
|
+
function renderGaugeSemi(spec, o) {
|
|
49
|
+
const { width, height, version, skin } = o;
|
|
50
|
+
const rows = gaugeRows(spec);
|
|
51
|
+
const n = rows.length;
|
|
52
|
+
// 几何(规范 860×480 实测):心 (430,340),首环 r=92,环距 34,弧宽 20。
|
|
53
|
+
// 半圆弧横向至 cx±r、纵向至 cy-r:画布窄/环多时按比例整体收缩,最外环不出画布
|
|
54
|
+
const cx = width / 2;
|
|
55
|
+
const cy = height * 0.7083;
|
|
56
|
+
let sw = height * 0.0417;
|
|
57
|
+
let r0 = height * 0.1917;
|
|
58
|
+
let pitch = height * 0.0708;
|
|
59
|
+
const rOut = r0 + (n - 1) * pitch + sw / 2;
|
|
60
|
+
const rLim = Math.min(cx, cy) - 8;
|
|
61
|
+
if (rOut > rLim) {
|
|
62
|
+
const k = rLim / rOut;
|
|
63
|
+
sw *= k;
|
|
64
|
+
r0 *= k;
|
|
65
|
+
pitch *= k;
|
|
66
|
+
}
|
|
67
|
+
const arc = (r, frac) => {
|
|
68
|
+
const a0 = Math.PI;
|
|
69
|
+
const a1 = Math.PI + Math.PI * frac;
|
|
70
|
+
const [x0, y0] = pol(cx, cy, r, -a0);
|
|
71
|
+
const [x1, y1] = pol(cx, cy, r, -a1);
|
|
72
|
+
// 半圆系的弧最多 180°,large 恒 0(>180° 才为 1,否则值弧会绕到底部走长弧)
|
|
73
|
+
return `M${r2(x0)} ${r2(y0)}A${r2(r)} ${r2(r)} 0 0 1 ${r2(x1)} ${r2(y1)}`;
|
|
74
|
+
};
|
|
75
|
+
const parts = [card(width, height, true)];
|
|
76
|
+
rows.forEach((row, i) => {
|
|
77
|
+
const r = r0 + i * pitch;
|
|
78
|
+
parts.push(`<path d="${arc(r, 1)}" fill="none" stroke="var(--d-faint)" stroke-width="${r2(sw)}" stroke-linecap="round"/>`);
|
|
79
|
+
if (row.pct > 0.5) {
|
|
80
|
+
parts.push(`<path d="${arc(r, Math.min(row.pct, 100) / 100)}" fill="none" stroke="${sVar(i % skin.series.length)}" stroke-width="${r2(sw)}" stroke-linecap="round"/>`);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
// 底部图例(多环才显示):方块 + "标签 值%"
|
|
84
|
+
if (spec.data?.length) {
|
|
85
|
+
const items = spec.data.map((d, i) => ({ name: `${trunc(d.label, 12)} ${Math.round(d.value)}%`, color: sVar(i % skin.series.length) }));
|
|
86
|
+
pushLegendRow(parts, items, cx, height * 0.9375, width, (it) => 15 + textW(it.name, 12) + 26, (lx, ly, it) => {
|
|
87
|
+
parts.push(`<rect x="${r2(lx)}" y="${r2(ly - 10)}" width="10" height="10" rx="3" fill="${it.color}"/>`);
|
|
88
|
+
parts.push(`<text x="${r2(lx + 16)}" y="${r2(ly)}" font-size="12" fill="var(--d-muted)">${esc(it.name)}</text>`);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
return svgDoc(width, height, 'gauge', version, spec.title ?? 'gauge', spec, skin, titleBlock(spec.title) + parts.join(''));
|
|
92
|
+
}
|
|
93
|
+
// 仪表盘 2 · 渐变多环(规范 gauge-2,860×480):
|
|
94
|
+
// 整圆环 pathLength=100 按百分比点亮,环环双色渐变,自顶顺时针
|
|
95
|
+
const RING_GRADS = [
|
|
96
|
+
['mint', 'green'],
|
|
97
|
+
['yellow', 'orange'],
|
|
98
|
+
['cyan', 'indigo'],
|
|
99
|
+
['violet', 'pink'],
|
|
100
|
+
['peach', 'lemon', 'sky'],
|
|
101
|
+
];
|
|
102
|
+
function renderGaugeRings(spec, o) {
|
|
103
|
+
const { width, height, version, skin } = o;
|
|
104
|
+
const rows = gaugeRows(spec);
|
|
105
|
+
const n = rows.length;
|
|
106
|
+
const G = C_gaugeGradient;
|
|
107
|
+
// 几何(规范 860×480 实测):心 (420,245),首环 r=154,环距 27(向内),弧宽 18。
|
|
108
|
+
// 整圆环:最外不出画布(四向),环多时收环距保最内环半径为正
|
|
109
|
+
const cx = width * 0.4884;
|
|
110
|
+
const cy = height * 0.5104;
|
|
111
|
+
let sw = height * 0.0375;
|
|
112
|
+
let r0 = height * 0.3208;
|
|
113
|
+
let pitch = height * 0.0563;
|
|
114
|
+
const rOut = r0 + sw / 2;
|
|
115
|
+
const rLim = Math.min(cx, width - cx, cy, height - cy) - 8;
|
|
116
|
+
if (rOut > rLim) {
|
|
117
|
+
const k = rLim / rOut;
|
|
118
|
+
sw *= k;
|
|
119
|
+
r0 *= k;
|
|
120
|
+
pitch *= k;
|
|
121
|
+
}
|
|
122
|
+
if (n > 1)
|
|
123
|
+
pitch = Math.min(pitch, (r0 - sw - 10) / (n - 1));
|
|
124
|
+
const gradIds = rows.map((_, i) => {
|
|
125
|
+
const names = RING_GRADS[i % RING_GRADS.length];
|
|
126
|
+
const stops = names.map((nm) => G[nm]).filter(Boolean);
|
|
127
|
+
const pair = stops.length >= 2 ? stops : [sVar(i % skin.series.length), sVar((i + 1) % skin.series.length)];
|
|
128
|
+
return pair;
|
|
129
|
+
});
|
|
130
|
+
const parts = [
|
|
131
|
+
card(width, height, true),
|
|
132
|
+
`<defs>${gradIds.map((pair, i) => `<linearGradient id="dgg-${i}" x1="0" x2="1">${pair.map((c, j) => `<stop offset="${r2(j / (pair.length - 1))}" stop-color="${c}"/>`).join('')}</linearGradient>`).join('')}</defs>`,
|
|
133
|
+
];
|
|
134
|
+
rows.forEach((row, i) => {
|
|
135
|
+
const r = r0 - i * pitch;
|
|
136
|
+
parts.push(`<circle cx="${r2(cx)}" cy="${r2(cy)}" r="${r2(r)}" fill="none" stroke="var(--d-faint)" stroke-width="${r2(sw)}"/>`);
|
|
137
|
+
if (row.pct > 0.5) {
|
|
138
|
+
parts.push(`<circle cx="${r2(cx)}" cy="${r2(cy)}" r="${r2(r)}" pathLength="100" fill="none" stroke="url(#dgg-${i})" stroke-width="${r2(sw)}" stroke-linecap="round" stroke-dasharray="${r2(Math.min(row.pct, 100))} 100" transform="rotate(-90 ${r2(cx)} ${r2(cy)})"/>`);
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
if (spec.data?.length) {
|
|
142
|
+
const items = spec.data.map((d, i) => ({ name: `${trunc(d.label, 12)} ${Math.round(d.value)}%`, grad: i }));
|
|
143
|
+
pushLegendRow(parts, items, width / 2, height * 0.9438, width, (it) => 17 + textW(it.name, 12) + 26, (lx, ly, it) => {
|
|
144
|
+
parts.push(`<rect x="${r2(lx)}" y="${r2(ly - 10)}" width="12" height="10" rx="3" fill="url(#dgg-${it.grad})"/>`);
|
|
145
|
+
parts.push(`<text x="${r2(lx + 18)}" y="${r2(ly)}" font-size="12" fill="var(--d-muted)">${esc(it.name)}</text>`);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
return svgDoc(width, height, 'gauge', version, spec.title ?? 'gauge', spec, skin, titleBlock(spec.title) + parts.join(''));
|
|
149
|
+
}
|
|
150
|
+
// 分区仪表盘(reliability,规范 饼图 6):四分区圆环 + 中心分值 + 底部区刻度条
|
|
151
|
+
const ZONES = [
|
|
152
|
+
{ upto: 0.45, name: 'Critical', seriesIdx: 0 },
|
|
153
|
+
{ upto: 0.65, name: 'At risk', seriesIdx: 1 },
|
|
154
|
+
{ upto: 0.82, name: 'Good', seriesIdx: 2 },
|
|
155
|
+
{ upto: 1, name: 'Excellent', seriesIdx: 3 },
|
|
156
|
+
];
|
|
157
|
+
function renderZoneGauge(spec, o) {
|
|
158
|
+
const { width, height, version, skin } = o;
|
|
159
|
+
const plain = skin.rules.canvas === 'plain';
|
|
160
|
+
if (spec.data?.length)
|
|
161
|
+
throw new ApiError(400, 't=reliability takes value=&max= (single score), not labels/values');
|
|
162
|
+
const value = spec.value ?? 0;
|
|
163
|
+
const frac = spec.max > 0 ? Math.min(1, value / spec.max) : 0;
|
|
164
|
+
const zone = [...ZONES].reverse().find((z) => frac <= z.upto) ?? ZONES[3];
|
|
165
|
+
const pad = 24;
|
|
166
|
+
let headSvg = '';
|
|
167
|
+
let headH = 0;
|
|
168
|
+
if (spec.title) {
|
|
169
|
+
headSvg = titleBlock(spec.title, pad);
|
|
170
|
+
headH = 26;
|
|
171
|
+
}
|
|
172
|
+
const barH = 44;
|
|
173
|
+
const cx = width / 2;
|
|
174
|
+
const availH = height - pad * 2 - headH - barH;
|
|
175
|
+
const R = Math.min(width / 2 - pad - 8, availH / 2 - 4, (168 / 520) * height); // 规范 pie-6:外径 168 封顶
|
|
176
|
+
const cy = pad + headH + availH / 2;
|
|
177
|
+
const r = R * (0.74 / 0.94); // 规范 pie-6:内径 132.3 / 外径 168
|
|
178
|
+
const parts = [card(width, height, plain)];
|
|
179
|
+
// 四分区环:240° 仪表弧,-30° 起逆时针,区序倒排(规范 pie-6);分隔 = 底色描边
|
|
180
|
+
const START6 = -Math.PI / 6;
|
|
181
|
+
const SPAN6 = Math.PI * 2 * (240 / 360);
|
|
182
|
+
const segs = ZONES.map((z, i) => ({ z, lo: i === 0 ? 0 : ZONES[i - 1].upto, hi: z.upto }));
|
|
183
|
+
let a = START6;
|
|
184
|
+
for (const { z, lo, hi } of [...segs].reverse()) {
|
|
185
|
+
const w = (hi - lo) * SPAN6;
|
|
186
|
+
const d = segPath(cx, cy, R, r, a, a + w, skin.rules.pieCorner);
|
|
187
|
+
if (d) {
|
|
188
|
+
parts.push(`<path d="${d}" fill="var(--d-s${z.seriesIdx + 1})" stroke="var(--d-halo)" stroke-width="${skin.rules.pieSliceGap}" stroke-linejoin="round" stroke-linecap="round"/>`);
|
|
189
|
+
}
|
|
190
|
+
a += w;
|
|
191
|
+
}
|
|
192
|
+
// 虚线刻度弧(同起止角)
|
|
193
|
+
const tickR = r - R / 12;
|
|
194
|
+
const [tsx, tsy] = pol(cx, cy, tickR, START6);
|
|
195
|
+
const [tex, tey] = pol(cx, cy, tickR, START6 + SPAN6);
|
|
196
|
+
parts.push(`<path d="M${r2(tsx)} ${r2(tsy)}A${r2(tickR)} ${r2(tickR)} 0 1 0 ${r2(tex)} ${r2(tey)}" fill="none" stroke="var(--d-muted)" stroke-width="1.3" stroke-linecap="round" stroke-dasharray="0.7 5.5" opacity="0.5"/>`);
|
|
197
|
+
// 中心分值
|
|
198
|
+
const fs = Math.min(52, Math.max(22, r * 0.5));
|
|
199
|
+
parts.push(`<text x="${r2(cx)}" y="${r2(cy + fs * 0.28)}" text-anchor="middle" font-size="${r2(fs)}" font-weight="600" letter-spacing="-0.03em" fill="var(--d-ink)" style="${TAB}">${fmtFull(value)}</text>`);
|
|
200
|
+
// 环下判词
|
|
201
|
+
parts.push(`<text x="${r2(cx)}" y="${r2(cy + R + 20)}" text-anchor="middle" font-size="13" font-weight="500" fill="var(--d-ink)">${esc(zone.name)}</text>`);
|
|
202
|
+
// 底部区刻度条:分段宽度=区间占比
|
|
203
|
+
const barY = height - pad - 24;
|
|
204
|
+
const barW = width - pad * 2;
|
|
205
|
+
let bx = pad;
|
|
206
|
+
let prev = 0;
|
|
207
|
+
for (const z of ZONES) {
|
|
208
|
+
const w = (z.upto - prev) * barW;
|
|
209
|
+
parts.push(`<rect x="${r2(bx)}" y="${barY}" width="${r2(w)}" height="6" rx="3" fill="var(--d-s${z.seriesIdx + 1})"/>`);
|
|
210
|
+
parts.push(`<text x="${r2(bx)}" y="${r2(barY - 8)}" font-size="11" fill="var(--d-muted)" style="${TAB}">${fmtNum(prev * spec.max)}</text>`);
|
|
211
|
+
bx += w;
|
|
212
|
+
prev = z.upto;
|
|
213
|
+
}
|
|
214
|
+
parts.push(`<text x="${r2(width - pad)}" y="${r2(barY - 8)}" text-anchor="end" font-size="11" fill="var(--d-muted)" style="${TAB}">${fmtNum(spec.max)}</text>`);
|
|
215
|
+
return svgDoc(width, height, 'gauge', version, spec.title ?? 'reliability', spec, skin, headSvg + parts.join(''));
|
|
216
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ChartSpec, MultiSpec, ChartInput } from '../specs/index.js';
|
|
2
|
+
import { type RenderOpts } from './svg.js';
|
|
3
|
+
export declare function renderHbarByMode(input: ChartInput, o: RenderOpts): string;
|
|
4
|
+
export declare function renderHbarOpendata(spec: ChartSpec, o: RenderOpts): string;
|
|
5
|
+
export declare function renderHbarCells(spec: ChartSpec, o: RenderOpts): string;
|
|
6
|
+
export declare function renderHbarDiverging(spec: MultiSpec, o: RenderOpts): string;
|
|
7
|
+
export declare function renderHbarLift(spec: MultiSpec, o: RenderOpts): string;
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
// 规范水平柱四形态(hbar-1..4):rows 逐行分类色 / cells 像素格 /
|
|
2
|
+
// diverging 蝴蝶图(左新建右关闭)/ lift 实色基础分+斜纹增益。
|
|
3
|
+
import { ApiError } from '../specs/index.js';
|
|
4
|
+
import { TAB, card } from '../theme.js';
|
|
5
|
+
import { esc, fmtFull, fmtNum, niceTicks, r2, sVar, textW, trunc, svgDoc } from './svg.js';
|
|
6
|
+
import { fitCut } from './measure.js';
|
|
7
|
+
import { renderHBar } from './hbar.js';
|
|
8
|
+
export function renderHbarByMode(input, o) {
|
|
9
|
+
const mode = o.skin.rules.hbarMode;
|
|
10
|
+
if (mode === 'diverging' || mode === 'lift') {
|
|
11
|
+
const need = mode === 'diverging' ? 'opened' : 'base';
|
|
12
|
+
const other = mode === 'diverging' ? 'closed' : 'lift';
|
|
13
|
+
if (!input.multi || input.spec.series.length !== 2) {
|
|
14
|
+
throw new ApiError(400, `this hbar style needs exactly two series, e.g. series=${need}:1,2&series=${other}:1,2`);
|
|
15
|
+
}
|
|
16
|
+
return mode === 'diverging' ? renderHbarDiverging(input.spec, o) : renderHbarLift(input.spec, o);
|
|
17
|
+
}
|
|
18
|
+
if (input.multi)
|
|
19
|
+
throw new ApiError(400, 'multi-series hbar is only supported for t=xcmd-issue / t=rank');
|
|
20
|
+
if (mode === 'cells')
|
|
21
|
+
return renderHbarCells(input.spec, o);
|
|
22
|
+
if (mode === 'rows')
|
|
23
|
+
return renderHbarOpendata(input.spec, o);
|
|
24
|
+
return renderHBar(input.spec, o);
|
|
25
|
+
}
|
|
26
|
+
// 水平柱 1 · OpenData(规范 hbar-1,960×480)
|
|
27
|
+
export function renderHbarOpendata(spec, o) {
|
|
28
|
+
const { version, skin } = o;
|
|
29
|
+
let width = o.width;
|
|
30
|
+
let height = o.height;
|
|
31
|
+
const n = spec.data.length;
|
|
32
|
+
// 整图自适应:行少(<7)时画布贴内容收缩,行距保持规范绝对值,宽度等比跟随;
|
|
33
|
+
// 显式 ?w=/?h= 不收缩
|
|
34
|
+
const shrunk = o.autoHeight && n < 7;
|
|
35
|
+
if (shrunk) {
|
|
36
|
+
height = Math.round(Math.max(200, 54 + (n - 1) * 52 + 28 + 86)); // n=7 验算:480 ✓
|
|
37
|
+
if (o.autoWidth)
|
|
38
|
+
width = Math.round(Math.max(400, o.width * (height / o.height)));
|
|
39
|
+
}
|
|
40
|
+
// 行距过密(>20 行)自动拉高(封顶 2.5×)
|
|
41
|
+
if (o.autoHeight) {
|
|
42
|
+
const nRows = spec.data.length;
|
|
43
|
+
const avail = height * (0.8375 - 0.1125);
|
|
44
|
+
const pitch0 = Math.min(height * 0.1083, (avail - height * 0.0583) / Math.max(nRows - 1, 1));
|
|
45
|
+
if (pitch0 > 0 && pitch0 < 20)
|
|
46
|
+
height = Math.round(height * Math.min(2.5, 20 / pitch0));
|
|
47
|
+
}
|
|
48
|
+
const max = Math.max(...spec.data.map((d) => d.value));
|
|
49
|
+
// 行标签列 = max(规范, 实测):放不下先截短(fitCut),再右吃余量
|
|
50
|
+
const plotW = width * 0.7188;
|
|
51
|
+
const specEnd = width * 0.1542;
|
|
52
|
+
const gapL = width * 0.175 - specEnd;
|
|
53
|
+
const lblCut = fitCut(spec.data.map((d) => d.label), 16, width - plotW - width * 0.02 - gapL - 8, 14);
|
|
54
|
+
const maxLbl = Math.max(...spec.data.map((d) => textW(trunc(d.label, lblCut), 14)), 0);
|
|
55
|
+
const labelEnd = Math.max(specEnd, maxLbl + 8);
|
|
56
|
+
const x = Math.round(labelEnd > specEnd ? labelEnd + gapL : width * 0.175);
|
|
57
|
+
// shrunk 时几何取规范绝对值,网格底贴末行
|
|
58
|
+
const top = shrunk ? 54 : height * 0.1125;
|
|
59
|
+
const gridBottom = shrunk ? top + (n - 1) * 52 + 28 : height * 0.8375;
|
|
60
|
+
const barH0 = shrunk ? 28 : height * 0.0583;
|
|
61
|
+
// 行距:规范 52,行多时压缩
|
|
62
|
+
const pitch = shrunk ? 52 : Math.min(height * 0.1083, (gridBottom - top - barH0) / Math.max(n - 1, 1));
|
|
63
|
+
const barH = Math.min(barH0, pitch * 0.62);
|
|
64
|
+
const gridTop = shrunk ? 32 : height * 0.0667;
|
|
65
|
+
const tickY = shrunk ? gridBottom + 28 : height * 0.8958;
|
|
66
|
+
const unitY = shrunk ? gridBottom + 60 : height * 0.9625;
|
|
67
|
+
const ticks = niceTicks(max, 5);
|
|
68
|
+
const yMax = ticks[ticks.length - 1];
|
|
69
|
+
// 刻度文字抽稀:大数(如 1000000.0)在窄画布上相邻刻度互叠 → 按间距隔一显示
|
|
70
|
+
const tickGap = plotW / Math.max(ticks.length - 1, 1);
|
|
71
|
+
const tickStride = Math.max(1, Math.ceil((Math.max(...ticks.map((t) => textW(fmtNum(t), 11))) + 6) / tickGap));
|
|
72
|
+
const parts = [card(width, height, true)];
|
|
73
|
+
// 竖直虚线网格 + 刻度(fmtNum 缩写大数:200k 而非 200000.0)
|
|
74
|
+
ticks.forEach((t, i) => {
|
|
75
|
+
const gx = x + (plotW * t) / yMax;
|
|
76
|
+
parts.push(`<line x1="${r2(gx)}" y1="${r2(gridTop)}" x2="${r2(gx)}" y2="${r2(gridBottom)}" stroke="var(--d-grid)" stroke-dasharray="4 6"/>`);
|
|
77
|
+
if (i % tickStride === 0 || i === ticks.length - 1) {
|
|
78
|
+
parts.push(`<text x="${r2(gx)}" y="${r2(tickY)}" text-anchor="middle" font-size="11" fill="var(--d-muted)" style="${TAB}">${fmtNum(t)}</text>`);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
// 参考线(?ref= 数值 + ?reflabel= 文案)
|
|
82
|
+
if (o.refLine !== undefined && o.refLine <= yMax) {
|
|
83
|
+
const rx = x + (plotW * o.refLine) / yMax;
|
|
84
|
+
parts.push(`<line x1="${r2(rx)}" y1="${r2(shrunk ? 36 : height * 0.075)}" x2="${r2(rx)}" y2="${r2(gridBottom)}" stroke="var(--d-faint)" stroke-width="1.5" stroke-dasharray="5 4"/>`);
|
|
85
|
+
if (o.refLabel) {
|
|
86
|
+
parts.push(`<text x="${r2(rx + 7)}" y="${r2(shrunk ? 48 : height * 0.1)}" font-size="11" fill="var(--d-faint)">${esc(trunc(o.refLabel, 40))}</text>`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
spec.data.forEach((d, i) => {
|
|
90
|
+
const y = top + i * pitch;
|
|
91
|
+
const w = (plotW * d.value) / yMax;
|
|
92
|
+
parts.push(`<text x="${r2(labelEnd)}" y="${r2(y + pitch * 0.36)}" text-anchor="end" font-size="14" fill="var(--d-muted)">${esc(trunc(d.label, lblCut))}</text>`);
|
|
93
|
+
parts.push(`<rect x="${x}" y="${r2(y)}" width="${r2(w)}" height="${r2(barH)}" rx="3" fill="${sVar(i % skin.series.length)}"/>`);
|
|
94
|
+
// 数值内标白字;放不下则外置墨色
|
|
95
|
+
const text = fmtFull(d.value);
|
|
96
|
+
if (textW(text, 11) + 14 <= w) {
|
|
97
|
+
parts.push(`<text x="${r2(x + w - 8)}" y="${r2(y + barH * 0.68)}" text-anchor="end" font-size="11" font-weight="700" fill="#FFFFFF" style="${TAB}">${text}</text>`);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
parts.push(`<text x="${r2(x + w + 8)}" y="${r2(y + barH * 0.68)}" font-size="11" font-weight="700" fill="var(--d-ink)" style="${TAB}">${text}</text>`);
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
if (spec.unit) {
|
|
104
|
+
parts.push(`<text x="${r2(x + plotW * 0.5)}" y="${r2(unitY)}" text-anchor="middle" font-size="13" fill="var(--d-muted)">${esc(trunc(spec.unit, 40))}</text>`);
|
|
105
|
+
}
|
|
106
|
+
return svgDoc(width, height, 'hbar', version, spec.title ?? 'hbar', spec, skin, parts.join(''));
|
|
107
|
+
}
|
|
108
|
+
// 水平柱 2 · X-CMD 语言像素格(规范 hbar-2,920×300)
|
|
109
|
+
export function renderHbarCells(spec, o) {
|
|
110
|
+
const { version, skin } = o;
|
|
111
|
+
let width = o.width;
|
|
112
|
+
let height = o.height;
|
|
113
|
+
const n = spec.data.length;
|
|
114
|
+
// 整图自适应:行少时贴内容收缩(行距/格高取规范绝对值),宽度等比跟随
|
|
115
|
+
const shrunk = o.autoHeight && n <= 4; // n=4 验算:86+3×48+16+54=300 ✓
|
|
116
|
+
if (shrunk) {
|
|
117
|
+
height = Math.round(Math.max(200, 86 + (n - 1) * 48 + 16 + 54));
|
|
118
|
+
if (o.autoWidth)
|
|
119
|
+
width = Math.round(Math.max(400, o.width * (height / o.height)));
|
|
120
|
+
}
|
|
121
|
+
const CELLS = 24;
|
|
122
|
+
const startX = width * 0.2935;
|
|
123
|
+
const valueEndX = width * 0.9196;
|
|
124
|
+
const labelX = width * 0.0826;
|
|
125
|
+
const span = width * 0.4891; // 24 格总宽(规范 270→720)
|
|
126
|
+
const cellPitch = span / CELLS;
|
|
127
|
+
const cw = cellPitch * (13 / 19);
|
|
128
|
+
const cellH = shrunk ? 16 : height * 0.0533;
|
|
129
|
+
const top = shrunk ? 86 : height * 0.2867;
|
|
130
|
+
const pitch = shrunk ? 48 : Math.min(height * 0.16, (height * 0.9157 - cellH - top) / Math.max(n, 1));
|
|
131
|
+
// 行标签在 labelX..startX 之间:放不下按预算截短(fs18),不与像素格重叠
|
|
132
|
+
const lblCut = fitCut(spec.data.map((d) => d.label), 16, startX - labelX - 10, 18);
|
|
133
|
+
const parts = [card(width, height, true)];
|
|
134
|
+
if (spec.title) {
|
|
135
|
+
parts.push(`<text x="${r2(labelX)}" y="${r2(shrunk ? 46 : height * 0.1533)}" font-size="19" font-weight="700" fill="${sVar(0)}">${esc(trunc(spec.title, 40))}</text>`);
|
|
136
|
+
}
|
|
137
|
+
parts.push(`<line x1="${r2(labelX)}" y1="${r2(shrunk ? 60 : height * 0.2)}" x2="${r2(valueEndX)}" y2="${r2(shrunk ? 60 : height * 0.2)}" stroke="var(--d-grid)"/>`);
|
|
138
|
+
spec.data.forEach((d, i) => {
|
|
139
|
+
const rowY = top + i * pitch;
|
|
140
|
+
const y = rowY + height * 0.01; // 规范:格子较行基线下移 3(0.01h)
|
|
141
|
+
parts.push(`<text x="${r2(labelX)}" y="${r2(rowY + height * 0.06)}" font-size="18" fill="var(--d-text)">${esc(trunc(d.label, lblCut))}</text>`);
|
|
142
|
+
const exact = (d.value / 100) * CELLS; // 值为百分比
|
|
143
|
+
for (let c = 0; c < CELLS; c++) {
|
|
144
|
+
const cx = startX + c * cellPitch;
|
|
145
|
+
parts.push(`<rect x="${r2(cx)}" y="${r2(y)}" width="${r2(cw)}" height="${r2(cellH)}" rx="1" fill="var(--d-faint)"/>`);
|
|
146
|
+
const cover = Math.max(0, Math.min(1, exact - c));
|
|
147
|
+
if (cover > 0) {
|
|
148
|
+
parts.push(`<rect x="${r2(cx)}" y="${r2(y)}" width="${r2(cw * cover)}" height="${r2(cellH)}" rx="1" fill="${sVar(0)}"/>`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
parts.push(`<text x="${r2(valueEndX)}" y="${r2(rowY + height * 0.06)}" text-anchor="end" font-size="18" font-weight="700" fill="var(--d-muted)" style="${TAB}">${Math.round(d.value * 10) / 10}%</text>`);
|
|
152
|
+
});
|
|
153
|
+
return svgDoc(width, height, 'hbar', version, spec.title ?? 'language share', spec, skin, parts.join(''));
|
|
154
|
+
}
|
|
155
|
+
// 水平柱 3 · X-CMD Issue 蝴蝶图(规范 hbar-3,820×370):左新建右关闭
|
|
156
|
+
export function renderHbarDiverging(spec, o) {
|
|
157
|
+
const { version, skin } = o;
|
|
158
|
+
let width = o.width;
|
|
159
|
+
let height = o.height;
|
|
160
|
+
const nD = spec.labels.length;
|
|
161
|
+
// 整图自适应:行少时贴内容收缩(规范行距 47/条高 22/底带 51),宽度等比跟随
|
|
162
|
+
const shrunk = o.autoHeight && nD <= 6; // 规范行数(6)也走绝对几何:比例压缩会给 38.66≠47
|
|
163
|
+
if (shrunk) {
|
|
164
|
+
height = Math.round(Math.max(200, 62 + (nD - 1) * 47 + 22 + 51)); // n=6 验算:370 ✓(规范行距47/条高22)
|
|
165
|
+
if (o.autoWidth)
|
|
166
|
+
width = Math.round(Math.max(400, o.width * (height / o.height)));
|
|
167
|
+
}
|
|
168
|
+
const [left, right] = spec.series;
|
|
169
|
+
const n = spec.labels.length;
|
|
170
|
+
const max = Math.max(...left.values, ...right.values, 1);
|
|
171
|
+
const center = width * 0.6159;
|
|
172
|
+
const half = width * 0.2744;
|
|
173
|
+
const gap = width * 0.0098;
|
|
174
|
+
const top = shrunk ? 62 : height * 0.1676;
|
|
175
|
+
const barH = shrunk ? 22 : Math.min(height * 0.0595, ((height * 0.8475 - top) / Math.max(n, 1)) * 0.47);
|
|
176
|
+
const pitch = shrunk ? 47 : (height * 0.8475 - top - barH) / Math.max(n, 1);
|
|
177
|
+
// 行名在 0.0707w..0.2707w 之间(其后是左值):放不下按预算截短(fs16)
|
|
178
|
+
const lblCut = fitCut(spec.labels, 12, width * 0.2707 - width * 0.0707 - 10, 16);
|
|
179
|
+
const parts = [card(width, height, true)];
|
|
180
|
+
// 图例(规范:圆点 swatch,中上)
|
|
181
|
+
const ly = shrunk ? 30 : height * 0.0811;
|
|
182
|
+
const lx1 = width * 0.4756;
|
|
183
|
+
const lx2 = width * 0.5756;
|
|
184
|
+
parts.push(`<circle cx="${r2(lx1)}" cy="${r2(shrunk ? 20 : height * 0.0541)}" r="5" fill="var(--d-s1)"/>`);
|
|
185
|
+
parts.push(`<text x="${r2(lx1 + 16)}" y="${r2(ly)}" font-size="12" fill="var(--d-muted)">${esc(trunc(left.name, 12))}</text>`);
|
|
186
|
+
parts.push(`<circle cx="${r2(lx2)}" cy="${r2(shrunk ? 20 : height * 0.0541)}" r="5" fill="var(--d-s2)"/>`);
|
|
187
|
+
parts.push(`<text x="${r2(lx2 + 16)}" y="${r2(ly)}" font-size="12" fill="var(--d-muted)">${esc(trunc(right.name, 12))}</text>`);
|
|
188
|
+
spec.labels.forEach((l, i) => {
|
|
189
|
+
const y = top + i * pitch;
|
|
190
|
+
const lw = (half * left.values[i]) / max;
|
|
191
|
+
const rw = (half * right.values[i]) / max;
|
|
192
|
+
parts.push(`<text x="${r2(width * 0.0707)}" y="${r2(y + barH * 0.77)}" font-size="16" fill="var(--d-muted)">${esc(trunc(l, lblCut))}</text>`);
|
|
193
|
+
parts.push(`<text x="${r2(width * 0.2707)}" y="${r2(y + barH * 0.77)}" text-anchor="end" font-size="15" font-weight="700" fill="var(--d-text)" style="${TAB}">${fmtFull(left.values[i])}</text>`);
|
|
194
|
+
parts.push(`<rect x="${r2(width * 0.2878)}" y="${r2(y)}" width="${r2(half)}" height="${r2(barH)}" rx="4" fill="var(--d-faint)"/>`);
|
|
195
|
+
parts.push(`<rect x="${r2(center - lw - gap)}" y="${r2(y)}" width="${r2(lw)}" height="${r2(barH)}" rx="4" fill="var(--d-s1)"/>`);
|
|
196
|
+
parts.push(`<rect x="${r2(center + gap)}" y="${r2(y)}" width="${r2(half)}" height="${r2(barH)}" rx="4" fill="var(--d-faint)"/>`);
|
|
197
|
+
parts.push(`<rect x="${r2(center + gap)}" y="${r2(y)}" width="${r2(rw)}" height="${r2(barH)}" rx="4" fill="var(--d-s2)"/>`);
|
|
198
|
+
parts.push(`<text x="${r2(width * 0.9268)}" y="${r2(y + barH * 0.77)}" font-size="15" font-weight="700" fill="var(--d-text)" style="${TAB}">${fmtFull(right.values[i])}</text>`);
|
|
199
|
+
});
|
|
200
|
+
return svgDoc(width, height, 'hbar', version, spec.title ?? 'issues', spec, skin, parts.join(''));
|
|
201
|
+
}
|
|
202
|
+
// 水平柱 4 · Search Intelligence 排行(规范 hbar-4,960×560):实色基础 + 斜纹增益
|
|
203
|
+
export function renderHbarLift(spec, o) {
|
|
204
|
+
const { version, skin } = o;
|
|
205
|
+
let width = o.width;
|
|
206
|
+
let height = o.height;
|
|
207
|
+
const [base, lift] = spec.series;
|
|
208
|
+
const n = spec.labels.length;
|
|
209
|
+
const max = Math.max(...base.values.map((v, i) => v + lift.values[i]));
|
|
210
|
+
// 整图自适应:行少时贴内容收缩(规范行距 43/条高 22/底带 96),宽度等比跟随
|
|
211
|
+
const shrunk = o.autoHeight && n <= 10; // 规范行数(10)也走绝对几何:比例压缩会给 39.8≠43
|
|
212
|
+
if (shrunk) {
|
|
213
|
+
height = Math.round(Math.max(200, 55 + (n - 1) * 43 + 22 + 96)); // n=10 验算:560 ✓(规范行距43/条高22)
|
|
214
|
+
if (o.autoWidth)
|
|
215
|
+
width = Math.round(Math.max(400, o.width * (height / o.height)));
|
|
216
|
+
}
|
|
217
|
+
const x0 = width * 0.2813;
|
|
218
|
+
const maxW = width * 0.6042;
|
|
219
|
+
const top = shrunk ? 55 : height * 0.0982;
|
|
220
|
+
const rowEnd = top + (n - 1) * (shrunk ? 43 : 0) + (shrunk ? 22 : 0);
|
|
221
|
+
const gridBottom = shrunk ? rowEnd + 10 : height * 0.8464; // 规范:网格底 474 = 末行底 464 + 10;刻度 +36、图例 +76 由既有偏移给出
|
|
222
|
+
const barH = shrunk ? 22 : Math.max(10, (gridBottom - top) * 0.51 / Math.max(n, 1));
|
|
223
|
+
const pitch = shrunk ? 43 : (gridBottom - top - barH) / Math.max(n, 1);
|
|
224
|
+
const ticks = niceTicks(max, 4);
|
|
225
|
+
const yMax = ticks[ticks.length - 1];
|
|
226
|
+
const baseColor = sVar(0);
|
|
227
|
+
const parts = [
|
|
228
|
+
card(width, height, true),
|
|
229
|
+
`<defs><pattern id="dgl-hatch" width="8" height="8" patternUnits="userSpaceOnUse" patternTransform="rotate(45)">` +
|
|
230
|
+
`<line x1="0" y1="0" x2="0" y2="8" stroke="${baseColor}" stroke-width="3"/></pattern></defs>`,
|
|
231
|
+
];
|
|
232
|
+
// 刻度同 opendata:fmtNum 缩写 + 间距不足时隔一显示(网格线保留,只抽稀文字)
|
|
233
|
+
const tickGap = maxW / Math.max(ticks.length - 1, 1);
|
|
234
|
+
const tickStride = Math.max(1, Math.ceil((Math.max(...ticks.map((t) => textW(fmtNum(t), 11))) + 6) / tickGap));
|
|
235
|
+
ticks.forEach((t, i) => {
|
|
236
|
+
const gx = x0 + (maxW * t) / yMax;
|
|
237
|
+
parts.push(`<line x1="${r2(gx)}" y1="${r2(shrunk ? 36 : height * 0.0643)}" x2="${r2(gx)}" y2="${r2(gridBottom)}" stroke="var(--d-grid)" stroke-dasharray="3 5"/>`);
|
|
238
|
+
if (i % tickStride === 0 || i === ticks.length - 1) {
|
|
239
|
+
parts.push(`<text x="${r2(gx)}" y="${r2(shrunk ? gridBottom + 26 : height * 0.8929)}" text-anchor="middle" font-size="11" fill="var(--d-muted)" style="${TAB}">${fmtNum(t)}</text>`);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
// 行名在 0.075w..0.2707w 之间(其后是左值):放不下按预算截短(fs14)
|
|
243
|
+
const nameCut = fitCut(spec.labels, 20, width * 0.2707 - width * 0.075 - 10, 14);
|
|
244
|
+
spec.labels.forEach((l, i) => {
|
|
245
|
+
const y = top + i * pitch;
|
|
246
|
+
const total = base.values[i] + lift.values[i];
|
|
247
|
+
const bw = (maxW * base.values[i]) / yMax;
|
|
248
|
+
const tw = (maxW * total) / yMax;
|
|
249
|
+
parts.push(`<text x="${r2(width * 0.0292)}" y="${r2(y + barH * 0.73)}" font-size="12" fill="var(--d-muted)" style="${TAB}">${i + 1}</text>`);
|
|
250
|
+
parts.push(`<rect x="${r2(width * 0.049)}" y="${r2(y + barH * 0.09)}" width="3" height="${r2(barH * 0.82)}" fill="${i === 0 ? '#E5A719' : 'var(--d-muted)'}"/>`);
|
|
251
|
+
parts.push(`<text x="${r2(width * 0.075)}" y="${r2(y + barH * 0.77)}" font-size="14" fill="var(--d-text)">${esc(trunc(l, nameCut))}</text>`);
|
|
252
|
+
parts.push(`<rect x="${r2(x0)}" y="${r2(y)}" width="${r2(bw)}" height="${r2(barH)}" rx="1" fill="${baseColor}"/>`);
|
|
253
|
+
if (tw > bw) {
|
|
254
|
+
parts.push(`<rect x="${r2(x0 + bw)}" y="${r2(y)}" width="${r2(tw - bw)}" height="${r2(barH)}" rx="1" fill="url(#dgl-hatch)" stroke="${baseColor}"/>`);
|
|
255
|
+
}
|
|
256
|
+
parts.push(`<text x="${r2(x0 + tw + 8)}" y="${r2(y + barH * 0.73)}" font-size="12" fill="var(--d-muted)" style="${TAB}">${fmtFull(total)} +${fmtFull(lift.values[i])}</text>`);
|
|
257
|
+
});
|
|
258
|
+
// 底部图例:系列名直读
|
|
259
|
+
const lgY = shrunk ? gridBottom + 66 : height * 0.9643;
|
|
260
|
+
parts.push(`<rect x="${r2(x0)}" y="${r2(lgY - 10)}" width="10" height="10" rx="2" fill="${baseColor}"/>`);
|
|
261
|
+
parts.push(`<text x="${r2(x0 + 16)}" y="${r2(lgY)}" font-size="11" fill="var(--d-muted)">${esc(trunc(base.name, 12))}</text>`);
|
|
262
|
+
parts.push(`<rect x="${r2(x0 + width * 0.1042)}" y="${r2(lgY - 10)}" width="10" height="10" rx="2" fill="url(#dgl-hatch)" stroke="${baseColor}"/>`);
|
|
263
|
+
parts.push(`<text x="${r2(x0 + width * 0.1042 + 16)}" y="${r2(lgY)}" font-size="11" fill="var(--d-muted)">${esc(trunc(lift.name, 12))}</text>`);
|
|
264
|
+
return svgDoc(width, height, 'hbar', version, spec.title ?? 'ranking', spec, skin, parts.join(''));
|
|
265
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { TAB, card } from '../theme.js';
|
|
2
|
+
import { esc, fmtNum, niceTicks, r2, svgDoc, textW, titleBlock, trunc } from './svg.js';
|
|
3
|
+
export function renderHBar(spec, o) {
|
|
4
|
+
const { width, version, skin } = o;
|
|
5
|
+
let height = o.height;
|
|
6
|
+
// 行距过密时行标签会重叠 → 自动拉高(封顶 2.5×)
|
|
7
|
+
if (o.autoHeight && spec.data.length > 0) {
|
|
8
|
+
const step0 = (height - 24 - 28 - (24 + (spec.title ? 32 : 0) + 8)) / spec.data.length;
|
|
9
|
+
if (step0 > 0 && step0 < 20)
|
|
10
|
+
height = Math.round(height * Math.min(2.5, 20 / step0));
|
|
11
|
+
}
|
|
12
|
+
const values = spec.data.map((d) => d.value);
|
|
13
|
+
const max = Math.max(...values);
|
|
14
|
+
const peakIdx = values.indexOf(max);
|
|
15
|
+
const n = spec.data.length;
|
|
16
|
+
const pad = 24;
|
|
17
|
+
const contentTop = pad + (spec.title ? 32 : 0) + 8;
|
|
18
|
+
const labelW = Math.min(160, Math.max(...spec.data.map((d) => textW(trunc(d.label, 16), 12.5))) + 4);
|
|
19
|
+
const left = pad + labelW + 14;
|
|
20
|
+
const right = width - pad - 44; // 右侧留给数值标注
|
|
21
|
+
const bottom = height - pad - 28;
|
|
22
|
+
const ticks = niceTicks(max, 4);
|
|
23
|
+
const xMax = ticks[ticks.length - 1];
|
|
24
|
+
const x = (v) => left + (v / xMax) * (right - left);
|
|
25
|
+
const parts = [card(width, height)];
|
|
26
|
+
// 竖向网格 + 底部刻度
|
|
27
|
+
for (const t of ticks) {
|
|
28
|
+
const gx = r2(x(t));
|
|
29
|
+
if (t > 0)
|
|
30
|
+
parts.push(`<line x1="${gx}" y1="${contentTop}" x2="${gx}" y2="${bottom}" stroke="var(--d-grid)" stroke-width="1"/>`);
|
|
31
|
+
parts.push(`<text x="${gx}" y="${r2(bottom + 16)}" text-anchor="middle" font-size="12" fill="var(--d-muted)" style="${TAB}">${fmtNum(t)}</text>`);
|
|
32
|
+
}
|
|
33
|
+
parts.push(`<line x1="${r2(left)}" y1="${contentTop}" x2="${r2(left)}" y2="${bottom}" stroke="var(--d-baseline)" stroke-width="1"/>`);
|
|
34
|
+
const step = (bottom - contentTop) / n;
|
|
35
|
+
const barH = Math.min(22, Math.max(5, step * 0.55));
|
|
36
|
+
const num = o.valueLabels ?? skin.rules.num;
|
|
37
|
+
spec.data.forEach((d, i) => {
|
|
38
|
+
const y = contentTop + step * i + (step - barH) / 2;
|
|
39
|
+
const x0 = left;
|
|
40
|
+
const x1 = x(d.value);
|
|
41
|
+
const m = Math.min(4, barH / 2, x1 - x0);
|
|
42
|
+
const isPeak = i === peakIdx;
|
|
43
|
+
// 仅右端(leading端)圆角
|
|
44
|
+
const path = `M${r2(x0)} ${r2(y)}H${r2(x1 - m)}Q${r2(x1)} ${r2(y)} ${r2(x1)} ${r2(y + m)}` +
|
|
45
|
+
`V${r2(y + barH - m)}Q${r2(x1)} ${r2(y + barH)} ${r2(x1 - m)} ${r2(y + barH)}H${r2(x0)}Z`;
|
|
46
|
+
parts.push(`<path d="${path}" fill="url(#dg-bar)"/>`);
|
|
47
|
+
parts.push(`<text x="${r2(pad + labelW)}" y="${r2(y + barH / 2 + 4)}" text-anchor="end" font-size="12.5" fill="var(--d-text)">${esc(trunc(d.label, 16))}</text>`);
|
|
48
|
+
if (d.value > 0 && (num === 'all' || (num === 'peak' && isPeak))) { // 零值不标(标注克制)
|
|
49
|
+
parts.push(`<text x="${r2(x1 + 8)}" y="${r2(y + barH / 2 + 4)}" font-size="${isPeak ? 13 : 11}" ${isPeak ? 'font-weight="600" fill="' + 'var(--d-ink)' + '"' : 'fill="' + 'var(--d-text)' + '"'} style="${TAB}">${fmtNum(d.value)}</text>`);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
const defs = `<defs><linearGradient id="dg-bar" x1="0" y1="0" x2="1" y2="0">` +
|
|
53
|
+
`<stop offset="0" stop-color="var(--d-ramp-b)"/><stop offset="1" stop-color="var(--d-ramp-a)"/></linearGradient></defs>`;
|
|
54
|
+
return svgDoc(width, height, 'hbar', version, spec.title ?? 'horizontal bar chart', spec, skin, titleBlock(spec.title) + defs + parts.join(''));
|
|
55
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { card } from '../theme.js';
|
|
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
|
+
import { lineLayout } from './line.js';
|
|
4
|
+
export function renderLineMulti(spec, o) {
|
|
5
|
+
const { width, height, version, skin } = o;
|
|
6
|
+
const allSegs = [];
|
|
7
|
+
const rules = skin.rules;
|
|
8
|
+
const k = spec.series.length;
|
|
9
|
+
const colors = spec.series.map((_, i) => sVar(i % skin.series.length));
|
|
10
|
+
const n = spec.labels.length;
|
|
11
|
+
const gMax = Math.max(...spec.series.flatMap((s) => s.values));
|
|
12
|
+
const [peakS, peakP] = findPeak(spec.series);
|
|
13
|
+
const pad = 24;
|
|
14
|
+
let headSvg = '';
|
|
15
|
+
if (rules.title === 'editorial' && spec.title) {
|
|
16
|
+
headSvg = editorialTitle(spec.title, width, pad).svg;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
headSvg = titleBlock(spec.title, pad);
|
|
20
|
+
}
|
|
21
|
+
const { left, right: right0, bottom, contentTop, xLabelY, legendY } = lineLayout(spec, o);
|
|
22
|
+
const ticks = niceTicks(gMax, 4);
|
|
23
|
+
// 端标签(OpenData):右侧预留系列名宽度,防止贴边/裁切
|
|
24
|
+
const endMax = rules.endLabels ? Math.max(...spec.series.map((s) => textW(trunc(s.name, 14), 11))) + 14 : 0;
|
|
25
|
+
const right = Math.min(right0, width - pad - endMax);
|
|
26
|
+
const yMax = ticks[ticks.length - 1];
|
|
27
|
+
const y = (v) => bottom - (v / yMax) * (bottom - contentTop);
|
|
28
|
+
const xs = (i) => (n === 1 ? (left + right) / 2 : left + ((right - left) * i) / (n - 1));
|
|
29
|
+
const parts = [card(width, height, rules.canvas === 'plain')];
|
|
30
|
+
if (rules.legend === 'top-left') {
|
|
31
|
+
parts.push(topLegend(spec.series.map((s, i) => ({ name: s.name, color: colors[i] })), left, legendY, 'left', rules.legendShape));
|
|
32
|
+
}
|
|
33
|
+
else if (rules.legend === 'top-right') {
|
|
34
|
+
parts.push(topLegend(spec.series.map((s, i) => ({ name: s.name, color: colors[i] })), right, legendY, 'right', rules.legendShape));
|
|
35
|
+
}
|
|
36
|
+
yAxisGrid(parts, ticks, y, left, right, bottom, rules.grid, rules.gridDash, rules.axisInline, rules.baseline, spec.labels.map((_, i) => xs(i)));
|
|
37
|
+
if (rules.axisTitle)
|
|
38
|
+
parts.push(axisTitleY(spec.unit, (contentTop + bottom) / 2, left - (rules.axisInline ? 17 : 26)));
|
|
39
|
+
const num = o.valueLabels ?? rules.num;
|
|
40
|
+
const placer = new LabelPlacer();
|
|
41
|
+
// 预留 Y 刻度带、X 标签带与图例区:数值标签不得压上去
|
|
42
|
+
reserveBands(placer, left, right0, contentTop, bottom, xLabelY, rules.legend === 'top-left' || rules.legend === 'top-right' ? spec.series : [], rules.legend === 'top-left' ? left : right, legendY);
|
|
43
|
+
const numItems = [];
|
|
44
|
+
spec.series.forEach((s, si) => {
|
|
45
|
+
const pts = s.values.map((v, i) => [xs(i), y(v)]);
|
|
46
|
+
allSegs.push(...toSegments(pts));
|
|
47
|
+
const dpath = rules.smooth ? smoothPath(pts) : `M${pts.map((p) => `${r2(p[0])} ${r2(p[1])}`).join('L')}`;
|
|
48
|
+
if (rules.glow) {
|
|
49
|
+
parts.push(`<defs><filter id="d-glow" x="-20%" y="-60%" width="140%" height="220%"><feGaussianBlur stdDeviation="7"/></filter>` +
|
|
50
|
+
`<linearGradient id="dg-glow-${si}" x1="0" y1="0" x2="1" y2="0">` +
|
|
51
|
+
`<stop stop-color="${colors[si]}"/><stop offset="1" stop-color="${colors[(si + 1) % k]}"/></linearGradient></defs>`);
|
|
52
|
+
parts.push(`<path d="${dpath}" fill="none" stroke="${colors[si]}" stroke-width="14" stroke-opacity="0.32" stroke-linecap="round" filter="url(#d-glow)"/>`);
|
|
53
|
+
parts.push(`<path d="${dpath}" fill="none" stroke="url(#dg-glow-${si})" stroke-width="${rules.lineWidth}" stroke-linecap="round" stroke-linejoin="round"/>`);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const dash = rules.dashSecondary && si > 0 ? ' stroke-dasharray="5 6"' : rules.lineDash ? ` stroke-dasharray="${rules.lineDash}"` : '';
|
|
57
|
+
parts.push(`<path d="${dpath}" fill="none" stroke="${colors[si]}" stroke-width="${rules.lineWidth}" stroke-linecap="round" stroke-linejoin="round"${dash}/>`);
|
|
58
|
+
}
|
|
59
|
+
if (rules.endLabels) {
|
|
60
|
+
// OpenData:线端系列名(彩色,白描边);碰撞即舍弃
|
|
61
|
+
const [ex, ey] = pts[n - 1];
|
|
62
|
+
const name = trunc(s.name, 14);
|
|
63
|
+
const w = textW(name, 11);
|
|
64
|
+
if (placer.fits(ex + 7, ey - 9, w, 13)) {
|
|
65
|
+
parts.push(`<text x="${r2(ex + 7)}" y="${r2(ey + 4)}" text-anchor="start" font-size="11" font-weight="600" fill="${colors[si]}" ` +
|
|
66
|
+
`paint-order="stroke" stroke="var(--d-halo)" stroke-width="3" stroke-linejoin="round">${esc(name)}</text>`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
pushPoints(parts, pts, colors[si], rules);
|
|
70
|
+
// 多系列折线只标"每条线自己的峰值"(全标会挤成一团;全局峰值只标一个也不对)
|
|
71
|
+
if (num !== 'none') {
|
|
72
|
+
let ownPeak = 0;
|
|
73
|
+
s.values.forEach((v, pi) => {
|
|
74
|
+
if (v > s.values[ownPeak])
|
|
75
|
+
ownPeak = pi;
|
|
76
|
+
});
|
|
77
|
+
numItems.push({ px: pts[ownPeak][0], py: pts[ownPeak][1], text: fmtNum(s.values[ownPeak]), isPeak: si === peakS && ownPeak === peakP });
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
// 数值标签:全有或全无(任一位置放不下则整层不标)
|
|
81
|
+
if (numItems.length > 0)
|
|
82
|
+
pushLineLabelsAllOrNothing(parts, placer, numItems, allSegs);
|
|
83
|
+
const xStride = labelStride(spec.labels, (right - left) / Math.max(n - 1, 1));
|
|
84
|
+
spec.labels.forEach((l, i) => {
|
|
85
|
+
if (i % xStride === 0)
|
|
86
|
+
parts.push(`<text x="${r2(xs(i))}" y="${r2(xLabelY)}" text-anchor="middle" font-size="12" fill="var(--d-muted)">${esc(trunc(l, 12))}</text>`);
|
|
87
|
+
});
|
|
88
|
+
return svgDoc(width, height, 'line', version, spec.title ?? 'line chart', spec, skin, headSvg + parts.join(''));
|
|
89
|
+
}
|