@x-cmd/draw 0.4.3 → 0.4.5
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/dist/render/ladder.d.ts +0 -6
- package/dist/render/ladder.js +109 -61
- package/dist/render/src-spec.js +13 -2
- package/dist/specs/ladder.d.ts +10 -2
- package/dist/specs/ladder.js +53 -6
- package/package.json +1 -1
package/dist/render/ladder.d.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
import type { LadderSpec } from '../specs/ladder.js';
|
|
2
2
|
import { type RenderOpts } from './svg.js';
|
|
3
|
-
/**
|
|
4
|
-
* 天梯图(单边)。
|
|
5
|
-
*
|
|
6
|
-
* 行从上到下按值排(调用方排好,这里只画);同一档的行连在一起,
|
|
7
|
-
* 档与档之间一条分隔线,档名在左边那一列。
|
|
8
|
-
*/
|
|
9
3
|
export declare function renderLadder(spec: LadderSpec, o: RenderOpts): string;
|
|
10
4
|
/**
|
|
11
5
|
* 双翼天梯图:左边 N 卡、右边 AMD/Intel。
|
package/dist/render/ladder.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { matchLadderRows } from '../specs/ladder.js';
|
|
2
2
|
import { TAB, card } from '../theme.js';
|
|
3
|
-
import { esc, fmtFull, fmtNum, niceTicks, r2, sVar, svgDoc, textW,
|
|
3
|
+
import { esc, fmtFull, fmtNum, niceTicks, r2, sVar, svgDoc, textW, trunc, } from './svg.js';
|
|
4
4
|
const PAD = 24;
|
|
5
5
|
const LEGEND_H = 26;
|
|
6
|
-
const VALUE_W =
|
|
7
|
-
const TIER_W = 34; // 档位名列的宽
|
|
6
|
+
const VALUE_W = 62; // 外端数值那一列的宽(具体数字要放得下 ✓)
|
|
8
7
|
const BAR_H = 18;
|
|
9
8
|
/** 连续同档的行算一组 */
|
|
10
9
|
function tierGroups(tiers) {
|
|
@@ -21,30 +20,62 @@ function tierGroups(tiers) {
|
|
|
21
20
|
return out;
|
|
22
21
|
}
|
|
23
22
|
/** 档位名列:组名竖排在组中间(组太矮就排在组首行上) */
|
|
24
|
-
function tierLabels(groups, yOf, pitch) {
|
|
25
|
-
const parts = [];
|
|
26
|
-
for (const g of groups) {
|
|
27
|
-
if (!g.tier)
|
|
28
|
-
continue;
|
|
29
|
-
const y = yOf(g.from);
|
|
30
|
-
const h = (g.to - g.from) * pitch;
|
|
31
|
-
const yy = h > 26 ? y + h / 2 : y + pitch / 2;
|
|
32
|
-
parts.push(`<text x="${r2(PAD + TIER_W / 2)}" y="${r2(yy + 4)}" text-anchor="middle" font-size="12" font-weight="600" fill="var(--d-muted)">${esc(g.tier)}</text>`);
|
|
33
|
-
}
|
|
34
|
-
return parts;
|
|
35
|
-
}
|
|
36
23
|
/** 一根柱 */
|
|
37
|
-
function bar(x0, y, len, h, color, tip) {
|
|
24
|
+
function bar(x0, y, len, h, color, tip, row = -1) {
|
|
38
25
|
return [
|
|
39
|
-
`<rect x="${r2(x0)}" y="${r2(y)}" width="${r2(Math.max(len, 1))}" height="${r2(h)}" rx="4" fill="${color}"${tip ? ` data-tip="${esc(tip)}"` : ''}/>`,
|
|
26
|
+
`<rect${row >= 0 ? ` data-row="${row}"` : ''} x="${r2(x0)}" y="${r2(y)}" width="${r2(Math.max(len, 1))}" height="${r2(h)}" rx="4" fill="${color}"${tip ? ` data-tip="${esc(tip)}"` : ''}/>`,
|
|
40
27
|
];
|
|
41
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* 行底色:鼠标悬停时**整行**亮一下 ✓
|
|
31
|
+
* (垫色,不要描边 ✓ —— 描边会像选中框 ✗)
|
|
32
|
+
* 它同时是这一行的「悬停热区」✓ —— 悬在行里的空白处也能亮 ✓。
|
|
33
|
+
*/
|
|
34
|
+
function rowBand(x, y, w, h, row, tip) {
|
|
35
|
+
return `<rect class="row-band" data-row="${row}" x="${r2(x)}" y="${r2(y)}" width="${r2(w)}" height="${r2(h)}" fill="var(--d-ink)" fill-opacity="0"${tip ? ` ${tip}` : ''}/>`;
|
|
36
|
+
}
|
|
42
37
|
/**
|
|
43
38
|
* 天梯图(单边)。
|
|
44
39
|
*
|
|
45
40
|
* 行从上到下按值排(调用方排好,这里只画);同一档的行连在一起,
|
|
46
41
|
* 档与档之间一条分隔线,档名在左边那一列。
|
|
47
42
|
*/
|
|
43
|
+
/**
|
|
44
|
+
* 品牌色:名字里认得出来就用它的 ✓
|
|
45
|
+
* (NVIDIA 绿 / AMD 红 / Intel 蓝 ✓ —— 「各自的主题色」✓)
|
|
46
|
+
*/
|
|
47
|
+
const BRAND_COLOR = [
|
|
48
|
+
[/nvidia|geforce|\brtx\b/i, '#76B900'],
|
|
49
|
+
[/amd|radeon|ryzen|epyc/i, '#ED1C24'],
|
|
50
|
+
[/intel|\barc\b|\bcore\b|xeon/i, '#0071C5'],
|
|
51
|
+
];
|
|
52
|
+
/** 名字里认得出是哪个牌子 ✓ */
|
|
53
|
+
function brandOf(name) {
|
|
54
|
+
for (const [re, c] of BRAND_COLOR)
|
|
55
|
+
if (re.test(name))
|
|
56
|
+
return c;
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
/** 这一侧用什么颜色:显式给的 ✓ → 名字里的品牌 ✓ → 缺省的序列色 ✓ */
|
|
60
|
+
function sideColor(side, fallback) {
|
|
61
|
+
if (!side)
|
|
62
|
+
return fallback;
|
|
63
|
+
if (side.color)
|
|
64
|
+
return side.color;
|
|
65
|
+
return brandOf(side.name ?? '') ?? fallback;
|
|
66
|
+
}
|
|
67
|
+
/** 名字前面的牌子:要去就去掉 ✓(「GeForce RTX 5090」→「RTX 5090」✓) */
|
|
68
|
+
const BRAND_WORDS = /^(nvidia|geforce|amd|radeon|ryzen|epyc|intel|core|xeon|arc)\s+/i;
|
|
69
|
+
function stripped(label, spec) {
|
|
70
|
+
const s = label ?? '';
|
|
71
|
+
return spec.stripbrand ? s.replace(BRAND_WORDS, '') : s;
|
|
72
|
+
}
|
|
73
|
+
/** 这一张卡用什么颜色:卡自己给的 ✓ → 卡名字里的品牌 ✓ → 那一侧的颜色 ✓ */
|
|
74
|
+
function itemColor(item, side, fallback) {
|
|
75
|
+
if (item.color)
|
|
76
|
+
return item.color;
|
|
77
|
+
return brandOf(item.label ?? '') ?? sideColor(side, fallback);
|
|
78
|
+
}
|
|
48
79
|
export function renderLadder(spec, o) {
|
|
49
80
|
const { width, version, skin } = o;
|
|
50
81
|
const cap = spec.labelmax ?? 22;
|
|
@@ -53,22 +84,24 @@ export function renderLadder(spec, o) {
|
|
|
53
84
|
const n = items.length;
|
|
54
85
|
if (!n)
|
|
55
86
|
return svgDoc(width, o.height, 'ladder', version, 'ladder', spec, skin, '');
|
|
56
|
-
//
|
|
87
|
+
// 标题/图例/图 三者的位置比例 ✓ —— 天梯图往往很高,标题要压得住 ✓
|
|
88
|
+
const titleSvg = spec.title
|
|
89
|
+
? `<text x="${PAD}" y="${PAD + 17}" font-size="22" font-weight="600" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(spec.title)}</text>`
|
|
90
|
+
: '';
|
|
91
|
+
const titleH = spec.title ? 38 : 0;
|
|
92
|
+
const legendItems = side?.name ? [{ name: side.name, color: sideColor(side, 'var(--d-s1)') }] : [];
|
|
93
|
+
const contentTop = PAD + titleH + (legendItems.length ? LEGEND_H : 0);
|
|
94
|
+
// 行多的时候画布自动拉高 —— 行距压到 16 以下,行标签就叠在一起了
|
|
95
|
+
// (h= 只是「最少这么高」✓:100 行的天梯图硬压进 480px ⇒ 后半截掉出画布 ✗)
|
|
57
96
|
let height = o.height;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (height < need)
|
|
62
|
-
height = Math.min(need, 4000);
|
|
63
|
-
}
|
|
64
|
-
const titleH = spec.title ? titleBlock(spec.title).length / 40 : 0; // 粗略,下面重算
|
|
65
|
-
const legendItems = side?.name ? [{ name: side.name, color: 'var(--d-s1)' }] : [];
|
|
66
|
-
const contentTop = PAD + (spec.title ? 32 : 0) + 8 + (legendItems.length ? LEGEND_H : 0);
|
|
97
|
+
const need = contentTop + 8 + PAD + 26 + n * 16;
|
|
98
|
+
if (height < need)
|
|
99
|
+
height = Math.min(need, 4000);
|
|
67
100
|
const avail = height - contentTop - PAD - 26;
|
|
68
101
|
const pitch = Math.max(12, Math.min(26, avail / Math.max(n, 1)));
|
|
69
102
|
const barH = Math.min(BAR_H, Math.max(6, pitch * 0.62));
|
|
70
103
|
const labelW = Math.min(190 + Math.max(0, cap - 22) * 7, Math.max(...items.map((it) => textW(trunc(it.label ?? '', cap), 12.5))) + 6);
|
|
71
|
-
const left = PAD +
|
|
104
|
+
const left = PAD + labelW + 14;
|
|
72
105
|
const right = width - PAD - VALUE_W;
|
|
73
106
|
const max = Math.max(...items.map((it) => it.value));
|
|
74
107
|
const ticks = niceTicks(max, 4);
|
|
@@ -90,24 +123,26 @@ export function renderLadder(spec, o) {
|
|
|
90
123
|
const y = contentTop + g.from * pitch;
|
|
91
124
|
parts.push(`<line x1="${r2(PAD)}" y1="${r2(y)}" x2="${r2(right)}" y2="${r2(y)}" stroke="var(--d-border)" stroke-width="1"/>`);
|
|
92
125
|
}
|
|
93
|
-
parts.push(...tierLabels(groups, yOf, pitch));
|
|
94
126
|
items.forEach((it, i) => {
|
|
95
127
|
const y = yOf(i);
|
|
96
128
|
const len = x(it.value) - left;
|
|
97
|
-
const color = sVar(0);
|
|
129
|
+
const color = itemColor(it, side, sVar(0)); // 每张卡按自己的牌子 ✓
|
|
98
130
|
const tip = o.interactive
|
|
99
131
|
? ` data-tip="${esc(`${it.label ?? ''} · ${fmtFull(it.value)}${spec.unit ? ' ' + spec.unit : ''}`)}"`
|
|
100
132
|
: '';
|
|
101
|
-
|
|
133
|
+
const row = ` data-row="${i}"`;
|
|
134
|
+
// 行底色(悬停整行亮 ✓)—— 画在柱与字之前,垫在最下面 ✓
|
|
135
|
+
parts.push(rowBand(PAD, contentTop + i * pitch, right - PAD, pitch, i, tip));
|
|
136
|
+
parts.push(...bar(left, y, len, barH, color, tip, i));
|
|
102
137
|
if (it.label) {
|
|
103
|
-
parts.push(`<text x="${r2(PAD
|
|
138
|
+
parts.push(`<text${row}${tip} x="${r2(PAD)}" y="${r2(y + barH / 2 + 4)}" text-anchor="start" font-size="12.5" fill="var(--d-text)">${esc(trunc(stripped(it.label, spec), cap))}</text>`);
|
|
104
139
|
}
|
|
105
|
-
parts.push(`<text x="${r2(right + 8)}" y="${r2(y + barH / 2 + 4)}" font-size="12" font-weight="600" fill="var(--d-text)"${tip}>${
|
|
140
|
+
parts.push(`<text${row} x="${r2(right + 8)}" y="${r2(y + barH / 2 + 4)}" font-size="12" font-weight="600" fill="var(--d-text)"${tip}>${fmtFull(it.value)}</text>`);
|
|
106
141
|
});
|
|
107
142
|
const legend = legendItems.length
|
|
108
|
-
? `<g data-legend="types">${legendItems.map((l, i) => `<rect x="${r2(PAD + i *
|
|
143
|
+
? `<g data-legend="types">${legendItems.map((l, i) => `<rect x="${r2(PAD + i * 130)}" y="${r2(contentTop - LEGEND_H + 5)}" width="15" height="15" rx="4" fill="${l.color}" fill-opacity="0.2" stroke="${l.color}" stroke-opacity="0.55"/><text x="${r2(PAD + i * 130 + 22)}" y="${r2(contentTop - LEGEND_H + 17)}" font-size="12" fill="var(--d-muted)">${esc(l.name)}</text>`).join('')}</g>`
|
|
109
144
|
: '';
|
|
110
|
-
return svgDoc(width, height, 'ladder', version, 'ladder', spec, skin, bg +
|
|
145
|
+
return svgDoc(width, height, 'ladder', version, 'ladder', spec, skin, bg + titleSvg + legend + parts.join(''));
|
|
111
146
|
}
|
|
112
147
|
/**
|
|
113
148
|
* 双翼天梯图:左边 N 卡、右边 AMD/Intel。
|
|
@@ -128,20 +163,19 @@ export function renderButterflyLadder(spec, o) {
|
|
|
128
163
|
if (!n) {
|
|
129
164
|
return svgDoc(width, o.height, 'butterfly-ladder', version, 'butterfly ladder', spec, skin, '');
|
|
130
165
|
}
|
|
131
|
-
// 行多的时候自动拉高(同上)
|
|
132
|
-
let height = o.height;
|
|
133
|
-
if (o.autoHeight) {
|
|
134
|
-
const legend = (spec.left?.name ? 1 : 0) + (spec.right?.name ? 1 : 0);
|
|
135
|
-
const head = PAD + (spec.title ? 32 : 0) + 8 + (legend ? LEGEND_H : 0);
|
|
136
|
-
const need = head + PAD + 26 + n * 16;
|
|
137
|
-
if (height < need)
|
|
138
|
-
height = Math.min(need, 4000);
|
|
139
|
-
}
|
|
140
166
|
const legendItems = [
|
|
141
|
-
spec.left?.name ? { name: spec.left.name, color: 'var(--d-s1)' } : null,
|
|
142
|
-
spec.right?.name ? { name: spec.right.name, color: 'var(--d-s2)' } : null,
|
|
167
|
+
spec.left?.name ? { name: spec.left.name, color: sideColor(spec.left, 'var(--d-s1)') } : null,
|
|
168
|
+
spec.right?.name ? { name: spec.right.name, color: sideColor(spec.right, 'var(--d-s2)') } : null,
|
|
143
169
|
].filter(Boolean);
|
|
144
|
-
const
|
|
170
|
+
const titleSvg = spec.title
|
|
171
|
+
? `<text x="${PAD}" y="${PAD + 17}" font-size="22" font-weight="600" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(spec.title)}</text>`
|
|
172
|
+
: '';
|
|
173
|
+
const contentTop = PAD + (spec.title ? 38 : 0) + (legendItems.length ? LEGEND_H : 0);
|
|
174
|
+
// 行多的时候画布自动拉高(同上:h= 只是「最少这么高」✓)
|
|
175
|
+
let height = o.height;
|
|
176
|
+
const need = contentTop + 8 + PAD + 26 + n * 16;
|
|
177
|
+
if (height < need)
|
|
178
|
+
height = Math.min(need, 4000);
|
|
145
179
|
const avail = height - contentTop - PAD - 26;
|
|
146
180
|
const pitch = Math.max(12, Math.min(26, avail / Math.max(n, 1)));
|
|
147
181
|
const barH = Math.min(BAR_H, Math.max(6, pitch * 0.62));
|
|
@@ -149,7 +183,7 @@ export function renderButterflyLadder(spec, o) {
|
|
|
149
183
|
const rLabels = rightItems.map((it) => it.label ?? '');
|
|
150
184
|
const lW = Math.max(0, ...lLabels.map((l) => textW(trunc(l, cap), 12.5)));
|
|
151
185
|
const rW = Math.max(0, ...rLabels.map((l) => textW(trunc(l, cap), 12.5)));
|
|
152
|
-
const plotL = PAD +
|
|
186
|
+
const plotL = PAD + lW + 10 + VALUE_W;
|
|
153
187
|
const plotR = width - PAD - rW - 10 - VALUE_W;
|
|
154
188
|
const centre = (plotL + plotR) / 2;
|
|
155
189
|
const half = (plotR - plotL) / 2;
|
|
@@ -161,6 +195,16 @@ export function renderButterflyLadder(spec, o) {
|
|
|
161
195
|
const bg = card(width, height);
|
|
162
196
|
const parts = [];
|
|
163
197
|
parts.push(`<line x1="${r2(centre)}" y1="${r2(contentTop)}" x2="${r2(centre)}" y2="${r2(height - PAD - 26)}" stroke="var(--d-border)"/>`);
|
|
198
|
+
// 标度:两翼共用一把(缺省自动挑 ✓;可以用 ticks= 指定 ✓ 如 1000,2000,3000 ✓)
|
|
199
|
+
const tickVals = (spec.ticks?.length ? [...spec.ticks].sort((a, b) => a - b) : niceTicks(leftMax, 4))
|
|
200
|
+
.filter((t) => t > 0);
|
|
201
|
+
const tickMax = tickVals[tickVals.length - 1] || leftMax;
|
|
202
|
+
const tickY = height - PAD - 26 + 16;
|
|
203
|
+
for (const t of tickVals) {
|
|
204
|
+
const dx = (t / tickMax) * half;
|
|
205
|
+
parts.push(`<text x="${r2(centre - dx)}" y="${r2(tickY)}" text-anchor="middle" font-size="11" fill="var(--d-muted)">${fmtNum(t)}</text>`);
|
|
206
|
+
parts.push(`<text x="${r2(centre + dx)}" y="${r2(tickY)}" text-anchor="middle" font-size="11" fill="var(--d-muted)">${fmtNum(t)}</text>`);
|
|
207
|
+
}
|
|
164
208
|
const yOf = (i) => contentTop + i * pitch + (pitch - barH) / 2;
|
|
165
209
|
// 档位:两侧的行是配对的,档以左边为准
|
|
166
210
|
const tiers = rows.map((r) => r[0]?.tier ?? r[1]?.tier);
|
|
@@ -171,40 +215,44 @@ export function renderButterflyLadder(spec, o) {
|
|
|
171
215
|
const y = contentTop + g.from * pitch;
|
|
172
216
|
parts.push(`<line x1="${r2(PAD)}" y1="${r2(y)}" x2="${r2(width - PAD)}" y2="${r2(y)}" stroke="var(--d-border)" stroke-width="1"/>`);
|
|
173
217
|
}
|
|
174
|
-
parts.push(...tierLabels(groups, yOf, pitch));
|
|
175
218
|
rows.forEach(([l, r], i) => {
|
|
176
219
|
const y = yOf(i);
|
|
220
|
+
const row = ` data-row="${i}"`;
|
|
221
|
+
// 行底色(悬停整行亮 ✓)—— 一行两翼一起亮 ✓
|
|
222
|
+
const bandTip = o.interactive
|
|
223
|
+
? ` data-tip="${esc(`${l?.label ?? ''}${r?.label ? ` · ${r.label}` : ''}`)}"`
|
|
224
|
+
: '';
|
|
225
|
+
parts.push(rowBand(PAD, contentTop + i * pitch, width - PAD * 2, pitch, i, bandTip));
|
|
177
226
|
if (l) {
|
|
178
227
|
const len = (l.value / (leftMax || 1)) * half;
|
|
179
228
|
const tip = o.interactive
|
|
180
229
|
? ` data-tip="${esc(`${l.label ?? ''} · ${fmtFull(l.value)}${spec.unit ? ' ' + spec.unit : ''}`)}"`
|
|
181
230
|
: '';
|
|
182
|
-
parts.push(...bar(centre - len, y, len, barH, sVar(0), tip));
|
|
231
|
+
parts.push(...bar(centre - len, y, len, barH, itemColor(l, spec.left, sVar(0)), tip, i));
|
|
183
232
|
if (l.label) {
|
|
184
|
-
parts.push(`<text x="${r2(
|
|
233
|
+
parts.push(`<text${row}${tip} x="${r2(PAD)}" y="${r2(y + barH / 2 + 4)}" text-anchor="start" font-size="12.5" fill="var(--d-text)">${esc(trunc(stripped(l.label, spec), cap))}</text>`);
|
|
185
234
|
}
|
|
186
|
-
//
|
|
187
|
-
//
|
|
188
|
-
|
|
189
|
-
const inBar = len > 56 && barH >= 11;
|
|
190
|
-
parts.push(`<text x="${r2(centre - len + (inBar ? 8 : -8))}" y="${r2(y + barH / 2 + 4)}" text-anchor="${inBar ? 'start' : 'end'}" font-size="12" font-weight="600" fill="${inBar ? '#fff' : 'var(--d-text)'}">${fmtNum(l.value)}</text>`);
|
|
235
|
+
// 数值:**一律画在 bar 外面** ✓(两边一致 ✓ —— 「左边的数据在 bar 里、右边在外面」看着别扭 ✓)
|
|
236
|
+
// 左边的柱往左长 ⇒ 数值在尾端的左边 ✓(紧挨着名字列 ✓)
|
|
237
|
+
parts.push(`<text${row} x="${r2(centre - len - 8)}" y="${r2(y + barH / 2 + 4)}" text-anchor="end" font-size="12" font-weight="600" fill="var(--d-text)"${tip}>${fmtFull(l.value)}</text>`);
|
|
191
238
|
}
|
|
192
239
|
if (r) {
|
|
193
240
|
const len = (r.value / (rightMax || 1)) * half;
|
|
194
241
|
const tip = o.interactive
|
|
195
242
|
? ` data-tip="${esc(`${r.label ?? ''} · ${fmtFull(r.value)}${spec.unit ? ' ' + spec.unit : ''}`)}"`
|
|
196
243
|
: '';
|
|
197
|
-
parts.push(...bar(centre, y, len, barH, sVar(1), tip));
|
|
244
|
+
parts.push(...bar(centre, y, len, barH, itemColor(r, spec.right, sVar(1)), tip, i));
|
|
198
245
|
if (r.label) {
|
|
199
|
-
parts.push(`<text x="${r2(plotR + VALUE_W + 10)}" y="${r2(y + barH / 2 + 4)}" font-size="12.5" fill="var(--d-text)">${esc(trunc(r.label, cap))}</text>`);
|
|
246
|
+
parts.push(`<text${row}${tip} x="${r2(plotR + VALUE_W + 10)}" y="${r2(y + barH / 2 + 4)}" font-size="12.5" fill="var(--d-text)">${esc(trunc(stripped(r.label, spec), cap))}</text>`);
|
|
200
247
|
}
|
|
201
|
-
|
|
248
|
+
// 右边同样:**一律画在 bar 外面** ✓
|
|
249
|
+
parts.push(`<text${row} x="${r2(centre + len + 8)}" y="${r2(y + barH / 2 + 4)}" text-anchor="start" font-size="12" font-weight="600" fill="var(--d-text)"${tip}>${fmtFull(r.value)}</text>`);
|
|
202
250
|
}
|
|
203
251
|
});
|
|
204
252
|
const legend = legendItems.length
|
|
205
|
-
? `<g data-legend="types">${legendItems.map((l, i) => `<rect x="${r2(PAD + i *
|
|
253
|
+
? `<g data-legend="types">${legendItems.map((l, i) => `<rect x="${r2(PAD + i * 140)}" y="${r2(contentTop - LEGEND_H + 5)}" width="15" height="15" rx="4" fill="${l.color}" fill-opacity="0.2" stroke="${l.color}" stroke-opacity="0.55"/><text x="${r2(PAD + i * 140 + 22)}" y="${r2(contentTop - LEGEND_H + 17)}" font-size="12" fill="var(--d-muted)">${esc(l.name)}</text>`).join('')}</g>`
|
|
206
254
|
: '';
|
|
207
|
-
return svgDoc(width, height, 'butterfly-ladder', version, 'butterfly ladder', spec, skin, bg +
|
|
255
|
+
return svgDoc(width, height, 'butterfly-ladder', version, 'butterfly ladder', spec, skin, bg + titleSvg + legend + parts.join(''));
|
|
208
256
|
}
|
|
209
257
|
/** 给 draw 的注册表用:这个 spec 该走哪个渲染器 */
|
|
210
258
|
export function isButterflyLadder(spec) {
|
package/dist/render/src-spec.js
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
// **一份实现** ✓ —— 库自己的 HTTP 层与站里的 worker 都用这个 ✓(别各抄一份 ✓)。
|
|
10
10
|
import { ApiError } from '../specs/index.js';
|
|
11
11
|
import { toRawGithub } from './onepage.js';
|
|
12
|
+
/** 取规格的上限:慢就是慢,不能一直挂着 ✓ */
|
|
13
|
+
const SRC_TIMEOUT_MS = 10_000;
|
|
12
14
|
/**
|
|
13
15
|
* 把 `?src=<url>` 取来的规格折进 `d=` —— 返回一个改好的 URL ✓
|
|
14
16
|
* (取来的 JSON 当成 d= 用 ✓;其余参数:标题、尺寸、风格照旧 ✓)。
|
|
@@ -24,10 +26,19 @@ export async function resolveSrcSpec(url) {
|
|
|
24
26
|
}
|
|
25
27
|
let up;
|
|
26
28
|
try {
|
|
27
|
-
|
|
29
|
+
// **超时要设** ✓ —— 上游慢/不回(实测 GitHub raw 偶尔十几秒 ✗),
|
|
30
|
+
// 不设的话请求一直挂着,页面转圈到天荒地老 ✗
|
|
31
|
+
up = await fetch(raw, {
|
|
32
|
+
headers: { 'user-agent': 'x-cmd/draw', accept: '*/*' },
|
|
33
|
+
redirect: 'follow',
|
|
34
|
+
signal: AbortSignal.timeout(SRC_TIMEOUT_MS),
|
|
35
|
+
});
|
|
28
36
|
}
|
|
29
37
|
catch (e) {
|
|
30
|
-
|
|
38
|
+
const timedOut = e instanceof Error && (e.name === 'TimeoutError' || e.name === 'AbortError');
|
|
39
|
+
throw new ApiError(timedOut ? 504 : 502, timedOut
|
|
40
|
+
? `upstream timed out after ${SRC_TIMEOUT_MS / 1000}s: ${raw}`
|
|
41
|
+
: `upstream fetch failed: ${e instanceof Error ? e.message : 'network error'}`);
|
|
31
42
|
}
|
|
32
43
|
if (up.status >= 400) {
|
|
33
44
|
throw new ApiError(up.status >= 500 ? 502 : 404, `upstream ${up.status} for ${raw}`);
|
package/dist/specs/ladder.d.ts
CHANGED
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
export interface LadderItem {
|
|
2
2
|
label?: string;
|
|
3
3
|
value: number;
|
|
4
|
+
/** 这一张的颜色(缺省:按名字认品牌 ✓ —— NVIDIA 绿 / AMD 红 / Intel 蓝 ✓) */
|
|
5
|
+
color?: string;
|
|
4
6
|
/** 档位(S / A / B / C / D…)。同一档的行会连在一起,档与档之间有分隔。 */
|
|
5
7
|
tier?: string;
|
|
6
8
|
}
|
|
7
9
|
export interface LadderSide {
|
|
8
10
|
name?: string;
|
|
11
|
+
/** 这一侧的颜色(缺省:按名字认品牌色 ✓ —— NVIDIA 绿 / AMD 红 / Intel 蓝 ✓) */
|
|
12
|
+
color?: string;
|
|
9
13
|
items: LadderItem[];
|
|
10
14
|
}
|
|
11
15
|
export interface LadderSpec {
|
|
@@ -13,6 +17,10 @@ export interface LadderSpec {
|
|
|
13
17
|
unit?: string;
|
|
14
18
|
/** 标签最多显示几个字(缺省 22)。名字长的数据用 labelmax= 调大。 */
|
|
15
19
|
labelmax?: number;
|
|
20
|
+
/** 标度线:要画哪些刻度(如 [1000, 2000, 3000] ✓);不给就自动挑 ✓ */
|
|
21
|
+
ticks?: number[];
|
|
22
|
+
/** 把名字前面的牌子去掉(「GeForce RTX 5090」→「RTX 5090」✓ —— 颜色已经说明了 ✓) */
|
|
23
|
+
stripbrand?: boolean;
|
|
16
24
|
/** 单边(天梯图) */
|
|
17
25
|
side?: LadderSide;
|
|
18
26
|
/** 双翼(双翼天梯图) */
|
|
@@ -24,7 +32,7 @@ export interface LadderSpec {
|
|
|
24
32
|
* 每一行只有一张卡,在它自己那一侧
|
|
25
33
|
* value —— **匹配排序**:值相近的排在同一行,两翼都有
|
|
26
34
|
*/
|
|
27
|
-
match?: 'rank' | 'value';
|
|
35
|
+
match?: 'rank' | 'value' | 'tier';
|
|
28
36
|
}
|
|
29
37
|
/** 解析 JSON 形态 */
|
|
30
38
|
export declare function parseLadderSpec(raw: string): LadderSpec;
|
|
@@ -40,4 +48,4 @@ export declare function ladderSpecFromParams(params: URLSearchParams): string |
|
|
|
40
48
|
*
|
|
41
49
|
* rank 那一种,两翼共用一把标尺(不然跨翼的长度没法比)。
|
|
42
50
|
*/
|
|
43
|
-
export declare function matchLadderRows(left: LadderItem[], right: LadderItem[], match?: 'rank' | 'value'): [LadderItem | undefined, LadderItem | undefined][];
|
|
51
|
+
export declare function matchLadderRows(left: LadderItem[], right: LadderItem[], match?: 'rank' | 'value' | 'tier'): [LadderItem | undefined, LadderItem | undefined][];
|
package/dist/specs/ladder.js
CHANGED
|
@@ -79,6 +79,13 @@ export function parseLadderSpec(raw) {
|
|
|
79
79
|
throw new ApiError(400, `"labelmax" must be a positive number.`);
|
|
80
80
|
spec.labelmax = n;
|
|
81
81
|
}
|
|
82
|
+
if (obj.ticks !== undefined) {
|
|
83
|
+
if (!Array.isArray(obj.ticks))
|
|
84
|
+
throw new ApiError(400, `"ticks" must be an array of numbers.`);
|
|
85
|
+
spec.ticks = obj.ticks.map((t) => Number(t)).filter((t) => Number.isFinite(t));
|
|
86
|
+
}
|
|
87
|
+
if (obj.stripbrand !== undefined)
|
|
88
|
+
spec.stripbrand = obj.stripbrand === true;
|
|
82
89
|
const side = parseSide(obj.side, 'side');
|
|
83
90
|
if (side)
|
|
84
91
|
spec.side = side;
|
|
@@ -90,8 +97,8 @@ export function parseLadderSpec(raw) {
|
|
|
90
97
|
spec.right = right;
|
|
91
98
|
if (obj.match !== undefined) {
|
|
92
99
|
const m = String(obj.match).toLowerCase();
|
|
93
|
-
if (m !== 'rank' && m !== 'value') {
|
|
94
|
-
throw new ApiError(400, `"match" must be "rank" or "
|
|
100
|
+
if (m !== 'rank' && m !== 'value' && m !== 'tier') {
|
|
101
|
+
throw new ApiError(400, `"match" must be "rank", "value" or "tier". Got: ${JSON.stringify(obj.match)}`);
|
|
95
102
|
}
|
|
96
103
|
spec.match = m;
|
|
97
104
|
}
|
|
@@ -122,6 +129,11 @@ export function ladderSpecFromParams(params) {
|
|
|
122
129
|
const labelmax = params.get('labelmax');
|
|
123
130
|
if (labelmax)
|
|
124
131
|
spec.labelmax = labelmax;
|
|
132
|
+
const tickRaw = params.get('ticks');
|
|
133
|
+
if (tickRaw)
|
|
134
|
+
spec.ticks = splitNumbers(tickRaw.split(',')).filter((n) => Number.isFinite(n));
|
|
135
|
+
if (params.get('stripbrand') === '1' || params.get('stripbrand') === 'true')
|
|
136
|
+
spec.stripbrand = true;
|
|
125
137
|
if (leftRaw.length || rightRaw.length) {
|
|
126
138
|
// 双翼
|
|
127
139
|
const lLabels = splitLabels(params.getAll('llabels'));
|
|
@@ -135,8 +147,8 @@ export function ladderSpecFromParams(params) {
|
|
|
135
147
|
if (right)
|
|
136
148
|
spec.right = right;
|
|
137
149
|
const match = (params.get('match') ?? 'rank').toLowerCase();
|
|
138
|
-
if (match !== 'rank' && match !== 'value') {
|
|
139
|
-
throw new ApiError(400, `"match" must be "rank" or "
|
|
150
|
+
if (match !== 'rank' && match !== 'value' && match !== 'tier') {
|
|
151
|
+
throw new ApiError(400, `"match" must be "rank", "value" or "tier". Got: ${JSON.stringify(match)}`);
|
|
140
152
|
}
|
|
141
153
|
spec.match = match;
|
|
142
154
|
}
|
|
@@ -212,7 +224,7 @@ function splitNumbers(raws) {
|
|
|
212
224
|
* rank 那一种,两翼共用一把标尺(不然跨翼的长度没法比)。
|
|
213
225
|
*/
|
|
214
226
|
export function matchLadderRows(left, right, match = 'rank') {
|
|
215
|
-
if (match
|
|
227
|
+
if (match === 'rank') {
|
|
216
228
|
const all = [
|
|
217
229
|
...left.map((item) => ({ item, side: 0 })),
|
|
218
230
|
...right.map((item) => ({ item, side: 1 })),
|
|
@@ -220,7 +232,42 @@ export function matchLadderRows(left, right, match = 'rank') {
|
|
|
220
232
|
all.sort((a, b) => b.item.value - a.item.value);
|
|
221
233
|
return all.map((x) => (x.side === 0 ? [x.item, undefined] : [undefined, x.item]));
|
|
222
234
|
}
|
|
223
|
-
|
|
235
|
+
if (match === 'tier') {
|
|
236
|
+
// **按档分块**:同一档的行连在一起 ✓
|
|
237
|
+
//
|
|
238
|
+
// 本质是「原始双翼算法(全排序 ✓)+ 一个抬升处理 ✓」:
|
|
239
|
+
// 1. **先全排序**:两边合起来按强弱排 ⇒ 档的次序、档内的次序都定了 ✓
|
|
240
|
+
// 2. **按档分块**;每块的行数 = max(左边的张数, 右边的张数) ✓
|
|
241
|
+
// ⇒ **两边都可能空档** ✓(哪边少,哪边空 ✓ —— 缺位是信号 ✓)
|
|
242
|
+
// 3. 块内:左边第 k 张对右边第 k 张 ✓
|
|
243
|
+
// ⇒ 右边的卡是「抬」到对得上的那一行的 ✓(它比左边的下一张强 ✓)
|
|
244
|
+
//
|
|
245
|
+
// 右边自己**按强弱排** ✓ ⇒ 抬升之后**顺序不变** ✓。
|
|
246
|
+
const all = [
|
|
247
|
+
...left.filter((x) => !!x).map((item) => ({ item, side: 0 })),
|
|
248
|
+
...right.filter((x) => !!x).map((item) => ({ item, side: 1 })),
|
|
249
|
+
];
|
|
250
|
+
all.sort((a, b) => b.item.value - a.item.value);
|
|
251
|
+
// 档的次序 = 全排序里第一次出现的次序 ✓;没写档的算一档 ✓
|
|
252
|
+
const blocks = [];
|
|
253
|
+
for (const { item, side } of all) {
|
|
254
|
+
const tier = item.tier ?? '';
|
|
255
|
+
let blk = blocks.find((b) => b.tier === tier);
|
|
256
|
+
if (!blk) {
|
|
257
|
+
blk = { tier, l: [], r: [] };
|
|
258
|
+
blocks.push(blk);
|
|
259
|
+
}
|
|
260
|
+
(side === 0 ? blk.l : blk.r).push(item);
|
|
261
|
+
}
|
|
262
|
+
const rows = [];
|
|
263
|
+
for (const blk of blocks) {
|
|
264
|
+
const n = Math.max(blk.l.length, blk.r.length); // 两边都可能空档 ✓
|
|
265
|
+
for (let i = 0; i < n; i++)
|
|
266
|
+
rows.push([blk.l[i], blk.r[i]]);
|
|
267
|
+
}
|
|
268
|
+
return rows;
|
|
269
|
+
}
|
|
270
|
+
// 匹配排序(value):**右边的每张卡抬升**,落到左边数值对得上的那一行 ✓
|
|
224
271
|
//
|
|
225
272
|
// 规则(定死的 ✓):对右边的一张卡 r,找**最后一个**满足 `left[n].value >= r.value`
|
|
226
273
|
// 的行 n ✓ —— 于是 `left[n] >= r > left[n+1]` ✓,右边的卡刚好夹在左右两行之间 ✓
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x-cmd/draw",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.5",
|
|
4
4
|
"description": "SVG chart rendering as a service, a library, and a CLI — pie/bar/hbar/line/area/radar/gauge/scatter/combo/sankey/qr, same params everywhere",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|