@x-cmd/draw 0.4.3 → 0.4.4

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.
@@ -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。
@@ -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, titleBlock, trunc, } from './svg.js';
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 = 54; // 外端数值那一列的宽
7
- const TIER_W = 34; // 档位名列的宽
6
+ const VALUE_W = 62; // 外端数值那一列的宽(具体数字要放得下 ✓)
8
7
  const BAR_H = 18;
9
8
  /** 连续同档的行算一组 */
10
9
  function tierGroups(tiers) {
@@ -21,18 +20,6 @@ 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
24
  function bar(x0, y, len, h, color, tip) {
38
25
  return [
@@ -45,6 +32,42 @@ function bar(x0, y, len, h, color, tip) {
45
32
  * 行从上到下按值排(调用方排好,这里只画);同一档的行连在一起,
46
33
  * 档与档之间一条分隔线,档名在左边那一列。
47
34
  */
35
+ /**
36
+ * 品牌色:名字里认得出来就用它的 ✓
37
+ * (NVIDIA 绿 / AMD 红 / Intel 蓝 ✓ —— 「各自的主题色」✓)
38
+ */
39
+ const BRAND_COLOR = [
40
+ [/nvidia|geforce|\brtx\b/i, '#76B900'],
41
+ [/amd|radeon|ryzen|epyc/i, '#ED1C24'],
42
+ [/intel|\barc\b|\bcore\b|xeon/i, '#0071C5'],
43
+ ];
44
+ /** 名字里认得出是哪个牌子 ✓ */
45
+ function brandOf(name) {
46
+ for (const [re, c] of BRAND_COLOR)
47
+ if (re.test(name))
48
+ return c;
49
+ return undefined;
50
+ }
51
+ /** 这一侧用什么颜色:显式给的 ✓ → 名字里的品牌 ✓ → 缺省的序列色 ✓ */
52
+ function sideColor(side, fallback) {
53
+ if (!side)
54
+ return fallback;
55
+ if (side.color)
56
+ return side.color;
57
+ return brandOf(side.name ?? '') ?? fallback;
58
+ }
59
+ /** 名字前面的牌子:要去就去掉 ✓(「GeForce RTX 5090」→「RTX 5090」✓) */
60
+ const BRAND_WORDS = /^(nvidia|geforce|amd|radeon|ryzen|epyc|intel|core|xeon|arc)\s+/i;
61
+ function stripped(label, spec) {
62
+ const s = label ?? '';
63
+ return spec.stripbrand ? s.replace(BRAND_WORDS, '') : s;
64
+ }
65
+ /** 这一张卡用什么颜色:卡自己给的 ✓ → 卡名字里的品牌 ✓ → 那一侧的颜色 ✓ */
66
+ function itemColor(item, side, fallback) {
67
+ if (item.color)
68
+ return item.color;
69
+ return brandOf(item.label ?? '') ?? sideColor(side, fallback);
70
+ }
48
71
  export function renderLadder(spec, o) {
49
72
  const { width, version, skin } = o;
50
73
  const cap = spec.labelmax ?? 22;
@@ -53,22 +76,24 @@ export function renderLadder(spec, o) {
53
76
  const n = items.length;
54
77
  if (!n)
55
78
  return svgDoc(width, o.height, 'ladder', version, 'ladder', spec, skin, '');
56
- // 行多的时候自动拉高 —— 行距压到 16 以下,行标签就叠在一起了
79
+ // 标题/图例/图 三者的位置比例 ✓ —— 天梯图往往很高,标题要压得住 ✓
80
+ const titleSvg = spec.title
81
+ ? `<text x="${PAD}" y="${PAD + 17}" font-size="22" font-weight="600" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(spec.title)}</text>`
82
+ : '';
83
+ const titleH = spec.title ? 38 : 0;
84
+ const legendItems = side?.name ? [{ name: side.name, color: sideColor(side, 'var(--d-s1)') }] : [];
85
+ const contentTop = PAD + titleH + (legendItems.length ? LEGEND_H : 0);
86
+ // 行多的时候画布自动拉高 —— 行距压到 16 以下,行标签就叠在一起了
87
+ // (h= 只是「最少这么高」✓:100 行的天梯图硬压进 480px ⇒ 后半截掉出画布 ✗)
57
88
  let height = o.height;
58
- if (o.autoHeight) {
59
- const head = PAD + (spec.title ? 32 : 0) + 8 + (side?.name ? LEGEND_H : 0);
60
- const need = head + PAD + 26 + n * 16;
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);
89
+ const need = contentTop + 8 + PAD + 26 + n * 16;
90
+ if (height < need)
91
+ height = Math.min(need, 4000);
67
92
  const avail = height - contentTop - PAD - 26;
68
93
  const pitch = Math.max(12, Math.min(26, avail / Math.max(n, 1)));
69
94
  const barH = Math.min(BAR_H, Math.max(6, pitch * 0.62));
70
95
  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 + TIER_W + labelW + 14;
96
+ const left = PAD + labelW + 14;
72
97
  const right = width - PAD - VALUE_W;
73
98
  const max = Math.max(...items.map((it) => it.value));
74
99
  const ticks = niceTicks(max, 4);
@@ -90,24 +115,23 @@ export function renderLadder(spec, o) {
90
115
  const y = contentTop + g.from * pitch;
91
116
  parts.push(`<line x1="${r2(PAD)}" y1="${r2(y)}" x2="${r2(right)}" y2="${r2(y)}" stroke="var(--d-border)" stroke-width="1"/>`);
92
117
  }
93
- parts.push(...tierLabels(groups, yOf, pitch));
94
118
  items.forEach((it, i) => {
95
119
  const y = yOf(i);
96
120
  const len = x(it.value) - left;
97
- const color = sVar(0);
121
+ const color = itemColor(it, side, sVar(0)); // 每张卡按自己的牌子 ✓
98
122
  const tip = o.interactive
99
123
  ? ` data-tip="${esc(`${it.label ?? ''} · ${fmtFull(it.value)}${spec.unit ? ' ' + spec.unit : ''}`)}"`
100
124
  : '';
101
125
  parts.push(...bar(left, y, len, barH, color, tip));
102
126
  if (it.label) {
103
- parts.push(`<text x="${r2(PAD + TIER_W + labelW)}" y="${r2(y + barH / 2 + 4)}" text-anchor="end" font-size="12.5" fill="var(--d-text)">${esc(trunc(it.label, cap))}</text>`);
127
+ parts.push(`<text 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
128
  }
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}>${fmtNum(it.value)}</text>`);
129
+ parts.push(`<text 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
130
  });
107
131
  const legend = legendItems.length
108
- ? `<g data-legend="types">${legendItems.map((l, i) => `<rect x="${r2(PAD + i * 110)}" y="${r2(contentTop - LEGEND_H + 4)}" width="14" height="14" rx="4" fill="${l.color}" fill-opacity="0.2" stroke="${l.color}" stroke-opacity="0.55"/><text x="${r2(PAD + i * 110 + 20)}" y="${r2(contentTop - LEGEND_H + 15)}" font-size="11.5" fill="var(--d-muted)">${esc(l.name)}</text>`).join('')}</g>`
132
+ ? `<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
133
  : '';
110
- return svgDoc(width, height, 'ladder', version, 'ladder', spec, skin, bg + titleBlock(spec.title) + legend + parts.join(''));
134
+ return svgDoc(width, height, 'ladder', version, 'ladder', spec, skin, bg + titleSvg + legend + parts.join(''));
111
135
  }
112
136
  /**
113
137
  * 双翼天梯图:左边 N 卡、右边 AMD/Intel。
@@ -128,20 +152,19 @@ export function renderButterflyLadder(spec, o) {
128
152
  if (!n) {
129
153
  return svgDoc(width, o.height, 'butterfly-ladder', version, 'butterfly ladder', spec, skin, '');
130
154
  }
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
155
  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,
156
+ spec.left?.name ? { name: spec.left.name, color: sideColor(spec.left, 'var(--d-s1)') } : null,
157
+ spec.right?.name ? { name: spec.right.name, color: sideColor(spec.right, 'var(--d-s2)') } : null,
143
158
  ].filter(Boolean);
144
- const contentTop = PAD + (spec.title ? 32 : 0) + 8 + (legendItems.length ? LEGEND_H : 0);
159
+ const titleSvg = spec.title
160
+ ? `<text x="${PAD}" y="${PAD + 17}" font-size="22" font-weight="600" letter-spacing="-0.02em" fill="var(--d-ink)">${esc(spec.title)}</text>`
161
+ : '';
162
+ const contentTop = PAD + (spec.title ? 38 : 0) + (legendItems.length ? LEGEND_H : 0);
163
+ // 行多的时候画布自动拉高(同上:h= 只是「最少这么高」✓)
164
+ let height = o.height;
165
+ const need = contentTop + 8 + PAD + 26 + n * 16;
166
+ if (height < need)
167
+ height = Math.min(need, 4000);
145
168
  const avail = height - contentTop - PAD - 26;
146
169
  const pitch = Math.max(12, Math.min(26, avail / Math.max(n, 1)));
147
170
  const barH = Math.min(BAR_H, Math.max(6, pitch * 0.62));
@@ -149,7 +172,7 @@ export function renderButterflyLadder(spec, o) {
149
172
  const rLabels = rightItems.map((it) => it.label ?? '');
150
173
  const lW = Math.max(0, ...lLabels.map((l) => textW(trunc(l, cap), 12.5)));
151
174
  const rW = Math.max(0, ...rLabels.map((l) => textW(trunc(l, cap), 12.5)));
152
- const plotL = PAD + TIER_W + lW + 10 + VALUE_W;
175
+ const plotL = PAD + lW + 10 + VALUE_W;
153
176
  const plotR = width - PAD - rW - 10 - VALUE_W;
154
177
  const centre = (plotL + plotR) / 2;
155
178
  const half = (plotR - plotL) / 2;
@@ -161,6 +184,16 @@ export function renderButterflyLadder(spec, o) {
161
184
  const bg = card(width, height);
162
185
  const parts = [];
163
186
  parts.push(`<line x1="${r2(centre)}" y1="${r2(contentTop)}" x2="${r2(centre)}" y2="${r2(height - PAD - 26)}" stroke="var(--d-border)"/>`);
187
+ // 标度:两翼共用一把(缺省自动挑 ✓;可以用 ticks= 指定 ✓ 如 1000,2000,3000 ✓)
188
+ const tickVals = (spec.ticks?.length ? [...spec.ticks].sort((a, b) => a - b) : niceTicks(leftMax, 4))
189
+ .filter((t) => t > 0);
190
+ const tickMax = tickVals[tickVals.length - 1] || leftMax;
191
+ const tickY = height - PAD - 26 + 16;
192
+ for (const t of tickVals) {
193
+ const dx = (t / tickMax) * half;
194
+ parts.push(`<text x="${r2(centre - dx)}" y="${r2(tickY)}" text-anchor="middle" font-size="11" fill="var(--d-muted)">${fmtNum(t)}</text>`);
195
+ parts.push(`<text x="${r2(centre + dx)}" y="${r2(tickY)}" text-anchor="middle" font-size="11" fill="var(--d-muted)">${fmtNum(t)}</text>`);
196
+ }
164
197
  const yOf = (i) => contentTop + i * pitch + (pitch - barH) / 2;
165
198
  // 档位:两侧的行是配对的,档以左边为准
166
199
  const tiers = rows.map((r) => r[0]?.tier ?? r[1]?.tier);
@@ -171,7 +204,6 @@ export function renderButterflyLadder(spec, o) {
171
204
  const y = contentTop + g.from * pitch;
172
205
  parts.push(`<line x1="${r2(PAD)}" y1="${r2(y)}" x2="${r2(width - PAD)}" y2="${r2(y)}" stroke="var(--d-border)" stroke-width="1"/>`);
173
206
  }
174
- parts.push(...tierLabels(groups, yOf, pitch));
175
207
  rows.forEach(([l, r], i) => {
176
208
  const y = yOf(i);
177
209
  if (l) {
@@ -179,32 +211,31 @@ export function renderButterflyLadder(spec, o) {
179
211
  const tip = o.interactive
180
212
  ? ` data-tip="${esc(`${l.label ?? ''} · ${fmtFull(l.value)}${spec.unit ? ' ' + spec.unit : ''}`)}"`
181
213
  : '';
182
- parts.push(...bar(centre - len, y, len, barH, sVar(0), tip));
214
+ parts.push(...bar(centre - len, y, len, barH, itemColor(l, spec.left, sVar(0)), tip));
183
215
  if (l.label) {
184
- parts.push(`<text x="${r2(plotL - VALUE_W - 10)}" y="${r2(y + barH / 2 + 4)}" text-anchor="end" font-size="12.5" fill="var(--d-text)">${esc(trunc(l.label, cap))}</text>`);
216
+ parts.push(`<text 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
217
  }
186
- // 数值:柱够长**而且够粗**就画在柱里 —— 左边的柱尾紧挨着名字列,
187
- // 画在外面会撞上;柱太薄的话字会糊在柱边上。
188
- // 左边的柱往左长,「柱里」是尾端的**右边**,所以文字从尾端往右排(start)。
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>`);
218
+ // 数值:**一律画在 bar 外面** ✓(两边一致 ✓ —— 「左边的数据在 bar 里、右边在外面」看着别扭 ✓)
219
+ // 左边的柱往左长 ⇒ 数值在尾端的左边 ✓(紧挨着名字列 ✓)
220
+ parts.push(`<text x="${r2(centre - len - 8)}" y="${r2(y + barH / 2 + 4)}" text-anchor="end" font-size="12" font-weight="600" fill="var(--d-text)">${fmtFull(l.value)}</text>`);
191
221
  }
192
222
  if (r) {
193
223
  const len = (r.value / (rightMax || 1)) * half;
194
224
  const tip = o.interactive
195
225
  ? ` data-tip="${esc(`${r.label ?? ''} · ${fmtFull(r.value)}${spec.unit ? ' ' + spec.unit : ''}`)}"`
196
226
  : '';
197
- parts.push(...bar(centre, y, len, barH, sVar(1), tip));
227
+ parts.push(...bar(centre, y, len, barH, itemColor(r, spec.right, sVar(1)), tip));
198
228
  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>`);
229
+ 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(stripped(r.label, spec), cap))}</text>`);
200
230
  }
201
- parts.push(`<text x="${r2(centre + len + 8)}" y="${r2(y + barH / 2 + 4)}" font-size="12" font-weight="600" fill="var(--d-text)">${fmtNum(r.value)}</text>`);
231
+ // 右边同样:**一律画在 bar 外面** ✓
232
+ parts.push(`<text x="${r2(centre + len + 8)}" y="${r2(y + barH / 2 + 4)}" text-anchor="start" font-size="12" font-weight="600" fill="var(--d-text)">${fmtFull(r.value)}</text>`);
202
233
  }
203
234
  });
204
235
  const legend = legendItems.length
205
- ? `<g data-legend="types">${legendItems.map((l, i) => `<rect x="${r2(PAD + i * 130)}" y="${r2(contentTop - LEGEND_H + 4)}" width="14" height="14" rx="4" fill="${l.color}" fill-opacity="0.2" stroke="${l.color}" stroke-opacity="0.55"/><text x="${r2(PAD + i * 130 + 20)}" y="${r2(contentTop - LEGEND_H + 15)}" font-size="11.5" fill="var(--d-muted)">${esc(l.name)}</text>`).join('')}</g>`
236
+ ? `<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
237
  : '';
207
- return svgDoc(width, height, 'butterfly-ladder', version, 'butterfly ladder', spec, skin, bg + titleBlock(spec.title) + legend + parts.join(''));
238
+ return svgDoc(width, height, 'butterfly-ladder', version, 'butterfly ladder', spec, skin, bg + titleSvg + legend + parts.join(''));
208
239
  }
209
240
  /** 给 draw 的注册表用:这个 spec 该走哪个渲染器 */
210
241
  export function isButterflyLadder(spec) {
@@ -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
- up = await fetch(raw, { headers: { 'user-agent': 'x-cmd/draw', accept: '*/*' }, redirect: 'follow' });
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
- throw new ApiError(502, `upstream fetch failed: ${e instanceof Error ? e.message : 'network error'}`);
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}`);
@@ -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][];
@@ -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 "value". Got: ${JSON.stringify(obj.match)}`);
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 "value". Got: ${JSON.stringify(match)}`);
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 !== 'value') {
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",
3
+ "version": "0.4.4",
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",