@fengwk/kchart 0.1.0 → 0.1.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.1
4
+
5
+ - Center the title and payload together both horizontally and vertically inside Event Modeling frames. Keep the payload rows left-aligned within that block.
6
+ - Draw Venn labels directly on the set and intersection fills, without label backplates or halos. Keep text clear of ring outlines and move labels below the diagram only when they cannot fit.
7
+ - Keep lines and points above columns in combo charts and Mermaid XY charts. Add a thin outline only where a line overlaps a low-contrast column.
8
+ - Raise warning-label contrast in the Cloud Music theme.
9
+ - Report an invalid top-level chart `id` once, while continuing to support row identifiers through `encode.id`.
10
+
3
11
  ## 0.1.0
4
12
 
5
13
  - First npm release with an asynchronous browser/Node ESM API, on-demand chart and diagram families, and separately loaded built-in maps.
@@ -14,7 +14,7 @@ var fg4 = "#726b6b";
14
14
  var accent = "#bc2420";
15
15
  var info = "#32669a";
16
16
  var ok = "#397949";
17
- var warn = "#9b641a";
17
+ var warn = "#8d5a17";
18
18
  var danger = "#921f3c";
19
19
  var ext = "#655f5e";
20
20
  var tones = [
@@ -1,6 +1,7 @@
1
1
  import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
2
  import { register } from "./registry.js";
3
3
  import { columnHeaders } from "../core/geometry.js";
4
+ import { contrast, pushContrast } from "../core/color.js";
4
5
  // src/chart/multivariate.js
5
6
  var multivariate_exports = /* @__PURE__ */ __exportAll({
6
7
  diagramCombo: () => diagramCombo,
@@ -233,6 +234,7 @@ var chart = (spec, ctx) => {
233
234
  return scene;
234
235
  }
235
236
  if (spec.chart === "combo") {
237
+ const diagramPanel = spec[diagramCombo];
236
238
  const cats = ctx.table(spec).categories, layers = spec.layers;
237
239
  const axisFields = (axis) => [...new Set(layers.filter((l) => l.axis === axis).map((l) => l.field))];
238
240
  const leftFields = axisFields("left"), rightFields = axisFields("right");
@@ -282,16 +284,19 @@ var chart = (spec, ctx) => {
282
284
  if (axisTicks.right) children.push(ctx.text(p.x + p.w + 8, y + 4, axisTicks.right.labels[i], { size: 12 }));
283
285
  });
284
286
  children.push(ctx.line(p.x, p.y + p.h, p.x + p.w, p.y + p.h, { stroke: ctx.color("axis") }));
285
- const overColumns = layers.some((l) => l.mark === "column");
286
- layers.forEach((layer, li) => {
287
- const vals = allValues(layer.field), xs = cats.map((_, i) => bandX(i, cats.length, p)), ys = vals.map((v) => finite(v) ? yFor(layer.field, v) : NaN), color = ctx.color(`tone${li % 6 + 1}`);
287
+ const xs = cats.map((_, i) => bandX(i, cats.length, p));
288
+ const traces = [], columnLayers = [];
289
+ layers.forEach((layer, index) => {
290
+ const vals = allValues(layer.field), ys = vals.map((v) => finite(v) ? yFor(layer.field, v) : NaN);
291
+ const color = ctx.color(`tone${index % 6 + 1}`);
288
292
  if (layer.mark === "column") {
289
- const group = layers.filter((l) => l.mark === "column" && l.axis === layer.axis), slot = group.indexOf(layer), bw = p.w / cats.length * .72 / group.length;
290
- vals.forEach((v, i) => {
291
- if (!finite(v)) return;
292
- const y = ys[i], base = y0(layer.field), left = xs[i] - bw * group.length / 2 + slot * bw;
293
- children.push(ctx.rect(left, Math.min(y, base), bw * .88, Math.max(1, Math.abs(base - y)), { fill: color }));
293
+ columnLayers.push({
294
+ layer,
295
+ index,
296
+ ys,
297
+ color
294
298
  });
299
+ return;
295
300
  }
296
301
  if (layer.mark === "area") {
297
302
  let segment = [];
@@ -310,37 +315,47 @@ var chart = (spec, ctx) => {
310
315
  segment = [];
311
316
  }
312
317
  };
313
- vals.forEach((v, i) => {
314
- if (finite(v)) segment.push([xs[i], ys[i]]);
318
+ ys.forEach((y, i) => {
319
+ if (finite(y)) segment.push([xs[i], y]);
315
320
  else close();
316
321
  });
317
322
  close();
318
323
  }
319
- if (layer.mark === "line" || layer.mark === "area") {
320
- const d = linePath(vals, xs, (v) => yFor(layer.field, v));
321
- if (overColumns) children.push(ctx.path(d, {
322
- fill: "none",
323
- stroke: ctx.palette.bg,
324
- "stroke-width": 5,
325
- "stroke-linejoin": "round",
326
- "stroke-linecap": "round"
327
- }));
328
- children.push(ctx.path(d, {
329
- fill: "none",
330
- stroke: color,
331
- "stroke-width": 2
332
- }));
333
- vals.forEach((v, i) => {
334
- if (finite(v)) children.push(ctx.circle(xs[i], ys[i], 2.6, {
335
- fill: color,
336
- ...overColumns ? {
337
- stroke: ctx.palette.bg,
338
- "stroke-width": 1.5
339
- } : {}
340
- }));
341
- });
342
- }
324
+ const points = ys.flatMap((y, i) => finite(y) ? [[xs[i], y]] : []);
325
+ if (points.length) traces.push({
326
+ d: linePath(vals, xs, (v) => yFor(layer.field, v)),
327
+ points,
328
+ color
329
+ });
343
330
  });
331
+ const traceMarks = ({ d, points }, ink, rim = false) => [ctx.path(d, {
332
+ fill: "none",
333
+ stroke: ink,
334
+ "stroke-width": rim ? 4 : 2,
335
+ ...rim ? {
336
+ "stroke-linejoin": "round",
337
+ "stroke-linecap": "round"
338
+ } : {}
339
+ }), ...points.map(([x, y]) => ctx.circle(x, y, rim ? 3.6 : 2.6, { fill: ink }))];
340
+ const markContrast = 3;
341
+ for (const { layer, index, ys, color } of columnLayers) {
342
+ const group = columnLayers.filter((item) => item.layer.axis === layer.axis);
343
+ const slot = group.findIndex((item) => item.index === index), bw = p.w / cats.length * .72 / group.length;
344
+ const base = y0(layer.field);
345
+ const boxes = ys.flatMap((y, i) => finite(y) ? [[
346
+ xs[i] - bw * group.length / 2 + slot * bw,
347
+ Math.min(y, base),
348
+ bw * .88,
349
+ Math.max(1, Math.abs(base - y))
350
+ ]] : []);
351
+ for (const box of boxes) children.push(ctx.rect(...box, { fill: color }));
352
+ const lowContrast = traces.filter((trace) => contrast(trace.color, color) < markContrast);
353
+ if (!boxes.length || !lowContrast.length) continue;
354
+ const clip = `combo-column-${diagramPanel ? `p${diagramPanel.index}-` : ""}${index}`;
355
+ children.push(ctx.el("defs", {}, [ctx.el("clipPath", { id: clip }, boxes.map((box) => ctx.rect(...box)))]));
356
+ children.push(ctx.el("g", { "clip-path": `url(#${clip})` }, lowContrast.flatMap((trace) => traceMarks(trace, pushContrast(trace.color, ctx.color("bg"), trace.color, markContrast), true))));
357
+ }
358
+ for (const trace of traces) children.push(...traceMarks(trace, trace.color));
344
359
  const comboNotes = [];
345
360
  for (const a of spec.annotations || []) {
346
361
  const layer = layers.find((l) => l.field === a.field);
@@ -390,7 +405,7 @@ var chart = (spec, ctx) => {
390
405
  }));
391
406
  });
392
407
  catLabels(cats, p);
393
- const showAxisInLegend = !spec[diagramCombo] || leftFields.length > 0 && rightFields.length > 0;
408
+ const showAxisInLegend = !diagramPanel || leftFields.length > 0 && rightFields.length > 0;
394
409
  const lh = legend(layers.map((l, i) => {
395
410
  const meta = ctx.field(l.field), unit = meta.unit || (meta.format === "percent" ? "%" : "");
396
411
  const axis = l.axis === "right" ? msg("右轴", "right axis") : msg("左轴", "left axis");
@@ -163,7 +163,11 @@ function validate(spec) {
163
163
  data: null
164
164
  };
165
165
  }
166
- keys(spec, TOP, "", errors);
166
+ for (const field of Object.keys(spec)) {
167
+ if (TOP.has(field)) continue;
168
+ if (field === "id") err(errors, "/id", "图表声明不接受根级 id", "移除根级 id;要标识数据行时,若图型支持,使用 encode.id 映射到行字段。");
169
+ else err(errors, ptr([field]), "未知字段");
170
+ }
167
171
  const c = typeof spec.chart === "string" ? catalog[spec.chart] : void 0;
168
172
  if (!c) err(errors, "/chart", "未知图型", "chart 必须是注册图型名。");
169
173
  for (const k of [
@@ -221,7 +225,7 @@ function validate(spec) {
221
225
  "interaction",
222
226
  "encode"
223
227
  ]);
224
- for (const field of Object.keys(spec)) if (!commonFields.has(field) && c.special[field] === void 0 && !(field === "unitMark" && [
228
+ for (const field of Object.keys(spec)) if (TOP.has(field) && !commonFields.has(field) && c.special[field] === void 0 && !(field === "unitMark" && [
225
229
  "bar",
226
230
  "column",
227
231
  "dumbbell",
@@ -16,6 +16,7 @@ var LANE_GAP = 12;
16
16
  var TRACK = 7;
17
17
  var SLOT = 6;
18
18
  var BOX_PAD = 10;
19
+ var GLYPH_GAP = 6;
19
20
  var SIDE_INSET = 12;
20
21
  var MIN_LANE_H = 64;
21
22
  var MIN_BOX_H = 48;
@@ -23,6 +24,7 @@ var MIN_BOX_W = 132;
23
24
  var MAX_BOX_W = 420;
24
25
  var NAME_STEP = 18;
25
26
  var DATA_STEP = 16;
27
+ var DATA_GAP = 4;
26
28
  var CAPTION_STEP = 18;
27
29
  var NAME_FONT = Object.freeze({
28
30
  size: 13,
@@ -478,11 +480,12 @@ function layout(model, options = {}, info = {}) {
478
480
  const boxes = model.frames.map((frame, index) => {
479
481
  const spec = TYPES[frame.type];
480
482
  const glyphW = Math.ceil(measure(spec.glyph, GLYPH_FONT));
481
- const nameLines = wrapText(frame.name, maxText - glyphW - 6, NAME_FONT, measure);
483
+ const nameLines = wrapText(frame.name, maxText - glyphW - GLYPH_GAP, NAME_FONT, measure);
482
484
  const data = dataLinesOf(frame, frame.dataRef ? blocks.get(frame.dataRef) : null, measure, maxText, narrow);
483
- const inner = Math.max(maxWidthOf(nameLines, NAME_FONT, measure) + glyphW + 6, data ? maxWidthOf(data.lines, DATA_FONT, measure) : 0);
484
- const w = Math.min(MAX_BOX_W, Math.max(MIN_BOX_W, snap(inner + 20)));
485
- const h = Math.max(MIN_BOX_H, snap(20 + nameLines.length * NAME_STEP + (data ? 4 + data.lines.length * DATA_STEP : 0)));
485
+ const contentW = Math.max(maxWidthOf(nameLines, NAME_FONT, measure) + glyphW + GLYPH_GAP, data ? maxWidthOf(data.lines, DATA_FONT, measure) : 0);
486
+ const w = Math.min(MAX_BOX_W, Math.max(MIN_BOX_W, snap(contentW + 20)));
487
+ const contentH = nameLines.length * NAME_STEP + (data?.lines.length ? DATA_GAP + data.lines.length * DATA_STEP : 0);
488
+ const h = Math.max(MIN_BOX_H, snap(20 + contentH));
486
489
  return {
487
490
  frame,
488
491
  index,
@@ -491,6 +494,8 @@ function layout(model, options = {}, info = {}) {
491
494
  glyphW,
492
495
  nameLines,
493
496
  data,
497
+ contentW,
498
+ contentH,
494
499
  w,
495
500
  h,
496
501
  x: 0,
@@ -865,6 +870,10 @@ function scene(geometry, ctx) {
865
870
  const stroke = tone ? ctx.tint(look.bg, tone, .45) : look.line2;
866
871
  const ink = ctx.textOn(fill);
867
872
  const glyphInk = tone ? ctx.textColor(tone) : look.fg3;
873
+ const contentX = box.x + Math.max(BOX_PAD, (box.w - box.contentW) / 2);
874
+ const contentY = box.y + (box.h - box.contentH) / 2;
875
+ const nameX = contentX + box.glyphW + GLYPH_GAP;
876
+ const nameY = contentY + NAME_STEP / 2;
868
877
  const parts = [ctx.rect(box.x, box.y, box.w, box.h, {
869
878
  fill,
870
879
  stroke,
@@ -874,22 +883,22 @@ function scene(geometry, ctx) {
874
883
  kind: "node",
875
884
  id: box.frame.id
876
885
  }
877
- }), ctx.text(box.x + BOX_PAD, box.y + BOX_PAD + NAME_STEP / 2, box.glyph, {
886
+ }), ctx.text(contentX, nameY, box.glyph, {
878
887
  size: GLYPH_FONT.size,
879
888
  weight: GLYPH_FONT.weight,
880
889
  fill: glyphInk,
881
890
  anchor: "start",
882
891
  "dominant-baseline": "central"
883
892
  })];
884
- for (const [index, line] of box.nameLines.entries()) parts.push(ctx.text(box.x + BOX_PAD + box.glyphW + 6, box.y + BOX_PAD + NAME_STEP / 2 + index * NAME_STEP, line, {
893
+ for (const [index, line] of box.nameLines.entries()) parts.push(ctx.text(nameX, nameY + index * NAME_STEP, line, {
885
894
  size: NAME_FONT.size,
886
895
  weight: NAME_FONT.weight,
887
896
  fill: ink,
888
897
  anchor: "start",
889
898
  "dominant-baseline": "central"
890
899
  }));
891
- const dataTop = box.y + BOX_PAD + box.nameLines.length * NAME_STEP + 4;
892
- for (const [index, line] of (box.data?.lines || []).entries()) parts.push(ctx.text(box.x + BOX_PAD, dataTop + DATA_STEP / 2 + index * DATA_STEP, line, {
900
+ const dataTop = contentY + box.nameLines.length * NAME_STEP + DATA_GAP;
901
+ for (const [index, line] of (box.data?.lines || []).entries()) parts.push(ctx.text(contentX, dataTop + DATA_STEP / 2 + index * DATA_STEP, line, {
893
902
  size: DATA_FONT.size,
894
903
  weight: DATA_FONT.weight,
895
904
  fill: ink,
@@ -296,7 +296,7 @@ function parseXychart(source, info) {
296
296
  errors
297
297
  };
298
298
  }
299
- function specOf(model, info) {
299
+ function specOf(model, info, panelIndex = 0) {
300
300
  const en = isEnglish(info?.lang);
301
301
  const subtitle = [model.xLabel, model.yLabel].filter(Boolean).join(" · ") || void 0;
302
302
  const domain = model.yDomain ? { domain: model.yDomain } : {};
@@ -304,7 +304,7 @@ function specOf(model, info) {
304
304
  const data = model.categories.map((category, index) => Object.fromEntries([["category", category], ...model.datasets.map((dataset, series) => [`v${series}`, dataset.values[index]])]));
305
305
  return {
306
306
  chart: "combo",
307
- [diagramCombo]: true,
307
+ [diagramCombo]: { index: panelIndex },
308
308
  ...model.title ? { title: model.title } : {},
309
309
  ...subtitle ? { subtitle } : {},
310
310
  data,
@@ -391,7 +391,7 @@ function panelsOf(model, info) {
391
391
  }
392
392
  var validate = (model, info) => panelsOf(model, info).flatMap((panel) => validateWith(specOf(panel, info), info, (detail) => lineFor(panel, detail)));
393
393
  var layout = (model, options, info) => {
394
- const results = panelsOf(model, info).map((panel) => renderWith(specOf(panel, info), options, info, (detail) => lineFor(panel, detail)));
394
+ const results = panelsOf(model, info).map((panel, index) => renderWith(specOf(panel, info, index), options, info, (detail) => lineFor(panel, detail)));
395
395
  if (results.some((result) => !result.scene)) return results.find((result) => !result.scene);
396
396
  const result = results[0];
397
397
  if (results.length > 1) {
@@ -1,4 +1,5 @@
1
1
  import { __exportAll } from "../_virtual/_rolldown/runtime.js";
2
+ import { contrast, pushContrast } from "../core/color.js";
2
3
  import { FONT, appearance, error, fallbackMeasure, warning, wrapText } from "./support.js";
3
4
  import { normalizeLabel } from "./label.js";
4
5
  import { presentation } from "./presentation.js";
@@ -14,7 +15,6 @@ var ITEM_GAP = 4;
14
15
  var SET_CLEAR = 12;
15
16
  var UNION_CLEAR = 10;
16
17
  var INK_GAP = 4;
17
- var CHIP_Y = 2;
18
18
  var SHELF_GAP = 26;
19
19
  var SHELF_PITCH = 28;
20
20
  var LEADER = 8;
@@ -25,6 +25,8 @@ var GRID_COLS = 3;
25
25
  var ITEM_CAP = 12;
26
26
  var SIZE_DEFAULT = 10;
27
27
  var RING_STROKE = 1.5;
28
+ var RING_OPACITY = .1;
29
+ var TEXT_CONTRAST = 4.5;
28
30
  var SPREADS = 17;
29
31
  var GROW = 1.12;
30
32
  var MAX_GROWTH = 6;
@@ -45,6 +47,10 @@ var ITEM_FONT = {
45
47
  size: FONT.label.size,
46
48
  weight: FONT.label.weight
47
49
  };
50
+ var UNION_FONT = {
51
+ ...ITEM_FONT,
52
+ weight: 600
53
+ };
48
54
  function stripComment(line) {
49
55
  let quote = false;
50
56
  for (let i = 0; i < line.length; i += 1) {
@@ -430,64 +436,37 @@ function boundsOf(rings) {
430
436
  }
431
437
  function planBlock(region, measure) {
432
438
  const slack = region.kind === "set" ? SET_CLEAR : UNION_CLEAR;
433
- const pad = region.kind === "set" ? 0 : 14;
434
- const texts = [...region.labels.map((label) => label.text), ...region.items.map((item) => item.text)];
435
- const font = region.kind === "set" ? SET_FONT : ITEM_FONT;
436
- const floor = Math.max(0, ...texts.flatMap((text) => String(text).split(/\s+/).filter(Boolean).map((word) => measure(word, font))));
437
- const cap = Math.max(48, floor + 2, (region.clear - slack) * 2 - 20 - pad);
438
- const wrap = (value, spec) => wrapText(value, cap, spec, (piece, spec2) => measure(piece, spec2));
439
- const labelFont = region.kind === "set" ? SET_FONT : ITEM_FONT;
440
- const labels = [];
441
- for (const label of region.labels) for (const row of wrap(label.text, labelFont)) labels.push({
442
- text: row,
443
- font: labelFont,
444
- icon: label.icon,
445
- link: label.link
446
- });
447
- const items = [];
448
- for (const item of region.items) for (const row of wrap(item.text, ITEM_FONT)) items.push({
449
- text: row,
450
- font: ITEM_FONT,
451
- icon: item.icon,
452
- link: item.link,
453
- status: item.status
454
- });
455
- const widest = (rows) => rows.length ? Math.max(...rows.map((row) => measure(row.text, row.font) + (row.icon ? 18 : 0))) : 0;
456
- const labelW = labels.length ? widest(labels) + pad : 0;
457
- const labelH = labels.length * ((region.kind === "set" ? HEAD_LINE : ITEM_LINE) + 4 / labels.length);
458
- const itemW = widest(items);
459
- const itemH = items.length * ITEM_LINE;
460
- const w = Math.max(labelW, itemW);
461
- const h = labelH + (items.length ? (labels.length ? ITEM_GAP : 0) + itemH : 0);
462
- const block = {
439
+ const labelFont = region.kind === "set" ? SET_FONT : UNION_FONT;
440
+ const floorOf = (entries, font) => Math.max(0, ...entries.flatMap(({ text }) => String(text).split(/\s+/).filter(Boolean).map((word) => measure(word, font))));
441
+ const cap = Math.max(48, floorOf(region.labels, labelFont) + 2, floorOf(region.items, ITEM_FONT) + 2, (region.clear - slack) * 2 - 20);
442
+ const rows = (entries, font, status) => entries.flatMap((entry) => wrapText(entry.text, cap, font, measure).map((text) => ({
443
+ text,
444
+ font,
445
+ icon: entry.icon,
446
+ link: entry.link,
447
+ status: entry.status || status
448
+ })));
449
+ const labels = rows(region.labels, labelFont, region.status);
450
+ const items = rows(region.items, ITEM_FONT, labels.length ? null : region.status);
451
+ const labelH = labels.length * (region.kind === "set" ? HEAD_LINE : ITEM_LINE);
452
+ const w = Math.max(0, ...[...labels, ...items].map((row) => rowWidth(row, measure)));
453
+ const h = labelH + (labels.length && items.length ? ITEM_GAP : 0) + items.length * ITEM_LINE;
454
+ return {
463
455
  labels,
464
456
  items,
465
- slack,
466
- pad,
467
- labelW,
468
457
  labelH,
469
- itemW,
470
- itemH,
471
458
  w,
472
459
  h,
473
- chip: region.kind !== "set" && labels.length > 0
460
+ room: h ? blockClearance(region, w, h) - INK_GAP : Infinity
474
461
  };
475
- block.room = h ? borderClearance(region, w, h) - INK_GAP : Infinity;
476
- block.fits = block.room >= -.5;
477
- return block;
478
462
  }
479
- function borderClearance(region, w, h) {
463
+ function rowWidth(row, measure) {
464
+ return measure(row.text, row.font) + (row.icon ? 18 : 0);
465
+ }
466
+ function blockClearance(region, w, h) {
480
467
  let worst = Infinity;
481
- for (const [sx, sy] of [
482
- [-1, -1],
483
- [1, -1],
484
- [1, 1],
485
- [-1, 1],
486
- [0, -1],
487
- [1, 0],
488
- [0, 1],
489
- [-1, 0]
490
- ]) worst = Math.min(worst, clearanceAt(region.x + sx * w / 2, region.y + sy * h / 2, region.inside, region.outside));
468
+ for (const ring of region.inside) worst = Math.min(worst, ring.r - Math.hypot(Math.abs(region.x - ring.cx) + w / 2, Math.abs(region.y - ring.cy) + h / 2));
469
+ for (const ring of region.outside) worst = Math.min(worst, Math.hypot(Math.max(0, Math.abs(region.x - ring.cx) - w / 2), Math.max(0, Math.abs(region.y - ring.cy) - h / 2)) - ring.r);
491
470
  return worst;
492
471
  }
493
472
  function regionList(rings, model) {
@@ -522,11 +501,14 @@ function regionList(rings, model) {
522
501
  for (const union of model.unions) {
523
502
  const key = union.ids.join("|");
524
503
  const existing = merged.get(key);
504
+ const labels = union.label || union.icon ? [{
505
+ text: union.label,
506
+ icon: union.icon,
507
+ line: union.line,
508
+ link: union.link
509
+ }] : [];
525
510
  if (existing) {
526
- if (union.label) existing.labels.push({
527
- text: union.label,
528
- line: union.line
529
- });
511
+ existing.labels.push(...labels);
530
512
  continue;
531
513
  }
532
514
  const region = {
@@ -535,12 +517,7 @@ function regionList(rings, model) {
535
517
  ids: union.ids,
536
518
  line: union.line,
537
519
  tone: rings.find((ring) => ring.id === union.ids[0])?.tone || 1,
538
- labels: union.label ? [{
539
- text: union.label,
540
- icon: union.icon,
541
- line: union.line,
542
- link: union.link
543
- }] : [],
520
+ labels,
544
521
  items: (groups.get(key) || []).map((text) => ({
545
522
  text: text.label,
546
523
  icon: text.icon,
@@ -581,24 +558,23 @@ function chooseSpread(ringsFor, regions, measure, preferred) {
581
558
  order.sort((left, right) => Math.abs(left - preferred) - Math.abs(right - preferred) || right - left);
582
559
  let best = null;
583
560
  for (const spread of order) {
584
- bindRegions(regions, ringsFor(spread));
585
- const worst = roomOf(regions, measure, true);
561
+ const rings = ringsFor(spread);
562
+ bindRegions(regions, rings);
563
+ const coarse = roomOf(regions, measure, true);
564
+ const worst = coarse >= 0 ? roomOf(regions, measure, false) : coarse;
565
+ if (worst >= 0) return {
566
+ rings,
567
+ worst
568
+ };
586
569
  if (!best || worst > best.worst + 1e-9) best = {
587
570
  spread,
588
571
  worst
589
572
  };
590
- if (worst >= 0) {
591
- bindRegions(regions, ringsFor(spread));
592
- roomOf(regions, measure, false);
593
- return {
594
- rings: ringsFor(spread),
595
- worst
596
- };
597
- }
598
573
  }
599
- bindRegions(regions, ringsFor(best.spread));
574
+ const rings = ringsFor(best.spread);
575
+ bindRegions(regions, rings);
600
576
  return {
601
- rings: ringsFor(best.spread),
577
+ rings,
602
578
  worst: roomOf(regions, measure, false)
603
579
  };
604
580
  }
@@ -606,33 +582,15 @@ function layoutBlock(region) {
606
582
  const block = region.block;
607
583
  const top = region.y - block.h / 2;
608
584
  const pitch = region.kind === "set" ? HEAD_LINE : ITEM_LINE;
609
- region.rows = [];
610
- region.chip = null;
611
- if (block.labels.length) {
612
- region.chip = {
613
- x: region.x - block.labelW / 2,
614
- y: top,
615
- w: block.labelW,
616
- h: block.labelH
617
- };
618
- block.labels.forEach((row, index) => region.rows.push({
619
- text: row.text,
620
- font: row.font,
621
- icon: row.icon,
622
- link: row.link,
623
- status: row.status,
624
- x: region.x,
625
- y: top + CHIP_Y + (index + .5) * pitch,
626
- anchor: "middle"
627
- }));
628
- }
585
+ region.rows = block.labels.map((row, index) => ({
586
+ ...row,
587
+ x: region.x,
588
+ y: top + (index + .5) * pitch,
589
+ anchor: "middle"
590
+ }));
629
591
  const itemTop = top + block.labelH + (block.labels.length && block.items.length ? ITEM_GAP : 0);
630
592
  block.items.forEach((row, index) => region.rows.push({
631
- text: row.text,
632
- font: row.font,
633
- icon: row.icon,
634
- link: row.link,
635
- status: row.status,
593
+ ...row,
636
594
  x: region.x,
637
595
  anchor: "middle",
638
596
  y: itemTop + (index + .5) * ITEM_LINE
@@ -655,18 +613,11 @@ function planRings(model, { measure, width, lang }) {
655
613
  const rings = chosen.rings;
656
614
  const shelf = [];
657
615
  for (const region of regions) {
658
- if (region.block.fits) continue;
616
+ if (region.block.room >= 0) continue;
659
617
  shelf.push({
660
618
  region,
661
619
  rows: [...region.block.labels, ...region.block.items]
662
620
  });
663
- region.block = {
664
- ...region.block,
665
- labels: [],
666
- labelW: 0,
667
- labelH: 0,
668
- chip: false
669
- };
670
621
  }
671
622
  const claimed = new Set(regions.map((region) => region.key));
672
623
  for (const text of model.texts) {
@@ -681,7 +632,10 @@ function planRings(model, { measure, width, lang }) {
681
632
  },
682
633
  rows: [{
683
634
  text: text.label,
684
- font: ITEM_FONT
635
+ font: ITEM_FONT,
636
+ icon: text.icon,
637
+ link: text.link,
638
+ status: text.status
685
639
  }]
686
640
  });
687
641
  }
@@ -692,7 +646,7 @@ function planRings(model, { measure, width, lang }) {
692
646
  notes.push(warning("DIAGRAM_HINT", lang, region.texts[ITEM_CAP].line, `「${region.key}」区域有 ${region.texts.length} 个 text,超过 ${ITEM_CAP} 个后很难阅读`, `Region "${region.key}" holds ${region.texts.length} text nodes; more than ${ITEM_CAP} is hard to read`, "把说明移到图外,或拆成多张图", "Move details outside, or split the diagram"));
693
647
  break;
694
648
  }
695
- const drawn = regions.filter((region) => region.block.h > 0 || region.block.labels.length);
649
+ const drawn = regions.filter((region) => region.block.h > 0 && region.block.room >= 0);
696
650
  for (const region of drawn) layoutBlock(region);
697
651
  return finish({
698
652
  mode: "rings",
@@ -876,20 +830,16 @@ function finish(plan, model, { measure, width }) {
876
830
  text: item.region.caption,
877
831
  font: ITEM_FONT,
878
832
  kind: "caption"
879
- }] : [], ...item.rows.map((row) => ({
880
- ...row,
881
- kind: row.font === SET_FONT ? "label" : "item"
882
- }))];
883
- const w = Math.max(0, ...rows.map((row) => measure(row.text, row.font)));
833
+ }] : [], ...item.rows];
834
+ const w = Math.max(0, ...rows.map((row) => rowWidth(row, measure)));
884
835
  if (cursor > plan.plot.x && cursor + w > plan.plot.x + limit) {
885
836
  cursor = plan.plot.x;
886
837
  top += rowH + GRID_GAP / 2;
887
838
  rowH = 0;
888
839
  }
889
840
  const lines = rows.map((row, index) => ({
890
- text: row.text,
891
- font: row.font,
892
- kind: row.kind,
841
+ ...row,
842
+ anchor: "start",
893
843
  x: cursor,
894
844
  y: top + (index + .5) * ITEM_LINE
895
845
  }));
@@ -932,10 +882,6 @@ function finish(plan, model, { measure, width }) {
932
882
  for (const region of plan.regions || []) {
933
883
  region.x += dx;
934
884
  region.y += dy;
935
- if (region.chip) {
936
- region.chip.x += dx;
937
- region.chip.y += dy;
938
- }
939
885
  }
940
886
  for (const item of shelf) {
941
887
  for (const line of item.lines) {
@@ -1003,8 +949,13 @@ function scene(plan, ctx) {
1003
949
  const look = appearance(ctx);
1004
950
  const paper = ctx.color("bg");
1005
951
  const tone = (index) => ctx.frameTone(ctx.color(`tone${(index - 1) % 6 + 1}`));
1006
- const row = (item) => {
1007
- const ink = item.status ? ctx.textColor(look[item.status]) : item.kind === "caption" ? look.fg3 : item.font === SET_FONT ? look.fg1 : look.fg2;
952
+ const row = (item, background) => {
953
+ let ink = item.status ? ctx.textColor(look[item.status]) : item.kind === "caption" ? look.fg3 : item.font === SET_FONT ? look.fg1 : look.fg2;
954
+ if (background && contrast(ink, background) < TEXT_CONTRAST) {
955
+ let target = ctx.textOn(background);
956
+ if (contrast(target, background) < TEXT_CONTRAST) target = contrast("#000000", background) >= TEXT_CONTRAST ? "#000000" : "#ffffff";
957
+ ink = pushContrast(ink, target, background, TEXT_CONTRAST);
958
+ }
1008
959
  const children = [ctx.text(item.x + (item.icon ? item.anchor === "middle" ? 9 : 18 : 0), item.y, item.text, {
1009
960
  anchor: item.anchor,
1010
961
  size: item.font.size,
@@ -1020,13 +971,14 @@ function scene(plan, ctx) {
1020
971
  };
1021
972
  const children = [];
1022
973
  if (plan.mode === "rings") {
1023
- for (const ring of plan.rings) children.push(ctx.circle(ring.cx, ring.cy, ring.r, {
1024
- fill: ring.status ? ctx.frameTone(look[ring.status]) : tone(ring.tone),
1025
- "fill-opacity": .1
974
+ const fills = plan.rings.map((ring) => ring.status ? ctx.frameTone(look[ring.status]) : tone(ring.tone));
975
+ for (const [index, ring] of plan.rings.entries()) children.push(ctx.circle(ring.cx, ring.cy, ring.r, {
976
+ fill: fills[index],
977
+ "fill-opacity": RING_OPACITY
1026
978
  }));
1027
- for (const ring of plan.rings) children.push(ctx.circle(ring.cx, ring.cy, ring.r, {
979
+ for (const [index, ring] of plan.rings.entries()) children.push(ctx.circle(ring.cx, ring.cy, ring.r, {
1028
980
  fill: "none",
1029
- stroke: ring.status ? ctx.frameTone(look[ring.status]) : tone(ring.tone),
981
+ stroke: fills[index],
1030
982
  "stroke-width": RING_STROKE,
1031
983
  meta: {
1032
984
  kind: "node",
@@ -1035,20 +987,16 @@ function scene(plan, ctx) {
1035
987
  }
1036
988
  }));
1037
989
  for (const region of plan.regions) {
1038
- if (!region.chip) continue;
1039
- children.push(ctx.rect(region.chip.x, region.chip.y, region.chip.w, region.chip.h, {
1040
- rx: look.smallRadius,
1041
- fill: region.status ? ctx.tint(paper, look[region.status], .12) : paper
1042
- }));
990
+ const background = plan.rings.reduce((under, ring, index) => region.ids.includes(ring.id) ? ctx.mix(under, fills[index], RING_OPACITY) : under, paper);
991
+ for (const item of region.rows) children.push(row(item, background));
1043
992
  }
1044
- for (const item of plan.rows) children.push(row(item));
1045
993
  for (const item of plan.shelf) {
1046
994
  if (item.from) children.push(ctx.line(item.from.x, item.from.y, item.to.x, item.to.y, {
1047
995
  stroke: ctx.tint(paper, tone(item.tone), .55),
1048
996
  "stroke-width": 1,
1049
997
  "stroke-dasharray": "3 3"
1050
998
  }));
1051
- for (const line of item.lines) children.push(row(line));
999
+ for (const line of item.lines) children.push(row(line, paper));
1052
1000
  }
1053
1001
  } else for (const panel of plan.panels) {
1054
1002
  const color = panel.status ? ctx.frameTone(look[panel.status]) : tone(panel.tone);