@fengwk/kchart 0.1.0 → 0.2.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.
Files changed (103) hide show
  1. package/CHANGELOG.md +43 -0
  2. package/README.md +31 -2
  3. package/README.zh-CN.md +31 -2
  4. package/dist/esm/_virtual/_kchart-families.js +2 -1
  5. package/dist/esm/assets/themes/cloudmusic.js +1 -1
  6. package/dist/esm/chart/composition.js +7 -5
  7. package/dist/esm/chart/maps.js +9 -7
  8. package/dist/esm/chart/mindmap.js +19 -5
  9. package/dist/esm/chart/multivariate.js +99 -49
  10. package/dist/esm/chart/relations.js +15 -7
  11. package/dist/esm/chart/spec.js +11 -4
  12. package/dist/esm/core/context.js +29 -10
  13. package/dist/esm/core/scene.js +238 -21
  14. package/dist/esm/core/text.js +1 -1
  15. package/dist/esm/diagram/block.js +287 -295
  16. package/dist/esm/diagram/blocks.js +541 -605
  17. package/dist/esm/diagram/board-paint.js +594 -0
  18. package/dist/esm/diagram/board-route.js +60 -0
  19. package/dist/esm/diagram/c4.js +75 -35
  20. package/dist/esm/diagram/class.js +3 -2
  21. package/dist/esm/diagram/cynefin.js +55 -9
  22. package/dist/esm/diagram/eventmodeling.js +21 -10
  23. package/dist/esm/diagram/gantt.js +37 -15
  24. package/dist/esm/diagram/gitgraph.js +26 -5
  25. package/dist/esm/diagram/graph-layout.js +39 -2
  26. package/dist/esm/diagram/graph-route.js +296 -38
  27. package/dist/esm/diagram/info.js +32 -0
  28. package/dist/esm/diagram/journey.js +5 -5
  29. package/dist/esm/diagram/kanban.js +3 -3
  30. package/dist/esm/diagram/layers.js +9 -7
  31. package/dist/esm/diagram/mindmap.js +5 -1
  32. package/dist/esm/diagram/numeric-composition.js +24 -4
  33. package/dist/esm/diagram/numeric-model.js +1 -1
  34. package/dist/esm/diagram/numeric-radar.js +25 -7
  35. package/dist/esm/diagram/numeric-sankey.js +4 -3
  36. package/dist/esm/diagram/numeric-xy.js +14 -4
  37. package/dist/esm/diagram/packet.js +4 -4
  38. package/dist/esm/diagram/quadrant.js +12 -5
  39. package/dist/esm/diagram/railroad-parse.js +20 -10
  40. package/dist/esm/diagram/railroad.js +1 -1
  41. package/dist/esm/diagram/registry.js +46 -8
  42. package/dist/esm/diagram/sequence.js +43 -6
  43. package/dist/esm/diagram/swimlane.js +2 -2
  44. package/dist/esm/diagram/time.js +3 -3
  45. package/dist/esm/diagram/timeline.js +5 -5
  46. package/dist/esm/diagram/treeview.js +5 -3
  47. package/dist/esm/diagram/venn.js +117 -149
  48. package/dist/esm/diagram/wardley.js +61 -26
  49. package/dist/esm/diagram/zenuml.js +338 -0
  50. package/dist/esm/index.d.ts +3 -1
  51. package/dist/esm/mermaid.d.ts +1 -1
  52. package/dist/esm/mermaid.js +2 -1
  53. package/dist/esm/node.js +8 -2
  54. package/dist/esm/src/families.js +6 -1
  55. package/dist/units/chart/composition.js +1 -1
  56. package/dist/units/chart/maps.js +1 -1
  57. package/dist/units/chart/mindmap.js +1 -1
  58. package/dist/units/chart/multivariate.js +1 -1
  59. package/dist/units/chart/relations.js +1 -1
  60. package/dist/units/chart/spec.js +1 -1
  61. package/dist/units/core/context.js +1 -1
  62. package/dist/units/core/scene.js +1 -1
  63. package/dist/units/core/text.js +1 -1
  64. package/dist/units/core/theme.js +1 -1
  65. package/dist/units/diagram/block.js +2 -2
  66. package/dist/units/diagram/blocks.js +2 -2
  67. package/dist/units/diagram/board-paint.js +1 -0
  68. package/dist/units/diagram/board-route.js +1 -0
  69. package/dist/units/diagram/c4.js +1 -1
  70. package/dist/units/diagram/class.js +2 -2
  71. package/dist/units/diagram/cynefin.js +2 -2
  72. package/dist/units/diagram/eventmodeling.js +4 -4
  73. package/dist/units/diagram/gantt.js +3 -3
  74. package/dist/units/diagram/gitgraph.js +2 -1
  75. package/dist/units/diagram/graph-layout.js +1 -1
  76. package/dist/units/diagram/graph-route.js +1 -1
  77. package/dist/units/diagram/info.js +2 -0
  78. package/dist/units/diagram/journey.js +3 -3
  79. package/dist/units/diagram/kanban.js +1 -1
  80. package/dist/units/diagram/layers.js +1 -1
  81. package/dist/units/diagram/mindmap.js +1 -1
  82. package/dist/units/diagram/numeric-composition.js +1 -1
  83. package/dist/units/diagram/numeric-model.js +1 -1
  84. package/dist/units/diagram/numeric-radar.js +2 -2
  85. package/dist/units/diagram/numeric-sankey.js +1 -1
  86. package/dist/units/diagram/numeric-xy.js +1 -1
  87. package/dist/units/diagram/packet.js +1 -1
  88. package/dist/units/diagram/quadrant.js +1 -1
  89. package/dist/units/diagram/railroad-parse.js +2 -2
  90. package/dist/units/diagram/railroad.js +1 -1
  91. package/dist/units/diagram/registry.js +2 -2
  92. package/dist/units/diagram/sequence.js +1 -1
  93. package/dist/units/diagram/swimlane.js +2 -2
  94. package/dist/units/diagram/time.js +1 -1
  95. package/dist/units/diagram/timeline.js +1 -1
  96. package/dist/units/diagram/treeview.js +1 -1
  97. package/dist/units/diagram/venn.js +2 -2
  98. package/dist/units/diagram/wardley.js +4 -4
  99. package/dist/units/diagram/zenuml.js +2 -0
  100. package/dist/units.json +82 -52
  101. package/package.json +1 -1
  102. package/dist/esm/diagram/relations.js +0 -966
  103. package/dist/units/diagram/relations.js +0 -3
@@ -136,7 +136,7 @@ function parse(body, info) {
136
136
  errors
137
137
  };
138
138
  }
139
- function layout(model, { width, ctx, measure, renderOptions }) {
139
+ function layout(model, { width, ctx, measure, renderOptions, compact, embedHeading }) {
140
140
  const { design: d } = ctx, { items, config, sections } = model;
141
141
  const vertical = model.direction === "TD";
142
142
  const finite = (value, fallback, floor) => Number.isFinite(Number(value)) ? Math.max(floor, Number(value)) : fallback;
@@ -150,15 +150,15 @@ function layout(model, { width, ctx, measure, renderOptions }) {
150
150
  };
151
151
  const lh = body.size + 5, iconSize = 16, iconGap = 6;
152
152
  const occupied = sections.reduce((n, s) => n + (s.name || s.icon || s.items.length ? Math.max(1, s.items.length) : 0), 0);
153
- const col = vertical ? Math.max(184, pad * 2 + 100) : Math.max(184, pad * 2 + 100, (width - left - 24) / occupied);
154
- const eventW = vertical ? Math.max(204, width - left - col - 76, pad * 2 + 100) : col - 2 * pad - 14;
153
+ const col = vertical ? Math.max(184, pad * 2 + 100) : Math.max(184, pad * 2 + 100, compact ? 0 : (width - left - 24) / occupied);
154
+ const eventW = vertical ? Math.max(204, compact ? 0 : width - left - col - 76, pad * 2 + 100) : col - 2 * pad - 14;
155
155
  const w = vertical ? left + col + eventW + 76 : left + col * occupied + 24;
156
156
  const children = [], scene = {
157
157
  width: w,
158
158
  height: 0,
159
159
  title: model.title,
160
160
  children,
161
- ...renderOptions?.frame ? { heading: model.title || void 0 } : {}
161
+ ...renderOptions?.frame || embedHeading ? { heading: model.title || void 0 } : {}
162
162
  };
163
163
  const add = (tag, attrs, meta) => children.push({
164
164
  ...ctx.el(tag, attrs),
@@ -209,7 +209,7 @@ function layout(model, { width, ctx, measure, renderOptions }) {
209
209
  };
210
210
  const boxColor = (status, fallback) => status && d[status] || fallback;
211
211
  const titleSlot = model.titleIcon ? 22 : 0;
212
- const title = !renderOptions?.frame && (model.title || model.titleIcon) ? wrap(model.title, w - 48 - titleSlot, FONT.title) : [];
212
+ const title = !renderOptions?.frame && !embedHeading && (model.title || model.titleIcon) ? wrap(model.title, w - 48 - titleSlot, FONT.title) : [];
213
213
  if (title.length && model.titleIcon) children.push(drawIcon(model.titleIcon, 32, 24, d.fg1));
214
214
  lines(title, 24 + titleSlot, 24, FONT.title, d.fg1, "start", 20);
215
215
  const top = title.length ? 24 + title.length * 20 : 24;
@@ -149,7 +149,7 @@ function parse(body, { firstLine = 2, headerLine = 1, lang, params, config }) {
149
149
  errors: [...errors, ...look.diagnostics()]
150
150
  };
151
151
  }
152
- function layout(model, { width, measure, lang }) {
152
+ function layout(model, { width, measure, lang, embedHeading }) {
153
153
  const available = Math.max(120, width - 48);
154
154
  const { settings } = model;
155
155
  const step = clamp(clamp(settings.rowIndent, 10, 4, 90) + clamp(settings.paddingX, 14, 4, 24), STEP, 12, 96);
@@ -187,7 +187,7 @@ function layout(model, { width, measure, lang }) {
187
187
  }
188
188
  const noteWidth = Math.max(0, ...rows.map((row) => maxWidthOf(row.notes, FONT.label, measure)));
189
189
  const inline = !noteWidth || end + 16 + noteWidth + MARGIN <= width + 240;
190
- const title = model.title ? wrapText(model.title, available, FONT.title, measure) : [];
190
+ const title = !embedHeading && model.title ? wrapText(model.title, available, FONT.title, measure) : [];
191
191
  let y = MARGIN + (title.length ? title.length * 19 + 12 : 0);
192
192
  let right = Math.max(end, MARGIN + maxWidthOf(title, FONT.title, measure));
193
193
  for (const row of rows) {
@@ -206,6 +206,7 @@ function layout(model, { width, measure, lang }) {
206
206
  return {
207
207
  rows,
208
208
  title,
209
+ ...embedHeading ? { heading: model.title } : {},
209
210
  width: naturalWidth,
210
211
  height: y + MARGIN,
211
212
  right,
@@ -279,7 +280,8 @@ function scene(plan, ctx) {
279
280
  return {
280
281
  width: plan.width,
281
282
  height: plan.height,
282
- children: [...edges, ...labels]
283
+ children: [...edges, ...labels],
284
+ ...plan.heading ? { heading: plan.heading } : {}
283
285
  };
284
286
  }
285
287
  var implementations = { "treeView-beta": {
@@ -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,43 +582,31 @@ 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
639
597
  }));
640
598
  }
641
- function planRings(model, { measure, width, lang }) {
642
- const available = Math.max(240, width - 48);
599
+ function planRings(model, { measure, width, lang, compact, embedHeading }) {
600
+ const available = compact ? 560 : Math.max(240, width - 48);
643
601
  const preferred = canonicalSpread(model.sets, 1);
644
602
  const unit = boundsOf(ringsAt(model.sets, 1, preferred));
645
- let R = Math.max(56, Math.min(available / Math.max(1, unit.w), PLOT_H / Math.max(1, unit.h)));
603
+ const maxLabel = Math.max(0, ...[
604
+ ...model.sets.map((set) => set.label || set.id),
605
+ ...model.unions.map((union) => union.label),
606
+ ...model.texts.map((text) => text.label)
607
+ ].map((label) => measure(label, SET_FONT)));
608
+ const preferredPlotWidth = Math.min(520, 260 + Math.min(100, (model.unions.length + model.texts.length) * 8) + Math.min(80, Math.max(0, model.sets.length - 2) * 30) + Math.min(80, maxLabel * .25));
609
+ let R = compact ? Math.max(56, Math.min(preferredPlotWidth / Math.max(1, unit.w), 320 / Math.max(1, unit.h))) : Math.max(56, Math.min(available / Math.max(1, unit.w), PLOT_H / Math.max(1, unit.h)));
646
610
  const regions = regionList(ringsAt(model.sets, R, preferred), model);
647
611
  let chosen = null;
648
612
  for (let pass = 0; pass <= MAX_GROWTH; pass += 1) {
@@ -655,18 +619,11 @@ function planRings(model, { measure, width, lang }) {
655
619
  const rings = chosen.rings;
656
620
  const shelf = [];
657
621
  for (const region of regions) {
658
- if (region.block.fits) continue;
622
+ if (region.block.room >= 0) continue;
659
623
  shelf.push({
660
624
  region,
661
625
  rows: [...region.block.labels, ...region.block.items]
662
626
  });
663
- region.block = {
664
- ...region.block,
665
- labels: [],
666
- labelW: 0,
667
- labelH: 0,
668
- chip: false
669
- };
670
627
  }
671
628
  const claimed = new Set(regions.map((region) => region.key));
672
629
  for (const text of model.texts) {
@@ -681,7 +638,10 @@ function planRings(model, { measure, width, lang }) {
681
638
  },
682
639
  rows: [{
683
640
  text: text.label,
684
- font: ITEM_FONT
641
+ font: ITEM_FONT,
642
+ icon: text.icon,
643
+ link: text.link,
644
+ status: text.status
685
645
  }]
686
646
  });
687
647
  }
@@ -692,7 +652,7 @@ function planRings(model, { measure, width, lang }) {
692
652
  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
653
  break;
694
654
  }
695
- const drawn = regions.filter((region) => region.block.h > 0 || region.block.labels.length);
655
+ const drawn = regions.filter((region) => region.block.h > 0 && region.block.room >= 0);
696
656
  for (const region of drawn) layoutBlock(region);
697
657
  return finish({
698
658
  mode: "rings",
@@ -704,10 +664,12 @@ function planRings(model, { measure, width, lang }) {
704
664
  notes
705
665
  }, model, {
706
666
  measure,
707
- width
667
+ width,
668
+ compact,
669
+ embedHeading
708
670
  });
709
671
  }
710
- function planGrid(model, { measure, width, lang }) {
672
+ function planGrid(model, { measure, width, lang, compact, embedHeading }) {
711
673
  const groups = /* @__PURE__ */ new Map();
712
674
  for (const text of model.texts) {
713
675
  const list = groups.get(text.key) || [];
@@ -785,9 +747,9 @@ function planGrid(model, { measure, width, lang }) {
785
747
  panel.h = 28 + panel.header.length * HEAD_LINE + (panel.captionLines.length ? 4 + panel.captionLines.length * ITEM_LINE : 0) + (panel.itemLines.length ? 6 + panel.itemLines.length * ITEM_LINE : 0);
786
748
  }
787
749
  const cellW = Math.max(...panels.map((panel) => panel.w));
788
- const available = Math.max(240, width - 48);
750
+ const available = compact ? 520 : Math.max(240, width - 48);
789
751
  let cols = Math.min(GRID_COLS, panels.length);
790
- while (cols > 1 && cols * cellW + (cols - 1) * GRID_GAP > available * 2) cols -= 1;
752
+ if (!compact) while (cols > 1 && cols * cellW + (cols - 1) * GRID_GAP > available * 2) cols -= 1;
791
753
  const rowCount = Math.ceil(panels.length / cols);
792
754
  const rowHeights = [];
793
755
  panels.forEach((panel, index) => {
@@ -857,12 +819,14 @@ function planGrid(model, { measure, width, lang }) {
857
819
  notes
858
820
  }, model, {
859
821
  measure,
860
- width
822
+ width,
823
+ compact,
824
+ embedHeading
861
825
  });
862
826
  }
863
- function finish(plan, model, { measure, width }) {
864
- const titleLines = model.title ? wrapText(model.title, Math.max(160, Math.min(width - 48, 480)), TITLE_FONT, (piece, spec) => measure(piece, spec)) : [];
865
- const titleH = titleLines.length ? titleLines.length * TITLE_LINE + TITLE_GAP : 0;
827
+ function finish(plan, model, { measure, width, compact, embedHeading }) {
828
+ const titleLines = model.title ? wrapText(model.title, compact ? 480 : Math.max(160, Math.min(width - 48, 480)), TITLE_FONT, (piece, spec) => measure(piece, spec)) : [];
829
+ const titleH = !embedHeading && titleLines.length ? titleLines.length * TITLE_LINE + TITLE_GAP : 0;
866
830
  const shelf = [];
867
831
  let shelfRight = plan.plot.x + plan.plot.w;
868
832
  let shelfBottom = plan.plot.y + plan.plot.h;
@@ -876,20 +840,16 @@ function finish(plan, model, { measure, width }) {
876
840
  text: item.region.caption,
877
841
  font: ITEM_FONT,
878
842
  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)));
843
+ }] : [], ...item.rows];
844
+ const w = Math.max(0, ...rows.map((row) => rowWidth(row, measure)));
884
845
  if (cursor > plan.plot.x && cursor + w > plan.plot.x + limit) {
885
846
  cursor = plan.plot.x;
886
847
  top += rowH + GRID_GAP / 2;
887
848
  rowH = 0;
888
849
  }
889
850
  const lines = rows.map((row, index) => ({
890
- text: row.text,
891
- font: row.font,
892
- kind: row.kind,
851
+ ...row,
852
+ anchor: "start",
893
853
  x: cursor,
894
854
  y: top + (index + .5) * ITEM_LINE
895
855
  }));
@@ -912,7 +872,7 @@ function finish(plan, model, { measure, width }) {
912
872
  }
913
873
  }
914
874
  const spanW = Math.max(plan.plot.x + plan.plot.w, shelfRight) - plan.plot.x;
915
- const contentW = Math.max(spanW, ...titleLines.map((row) => measure(row, TITLE_FONT)));
875
+ const contentW = Math.max(spanW, ...(embedHeading ? [] : titleLines).map((row) => measure(row, TITLE_FONT)));
916
876
  const dx = MARGIN + (contentW - spanW) / 2 - plan.plot.x;
917
877
  const dy = MARGIN + titleH - plan.plot.y;
918
878
  const height = Math.ceil(shelfBottom - plan.plot.y + titleH + 48);
@@ -932,10 +892,6 @@ function finish(plan, model, { measure, width }) {
932
892
  for (const region of plan.regions || []) {
933
893
  region.x += dx;
934
894
  region.y += dy;
935
- if (region.chip) {
936
- region.chip.x += dx;
937
- region.chip.y += dy;
938
- }
939
895
  }
940
896
  for (const item of shelf) {
941
897
  for (const line of item.lines) {
@@ -960,6 +916,10 @@ function finish(plan, model, { measure, width }) {
960
916
  x: MARGIN + contentW / 2,
961
917
  top: 34
962
918
  } : null,
919
+ ...embedHeading ? {
920
+ embedHeading,
921
+ heading: model.title
922
+ } : {},
963
923
  rings: plan.rings || [],
964
924
  regions: plan.regions || [],
965
925
  panels: plan.panels || [],
@@ -976,6 +936,7 @@ function layout(model, options = {}) {
976
936
  const measure = options.measure || fallbackMeasure;
977
937
  const width = Math.max(1, Number(options.width) || 800);
978
938
  const lang = options.lang || "zh";
939
+ const compact = options.compact === true, { embedHeading } = options;
979
940
  if (!model.sets.length) return {
980
941
  width,
981
942
  height: 0,
@@ -984,11 +945,15 @@ function layout(model, options = {}) {
984
945
  const plan = model.sets.length <= RING_MAX ? planRings(model, {
985
946
  measure,
986
947
  width,
987
- lang
948
+ lang,
949
+ compact,
950
+ embedHeading
988
951
  }) : planGrid(model, {
989
952
  measure,
990
953
  width,
991
- lang
954
+ lang,
955
+ compact,
956
+ embedHeading
992
957
  });
993
958
  const errors = [...plan.notes];
994
959
  const parts = model.sets.length + model.unions.length + model.texts.length;
@@ -1003,8 +968,13 @@ function scene(plan, ctx) {
1003
968
  const look = appearance(ctx);
1004
969
  const paper = ctx.color("bg");
1005
970
  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;
971
+ const row = (item, background) => {
972
+ let ink = item.status ? ctx.textColor(look[item.status]) : item.kind === "caption" ? look.fg3 : item.font === SET_FONT ? look.fg1 : look.fg2;
973
+ if (background && contrast(ink, background) < TEXT_CONTRAST) {
974
+ let target = ctx.textOn(background);
975
+ if (contrast(target, background) < TEXT_CONTRAST) target = contrast("#000000", background) >= TEXT_CONTRAST ? "#000000" : "#ffffff";
976
+ ink = pushContrast(ink, target, background, TEXT_CONTRAST);
977
+ }
1008
978
  const children = [ctx.text(item.x + (item.icon ? item.anchor === "middle" ? 9 : 18 : 0), item.y, item.text, {
1009
979
  anchor: item.anchor,
1010
980
  size: item.font.size,
@@ -1020,13 +990,14 @@ function scene(plan, ctx) {
1020
990
  };
1021
991
  const children = [];
1022
992
  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
993
+ const fills = plan.rings.map((ring) => ring.status ? ctx.frameTone(look[ring.status]) : tone(ring.tone));
994
+ for (const [index, ring] of plan.rings.entries()) children.push(ctx.circle(ring.cx, ring.cy, ring.r, {
995
+ fill: fills[index],
996
+ "fill-opacity": RING_OPACITY
1026
997
  }));
1027
- for (const ring of plan.rings) children.push(ctx.circle(ring.cx, ring.cy, ring.r, {
998
+ for (const [index, ring] of plan.rings.entries()) children.push(ctx.circle(ring.cx, ring.cy, ring.r, {
1028
999
  fill: "none",
1029
- stroke: ring.status ? ctx.frameTone(look[ring.status]) : tone(ring.tone),
1000
+ stroke: fills[index],
1030
1001
  "stroke-width": RING_STROKE,
1031
1002
  meta: {
1032
1003
  kind: "node",
@@ -1035,20 +1006,16 @@ function scene(plan, ctx) {
1035
1006
  }
1036
1007
  }));
1037
1008
  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
- }));
1009
+ const background = plan.rings.reduce((under, ring, index) => region.ids.includes(ring.id) ? ctx.mix(under, fills[index], RING_OPACITY) : under, paper);
1010
+ for (const item of region.rows) children.push(row(item, background));
1043
1011
  }
1044
- for (const item of plan.rows) children.push(row(item));
1045
1012
  for (const item of plan.shelf) {
1046
1013
  if (item.from) children.push(ctx.line(item.from.x, item.from.y, item.to.x, item.to.y, {
1047
1014
  stroke: ctx.tint(paper, tone(item.tone), .55),
1048
1015
  "stroke-width": 1,
1049
1016
  "stroke-dasharray": "3 3"
1050
1017
  }));
1051
- for (const line of item.lines) children.push(row(line));
1018
+ for (const line of item.lines) children.push(row(line, paper));
1052
1019
  }
1053
1020
  } else for (const panel of plan.panels) {
1054
1021
  const color = panel.status ? ctx.frameTone(look[panel.status]) : tone(panel.tone);
@@ -1066,7 +1033,7 @@ function scene(plan, ctx) {
1066
1033
  }));
1067
1034
  for (const item of panel.rows) children.push(row(item));
1068
1035
  }
1069
- if (plan.title) plan.title.lines.forEach((line, index) => children.push(ctx.text(plan.title.x, plan.title.top + index * TITLE_LINE, line, {
1036
+ if (!plan.embedHeading && plan.title) plan.title.lines.forEach((line, index) => children.push(ctx.text(plan.title.x, plan.title.top + index * TITLE_LINE, line, {
1070
1037
  anchor: "middle",
1071
1038
  size: TITLE_FONT.size,
1072
1039
  weight: TITLE_FONT.weight,
@@ -1077,6 +1044,7 @@ function scene(plan, ctx) {
1077
1044
  width: plan.width,
1078
1045
  height: plan.height,
1079
1046
  title: plan.title ? plan.title.lines.join(" ") : void 0,
1047
+ ...plan.embedHeading ? { heading: plan.heading } : {},
1080
1048
  children,
1081
1049
  anchor: plan.anchor
1082
1050
  };