@fengwk/kchart 0.0.0-stage → 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 +17 -0
- package/LICENSE +21 -0
- package/NOTICE.md +11 -0
- package/README.md +68 -2
- package/README.zh-CN.md +69 -0
- package/dist/LICENSE +21 -0
- package/dist/NOTICE.md +11 -0
- package/dist/assets/ICONS-LICENSE.txt +53 -0
- package/dist/assets/maps/LICENSE.md +75 -0
- package/dist/assets/maps/METADATA.md +9 -0
- package/dist/assets/maps/REGIONS.md +28 -0
- package/dist/assets/maps/us-states.geojson +1 -0
- package/dist/assets/maps/world.geojson +1 -0
- package/dist/esm/_virtual/_kchart-families.js +48 -0
- package/dist/esm/_virtual/_rolldown/runtime.js +12 -0
- package/dist/esm/assets/icons.js +11 -0
- package/dist/esm/assets/maps/us-states.js +3 -0
- package/dist/esm/assets/maps/world.js +3 -0
- package/dist/esm/assets/themes/claude.js +69 -0
- package/dist/esm/assets/themes/cloudmusic.js +62 -0
- package/dist/esm/assets/themes/kanagawa.js +67 -0
- package/dist/esm/assets/themes/obsidian.js +67 -0
- package/dist/esm/assets/themes/paper.js +61 -0
- package/dist/esm/assets/themes/terminal.js +60 -0
- package/dist/esm/assets/themes/tokyo.js +62 -0
- package/dist/esm/chart/basic.js +1313 -0
- package/dist/esm/chart/catalog.js +476 -0
- package/dist/esm/chart/composition.js +844 -0
- package/dist/esm/chart/context.js +189 -0
- package/dist/esm/chart/distribution.js +442 -0
- package/dist/esm/chart/fields.js +67 -0
- package/dist/esm/chart/maps.js +563 -0
- package/dist/esm/chart/mindmap.js +629 -0
- package/dist/esm/chart/multivariate.js +1229 -0
- package/dist/esm/chart/registry.js +108 -0
- package/dist/esm/chart/relations.js +949 -0
- package/dist/esm/chart/spec.js +1415 -0
- package/dist/esm/chart/stat-compare.js +302 -0
- package/dist/esm/chart/stat-trend.js +181 -0
- package/dist/esm/chart/statistics.js +1007 -0
- package/dist/esm/chart/unit-variants.js +645 -0
- package/dist/esm/chart/variants.js +552 -0
- package/dist/esm/core/color.js +197 -0
- package/dist/esm/core/context.js +256 -0
- package/dist/esm/core/diagnostics.js +23 -0
- package/dist/esm/core/format.js +167 -0
- package/dist/esm/core/geometry.js +130 -0
- package/dist/esm/core/icon-data.js +5 -0
- package/dist/esm/core/icons.js +173 -0
- package/dist/esm/core/scale.js +112 -0
- package/dist/esm/core/scene.js +234 -0
- package/dist/esm/core/svg.js +89 -0
- package/dist/esm/core/text.js +184 -0
- package/dist/esm/core/theme.js +318 -0
- package/dist/esm/diagram/agentflow.js +596 -0
- package/dist/esm/diagram/architecture.js +729 -0
- package/dist/esm/diagram/block.js +988 -0
- package/dist/esm/diagram/blocks.js +687 -0
- package/dist/esm/diagram/c4.js +1002 -0
- package/dist/esm/diagram/class.js +1158 -0
- package/dist/esm/diagram/cynefin.js +749 -0
- package/dist/esm/diagram/emphasis.js +7 -0
- package/dist/esm/diagram/er.js +657 -0
- package/dist/esm/diagram/eventmodeling.js +948 -0
- package/dist/esm/diagram/gantt-date.js +323 -0
- package/dist/esm/diagram/gantt.js +661 -0
- package/dist/esm/diagram/gitgraph.js +472 -0
- package/dist/esm/diagram/graph-layout.js +4046 -0
- package/dist/esm/diagram/graph-parse.js +1366 -0
- package/dist/esm/diagram/graph-route.js +490 -0
- package/dist/esm/diagram/graph.js +553 -0
- package/dist/esm/diagram/ishikawa.js +496 -0
- package/dist/esm/diagram/journey.js +512 -0
- package/dist/esm/diagram/kanban.js +280 -0
- package/dist/esm/diagram/label.js +56 -0
- package/dist/esm/diagram/layers.js +385 -0
- package/dist/esm/diagram/map-label.js +44 -0
- package/dist/esm/diagram/mindmap.js +277 -0
- package/dist/esm/diagram/numeric-composition.js +78 -0
- package/dist/esm/diagram/numeric-model.js +86 -0
- package/dist/esm/diagram/numeric-parse.js +205 -0
- package/dist/esm/diagram/numeric-radar.js +355 -0
- package/dist/esm/diagram/numeric-sankey.js +201 -0
- package/dist/esm/diagram/numeric-xy.js +422 -0
- package/dist/esm/diagram/packet.js +175 -0
- package/dist/esm/diagram/presentation.js +252 -0
- package/dist/esm/diagram/quadrant.js +297 -0
- package/dist/esm/diagram/railroad-parse.js +465 -0
- package/dist/esm/diagram/railroad.js +294 -0
- package/dist/esm/diagram/registry.js +296 -0
- package/dist/esm/diagram/relations.js +966 -0
- package/dist/esm/diagram/requirement.js +341 -0
- package/dist/esm/diagram/sequence.js +1228 -0
- package/dist/esm/diagram/shapes.js +366 -0
- package/dist/esm/diagram/source.js +538 -0
- package/dist/esm/diagram/support.js +153 -0
- package/dist/esm/diagram/swimlane-mermaid.js +140 -0
- package/dist/esm/diagram/swimlane.js +1186 -0
- package/dist/esm/diagram/time.js +748 -0
- package/dist/esm/diagram/timeline.js +376 -0
- package/dist/esm/diagram/tree-family-icon.js +22 -0
- package/dist/esm/diagram/treeview.js +293 -0
- package/dist/esm/diagram/usecase.js +852 -0
- package/dist/esm/diagram/venn.js +1038 -0
- package/dist/esm/diagram/wardley.js +838 -0
- package/dist/esm/index.d.ts +133 -0
- package/dist/esm/index.js +92 -0
- package/dist/esm/interaction.js +561 -0
- package/dist/esm/mermaid.d.ts +28 -0
- package/dist/esm/mermaid.js +70 -0
- package/dist/esm/node.d.ts +68 -0
- package/dist/esm/node.js +373 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/page-api.js +65 -0
- package/dist/esm/runtime.js +352 -0
- package/dist/esm/src/families.js +209 -0
- package/dist/page-api.js +1 -0
- package/dist/units/chart/basic.js +1 -0
- package/dist/units/chart/catalog.js +1 -0
- package/dist/units/chart/composition.js +1 -0
- package/dist/units/chart/context.js +1 -0
- package/dist/units/chart/distribution.js +1 -0
- package/dist/units/chart/fields.js +1 -0
- package/dist/units/chart/maps.js +1 -0
- package/dist/units/chart/mindmap.js +1 -0
- package/dist/units/chart/multivariate.js +1 -0
- package/dist/units/chart/registry.js +1 -0
- package/dist/units/chart/relations.js +1 -0
- package/dist/units/chart/spec.js +3 -0
- package/dist/units/chart/stat-compare.js +1 -0
- package/dist/units/chart/stat-trend.js +1 -0
- package/dist/units/chart/statistics.js +1 -0
- package/dist/units/chart/unit-variants.js +1 -0
- package/dist/units/chart/variants.js +1 -0
- package/dist/units/core/color.js +1 -0
- package/dist/units/core/context.js +1 -0
- package/dist/units/core/diagnostics.js +1 -0
- package/dist/units/core/format.js +1 -0
- package/dist/units/core/geometry.js +1 -0
- package/dist/units/core/icon-data.js +1 -0
- package/dist/units/core/icons.js +1 -0
- package/dist/units/core/scale.js +1 -0
- package/dist/units/core/scene.js +1 -0
- package/dist/units/core/svg.js +1 -0
- package/dist/units/core/text.js +2 -0
- package/dist/units/core/theme.js +1 -0
- package/dist/units/diagram/agentflow.js +12 -0
- package/dist/units/diagram/architecture.js +4 -0
- package/dist/units/diagram/block.js +2 -0
- package/dist/units/diagram/blocks.js +2 -0
- package/dist/units/diagram/c4.js +6 -0
- package/dist/units/diagram/class.js +5 -0
- package/dist/units/diagram/cynefin.js +4 -0
- package/dist/units/diagram/emphasis.js +1 -0
- package/dist/units/diagram/er.js +9 -0
- package/dist/units/diagram/eventmodeling.js +5 -0
- package/dist/units/diagram/gantt-date.js +1 -0
- package/dist/units/diagram/gantt.js +3 -0
- package/dist/units/diagram/gitgraph.js +2 -0
- package/dist/units/diagram/graph-layout.js +4 -0
- package/dist/units/diagram/graph-parse.js +9 -0
- package/dist/units/diagram/graph-route.js +1 -0
- package/dist/units/diagram/graph.js +1 -0
- package/dist/units/diagram/ishikawa.js +2 -0
- package/dist/units/diagram/journey.js +5 -0
- package/dist/units/diagram/kanban.js +2 -0
- package/dist/units/diagram/label.js +4 -0
- package/dist/units/diagram/layers.js +1 -0
- package/dist/units/diagram/map-label.js +1 -0
- package/dist/units/diagram/mindmap.js +2 -0
- package/dist/units/diagram/numeric-composition.js +1 -0
- package/dist/units/diagram/numeric-model.js +1 -0
- package/dist/units/diagram/numeric-parse.js +2 -0
- package/dist/units/diagram/numeric-radar.js +3 -0
- package/dist/units/diagram/numeric-sankey.js +2 -0
- package/dist/units/diagram/numeric-xy.js +2 -0
- package/dist/units/diagram/packet.js +2 -0
- package/dist/units/diagram/presentation.js +2 -0
- package/dist/units/diagram/quadrant.js +2 -0
- package/dist/units/diagram/railroad-parse.js +4 -0
- package/dist/units/diagram/railroad.js +1 -0
- package/dist/units/diagram/registry.js +2 -0
- package/dist/units/diagram/relations.js +3 -0
- package/dist/units/diagram/requirement.js +3 -0
- package/dist/units/diagram/sequence.js +2 -0
- package/dist/units/diagram/shapes.js +60 -0
- package/dist/units/diagram/source.js +20 -0
- package/dist/units/diagram/support.js +3 -0
- package/dist/units/diagram/swimlane-mermaid.js +3 -0
- package/dist/units/diagram/swimlane.js +2 -0
- package/dist/units/diagram/time.js +3 -0
- package/dist/units/diagram/timeline.js +2 -0
- package/dist/units/diagram/tree-family-icon.js +1 -0
- package/dist/units/diagram/treeview.js +2 -0
- package/dist/units/diagram/usecase.js +3 -0
- package/dist/units/diagram/venn.js +2 -0
- package/dist/units/diagram/wardley.js +4 -0
- package/dist/units/interaction.js +1 -0
- package/dist/units/runtime.js +1 -0
- package/dist/units.json +712 -0
- package/package.json +30 -4
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { decorate as decorate$1, layouts } from "./registry.js";
|
|
3
|
+
import "./basic.js";
|
|
4
|
+
import "./statistics.js";
|
|
5
|
+
import { labelLanes } from "../core/geometry.js";
|
|
6
|
+
import { geometry } from "../core/icons.js";
|
|
7
|
+
// src/chart/unit-variants.js
|
|
8
|
+
var unit_variants_exports = /* @__PURE__ */ __exportAll({ install: () => install });
|
|
9
|
+
var flatMap = (nodes, visit) => nodes.flatMap((node) => {
|
|
10
|
+
const replacement = visit(node);
|
|
11
|
+
if (replacement) return replacement;
|
|
12
|
+
if (node.children) return [{
|
|
13
|
+
...node,
|
|
14
|
+
children: flatMap(node.children, visit)
|
|
15
|
+
}];
|
|
16
|
+
return [node];
|
|
17
|
+
});
|
|
18
|
+
var clipRect = (ctx, id, x, y, width, height) => ctx.el("clipPath", { id }, [ctx.rect(x, y, Math.max(0, width), Math.max(0, height))]);
|
|
19
|
+
var isEnglish = (lang) => /^en(?:-|$)/i.test(String(lang || ""));
|
|
20
|
+
function unitCount(value, unit, ctx) {
|
|
21
|
+
const count = Math.abs(value) / unit;
|
|
22
|
+
if (!Number.isFinite(count)) ctx.fit("单位数量无法稳定计算", "调整unitMark.value,使计数保持有限");
|
|
23
|
+
return count;
|
|
24
|
+
}
|
|
25
|
+
function ensureSymbolBudget(values, unit, ctx) {
|
|
26
|
+
let total = 0;
|
|
27
|
+
for (const value of values) {
|
|
28
|
+
if (!Number.isFinite(value) || value === 0) continue;
|
|
29
|
+
total += Math.ceil(unitCount(value, unit, ctx));
|
|
30
|
+
if (total > 2e3) ctx.fit("全图单位符号总数超过2000,无法清晰呈现", "增大unitMark.value,使所有记录的符号总数不超过2000");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function requireReadableSlot(length, thickness, minimum, ctx, fix = "增大unitMark.value或改用solid变体") {
|
|
34
|
+
if (!(Number.isFinite(length) && Number.isFinite(thickness)) || length < minimum || thickness < minimum) ctx.fit("单位符号在当前图幅中太密或太小,无法辨认", fix);
|
|
35
|
+
}
|
|
36
|
+
function unitDescription(spec, ctx, field) {
|
|
37
|
+
const label = spec.unitMark.label, english = isEnglish(ctx.lang);
|
|
38
|
+
const withUnit = ctx.format(spec.unitMark.value, field, true), bare = ctx.format(spec.unitMark.value, field, false);
|
|
39
|
+
const amount = withUnit !== bare ? withUnit : `${bare}${english || !/^[\u3400-\u9fff]/u.test(label) ? " " : ""}${label}`;
|
|
40
|
+
if (spec.chart === "histogram" && spec.variant === "rungs") return english ? `Each cell represents ${amount}` : `每格表示 ${amount}`;
|
|
41
|
+
return english ? `Each mark represents ${amount}` : `每个符号表示 ${amount}`;
|
|
42
|
+
}
|
|
43
|
+
function headerLayout(ctx, text, width) {
|
|
44
|
+
const lines = ctx.wrap(text, Math.max(24, width - 16), 12, 400);
|
|
45
|
+
return {
|
|
46
|
+
lines,
|
|
47
|
+
height: Math.max(24, lines.length * 16 + 8)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
function withHeader(scene, ctx, text, options = {}) {
|
|
51
|
+
const { lines, height } = headerLayout(ctx, text, scene.width);
|
|
52
|
+
const header = ctx.textLines(8, 16, lines, {
|
|
53
|
+
size: 12,
|
|
54
|
+
fill: ctx.color("muted"),
|
|
55
|
+
meta: options.meta
|
|
56
|
+
});
|
|
57
|
+
return {
|
|
58
|
+
...scene,
|
|
59
|
+
height: scene.height + height,
|
|
60
|
+
children: [header, ctx.el("g", { transform: `translate(0 ${height})` }, scene.children)]
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
function symbolGeometry(iconName) {
|
|
64
|
+
const iconGeometry = geometry(iconName);
|
|
65
|
+
if (!iconGeometry) throw new Error(`pictorial图标不存在:${iconName}`);
|
|
66
|
+
if (iconGeometry.set !== "tabler") throw new Error("pictorial计数不支持品牌图标,请使用通用Tabler图标");
|
|
67
|
+
return iconGeometry;
|
|
68
|
+
}
|
|
69
|
+
function makePictorial(ctx, geometry, x, y, width, height, color, clipId, meta) {
|
|
70
|
+
const parts = geometry.viewBox.trim().split(/[ ,]+/).map(Number);
|
|
71
|
+
const [vx, vy, vw, vh] = parts;
|
|
72
|
+
if (parts.length !== 4 || !parts.every(Number.isFinite) || vw <= 0 || vh <= 0) throw new Error("pictorial图标viewBox无效");
|
|
73
|
+
const scale = Math.min(width / vw, height / vh);
|
|
74
|
+
const dx = x + (width - vw * scale) / 2 - vx * scale;
|
|
75
|
+
const dy = y + (height - vh * scale) / 2 - vy * scale;
|
|
76
|
+
const children = geometry.elements.map(({ tag, attrs }) => ctx.el(tag, attrs));
|
|
77
|
+
const transformed = ctx.el("g", {
|
|
78
|
+
transform: `translate(${dx} ${dy}) scale(${scale})`,
|
|
79
|
+
fill: "none",
|
|
80
|
+
stroke: color,
|
|
81
|
+
"stroke-width": 2,
|
|
82
|
+
"stroke-linecap": "round",
|
|
83
|
+
"stroke-linejoin": "round"
|
|
84
|
+
}, children);
|
|
85
|
+
const clipped = ctx.el("g", { "clip-path": `url(#${clipId})` }, [transformed]);
|
|
86
|
+
clipped.meta = meta;
|
|
87
|
+
return clipped;
|
|
88
|
+
}
|
|
89
|
+
function unitMarks(spec, ctx, scene, mode) {
|
|
90
|
+
if (spec.stack === "percent") ctx.fit("单位计数不适用于百分比堆叠", "改用solid变体或绝对堆叠");
|
|
91
|
+
const { value: unit, label } = spec.unitMark;
|
|
92
|
+
const geometry = mode === "pictorial" ? symbolGeometry(spec.unitMark.icon) : null;
|
|
93
|
+
let clipIndex = 0;
|
|
94
|
+
const additions = [];
|
|
95
|
+
const offsets = /* @__PURE__ */ new Map();
|
|
96
|
+
let pictorialWidth = scene.width;
|
|
97
|
+
const unitText = unitDescription(spec, ctx, Array.isArray(spec.encode.value) ? spec.encode.value[0] : spec.encode.value);
|
|
98
|
+
const key = (meta) => `${meta.record ?? ""}|${meta.series ?? ""}|${meta.category ?? ""}`;
|
|
99
|
+
const children = flatMap(scene.children, (node) => {
|
|
100
|
+
if (!(node.tag === "rect" && node.meta?.kind === "record" && Number.isFinite(node.meta.value) && node.meta.value > 0 && Number(node.attrs.width) > 0 && Number(node.attrs.height) > 0)) return null;
|
|
101
|
+
const value = node.meta.value, count = unitCount(value, unit, ctx);
|
|
102
|
+
const horizontal = spec.chart === "bar";
|
|
103
|
+
const x = Number(node.attrs.x), y = Number(node.attrs.y), width = Number(node.attrs.width), height = Number(node.attrs.height);
|
|
104
|
+
const marks = [], fullCount = Math.floor(count), fraction = count - fullCount;
|
|
105
|
+
const actualCount = fullCount + (fraction > 0 ? 1 : 0);
|
|
106
|
+
const color = node.attrs.fill || ctx.color("ink");
|
|
107
|
+
let shift = 0;
|
|
108
|
+
if (mode === "pictorial") {
|
|
109
|
+
const iconSize = 20, pitch = 24;
|
|
110
|
+
requireReadableSlot(iconSize, iconSize, 18, ctx, "增加图表宽度或减少系列数,使图标至少为18px");
|
|
111
|
+
for (let i = 0; i < actualCount; i += 1) {
|
|
112
|
+
const part = i === fullCount && fraction > 0 ? fraction : 1;
|
|
113
|
+
const clipWidth = horizontal ? iconSize * part : iconSize;
|
|
114
|
+
const clipHeight = horizontal ? iconSize : iconSize * part;
|
|
115
|
+
const slotX = horizontal ? x + i * pitch : x + (width - iconSize) / 2;
|
|
116
|
+
const slotY = horizontal ? y + (height - iconSize) / 2 : y + height - iconSize - i * pitch;
|
|
117
|
+
const clipX = slotX;
|
|
118
|
+
const clipY = horizontal ? slotY : slotY + iconSize - clipHeight;
|
|
119
|
+
if (part < 1) {
|
|
120
|
+
const ghostId = `unit-clip-${clipIndex++}`;
|
|
121
|
+
additions.push(clipRect(ctx, ghostId, slotX, slotY, iconSize, iconSize));
|
|
122
|
+
const ghostColor = ctx.mix(ctx.palette.ink, ctx.palette.bg, .82);
|
|
123
|
+
marks.push(makePictorial(ctx, geometry, slotX, slotY, iconSize, iconSize, ghostColor, ghostId, {
|
|
124
|
+
...node.meta,
|
|
125
|
+
kind: "pictorial-ghost",
|
|
126
|
+
fraction: 1,
|
|
127
|
+
unit
|
|
128
|
+
}));
|
|
129
|
+
}
|
|
130
|
+
const id = `unit-clip-${clipIndex++}`;
|
|
131
|
+
additions.push(clipRect(ctx, id, clipX, clipY, clipWidth, clipHeight));
|
|
132
|
+
marks.push(makePictorial(ctx, geometry, slotX, slotY, iconSize, iconSize, color, id, {
|
|
133
|
+
...node.meta,
|
|
134
|
+
fraction: part,
|
|
135
|
+
unit
|
|
136
|
+
}));
|
|
137
|
+
}
|
|
138
|
+
const originalEnd = horizontal ? x + width : y;
|
|
139
|
+
const iconExtent = actualCount * pitch - 4;
|
|
140
|
+
const pictorialEnd = horizontal ? x + iconExtent : y + height - iconExtent;
|
|
141
|
+
shift = pictorialEnd - originalEnd;
|
|
142
|
+
offsets.set(key(node.meta), horizontal ? {
|
|
143
|
+
dx: shift,
|
|
144
|
+
dy: 0
|
|
145
|
+
} : {
|
|
146
|
+
dx: 0,
|
|
147
|
+
dy: shift
|
|
148
|
+
});
|
|
149
|
+
if (horizontal) pictorialWidth = Math.max(pictorialWidth, pictorialEnd + 48);
|
|
150
|
+
} else {
|
|
151
|
+
const span = (horizontal ? width : height) / count;
|
|
152
|
+
requireReadableSlot(span, horizontal ? height : width, 6, ctx);
|
|
153
|
+
const gap = 2.5;
|
|
154
|
+
for (let i = 0; i < actualCount; i += 1) {
|
|
155
|
+
const part = i === fullCount && fraction > 0 ? fraction : 1;
|
|
156
|
+
const symbolSpan = span - gap;
|
|
157
|
+
const visibleSpan = symbolSpan * part;
|
|
158
|
+
const at = i * span;
|
|
159
|
+
const id = `unit-clip-${clipIndex++}`;
|
|
160
|
+
const rx = horizontal ? x + at : x;
|
|
161
|
+
const ry = horizontal ? y : y + height - at - span;
|
|
162
|
+
const rw = horizontal ? symbolSpan : width;
|
|
163
|
+
const rh = horizontal ? Math.max(0, height - 0) : symbolSpan;
|
|
164
|
+
if (part < 1) additions.push(clipRect(ctx, id, rx, horizontal ? ry : ry + symbolSpan - visibleSpan, horizontal ? visibleSpan : rw, horizontal ? rh : visibleSpan));
|
|
165
|
+
const radius = Math.min(7, rw / 2, rh / 2);
|
|
166
|
+
marks.push(ctx.rect(rx, ry, rw, rh, {
|
|
167
|
+
rx: radius,
|
|
168
|
+
fill: color,
|
|
169
|
+
...part < 1 ? { "clip-path": `url(#${id})` } : {},
|
|
170
|
+
meta: {
|
|
171
|
+
...node.meta,
|
|
172
|
+
unit,
|
|
173
|
+
fraction: part
|
|
174
|
+
}
|
|
175
|
+
}));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return marks;
|
|
179
|
+
});
|
|
180
|
+
let finalChildren = [...additions, ...children];
|
|
181
|
+
if (mode === "pictorial") finalChildren = flatMap(finalChildren, (node) => {
|
|
182
|
+
if (node.tag === "line" && !node.meta) return [];
|
|
183
|
+
if (node.tag === "text" && !node.meta) return [];
|
|
184
|
+
if (node.tag === "text" && node.meta?.kind === "value") {
|
|
185
|
+
const shift = offsets.get(key(node.meta));
|
|
186
|
+
if (shift) return [{
|
|
187
|
+
...node,
|
|
188
|
+
attrs: {
|
|
189
|
+
...node.attrs,
|
|
190
|
+
x: Number(node.attrs.x) + shift.dx,
|
|
191
|
+
y: Number(node.attrs.y) + shift.dy
|
|
192
|
+
}
|
|
193
|
+
}];
|
|
194
|
+
}
|
|
195
|
+
return null;
|
|
196
|
+
});
|
|
197
|
+
return withHeader({
|
|
198
|
+
...scene,
|
|
199
|
+
width: pictorialWidth,
|
|
200
|
+
children: finalChildren
|
|
201
|
+
}, ctx, unitText, { meta: {
|
|
202
|
+
kind: "unit-mark-label",
|
|
203
|
+
label,
|
|
204
|
+
value: unit
|
|
205
|
+
} });
|
|
206
|
+
}
|
|
207
|
+
function lollipop(spec, ctx, scene) {
|
|
208
|
+
const shifted = flatMap(scene.children, (node) => {
|
|
209
|
+
if (node.tag !== "rect" || node.meta?.kind !== "record" || !Number.isFinite(node.meta.value)) return null;
|
|
210
|
+
const value = node.meta.value;
|
|
211
|
+
const x = Number(node.attrs.x), y = Number(node.attrs.y), width = Number(node.attrs.width), height = Number(node.attrs.height);
|
|
212
|
+
const horizontal = spec.chart === "bar";
|
|
213
|
+
const x1 = horizontal ? value >= 0 ? x : x + width : x + width / 2;
|
|
214
|
+
const y1 = horizontal ? y + height / 2 : value >= 0 ? y + height : y;
|
|
215
|
+
const x2 = horizontal ? value >= 0 ? x + width : x : x + width / 2;
|
|
216
|
+
const y2 = horizontal ? y + height / 2 : value >= 0 ? y : y + height;
|
|
217
|
+
const fill = node.attrs.fill || ctx.color("ink");
|
|
218
|
+
return [ctx.line(x1, y1, x2, y2, {
|
|
219
|
+
stroke: fill,
|
|
220
|
+
"stroke-width": 2,
|
|
221
|
+
"stroke-linecap": "round",
|
|
222
|
+
meta: {
|
|
223
|
+
...node.meta,
|
|
224
|
+
kind: "connector"
|
|
225
|
+
}
|
|
226
|
+
}), ctx.circle(x2, y2, 5, {
|
|
227
|
+
fill,
|
|
228
|
+
stroke: ctx.color("bg"),
|
|
229
|
+
"stroke-width": 2,
|
|
230
|
+
meta: node.meta
|
|
231
|
+
})];
|
|
232
|
+
}).map((node) => {
|
|
233
|
+
if (node.tag !== "text" || node.meta?.kind !== "value") return node;
|
|
234
|
+
const horizontal = spec.chart === "bar";
|
|
235
|
+
const next = {
|
|
236
|
+
...node,
|
|
237
|
+
attrs: { ...node.attrs }
|
|
238
|
+
};
|
|
239
|
+
if (horizontal) next.attrs.x = Number(node.attrs.x) + (Number(node.meta.value) >= 0 ? 14 : -14);
|
|
240
|
+
else if (Number(node.meta.value) < 0) next.attrs.y = Number(node.attrs.y) + 14;
|
|
241
|
+
next.attrs.fill = ctx.textColor("fg");
|
|
242
|
+
return next;
|
|
243
|
+
});
|
|
244
|
+
return {
|
|
245
|
+
...scene,
|
|
246
|
+
children: shifted
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
function dumbbellRungs(spec, ctx) {
|
|
250
|
+
const e = spec.encode, rows = ctx.table(spec).rows, unit = spec.unitMark.value;
|
|
251
|
+
const values = rows.flatMap((row) => [row[e.before], row[e.after]]);
|
|
252
|
+
ensureSymbolBudget(values, unit, ctx);
|
|
253
|
+
const categoryLines = rows.map((row) => ctx.wrap(String(row[e.category]), Math.max(32, ctx.width * .28), 12, 400));
|
|
254
|
+
const categoryWidth = Math.max(48, ...categoryLines.flatMap((lines) => lines.map((line) => ctx.measure(line, 12, 400)))) + 14;
|
|
255
|
+
const readout = (row, role, fallback) => {
|
|
256
|
+
const configured = ctx.field(role).label;
|
|
257
|
+
return `${configured && configured !== role ? configured : isEnglish(ctx.lang) ? fallback : fallback === "before" ? "之前" : "之后"} ${ctx.format(row[role], role)}`;
|
|
258
|
+
};
|
|
259
|
+
const right = Math.max(16, ...rows.flatMap((row) => [ctx.measure(readout(row, e.before, "before"), 12, 600), ctx.measure(readout(row, e.after, "after"), 12, 600)])) + 18;
|
|
260
|
+
const left = Math.max(categoryWidth, ...ctx.ticks(values, e.before, {
|
|
261
|
+
zero: true,
|
|
262
|
+
count: 2
|
|
263
|
+
}).labels.map((text) => ctx.measure(text) + 14));
|
|
264
|
+
const plotRight = Math.max(left + 80, ctx.width - right);
|
|
265
|
+
const ticks = ctx.ticks(values, e.before, {
|
|
266
|
+
zero: true,
|
|
267
|
+
count: 5,
|
|
268
|
+
length: plotRight - left
|
|
269
|
+
});
|
|
270
|
+
const rowHeight = Math.max(52, ...categoryLines.map((lines) => lines.length * 16 + 16));
|
|
271
|
+
const top = 34, bottom = top + rowHeight * rows.length;
|
|
272
|
+
const x = ctx.linear(ticks.min, ticks.max, left, plotRight), children = [];
|
|
273
|
+
ticks.ticks.forEach((tick, index) => {
|
|
274
|
+
const px = x(tick);
|
|
275
|
+
if (tick !== 0) children.push(ctx.line(px, top, px, bottom, {
|
|
276
|
+
stroke: ctx.color("grid"),
|
|
277
|
+
"stroke-width": 1
|
|
278
|
+
}));
|
|
279
|
+
children.push(ctx.text(px, bottom + 18, ticks.labels[index], {
|
|
280
|
+
anchor: "middle",
|
|
281
|
+
size: 12,
|
|
282
|
+
fill: ctx.color("muted")
|
|
283
|
+
}));
|
|
284
|
+
});
|
|
285
|
+
rows.forEach((row, index) => {
|
|
286
|
+
const cy = top + rowHeight * (index + .5), record = row[e.id] ?? String(index);
|
|
287
|
+
const categoryMeta = {
|
|
288
|
+
kind: "category",
|
|
289
|
+
record,
|
|
290
|
+
label: String(row[e.category])
|
|
291
|
+
};
|
|
292
|
+
categoryLines[index].forEach((line, lineIndex) => {
|
|
293
|
+
children.push(ctx.text(left - 10, cy + 4 - (categoryLines[index].length - 1) * 8 + lineIndex * 16, line, {
|
|
294
|
+
anchor: "end",
|
|
295
|
+
size: 12,
|
|
296
|
+
fill: ctx.color("fg"),
|
|
297
|
+
meta: categoryMeta
|
|
298
|
+
}));
|
|
299
|
+
});
|
|
300
|
+
const a = row[e.before], b = row[e.after];
|
|
301
|
+
children.push(ctx.line(x(a), cy, x(b), cy, {
|
|
302
|
+
stroke: ctx.color("grid"),
|
|
303
|
+
"stroke-width": 2,
|
|
304
|
+
meta: {
|
|
305
|
+
kind: "connector",
|
|
306
|
+
record
|
|
307
|
+
}
|
|
308
|
+
}));
|
|
309
|
+
for (const [value, toneIndex, role] of [[
|
|
310
|
+
a,
|
|
311
|
+
0,
|
|
312
|
+
e.before
|
|
313
|
+
], [
|
|
314
|
+
b,
|
|
315
|
+
1,
|
|
316
|
+
e.after
|
|
317
|
+
]]) {
|
|
318
|
+
const count = unitCount(value, unit, ctx), step = (plotRight - left) * unit / (ticks.max - ticks.min);
|
|
319
|
+
const markY = cy + (toneIndex ? 10 : -10), color = ctx.color(toneIndex ? "accent" : "ink"), direction = value < 0 ? -1 : 1, zeroX = x(0);
|
|
320
|
+
for (let i = 0; i < Math.ceil(count); i += 1) {
|
|
321
|
+
const fraction = i === Math.floor(count) && count % 1 > 0 ? count % 1 : 1;
|
|
322
|
+
const nearX = zeroX + direction * i * step, markWidth = step * .7;
|
|
323
|
+
const markX = direction > 0 ? nearX + step * .15 : nearX - step * .85;
|
|
324
|
+
const markHeight = 8, markTop = markY - markHeight / 2;
|
|
325
|
+
requireReadableSlot(markWidth, markHeight, 4, ctx, "增加图表宽度或增大unitMark.value");
|
|
326
|
+
const id = `dumbbell-unit-clip-${index}-${toneIndex}-${i}`;
|
|
327
|
+
if (fraction < 1) {
|
|
328
|
+
const clipX = direction > 0 ? markX : markX + markWidth * (1 - fraction);
|
|
329
|
+
children.push(clipRect(ctx, id, clipX, markTop, markWidth * fraction, markHeight));
|
|
330
|
+
}
|
|
331
|
+
children.push(ctx.rect(markX, markTop, markWidth, markHeight, {
|
|
332
|
+
rx: markHeight / 2,
|
|
333
|
+
fill: color,
|
|
334
|
+
...fraction < 1 ? { "clip-path": `url(#${id})` } : {},
|
|
335
|
+
meta: {
|
|
336
|
+
kind: "record",
|
|
337
|
+
record,
|
|
338
|
+
value,
|
|
339
|
+
field: role,
|
|
340
|
+
category: String(row[e.category]),
|
|
341
|
+
unit,
|
|
342
|
+
fraction
|
|
343
|
+
}
|
|
344
|
+
}));
|
|
345
|
+
}
|
|
346
|
+
children.push(ctx.text(plotRight + 8, markY + 4, readout(row, role, toneIndex ? "after" : "before"), {
|
|
347
|
+
size: 12,
|
|
348
|
+
weight: 600,
|
|
349
|
+
fill: ctx.textColor(toneIndex ? "accent" : "ink"),
|
|
350
|
+
meta: {
|
|
351
|
+
kind: "value",
|
|
352
|
+
record,
|
|
353
|
+
value,
|
|
354
|
+
field: role,
|
|
355
|
+
label: readout(row, role, toneIndex ? "after" : "before")
|
|
356
|
+
}
|
|
357
|
+
}));
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
return withHeader({
|
|
361
|
+
width: Math.max(ctx.width, plotRight + right),
|
|
362
|
+
height: bottom + 28,
|
|
363
|
+
children,
|
|
364
|
+
title: spec.title || "dumbbell chart",
|
|
365
|
+
description: "dumbbell chart with unit rungs"
|
|
366
|
+
}, ctx, unitDescription(spec, ctx, e.before), { meta: {
|
|
367
|
+
kind: "unit-mark-label",
|
|
368
|
+
label: spec.unitMark.label,
|
|
369
|
+
value: unit
|
|
370
|
+
} });
|
|
371
|
+
}
|
|
372
|
+
function waterfall(spec, ctx, next) {
|
|
373
|
+
ensureSymbolBudget(spec.data.map((row) => row[spec.encode.delta]), spec.unitMark.value, ctx);
|
|
374
|
+
const scene = next();
|
|
375
|
+
let stage = 0, clipIndex = 0;
|
|
376
|
+
const clips = [];
|
|
377
|
+
const children = flatMap(scene.children, (node) => {
|
|
378
|
+
if (node.tag !== "g" || !node.children?.some((child) => child.tag === "rect")) return null;
|
|
379
|
+
const stageIndex = stage++;
|
|
380
|
+
if (stageIndex === 0 || stageIndex > spec.data.length) return [node];
|
|
381
|
+
const bar = node.children.find((child) => child.tag === "rect");
|
|
382
|
+
const row = spec.data[stageIndex - 1], delta = row[spec.encode.delta], value = Math.abs(delta);
|
|
383
|
+
if (!value) return [];
|
|
384
|
+
const count = unitCount(value, spec.unitMark.value, ctx), x = Number(bar.attrs.x), y = Number(bar.attrs.y);
|
|
385
|
+
const width = Number(bar.attrs.width), height = Number(bar.attrs.height), span = height / count, marks = [];
|
|
386
|
+
const gap = Math.min(2.5, span * .16), symbolHeight = span - gap;
|
|
387
|
+
for (let i = 0; i < Math.ceil(count); i += 1) {
|
|
388
|
+
const fraction = i === Math.floor(count) && count % 1 > 0 ? count % 1 : 1;
|
|
389
|
+
const markY = (delta >= 0 ? y + height - (i + 1) * span : y + i * span) + gap / 2;
|
|
390
|
+
const clipY = markY + (delta >= 0 ? symbolHeight * (1 - fraction) : 0);
|
|
391
|
+
const visibleHeight = symbolHeight * fraction;
|
|
392
|
+
requireReadableSlot(width, visibleHeight, 6, ctx, "增大图表宽度或unitMark.value,确保每档高度至少可读");
|
|
393
|
+
const id = `waterfall-unit-clip-${clipIndex++}`;
|
|
394
|
+
clips.push(clipRect(ctx, id, x, clipY, width, visibleHeight));
|
|
395
|
+
marks.push(ctx.rect(x, markY, width, symbolHeight, {
|
|
396
|
+
rx: Math.min(5, width / 2, symbolHeight / 2),
|
|
397
|
+
fill: bar.attrs.fill || ctx.color(delta >= 0 ? "accent" : "tone5"),
|
|
398
|
+
"clip-path": `url(#${id})`,
|
|
399
|
+
meta: {
|
|
400
|
+
kind: "record",
|
|
401
|
+
record: row[spec.encode.id] ?? String(stageIndex - 1),
|
|
402
|
+
category: row[spec.encode.category],
|
|
403
|
+
value: delta,
|
|
404
|
+
unit: spec.unitMark.value,
|
|
405
|
+
fraction
|
|
406
|
+
}
|
|
407
|
+
}));
|
|
408
|
+
}
|
|
409
|
+
return [{
|
|
410
|
+
...node,
|
|
411
|
+
children: marks
|
|
412
|
+
}];
|
|
413
|
+
});
|
|
414
|
+
const field = spec.encode.delta;
|
|
415
|
+
return withHeader({
|
|
416
|
+
...scene,
|
|
417
|
+
children: [...clips, ...children]
|
|
418
|
+
}, ctx, unitDescription(spec, ctx, field), { meta: {
|
|
419
|
+
kind: "unit-mark-label",
|
|
420
|
+
label: spec.unitMark.label,
|
|
421
|
+
value: spec.unitMark.value
|
|
422
|
+
} });
|
|
423
|
+
}
|
|
424
|
+
function histogram(spec, ctx) {
|
|
425
|
+
const e = spec.encode, rows = spec.data, widths = rows.map((r) => r[e.upper] - r[e.lower]);
|
|
426
|
+
ensureSymbolBudget(rows.map((row) => row[e.count]), spec.unitMark.value, ctx);
|
|
427
|
+
const densities = rows.map((r, i) => r[e.count] / widths[i]);
|
|
428
|
+
const countDomain = ctx.field(e.count).domain;
|
|
429
|
+
const densityDomain = Array.isArray(countDomain) ? countDomain.flatMap((value) => widths.map((width) => value / width)) : [];
|
|
430
|
+
const ticks = ctx.ticks([
|
|
431
|
+
...densities,
|
|
432
|
+
...densityDomain,
|
|
433
|
+
0
|
|
434
|
+
], void 0, {
|
|
435
|
+
zero: true,
|
|
436
|
+
count: 4
|
|
437
|
+
});
|
|
438
|
+
const left = Math.max(56, ...ticks.labels.map((text) => ctx.measure(text) + 14));
|
|
439
|
+
const first = rows[0][e.lower], last = rows.at(-1)[e.upper];
|
|
440
|
+
const annotations = (spec.annotations || []).filter((a) => [e.lower, e.upper].includes(a.field) && a.kind !== "note");
|
|
441
|
+
const annotationX = annotations.flatMap((a) => a.kind === "rule" ? [a.value] : [a.from, a.to]);
|
|
442
|
+
const xDomains = [ctx.field(e.lower).domain, ctx.field(e.upper).domain].filter(Array.isArray);
|
|
443
|
+
const domainLow = Math.min(first, ...xDomains.map((domain) => domain[0]), ...annotationX);
|
|
444
|
+
const domainHigh = Math.max(last, ...xDomains.map((domain) => domain[1]), ...annotationX);
|
|
445
|
+
const boundaryLabels = [.../* @__PURE__ */ new Set([
|
|
446
|
+
domainLow,
|
|
447
|
+
first,
|
|
448
|
+
...rows.map((row) => row[e.upper]),
|
|
449
|
+
domainHigh
|
|
450
|
+
])].sort((a, b) => a - b);
|
|
451
|
+
const boundaryText = boundaryLabels.map((tick) => ctx.format(tick, e.lower));
|
|
452
|
+
const endpointWidth = ctx.measure(boundaryText[0], 12, 400) + ctx.measure(boundaryText.at(-1), 12, 400) + 8;
|
|
453
|
+
const right = Math.max(left + 120, ctx.width - 8, left + endpointWidth);
|
|
454
|
+
const x = (value) => left + (value - domainLow) / (domainHigh - domainLow || 1) * (right - left);
|
|
455
|
+
const lanes = labelLanes(ctx, annotations.filter((a) => a.label).map((a) => ({
|
|
456
|
+
a,
|
|
457
|
+
text: a.label,
|
|
458
|
+
x: a.kind === "rule" ? x(a.value) : (x(a.from) + x(a.to)) / 2
|
|
459
|
+
})), {
|
|
460
|
+
left,
|
|
461
|
+
right
|
|
462
|
+
});
|
|
463
|
+
const annotationHeight = lanes.height ? lanes.height + 4 : 0;
|
|
464
|
+
const top = 34 + annotationHeight, bottom = 254 + annotationHeight;
|
|
465
|
+
const y = ctx.linear(ticks.min, ticks.max, bottom, top), children = [];
|
|
466
|
+
ticks.ticks.forEach((tick, index) => {
|
|
467
|
+
const py = y(tick);
|
|
468
|
+
if (tick !== 0) children.push(ctx.line(left, py, right, py, {
|
|
469
|
+
stroke: ctx.color("grid"),
|
|
470
|
+
"stroke-width": 1
|
|
471
|
+
}));
|
|
472
|
+
children.push(ctx.text(left - 10, py + 4, ticks.labels[index], {
|
|
473
|
+
anchor: "end",
|
|
474
|
+
size: 12,
|
|
475
|
+
fill: ctx.color("muted")
|
|
476
|
+
}));
|
|
477
|
+
});
|
|
478
|
+
const labelBottom = /* @__PURE__ */ new Map();
|
|
479
|
+
lanes.items.forEach((item) => {
|
|
480
|
+
const baseline = 42 + item.offset;
|
|
481
|
+
labelBottom.set(item.a, baseline + (item.lines.length - 1) * 16 + 4);
|
|
482
|
+
children.push(ctx.textLines(item.cx, baseline, item.lines, {
|
|
483
|
+
anchor: "middle",
|
|
484
|
+
size: 12,
|
|
485
|
+
fill: ctx.textColor("accent"),
|
|
486
|
+
meta: {
|
|
487
|
+
kind: "annotation",
|
|
488
|
+
label: item.a.label
|
|
489
|
+
}
|
|
490
|
+
}));
|
|
491
|
+
});
|
|
492
|
+
annotations.forEach((annotation) => {
|
|
493
|
+
const from = annotation.kind === "rule" ? annotation.value : annotation.from;
|
|
494
|
+
const to = annotation.kind === "rule" ? annotation.value : annotation.to;
|
|
495
|
+
if (annotation.kind === "rule") children.push(ctx.line(x(from), (labelBottom.get(annotation) ?? top - 2) + 2, x(from), bottom, {
|
|
496
|
+
stroke: ctx.color("accent"),
|
|
497
|
+
"stroke-width": 1.5,
|
|
498
|
+
"stroke-dasharray": "4 4",
|
|
499
|
+
meta: {
|
|
500
|
+
kind: "annotation",
|
|
501
|
+
label: annotation.label || ""
|
|
502
|
+
}
|
|
503
|
+
}));
|
|
504
|
+
else children.push(ctx.rect(x(from), top, Math.max(0, x(to) - x(from)), bottom - top, {
|
|
505
|
+
fill: ctx.color("accent"),
|
|
506
|
+
"fill-opacity": .12,
|
|
507
|
+
meta: {
|
|
508
|
+
kind: "annotation",
|
|
509
|
+
label: annotation.label || ""
|
|
510
|
+
}
|
|
511
|
+
}));
|
|
512
|
+
});
|
|
513
|
+
const unit = spec.unitMark.value, label = spec.unitMark.label;
|
|
514
|
+
let clipIndex = 0;
|
|
515
|
+
rows.forEach((row, index) => {
|
|
516
|
+
const bx = x(row[e.lower]), bw = x(row[e.upper]) - bx, barHeight = y(0) - y(densities[index]);
|
|
517
|
+
const count = unitCount(row[e.count], unit, ctx);
|
|
518
|
+
if (row[e.count] === 0) return;
|
|
519
|
+
const slotHeight = barHeight / count, gap = slotHeight * .1, symbolHeight = slotHeight - gap;
|
|
520
|
+
for (let mark = 0; mark < Math.ceil(count); mark += 1) {
|
|
521
|
+
const fraction = mark === Math.floor(count) && count % 1 > 0 ? count % 1 : 1;
|
|
522
|
+
const markY = bottom - (mark + 1) * slotHeight + gap / 2;
|
|
523
|
+
const visibleHeight = symbolHeight * fraction, clipY = markY + symbolHeight * (1 - fraction), id = `hist-unit-clip-${clipIndex++}`;
|
|
524
|
+
requireReadableSlot(bw, visibleHeight, 8, ctx, "增大图表宽度或unitMark.value,确保分箱与单位档均可辨认");
|
|
525
|
+
children.push(clipRect(ctx, id, bx, clipY, bw, visibleHeight));
|
|
526
|
+
children.push(ctx.rect(bx, markY, bw, symbolHeight, {
|
|
527
|
+
rx: Math.min(4, bw / 2, symbolHeight / 2),
|
|
528
|
+
fill: ctx.color("tone1"),
|
|
529
|
+
"clip-path": `url(#${id})`,
|
|
530
|
+
meta: {
|
|
531
|
+
kind: "record",
|
|
532
|
+
record: String(index),
|
|
533
|
+
value: row[e.count],
|
|
534
|
+
unit,
|
|
535
|
+
fraction,
|
|
536
|
+
lower: row[e.lower],
|
|
537
|
+
upper: row[e.upper]
|
|
538
|
+
}
|
|
539
|
+
}));
|
|
540
|
+
}
|
|
541
|
+
});
|
|
542
|
+
const boundaryX = boundaryLabels.map(x);
|
|
543
|
+
const boundaryWidth = boundaryText.map((text) => ctx.measure(text, 12, 400));
|
|
544
|
+
const lastBoundary = boundaryLabels.length - 1;
|
|
545
|
+
const boundaryLeft = (index) => {
|
|
546
|
+
const width = boundaryWidth[index];
|
|
547
|
+
if (index === 0) return boundaryX[index];
|
|
548
|
+
if (index === lastBoundary) return boundaryX[index] - width;
|
|
549
|
+
return Math.max(left + width / 2, Math.min(right - width / 2, boundaryX[index])) - width / 2;
|
|
550
|
+
};
|
|
551
|
+
const boundaryRight = (index) => boundaryLeft(index) + boundaryWidth[index];
|
|
552
|
+
let visible = lastBoundary > 0 ? [0, lastBoundary] : [0];
|
|
553
|
+
for (let step = 1; step < lastBoundary; step += 1) {
|
|
554
|
+
const pick = [];
|
|
555
|
+
for (let index = 0; index < lastBoundary; index += step) pick.push(index);
|
|
556
|
+
pick.push(lastBoundary);
|
|
557
|
+
while (pick.length > 2 && boundaryLeft(lastBoundary) < boundaryRight(pick.at(-2)) + 8) pick.splice(-2, 1);
|
|
558
|
+
if (pick.every((index, k) => !k || boundaryLeft(index) >= boundaryRight(pick[k - 1]) + 8)) {
|
|
559
|
+
visible = pick;
|
|
560
|
+
break;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
boundaryLabels.forEach((tick, index) => {
|
|
564
|
+
if (!visible.includes(index)) return;
|
|
565
|
+
const anchor = index === 0 ? "start" : index === boundaryLabels.length - 1 ? "end" : "middle";
|
|
566
|
+
const half = ctx.measure(boundaryText[index], 12, 400) / 2;
|
|
567
|
+
const px = anchor === "start" ? boundaryX[index] : anchor === "end" ? boundaryX[index] : Math.max(left + half, Math.min(right - half, boundaryX[index]));
|
|
568
|
+
children.push(ctx.text(px, bottom + 20, boundaryText[index], {
|
|
569
|
+
anchor,
|
|
570
|
+
size: 12,
|
|
571
|
+
fill: ctx.color("muted")
|
|
572
|
+
}));
|
|
573
|
+
});
|
|
574
|
+
children.push(ctx.line(left, bottom, right, bottom, {
|
|
575
|
+
stroke: ctx.color("axis"),
|
|
576
|
+
"stroke-width": 1
|
|
577
|
+
}));
|
|
578
|
+
const countField = ctx.field(e.count), binField = ctx.field(e.lower);
|
|
579
|
+
const areaLabel = `${countField.label && countField.label !== e.count ? countField.label : isEnglish(ctx.lang) ? "Frequency" : "频数"} / ${binField.unit || (isEnglish(ctx.lang) ? "bin width" : "箱宽")}`;
|
|
580
|
+
return withHeader({
|
|
581
|
+
width: Math.max(ctx.width, right),
|
|
582
|
+
height: bottom + 32,
|
|
583
|
+
children,
|
|
584
|
+
title: spec.title || "histogram chart",
|
|
585
|
+
description: "Histogram uses actual bin widths; each rung area represents frequency."
|
|
586
|
+
}, ctx, `${unitDescription(spec, ctx, e.count)} · ${areaLabel}`, { meta: {
|
|
587
|
+
kind: "unit-mark-label",
|
|
588
|
+
label,
|
|
589
|
+
value: unit
|
|
590
|
+
} });
|
|
591
|
+
}
|
|
592
|
+
function dedupeReadouts(scene) {
|
|
593
|
+
const seen = /* @__PURE__ */ new Set();
|
|
594
|
+
const visit = (nodes) => (nodes || []).flatMap((node) => {
|
|
595
|
+
if (node.tag === "text" && node.meta?.kind === "value") {
|
|
596
|
+
const key = `${node.text}@${Math.round(Number(node.attrs.x))},${Math.round(Number(node.attrs.y))}`;
|
|
597
|
+
if (seen.has(key)) return [];
|
|
598
|
+
seen.add(key);
|
|
599
|
+
}
|
|
600
|
+
if (node.children) return [{
|
|
601
|
+
...node,
|
|
602
|
+
children: visit(node.children)
|
|
603
|
+
}];
|
|
604
|
+
return [node];
|
|
605
|
+
});
|
|
606
|
+
return {
|
|
607
|
+
...scene,
|
|
608
|
+
children: visit(scene.children)
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
function decorate(spec, ctx, next) {
|
|
612
|
+
const variant = spec.variant;
|
|
613
|
+
if (!variant || variant === "solid") return next();
|
|
614
|
+
if (["bar", "column"].includes(spec.chart)) {
|
|
615
|
+
if (![
|
|
616
|
+
"rungs",
|
|
617
|
+
"pictorial",
|
|
618
|
+
"lollipop"
|
|
619
|
+
].includes(variant)) return next();
|
|
620
|
+
if (variant === "lollipop") return dedupeReadouts(lollipop(spec, ctx, next()));
|
|
621
|
+
if (spec.stack === "percent") ctx.fit("单位计数不适用于百分比堆叠", "改用solid变体或绝对堆叠");
|
|
622
|
+
const fields = Array.isArray(spec.encode.value) ? spec.encode.value : [spec.encode.value];
|
|
623
|
+
ensureSymbolBudget(spec.data.flatMap((row) => fields.map((field) => row[field])), spec.unitMark.value, ctx);
|
|
624
|
+
return dedupeReadouts(unitMarks(spec, ctx, next(), variant === "pictorial" ? "pictorial" : "rungs"));
|
|
625
|
+
}
|
|
626
|
+
if (spec.chart === "dumbbell" && variant === "rungs") return dumbbellRungs(spec, ctx);
|
|
627
|
+
if (spec.chart === "waterfall" && variant === "rungs") return waterfall(spec, ctx, next);
|
|
628
|
+
if (spec.chart === "histogram" && variant === "rungs") return histogram(spec, ctx);
|
|
629
|
+
return next();
|
|
630
|
+
}
|
|
631
|
+
function install({ layouts, decorate: registerDecorator }) {
|
|
632
|
+
const available = [
|
|
633
|
+
"bar",
|
|
634
|
+
"column",
|
|
635
|
+
"dumbbell",
|
|
636
|
+
"waterfall",
|
|
637
|
+
"histogram"
|
|
638
|
+
].filter((name) => layouts.has(name));
|
|
639
|
+
if (available.length) registerDecorator(available, decorate);
|
|
640
|
+
}
|
|
641
|
+
install({
|
|
642
|
+
layouts,
|
|
643
|
+
decorate: decorate$1
|
|
644
|
+
});
|
|
645
|
+
export { install, unit_variants_exports };
|