@x-cmd/draw 0.5.8 → 0.5.10

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.
@@ -234,6 +234,8 @@ export function renderButterfly(spec, o) {
234
234
  }
235
235
  else {
236
236
  /* ---- 上下版:类别是列,柱自中轴往上/往下长 */
237
+ // 柱端数值的盒子(折线的数值要躲开它 ⇒ 两个数字不叠在一起 ✗)
238
+ const barLabelBoxes = [];
237
239
  const labelH = 22;
238
240
  // 往下长的柱子,数值写在柱底外侧 → 底下要给数值留一条带,
239
241
  // 否则最长的那根柱(数值到头)会和类目标签叠在一起
@@ -308,7 +310,12 @@ export function renderButterfly(spec, o) {
308
310
  if (!(v > 0))
309
311
  return;
310
312
  const vy = dir < 0 ? centreY - totalLen - 6 : centreY + totalLen + 6;
311
- tbLabels.push(`<text x="${r2(cx)}" y="${r2(vy + (dir < 0 ? 0 : 10))}" text-anchor="middle" font-size="12" font-weight="600" fill="var(--d-text)" style="${TAB}">${esc(fmtFull(v))}</text>`);
313
+ const tv = fmtFull(v);
314
+ const ty2 = vy + (dir < 0 ? 0 : 10);
315
+ tbLabels.push(`<text x="${r2(cx)}" y="${r2(ty2)}" text-anchor="middle" font-size="12" font-weight="600" fill="var(--d-text)" style="${TAB}">${esc(tv)}</text>`);
316
+ // 柱端数值的盒子(折线的数值要躲开它 ⇒ 两个数字不叠在一起 ✗)
317
+ const tw2 = textW(tv, 12);
318
+ barLabelBoxes.push({ x0: cx - tw2 / 2, x1: cx + tw2 / 2, y0: ty2 - 12, y1: ty2 + 3 });
312
319
  };
313
320
  drawSide(left, leftTot, -1);
314
321
  drawSide(right, rightTot, 1);
@@ -325,10 +332,25 @@ export function renderButterfly(spec, o) {
325
332
  if (pts.length > 1) {
326
333
  parts.push(`<path d="${pts.map((p, i) => `${i === 0 ? 'M' : 'L'}${r2(p[0])} ${r2(p[1])}`).join('')}" fill="none" stroke="var(--d-s9)" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/>`);
327
334
  pts.forEach((p) => parts.push(`<circle cx="${r2(p[0])}" cy="${r2(p[1])}" r="3" fill="var(--d-s9)" stroke="var(--d-card)" stroke-width="1.5"/>`));
335
+ // 线的数值:在点周围几个位置里挑一个**不撞柱端数值**的,并垫一块画布色
336
+ // (柱短的时候,柱端的数值就在点的旁边 ⇒ 两个数字叠在一起 ✗)
337
+ const cand = [[-16, 0], [-16, -18], [-16, 18], [14, 0], [14, -18], [14, 18]];
328
338
  line.values.forEach((v, i) => {
329
339
  const text = fmtFull(v);
330
340
  const tw = textW(text, 11);
331
- parts.push(`<rect x="${r2(pts[i][0] - tw / 2 - 4)}" y="${r2(pts[i][1] - 24)}" width="${r2(tw + 8)}" height="14" rx="4" fill="var(--d-card)" fill-opacity="0.92"/>`, `<text x="${r2(pts[i][0])}" y="${r2(pts[i][1] - 14)}" text-anchor="middle" font-size="11" font-weight="600" fill="var(--d-s9)" style="${TAB}">${esc(text)}</text>`);
341
+ let bx = pts[i][0] - tw / 2 - 4;
342
+ let by = pts[i][1] - 24;
343
+ for (const [dy, dx] of cand) {
344
+ const cx = pts[i][0] + dx - tw / 2 - 4;
345
+ const cy = pts[i][1] + dy - 11;
346
+ const hit = barLabelBoxes.some((b) => cx < b.x1 && cx + tw + 8 > b.x0 && cy < b.y1 && cy + 14 > b.y0);
347
+ if (!hit) {
348
+ bx = cx;
349
+ by = cy;
350
+ break;
351
+ }
352
+ }
353
+ parts.push(`<rect x="${r2(bx)}" y="${r2(by)}" width="${r2(tw + 8)}" height="14" rx="4" fill="var(--d-card)" fill-opacity="0.92"/>`, `<text x="${r2(bx + 4 + tw / 2)}" y="${r2(by + 11)}" text-anchor="middle" font-size="11" font-weight="600" fill="var(--d-s9)" style="${TAB}">${esc(text)}</text>`);
332
354
  });
333
355
  }
334
356
  // 柱端的数值画在折线之上
@@ -68,6 +68,18 @@ export declare const HEAD_RULES: {
68
68
  export declare function headLayout(option: Obj): void;
69
69
  /** 把用户的 option 补齐缺省(皮肤色板/字体/轴/标题/图例)—— **浏览器端也用这一份** */
70
70
  export declare function echartsOption(user: Obj, skin: Skin, width: number, height: number): EChartsOption;
71
+ /**
72
+ * 扇区/块**里面**的字:颜色跟**那一块的底色**走。
73
+ *
74
+ * 浅块用深字、深块用白字 —— 漏斗最下面那块深蓝上的字用深色就看不见了 ✗(#188)。
75
+ * 规则跟地图那一处一样(inkOf),只是这边算的是**色板**上的颜色。
76
+ *
77
+ * 只在用户没写颜色时落值(用户值恒优先)✓;
78
+ * 服务端与浏览器端**共用这一份** ⇒ 两端同图 ✓。
79
+ */
80
+ export declare function applyInsideLabelInk(option: Obj, skin: Skin): void;
81
+ /** 一块底色上的字该是什么色:底色亮 ⇒ 墨字,底色暗 ⇒ 白字 */
82
+ export declare function inkOn(hex: string): string;
71
83
  /**
72
84
  * Render a parsed ECharts option object to a skinned SVG — shared by the HTTP
73
85
  * layer (`?d=` urlencoded JSON) and the CLI (inline JSON / file / stdin, where
@@ -252,6 +252,8 @@ export function echartsOption(user, skin, width, height) {
252
252
  fillDefaults(option.legend, { textStyle: { color: 'var(--d-muted)' } });
253
253
  // 标题带/图例/绘图区排开:图例在顶部时不能压住标题(仓库硬规则:文字不得重叠)
254
254
  headLayout(option);
255
+ // 块里面的字按那一块的底色取色(漏斗那块深蓝上的字原先是深色 ⇒ 看不见 ✗)
256
+ applyInsideLabelInk(option, skin);
255
257
  // SSR 补轴:echarts SSR 不像浏览器那样为笛卡尔系列自动建坐标系,裸 option 会抛
256
258
  // 'xAxis "0" not found'。按浏览器对等行为补空轴对象({} = 缺省 x=category/y=value),
257
259
  // 仅在用户完全没写该轴时落;写了一根(多轴场景)也不动
@@ -301,6 +303,56 @@ export function echartsOption(user, skin, width, height) {
301
303
  }
302
304
  return option;
303
305
  }
306
+ /**
307
+ * 扇区/块**里面**的字:颜色跟**那一块的底色**走。
308
+ *
309
+ * 浅块用深字、深块用白字 —— 漏斗最下面那块深蓝上的字用深色就看不见了 ✗(#188)。
310
+ * 规则跟地图那一处一样(inkOf),只是这边算的是**色板**上的颜色。
311
+ *
312
+ * 只在用户没写颜色时落值(用户值恒优先)✓;
313
+ * 服务端与浏览器端**共用这一份** ⇒ 两端同图 ✓。
314
+ */
315
+ export function applyInsideLabelInk(option, skin) {
316
+ const series = Array.isArray(option.series) ? option.series : isObj(option.series) ? [option.series] : [];
317
+ for (const s of series) {
318
+ if (!isObj(s) || s.type !== 'funnel')
319
+ continue;
320
+ // 标签在块**里面**才需要跟着底色走(在外面的字压在页面上 ⇒ 用墨色 ✓)
321
+ if (!isObj(s.label) || s.label.position !== 'inside')
322
+ continue;
323
+ if (!Array.isArray(s.data))
324
+ continue;
325
+ s.data.forEach((it, i) => {
326
+ if (!isObj(it))
327
+ return;
328
+ const duo = skin.series[i];
329
+ const hex = pickHex(it, duo, skin.scheme);
330
+ if (!hex)
331
+ return;
332
+ const label = isObj(it.label) ? { ...it.label } : {};
333
+ if (label.color === undefined)
334
+ label.color = inkOn(hex);
335
+ it.label = label;
336
+ });
337
+ }
338
+ }
339
+ /** 这一块的颜色:显式写的 ?? 色板上的第 i 号(按当前明暗取那一档) */
340
+ function pickHex(item, duo, scheme) {
341
+ const own = isObj(item.itemStyle) && typeof item.itemStyle.color === 'string' ? item.itemStyle.color : undefined;
342
+ if (own)
343
+ return own;
344
+ if (typeof item.color === 'string')
345
+ return item.color;
346
+ return duo ? duo[scheme === 'dark' ? 'dark' : 'light'] : undefined;
347
+ }
348
+ /** 一块底色上的字该是什么色:底色亮 ⇒ 墨字,底色暗 ⇒ 白字 */
349
+ export function inkOn(hex) {
350
+ const m = /^#?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(hex.trim());
351
+ if (!m)
352
+ return 'var(--d-text)';
353
+ const [r, g, b] = [m[1], m[2], m[3]].map((h) => parseInt(h, 16));
354
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b < 140 ? '#ffffff' : '#0f172a';
355
+ }
304
356
  // 无障碍标签:取 title.text(与核心层 aria=标题 的约定一致)
305
357
  function ariaOf(option) {
306
358
  const t = option.title;
@@ -16,6 +16,7 @@
16
16
  // 出来的 SVG 自足,浏览器和 PNG 光栅化一个样。
17
17
  import { ApiError, clip } from '../specs/base.js';
18
18
  import { THEMES, FONT_STACK, withBackground, withFont, setSize, ensureSvgNs, ensureViewBox, } from './source-theme.js';
19
+ import { textW } from './svg.js';
19
20
  import { parseAbc, keyAlterations, beatsOf, transposeKey, AbcError } from './abc-parse.js';
20
21
  /* ---------------------------------------------------------------- 布局常数 */
21
22
  /** 一个音符占多宽 */
@@ -77,8 +78,14 @@ export async function renderJianpu(src, opts = {}) {
77
78
  head.push(`<text x="86" y="26" font-size="15" fill="${theme.muted}">${meter ? `${meter[1]}/${meter[2]}` : tune.meter}</text>`);
78
79
  if (tune.tempo)
79
80
  head.push(`<text x="140" y="26" font-size="14" fill="${theme.muted}">${esc(tune.tempo)}</text>`);
80
- if (tune.title)
81
- head.push(`<text x="${(opts.width ?? 560) / 2}" y="26" text-anchor="middle" font-size="16" font-weight="600" fill="${theme.ink}">${esc(tune.title)}</text>`);
81
+ // 标题:在**头部剩下的地方**里居中(原来在整个画布居中 ⇒ 标题长了就压到拍号/速度上 ✗)
82
+ if (tune.title) {
83
+ const headEnd = 140 + (tune.tempo ? 60 : 0); // 拍号/速度占的那一段
84
+ const tw = textW(tune.title, 16);
85
+ const avail = (opts.width ?? 560) - headEnd - 16;
86
+ const tx = tw < avail ? headEnd + avail / 2 : (opts.width ?? 560) / 2;
87
+ head.push(`<text x="${r2(tx)}" y="26" text-anchor="middle" font-size="16" font-weight="600" fill="${theme.ink}">${esc(tune.title)}</text>`);
88
+ }
82
89
  // 正文:一格一个音符
83
90
  const cells = [];
84
91
  const state = { ...keyAlts };
@@ -88,10 +95,13 @@ export async function renderJianpu(src, opts = {}) {
88
95
  const bar = () => {
89
96
  cells.push(`<line x1="${r2(x - 5)}" y1="${y - 22}" x2="${r2(x - 5)}" y2="${y + 18}" stroke="${theme.line}" stroke-width="1"/>`);
90
97
  };
98
+ // 每行几个音符:按画布宽算(死用 PER_LINE ⇒ 窄画布上音符跑出右边界 ✗)
99
+ const width = opts.width ?? 560;
100
+ const perLine = Math.max(1, Math.min(PER_LINE, Math.floor((width - 28) / SLOT)));
91
101
  // 谱杠的位置(ABC 里小节线前面的音符个数)
92
102
  const barAt = new Set(tune.bars);
93
103
  tune.notes.forEach((n, i) => {
94
- if (col >= PER_LINE) {
104
+ if (col >= perLine) {
95
105
  col = 0;
96
106
  y += LINE_GAP;
97
107
  x = 14;
@@ -103,7 +113,6 @@ export async function renderJianpu(src, opts = {}) {
103
113
  col++;
104
114
  });
105
115
  bar();
106
- const width = opts.width ?? 560;
107
116
  const height = y + 40;
108
117
  let out = `<svg xmlns="http://www.w3.org/2000/svg" width="${r2(width)}" height="${r2(height)}" viewBox="0 0 ${r2(width)} ${r2(height)}">`;
109
118
  out += head.join('');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-cmd/draw",
3
- "version": "0.5.8",
3
+ "version": "0.5.10",
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",