@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,1007 @@
|
|
|
1
|
+
import { __exportAll } from "../_virtual/_rolldown/runtime.js";
|
|
2
|
+
import { register } from "./registry.js";
|
|
3
|
+
import { apportion, columnHeaders, labelLanes } from "../core/geometry.js";
|
|
4
|
+
import { compare } from "./stat-compare.js";
|
|
5
|
+
import { smallMultiples, sparkline } from "./stat-trend.js";
|
|
6
|
+
// src/chart/statistics.js
|
|
7
|
+
var statistics_exports = /* @__PURE__ */ __exportAll({
|
|
8
|
+
install: () => install,
|
|
9
|
+
renderStatistics: () => renderStatistics,
|
|
10
|
+
waffle: () => waffle
|
|
11
|
+
});
|
|
12
|
+
var HEAT_SHARES = [
|
|
13
|
+
.42,
|
|
14
|
+
.56,
|
|
15
|
+
.7,
|
|
16
|
+
.84,
|
|
17
|
+
.96
|
|
18
|
+
];
|
|
19
|
+
function heatFill(level, negative, ctx) {
|
|
20
|
+
const ink = negative ? ctx.scheme === "dark" ? ctx.palette.accent : ctx.palette.tone4 : ctx.palette.ink;
|
|
21
|
+
return ctx.mix(ink, ctx.palette.bg, 1 - HEAT_SHARES[level - 1]);
|
|
22
|
+
}
|
|
23
|
+
var createScene = (spec, ctx, partial) => {
|
|
24
|
+
const scene = ctx.scene(partial.height, partial.children, `${spec.chart} chart`);
|
|
25
|
+
scene.width = partial.width;
|
|
26
|
+
scene.description = `${spec.chart} statistics chart`;
|
|
27
|
+
return scene;
|
|
28
|
+
};
|
|
29
|
+
function heatmap(spec, ctx) {
|
|
30
|
+
const e = spec.encode;
|
|
31
|
+
const children = [];
|
|
32
|
+
const rowNames = ctx.field(e.row).domain || [...new Set(spec.data.map((row) => row[e.row]))];
|
|
33
|
+
const colNames = ctx.field(e.column).domain || [...new Set(spec.data.map((row) => row[e.column]))];
|
|
34
|
+
const rowWidth = Math.min(Math.max(0, ...rowNames.map((name) => ctx.measure(String(name), 12, 400) + 8)), Math.max(72, ctx.width * .28));
|
|
35
|
+
const left = rowWidth + 8;
|
|
36
|
+
const headers = columnHeaders(ctx, colNames, {
|
|
37
|
+
left,
|
|
38
|
+
right: 12
|
|
39
|
+
});
|
|
40
|
+
const rowLines = rowNames.map((name) => ctx.wrap(String(name), rowWidth, 12));
|
|
41
|
+
const cw = headers.slot;
|
|
42
|
+
const ch = Math.max(38, Math.max(1, ...rowLines.map((lines) => lines.length)) * 20 + 8);
|
|
43
|
+
const top = 34 + (headers.years ? 16 : 0);
|
|
44
|
+
const values = spec.data.map((row) => row[e.value]).filter(Number.isFinite);
|
|
45
|
+
const [low, high] = ctx.field(e.value).domain || [Math.min(0, ...values), Math.max(0, ...values)];
|
|
46
|
+
const max = Math.max(Math.abs(low), Math.abs(high)) || 1;
|
|
47
|
+
const fillOf = (value) => {
|
|
48
|
+
return heatFill(Math.max(1, Math.min(5, Math.ceil(Math.abs(value) / max * 5))), value < 0, ctx);
|
|
49
|
+
};
|
|
50
|
+
headers.items.forEach((item, j) => {
|
|
51
|
+
if (item.year) children.push(ctx.text(left + j * cw, 18, item.year, {
|
|
52
|
+
fill: ctx.palette.muted,
|
|
53
|
+
"font-size": 12
|
|
54
|
+
}));
|
|
55
|
+
if (item.lines.length) children.push(ctx.textLines(left + (j + .5) * cw, headers.years ? 34 : 18, item.lines, {
|
|
56
|
+
anchor: "middle",
|
|
57
|
+
fill: ctx.palette.muted,
|
|
58
|
+
"font-size": 12
|
|
59
|
+
}));
|
|
60
|
+
});
|
|
61
|
+
rowNames.forEach((name, i) => {
|
|
62
|
+
const y = top + i * ch;
|
|
63
|
+
const lines = rowLines[i];
|
|
64
|
+
children.push(ctx.textLines(left - 8, y + ch / 2 - (lines.length - 1) * 10, lines, {
|
|
65
|
+
anchor: "end",
|
|
66
|
+
fill: ctx.palette.muted,
|
|
67
|
+
"font-size": 12,
|
|
68
|
+
leading: 20
|
|
69
|
+
}));
|
|
70
|
+
colNames.forEach((col, j) => {
|
|
71
|
+
const cell = spec.data.find((row) => row[e.row] === name && row[e.column] === col);
|
|
72
|
+
if (!cell || cell[e.value] == null) return;
|
|
73
|
+
const value = cell[e.value];
|
|
74
|
+
const fill = fillOf(value);
|
|
75
|
+
children.push(ctx.rect(left + j * cw + 2, y + 2, cw - 4, ch - 4, {
|
|
76
|
+
rx: 7,
|
|
77
|
+
fill,
|
|
78
|
+
meta: {
|
|
79
|
+
record: `${name}/${col}`,
|
|
80
|
+
row: name,
|
|
81
|
+
column: col,
|
|
82
|
+
value
|
|
83
|
+
}
|
|
84
|
+
}));
|
|
85
|
+
const formatted = ctx.format(value, e.value);
|
|
86
|
+
if (ctx.measure(formatted, 12, 600) + 8 <= cw) children.push(ctx.text(left + (j + .5) * cw, y + 23, formatted, {
|
|
87
|
+
anchor: "middle",
|
|
88
|
+
fill: ctx.textOn(fill),
|
|
89
|
+
"font-size": 12,
|
|
90
|
+
"font-weight": 600
|
|
91
|
+
}));
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
const width = Math.max(ctx.width, left + colNames.length * cw + 12);
|
|
95
|
+
const signed = low < 0 && high > 0;
|
|
96
|
+
const english = /^en(?:-|$)/i.test(String(ctx.lang || ""));
|
|
97
|
+
const labels = english ? {
|
|
98
|
+
low: "Low",
|
|
99
|
+
zero: "Zero",
|
|
100
|
+
high: "High"
|
|
101
|
+
} : {
|
|
102
|
+
low: "低",
|
|
103
|
+
zero: "零",
|
|
104
|
+
high: "高"
|
|
105
|
+
};
|
|
106
|
+
const endpoint = (value, kind) => ({
|
|
107
|
+
label: `${labels[value === 0 ? "zero" : kind]} · ${signed && value > 0 ? "+" : ""}${ctx.format(value, e.value)}`,
|
|
108
|
+
color: fillOf(value),
|
|
109
|
+
meta: {
|
|
110
|
+
endpoint: kind,
|
|
111
|
+
value
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
const endpoints = [endpoint(low, "low")];
|
|
115
|
+
if (signed) endpoints.push(endpoint(0, "zero"));
|
|
116
|
+
if (high !== low) endpoints.push(endpoint(high, high === 0 ? "zero" : "high"));
|
|
117
|
+
const budget = Math.max(48, ctx.width - 24);
|
|
118
|
+
const legend = ctx.legend(endpoints, budget);
|
|
119
|
+
const note = signed ? english ? "Hue separates negative and positive values. Shade shows magnitude. Blank = missing." : "色相区分负值与正值;颜色强度表示绝对值。空白表示缺失。" : english ? "Color scale. Blank = missing." : "色阶对应数值,空白表示缺失。";
|
|
120
|
+
const noteLines = ctx.wrap(note, budget, 12);
|
|
121
|
+
const legendY = top + rowNames.length * ch + 10;
|
|
122
|
+
const noteY = legendY + legend.height + 15;
|
|
123
|
+
children.push(ctx.el("g", { transform: `translate(12 ${legendY})` }, legend.children));
|
|
124
|
+
children.push(ctx.textLines(12, noteY, noteLines, {
|
|
125
|
+
size: 12,
|
|
126
|
+
fill: ctx.palette.muted
|
|
127
|
+
}));
|
|
128
|
+
return {
|
|
129
|
+
width,
|
|
130
|
+
height: noteY + (noteLines.length - 1) * 16 + 8,
|
|
131
|
+
children
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
function annotatedScene(spec, ctx) {
|
|
135
|
+
const children = [], e = spec.encode;
|
|
136
|
+
const addText = (x, y, text, anchor = "middle") => children.push(ctx.text(x, y, text, {
|
|
137
|
+
anchor,
|
|
138
|
+
fill: ctx.palette.muted,
|
|
139
|
+
"font-size": 12
|
|
140
|
+
}));
|
|
141
|
+
if (spec.chart === "scatter") {
|
|
142
|
+
const xs = spec.data.map((r) => r[e.x]), ys = spec.data.map((r) => r[e.y]);
|
|
143
|
+
const fx = ctx.field(e.x), fy = ctx.field(e.y), logX = fx.scale === "log", logY = fy.scale === "log";
|
|
144
|
+
if (logX && (fx.domain?.[0] <= 0 || fx.domain?.[1] <= 0) || logY && (fy.domain?.[0] <= 0 || fy.domain?.[1] <= 0)) throw new Error("Log-axis domains must be positive");
|
|
145
|
+
const tx = (v) => logX ? Math.log(v) : v, ty = (v) => logY ? Math.log(v) : v;
|
|
146
|
+
const bounds = (values, meta, transform, fieldName) => {
|
|
147
|
+
const domain = Array.isArray(meta.domain) ? meta.domain.map(transform) : [];
|
|
148
|
+
const annotated = (spec.annotations || []).filter((a) => a.field === fieldName && a.kind !== "note").flatMap((a) => a.kind === "rule" ? [a.value] : [a.from, a.to]);
|
|
149
|
+
if (logX && fieldName === e.x || logY && fieldName === e.y) {
|
|
150
|
+
if (annotated.some((value) => value <= 0)) throw new Error("Log-axis annotations must be positive");
|
|
151
|
+
}
|
|
152
|
+
const vals = [...values, ...annotated].map(transform), lo = Math.min(...vals, ...domain), hi = Math.max(...vals, ...domain);
|
|
153
|
+
const span = hi - lo || Math.abs(hi) * .1 || 1;
|
|
154
|
+
return meta.domain ? [lo, hi] : [lo - span * .06, hi + span * .06];
|
|
155
|
+
};
|
|
156
|
+
const [xmin, xmax] = bounds(xs, fx, tx, e.x), [ymin, ymax] = bounds(ys, fy, ty, e.y);
|
|
157
|
+
const xScale = logX ? null : ctx.ticks(xs, e.x, {
|
|
158
|
+
count: 4,
|
|
159
|
+
length: Math.max(48, ctx.width - 96)
|
|
160
|
+
});
|
|
161
|
+
const yScale = logY ? null : ctx.ticks(ys, e.y, { count: 4 });
|
|
162
|
+
const xDomain = xScale ? [xScale.min, xScale.max] : [Math.exp(xmin), Math.exp(xmax)];
|
|
163
|
+
const yDomain = yScale ? [yScale.min, yScale.max] : [Math.exp(ymin), Math.exp(ymax)];
|
|
164
|
+
const yTickLabels = (() => {
|
|
165
|
+
if (fy.scale === "log") return null;
|
|
166
|
+
return yScale.labels;
|
|
167
|
+
})();
|
|
168
|
+
const logYLabels = logY ? Array.from({ length: Math.max(0, Math.min(10, Math.floor(ymax / Math.LN10) - Math.ceil(ymin / Math.LN10) + 1)) }, (_, index) => ctx.format(10 ** (Math.ceil(ymin / Math.LN10) + index), e.y)) : [];
|
|
169
|
+
const yTickWidth = Math.max(24, ...[...yTickLabels || [], ...logYLabels].map((label) => ctx.measure(label, 12, 400) + 8));
|
|
170
|
+
const left = Math.max(40, Math.ceil(yTickWidth + 16));
|
|
171
|
+
const markMax = e.size ? 12 : 5;
|
|
172
|
+
const right = ctx.width - Math.max(8, markMax + 2);
|
|
173
|
+
const axisName = (fieldName) => [ctx.field(fieldName).label || fieldName, ctx.field(fieldName).unit].filter(Boolean).join(" · ");
|
|
174
|
+
const yTitleLines = ctx.wrap(axisName(e.y), Math.max(48, right - 8), 12, 400);
|
|
175
|
+
const xTitleLines = ctx.wrap(axisName(e.x), Math.max(48, right - left), 12, 400);
|
|
176
|
+
const series = e.series ? [...new Set(spec.data.map((row) => row[e.series]))] : [];
|
|
177
|
+
const legendY = 16 + (series.length ? 20 : 0);
|
|
178
|
+
const legend = ctx.legend(series.map((label, i) => ({
|
|
179
|
+
label: String(label),
|
|
180
|
+
color: ctx.palette[`tone${i % 6 + 1}`]
|
|
181
|
+
})), Math.max(48, right - left));
|
|
182
|
+
const baseTop = series.length ? legendY + 8 + legend.height : 8;
|
|
183
|
+
const plotHeight = Math.round(Math.max(200, Math.min(360, (right - left) * .6)));
|
|
184
|
+
const yTitleBlock = yTitleLines.length * 16 + 8;
|
|
185
|
+
let top = baseTop + yTitleBlock, bottom = top + plotHeight;
|
|
186
|
+
const x = (v) => left + (tx(v) - tx(xDomain[0])) / (tx(xDomain[1]) - tx(xDomain[0]) || 1) * (right - left);
|
|
187
|
+
const y = (v) => bottom - (ty(v) - ty(yDomain[0])) / (ty(yDomain[1]) - ty(yDomain[0]) || 1) * (bottom - top);
|
|
188
|
+
const sizeMax = Math.max(1, ...spec.data.map((row) => e.size ? row[e.size] : 1));
|
|
189
|
+
const radiusOf = (row) => e.size ? Math.sqrt(row[e.size] / sizeMax) * markMax : markMax;
|
|
190
|
+
const bandFill = ctx.mix(ctx.palette.accent, ctx.palette.bg, .82);
|
|
191
|
+
const notes = placeAnnotations();
|
|
192
|
+
const bandHeight = notes.banded.length ? notes.banded.reduce((sum, item) => sum + item.lines.length * 16, 0) + 8 : 0;
|
|
193
|
+
top += bandHeight;
|
|
194
|
+
bottom += bandHeight;
|
|
195
|
+
function placeAnnotations() {
|
|
196
|
+
const boxOf = (px, py, lines, anchor) => {
|
|
197
|
+
const width = Math.max(...lines.map((line) => ctx.measure(line, 12, 400)));
|
|
198
|
+
const x0 = anchor === "end" ? px - width : anchor === "middle" ? px - width / 2 : px;
|
|
199
|
+
return {
|
|
200
|
+
x0,
|
|
201
|
+
x1: x0 + width,
|
|
202
|
+
y0: py - 12,
|
|
203
|
+
y1: py + (lines.length - 1) * 16 + 4
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
const overlaps = (a, b) => a.x0 < b.x1 + 2 && b.x0 < a.x1 + 2 && a.y0 < b.y1 + 2 && b.y0 < a.y1 + 2;
|
|
207
|
+
const blocked = spec.data.map((row) => {
|
|
208
|
+
const cx = x(row[e.x]), cy = y(row[e.y]), r = radiusOf(row);
|
|
209
|
+
return {
|
|
210
|
+
x0: cx - r,
|
|
211
|
+
x1: cx + r,
|
|
212
|
+
y0: cy - r,
|
|
213
|
+
y1: cy + r
|
|
214
|
+
};
|
|
215
|
+
});
|
|
216
|
+
const bands = (spec.annotations || []).filter((a) => a.kind === "band").map((a) => a.field === e.x ? {
|
|
217
|
+
x0: Math.min(x(a.from), x(a.to)),
|
|
218
|
+
x1: Math.max(x(a.from), x(a.to)),
|
|
219
|
+
y0: top,
|
|
220
|
+
y1: bottom
|
|
221
|
+
} : {
|
|
222
|
+
x0: left,
|
|
223
|
+
x1: right,
|
|
224
|
+
y0: Math.min(y(a.from), y(a.to)),
|
|
225
|
+
y1: Math.max(y(a.from), y(a.to))
|
|
226
|
+
});
|
|
227
|
+
const option = (px, baseline, anchor, width, text) => {
|
|
228
|
+
const lines = ctx.wrap(text, Math.max(48, width), 12, 400);
|
|
229
|
+
const py = baseline(lines.length);
|
|
230
|
+
return {
|
|
231
|
+
px,
|
|
232
|
+
py,
|
|
233
|
+
anchor,
|
|
234
|
+
lines,
|
|
235
|
+
box: boxOf(px, py, lines, anchor)
|
|
236
|
+
};
|
|
237
|
+
};
|
|
238
|
+
const above = (edge) => (n) => edge - 4 - (n - 1) * 16;
|
|
239
|
+
const below = (edge) => () => edge + 14;
|
|
240
|
+
const placed = [], banded = [];
|
|
241
|
+
for (const a of spec.annotations || []) {
|
|
242
|
+
const text = a.kind === "note" ? a.text : a.label;
|
|
243
|
+
if (!text) continue;
|
|
244
|
+
let options = [];
|
|
245
|
+
if (a.kind === "note") {
|
|
246
|
+
const row = spec.data.find((candidate) => candidate[e.id] === a.record);
|
|
247
|
+
if (!row) continue;
|
|
248
|
+
const cx = x(row[e.x]), cy = y(row[e.y]), r = radiusOf(row);
|
|
249
|
+
options = [
|
|
250
|
+
option(cx + r + 3, above(cy), "start", right - cx - r - 3, text),
|
|
251
|
+
option(cx + r + 3, below(cy), "start", right - cx - r - 3, text),
|
|
252
|
+
option(cx - r - 3, above(cy), "end", cx - r - 3 - left, text),
|
|
253
|
+
option(cx - r - 3, below(cy), "end", cx - r - 3 - left, text),
|
|
254
|
+
option(cx, above(cy - r - 2), "middle", Math.min(160, right - left), text),
|
|
255
|
+
option(cx, below(cy + r + 2), "middle", Math.min(160, right - left), text)
|
|
256
|
+
];
|
|
257
|
+
} else if (a.kind === "rule" && a.field === e.y) {
|
|
258
|
+
const vy = y(a.value);
|
|
259
|
+
options = [
|
|
260
|
+
[
|
|
261
|
+
left + 4,
|
|
262
|
+
above(vy - 1),
|
|
263
|
+
"start"
|
|
264
|
+
],
|
|
265
|
+
[
|
|
266
|
+
right - 4,
|
|
267
|
+
above(vy - 1),
|
|
268
|
+
"end"
|
|
269
|
+
],
|
|
270
|
+
[
|
|
271
|
+
left + 4,
|
|
272
|
+
below(vy + 1),
|
|
273
|
+
"start"
|
|
274
|
+
],
|
|
275
|
+
[
|
|
276
|
+
right - 4,
|
|
277
|
+
below(vy + 1),
|
|
278
|
+
"end"
|
|
279
|
+
]
|
|
280
|
+
].map(([px, baseline, anchor]) => option(px, baseline, anchor, right - left - 8, text));
|
|
281
|
+
} else if (a.kind === "rule" && a.field === e.x) {
|
|
282
|
+
const vx = x(a.value);
|
|
283
|
+
options = [
|
|
284
|
+
option(vx + 4, () => top + 14, "start", right - vx - 8, text),
|
|
285
|
+
option(vx - 4, () => top + 14, "end", vx - 8 - left, text),
|
|
286
|
+
option(vx + 4, above(bottom - 2), "start", right - vx - 8, text),
|
|
287
|
+
option(vx - 4, above(bottom - 2), "end", vx - 8 - left, text)
|
|
288
|
+
];
|
|
289
|
+
} else if (a.kind === "band") {
|
|
290
|
+
const band = a.field === e.x ? {
|
|
291
|
+
mid: (x(a.from) + x(a.to)) / 2,
|
|
292
|
+
width: Math.abs(x(a.to) - x(a.from)) - 8
|
|
293
|
+
} : { mid: (y(a.from) + y(a.to)) / 2 };
|
|
294
|
+
options = a.field === e.x ? [option(band.mid, () => top + 14, "middle", band.width, text), option(band.mid, above(bottom - 2), "middle", band.width, text)] : [option(right - 4, (n) => band.mid + 4 - (n - 1) * 8, "end", right - left - 8, text), option(left + 4, (n) => band.mid + 4 - (n - 1) * 8, "start", right - left - 8, text)];
|
|
295
|
+
}
|
|
296
|
+
const fits = (item) => item.box.x0 >= left && item.box.x1 <= right && item.box.y0 >= top && item.box.y1 <= bottom && !blocked.some((other) => overlaps(item.box, other));
|
|
297
|
+
const hit = options.find(fits);
|
|
298
|
+
const inBand = (box) => bands.some((band) => overlaps(box, band));
|
|
299
|
+
const color = (box) => inBand(box) ? ctx.textOn(bandFill, { opacity: .55 }) : ctx.textColor(a.kind === "note" ? "fg" : "accent");
|
|
300
|
+
if (hit) {
|
|
301
|
+
placed.push({
|
|
302
|
+
...hit,
|
|
303
|
+
fill: color(hit.box)
|
|
304
|
+
});
|
|
305
|
+
blocked.push(hit.box);
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
const row = a.kind === "note" ? spec.data.find((candidate) => candidate[e.id] === a.record) : null;
|
|
309
|
+
const owner = row ? `${row[e.label] ?? a.record}${ctx.lang?.startsWith("en") ? ": " : ":"}` : "";
|
|
310
|
+
banded.push({
|
|
311
|
+
lines: ctx.wrap(`${owner}${text}`, Math.max(48, right - left - 8), 12, 400),
|
|
312
|
+
fill: ctx.textColor(a.kind === "note" ? "fg" : "accent")
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
return {
|
|
316
|
+
placed,
|
|
317
|
+
banded
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
const ticks = (lo, hi, meta, scale, span, horizontal) => {
|
|
321
|
+
if (meta.scale === "log") {
|
|
322
|
+
const a = Math.ceil(lo / Math.LN10), b = Math.floor(hi / Math.LN10);
|
|
323
|
+
const all = Array.from({ length: Math.max(0, Math.min(10, b - a + 1)) }, (_, i) => 10 ** (a + i));
|
|
324
|
+
const candidates = all.length ? all : [Math.exp(lo), Math.exp(hi)];
|
|
325
|
+
const widest = Math.max(...candidates.map((value) => ctx.measure(ctx.format(value, meta === fx ? e.x : e.y), 12, 400)));
|
|
326
|
+
const step = Math.max(1, Math.ceil((widest + (horizontal ? 8 : 14)) / (span / Math.max(1, candidates.length - 1))));
|
|
327
|
+
const selected = [];
|
|
328
|
+
candidates.forEach((value, index) => {
|
|
329
|
+
if (index % step !== 0 && index !== candidates.length - 1) return;
|
|
330
|
+
const label = ctx.format(value, meta === fx ? e.x : e.y);
|
|
331
|
+
const width = ctx.measure(label, 12, 400);
|
|
332
|
+
const prior = selected.at(-1);
|
|
333
|
+
const distance = prior ? (Math.log(value) - Math.log(prior.value)) / Math.max(1e-9, hi - lo) * span : Infinity;
|
|
334
|
+
if (!prior || distance >= (width + prior.width) / 2 + (horizontal ? 8 : 14)) selected.push({
|
|
335
|
+
value,
|
|
336
|
+
width
|
|
337
|
+
});
|
|
338
|
+
});
|
|
339
|
+
return {
|
|
340
|
+
values: selected.map((tick) => tick.value),
|
|
341
|
+
labels: null
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
values: scale.ticks,
|
|
346
|
+
labels: scale.labels
|
|
347
|
+
};
|
|
348
|
+
};
|
|
349
|
+
const yTicks = ticks(ymin, ymax, fy, yScale, bottom - top, false);
|
|
350
|
+
const xTicks = ticks(xmin, xmax, fx, xScale, right - left, true);
|
|
351
|
+
const xLabels = xTicks.values.map((t, i) => xTicks.labels?.[i] ?? ctx.format(t, e.x));
|
|
352
|
+
yTicks.values.forEach((t, i) => {
|
|
353
|
+
children.push(ctx.line(left, y(t), right, y(t), {
|
|
354
|
+
stroke: ctx.palette.grid,
|
|
355
|
+
"stroke-width": 1
|
|
356
|
+
}));
|
|
357
|
+
addText(left - 8, y(t) + 4, yTicks.labels?.[i] ?? ctx.format(t, e.y), "end");
|
|
358
|
+
});
|
|
359
|
+
const placedX = xTicks.values.map((t, i) => {
|
|
360
|
+
children.push(ctx.line(x(t), top, x(t), bottom, {
|
|
361
|
+
stroke: ctx.palette.grid,
|
|
362
|
+
"stroke-width": 1
|
|
363
|
+
}));
|
|
364
|
+
const half = ctx.measure(xLabels[i], 12, 400) / 2;
|
|
365
|
+
return {
|
|
366
|
+
label: xLabels[i],
|
|
367
|
+
half,
|
|
368
|
+
px: Math.max(half + 8, Math.min(ctx.width - half - 8, x(t)))
|
|
369
|
+
};
|
|
370
|
+
});
|
|
371
|
+
const keptX = [];
|
|
372
|
+
const clashes = (prior, item) => prior.px + prior.half + 8 > item.px - item.half;
|
|
373
|
+
placedX.forEach((item, i) => {
|
|
374
|
+
if (i < placedX.length - 1) {
|
|
375
|
+
if (!keptX.length || !clashes(keptX.at(-1), item)) keptX.push(item);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
while (keptX.length > 1 && clashes(keptX.at(-1), item)) keptX.pop();
|
|
379
|
+
if (!keptX.length || !clashes(keptX.at(-1), item)) keptX.push(item);
|
|
380
|
+
});
|
|
381
|
+
keptX.forEach(({ label, px }) => addText(px, bottom + 18, label));
|
|
382
|
+
const colors = series.map((_, i) => ctx.palette[`tone${i % 6 + 1}`]);
|
|
383
|
+
children.push(...legend.children.map((node) => ({
|
|
384
|
+
...node,
|
|
385
|
+
attrs: {
|
|
386
|
+
...node.attrs,
|
|
387
|
+
transform: `translate(${left} ${legendY - 20})`
|
|
388
|
+
}
|
|
389
|
+
})));
|
|
390
|
+
spec.data.forEach((row, i) => {
|
|
391
|
+
const r = radiusOf(row);
|
|
392
|
+
const seriesIndex = e.series ? series.indexOf(row[e.series]) : -1;
|
|
393
|
+
const mark = ctx.circle(x(row[e.x]), y(row[e.y]), r, {
|
|
394
|
+
fill: seriesIndex >= 0 ? colors[seriesIndex] : ctx.palette.accent,
|
|
395
|
+
stroke: ctx.palette.bg,
|
|
396
|
+
"stroke-width": 2
|
|
397
|
+
});
|
|
398
|
+
const title = `${row[e.label] ?? row[e.id] ?? i + 1}:${ctx.format(row[e.x], e.x)},${ctx.format(row[e.y], e.y)}${e.size ? `,${ctx.format(row[e.size], e.size)}` : ""}`;
|
|
399
|
+
children.push(ctx.titled(mark, title));
|
|
400
|
+
});
|
|
401
|
+
for (const a of spec.annotations || []) {
|
|
402
|
+
if (a.kind === "rule" && a.field === e.x) children.push(ctx.line(x(a.value), top, x(a.value), bottom, {
|
|
403
|
+
stroke: ctx.palette.accent,
|
|
404
|
+
"stroke-width": 1.5,
|
|
405
|
+
"stroke-dasharray": "4 4"
|
|
406
|
+
}));
|
|
407
|
+
if (a.kind === "rule" && a.field === e.y) children.push(ctx.line(left, y(a.value), right, y(a.value), {
|
|
408
|
+
stroke: ctx.palette.accent,
|
|
409
|
+
"stroke-width": 1.5,
|
|
410
|
+
"stroke-dasharray": "4 4"
|
|
411
|
+
}));
|
|
412
|
+
if (a.kind === "band" && a.field === e.x) children.push(ctx.rect(x(a.from), top, Math.max(0, x(a.to) - x(a.from)), bottom - top, {
|
|
413
|
+
fill: ctx.mix(ctx.palette.accent, ctx.palette.bg, .82),
|
|
414
|
+
opacity: .55
|
|
415
|
+
}));
|
|
416
|
+
if (a.kind === "band" && a.field === e.y) children.push(ctx.rect(left, y(a.to), right - left, Math.max(0, y(a.from) - y(a.to)), {
|
|
417
|
+
fill: ctx.mix(ctx.palette.accent, ctx.palette.bg, .82),
|
|
418
|
+
opacity: .55
|
|
419
|
+
}));
|
|
420
|
+
}
|
|
421
|
+
for (const item of notes.placed) children.push(ctx.textLines(item.px, item.py + bandHeight, item.lines, {
|
|
422
|
+
anchor: item.anchor,
|
|
423
|
+
fill: item.fill,
|
|
424
|
+
"font-size": 12
|
|
425
|
+
}));
|
|
426
|
+
if (e.label && spec.data.length <= 40) {
|
|
427
|
+
const hitBox = (a, b) => {
|
|
428
|
+
const pad = b.label ? 0 : 2;
|
|
429
|
+
return a.x0 < b.x1 + pad && b.x0 < a.x1 + pad && a.y0 < b.y1 + pad && b.y0 < a.y1 + pad;
|
|
430
|
+
};
|
|
431
|
+
const dots = spec.data.map((row) => ({
|
|
432
|
+
cx: x(row[e.x]),
|
|
433
|
+
cy: y(row[e.y]),
|
|
434
|
+
r: radiusOf(row)
|
|
435
|
+
}));
|
|
436
|
+
const gapTo = (dot, box) => Math.hypot(Math.max(box.x0 - dot.cx, 0, dot.cx - box.x1), Math.max(box.y0 - dot.cy, 0, dot.cy - box.y1)) - dot.r;
|
|
437
|
+
const owns = (own, { box, py }) => {
|
|
438
|
+
const glyphs = {
|
|
439
|
+
x0: box.x0,
|
|
440
|
+
x1: box.x1,
|
|
441
|
+
y0: py - 10,
|
|
442
|
+
y1: py + 1
|
|
443
|
+
};
|
|
444
|
+
return dots.every((dot) => dot === own || gapTo(dot, glyphs) >= gapTo(own, glyphs) + 1);
|
|
445
|
+
};
|
|
446
|
+
const taken = [
|
|
447
|
+
...dots.map(({ cx, cy, r }) => ({
|
|
448
|
+
x0: cx - r,
|
|
449
|
+
x1: cx + r,
|
|
450
|
+
y0: cy - r,
|
|
451
|
+
y1: cy + r
|
|
452
|
+
})),
|
|
453
|
+
...spec.variant === "plumb" ? dots.map(({ cx, cy, r }) => ({
|
|
454
|
+
x0: cx - .5,
|
|
455
|
+
x1: cx + .5,
|
|
456
|
+
y0: cy + r,
|
|
457
|
+
y1: bottom
|
|
458
|
+
})) : [],
|
|
459
|
+
...notes.placed.map((item) => ({
|
|
460
|
+
...item.box,
|
|
461
|
+
y0: item.box.y0 + bandHeight,
|
|
462
|
+
y1: item.box.y1 + bandHeight
|
|
463
|
+
})),
|
|
464
|
+
...yTitleLines.map((line, i) => ({
|
|
465
|
+
x0: 8,
|
|
466
|
+
x1: 8 + ctx.measure(line, 12, 400),
|
|
467
|
+
y0: top - yTitleBlock + i * 16,
|
|
468
|
+
y1: top - yTitleBlock + i * 16 + 15
|
|
469
|
+
}))
|
|
470
|
+
];
|
|
471
|
+
spec.data.forEach((row, index) => {
|
|
472
|
+
const text = row[e.label] == null ? "" : String(row[e.label]);
|
|
473
|
+
if (!text) return;
|
|
474
|
+
const own = dots[index], { cx, cy, r } = own, w = ctx.measure(text, 12, 400);
|
|
475
|
+
const hit = [
|
|
476
|
+
[
|
|
477
|
+
cx + r + 4,
|
|
478
|
+
cy + 4,
|
|
479
|
+
"start"
|
|
480
|
+
],
|
|
481
|
+
[
|
|
482
|
+
cx - r - 4,
|
|
483
|
+
cy + 4,
|
|
484
|
+
"end"
|
|
485
|
+
],
|
|
486
|
+
[
|
|
487
|
+
cx + r + 2,
|
|
488
|
+
cy - r - 2,
|
|
489
|
+
"start"
|
|
490
|
+
],
|
|
491
|
+
[
|
|
492
|
+
cx + r + 2,
|
|
493
|
+
cy + r + 12,
|
|
494
|
+
"start"
|
|
495
|
+
],
|
|
496
|
+
[
|
|
497
|
+
cx - r - 2,
|
|
498
|
+
cy - r - 2,
|
|
499
|
+
"end"
|
|
500
|
+
],
|
|
501
|
+
[
|
|
502
|
+
cx - r - 2,
|
|
503
|
+
cy + r + 12,
|
|
504
|
+
"end"
|
|
505
|
+
],
|
|
506
|
+
[
|
|
507
|
+
cx,
|
|
508
|
+
cy - r - 5,
|
|
509
|
+
"middle"
|
|
510
|
+
],
|
|
511
|
+
...spec.variant === "plumb" ? [] : [[
|
|
512
|
+
cx,
|
|
513
|
+
cy + r + 15,
|
|
514
|
+
"middle"
|
|
515
|
+
]]
|
|
516
|
+
].map(([px, py, anchor]) => {
|
|
517
|
+
const x0 = anchor === "end" ? px - w : anchor === "middle" ? px - w / 2 : px;
|
|
518
|
+
return {
|
|
519
|
+
px,
|
|
520
|
+
py,
|
|
521
|
+
anchor,
|
|
522
|
+
box: {
|
|
523
|
+
x0,
|
|
524
|
+
x1: x0 + w,
|
|
525
|
+
y0: py - 12,
|
|
526
|
+
y1: py + 3
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
}).find((candidate) => {
|
|
530
|
+
const { box } = candidate;
|
|
531
|
+
return box.x0 >= left && box.x1 <= right && box.y0 >= top - 8 && box.y1 <= bottom && !taken.some((other) => hitBox(box, other)) && owns(own, candidate);
|
|
532
|
+
});
|
|
533
|
+
if (!hit) return;
|
|
534
|
+
taken.push({
|
|
535
|
+
...hit.box,
|
|
536
|
+
label: true
|
|
537
|
+
});
|
|
538
|
+
children.push(ctx.text(hit.px, hit.py, text, {
|
|
539
|
+
anchor: hit.anchor,
|
|
540
|
+
size: 12,
|
|
541
|
+
fill: ctx.textColor("fg"),
|
|
542
|
+
meta: {
|
|
543
|
+
kind: "point-label",
|
|
544
|
+
label: text,
|
|
545
|
+
cx,
|
|
546
|
+
cy
|
|
547
|
+
}
|
|
548
|
+
}));
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
let bandY = baseTop + 12;
|
|
552
|
+
for (const item of notes.banded) {
|
|
553
|
+
children.push(ctx.textLines(left + 4, bandY, item.lines, {
|
|
554
|
+
fill: item.fill,
|
|
555
|
+
"font-size": 12
|
|
556
|
+
}));
|
|
557
|
+
bandY += item.lines.length * 16;
|
|
558
|
+
}
|
|
559
|
+
children.push(ctx.textLines(8, top - yTitleBlock + 12, yTitleLines, {
|
|
560
|
+
fill: ctx.palette.muted,
|
|
561
|
+
"font-size": 12,
|
|
562
|
+
meta: {
|
|
563
|
+
kind: "axis-title",
|
|
564
|
+
axis: "y"
|
|
565
|
+
}
|
|
566
|
+
}));
|
|
567
|
+
children.push(ctx.textLines(right, bottom + 40, xTitleLines, {
|
|
568
|
+
anchor: "end",
|
|
569
|
+
fill: ctx.palette.muted,
|
|
570
|
+
"font-size": 12,
|
|
571
|
+
meta: {
|
|
572
|
+
kind: "axis-title",
|
|
573
|
+
axis: "x"
|
|
574
|
+
}
|
|
575
|
+
}));
|
|
576
|
+
return {
|
|
577
|
+
width: ctx.width,
|
|
578
|
+
height: bottom + 40 + (xTitleLines.length - 1) * 16 + 12,
|
|
579
|
+
children
|
|
580
|
+
};
|
|
581
|
+
}
|
|
582
|
+
if (spec.chart === "waterfall") {
|
|
583
|
+
const rows = spec.data;
|
|
584
|
+
const start = spec.start ?? 0;
|
|
585
|
+
const startLabel = ctx.lang?.startsWith("en") ? "Start" : "起点";
|
|
586
|
+
const endLabel = ctx.lang?.startsWith("en") ? "End" : "终点";
|
|
587
|
+
const stages = [{
|
|
588
|
+
label: startLabel,
|
|
589
|
+
before: 0,
|
|
590
|
+
after: start,
|
|
591
|
+
value: start,
|
|
592
|
+
total: true,
|
|
593
|
+
record: null,
|
|
594
|
+
id: "start"
|
|
595
|
+
}];
|
|
596
|
+
let current = start;
|
|
597
|
+
rows.forEach((row, index) => {
|
|
598
|
+
const delta = row[e.delta];
|
|
599
|
+
const before = current;
|
|
600
|
+
current += delta;
|
|
601
|
+
stages.push({
|
|
602
|
+
label: String(row[e.category]),
|
|
603
|
+
before,
|
|
604
|
+
after: current,
|
|
605
|
+
value: delta,
|
|
606
|
+
delta,
|
|
607
|
+
total: false,
|
|
608
|
+
record: index,
|
|
609
|
+
id: e.id ? row[e.id] : String(index)
|
|
610
|
+
});
|
|
611
|
+
});
|
|
612
|
+
stages.push({
|
|
613
|
+
label: endLabel,
|
|
614
|
+
before: 0,
|
|
615
|
+
after: current,
|
|
616
|
+
value: current,
|
|
617
|
+
total: true,
|
|
618
|
+
record: null,
|
|
619
|
+
id: "end"
|
|
620
|
+
});
|
|
621
|
+
const field = e.delta;
|
|
622
|
+
const unit = ctx.field(field).unit || spec.unit;
|
|
623
|
+
const values = stages.flatMap((stage) => stage.total ? [stage.after] : [stage.before, stage.after]);
|
|
624
|
+
const minimum = Math.min(...values), maximum = Math.max(...values), span = maximum - minimum;
|
|
625
|
+
const truncated = minimum > 0 && span <= .35 * maximum;
|
|
626
|
+
let scale = ctx.ticks(values, field, {
|
|
627
|
+
zero: !truncated,
|
|
628
|
+
count: 4
|
|
629
|
+
});
|
|
630
|
+
let baseline = 0;
|
|
631
|
+
if (truncated) {
|
|
632
|
+
const threshold = minimum - .08 * span;
|
|
633
|
+
baseline = Math.floor((threshold + Number.EPSILON * Math.abs(threshold)) / scale.step) * scale.step;
|
|
634
|
+
if (!(baseline > 0 && baseline <= threshold)) {
|
|
635
|
+
scale = ctx.ticks(values, field, {
|
|
636
|
+
zero: false,
|
|
637
|
+
count: 4
|
|
638
|
+
});
|
|
639
|
+
baseline = Math.floor((threshold + Number.EPSILON * Math.abs(threshold)) / scale.step) * scale.step;
|
|
640
|
+
}
|
|
641
|
+
if (!(baseline > 0 && baseline <= threshold)) {
|
|
642
|
+
baseline = 0;
|
|
643
|
+
scale = ctx.ticks(values, field, {
|
|
644
|
+
zero: true,
|
|
645
|
+
count: 4
|
|
646
|
+
});
|
|
647
|
+
} else {
|
|
648
|
+
const ticks = [];
|
|
649
|
+
for (let value = baseline; value <= scale.max + scale.step * 1e-8; value += scale.step) ticks.push(Number(value.toPrecision(12)));
|
|
650
|
+
if (ticks.at(-1) < maximum) ticks.push(Number((ticks.at(-1) + scale.step).toPrecision(12)));
|
|
651
|
+
scale = {
|
|
652
|
+
...scale,
|
|
653
|
+
min: baseline,
|
|
654
|
+
ticks,
|
|
655
|
+
labels: ticks.map((value) => ctx.format(value, field, false))
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
const axisBreak = truncated && baseline > 0;
|
|
660
|
+
const left = Math.max(40, ...scale.labels.map((label) => ctx.measure(label, 12, 400) + 18));
|
|
661
|
+
const rightMargin = 10;
|
|
662
|
+
const number = (value) => ctx.format(value, field, false);
|
|
663
|
+
const readouts = stages.map((stage) => stage.total ? number(stage.after) : `${stage.delta > 0 ? "+" : ""}${number(stage.delta)}`);
|
|
664
|
+
const labelMeasure = (text) => ctx.measure(text, 12, 400);
|
|
665
|
+
const widestToken = Math.max(12, ...stages.map((stage) => Math.max(...String(stage.label).trim().split(/\s+/).map(labelMeasure))));
|
|
666
|
+
const minSlot = Math.max(26, Math.min(160, widestToken + 8));
|
|
667
|
+
const width = Math.max(ctx.width, left + stages.length * minSlot + rightMargin);
|
|
668
|
+
const right = width - rightMargin;
|
|
669
|
+
const slot = (right - left) / stages.length;
|
|
670
|
+
const labelWidth = Math.max(18, Math.min(slot - 8, widestToken));
|
|
671
|
+
const labelLines = stages.map((stage) => ctx.wrap(stage.label, labelWidth, 12, 400));
|
|
672
|
+
const labelDepth = Math.max(1, ...labelLines.map((lines) => lines.length));
|
|
673
|
+
const readoutWidth = readouts.map((text, index) => ctx.measure(text, 12, stages[index].total ? 700 : 600));
|
|
674
|
+
const readoutPitch = 16;
|
|
675
|
+
const head = (unit || axisBreak ? 22 : 8) + (axisBreak && unit ? 16 : 0);
|
|
676
|
+
const smallestDelta = Math.min(...rows.map((row) => Math.abs(row[e.delta])).filter((value) => value > 0));
|
|
677
|
+
const bottom = axisBreak ? Math.max(head + 220, head + 8 * (scale.max - baseline) / (Number.isFinite(smallestDelta) ? smallestDelta : 1)) : head + 236;
|
|
678
|
+
const yFor = ctx.linear(axisBreak ? baseline : scale.min, scale.max, bottom, head + readoutPitch);
|
|
679
|
+
const barTop = stages.map((stage) => {
|
|
680
|
+
const before = axisBreak && stage.total ? baseline : stage.before;
|
|
681
|
+
return Math.min(yFor(before), yFor(stage.after));
|
|
682
|
+
});
|
|
683
|
+
const order = stages.map((_, index) => index).sort((a, b) => barTop[a] - barTop[b] || a - b);
|
|
684
|
+
const lanes = [];
|
|
685
|
+
const taken = [];
|
|
686
|
+
order.forEach((index) => {
|
|
687
|
+
const center = left + slot * (index + .5);
|
|
688
|
+
const half = readoutWidth[index] / 2;
|
|
689
|
+
const readoutX = Math.max(half + 2, Math.min(width - rightMargin - half, center));
|
|
690
|
+
let lane = 0;
|
|
691
|
+
const hits = (candidate) => taken.some((box) => box.lane === candidate && Math.abs(box.x - readoutX) < (box.width + readoutWidth[index]) / 2 + 8);
|
|
692
|
+
while (hits(lane)) lane += 1;
|
|
693
|
+
lanes[index] = lane;
|
|
694
|
+
taken.push({
|
|
695
|
+
x: readoutX,
|
|
696
|
+
width: readoutWidth[index],
|
|
697
|
+
lane
|
|
698
|
+
});
|
|
699
|
+
});
|
|
700
|
+
const top = head + (Math.max(...lanes) + 1) * readoutPitch + 8;
|
|
701
|
+
const y = ctx.linear(axisBreak ? baseline : scale.min, scale.max, bottom, top);
|
|
702
|
+
const children = [];
|
|
703
|
+
scale.ticks.forEach((tick, index) => {
|
|
704
|
+
const py = y(tick);
|
|
705
|
+
if (!(axisBreak && index === 0)) children.push(ctx.line(left, py, right, py, {
|
|
706
|
+
stroke: ctx.palette.grid,
|
|
707
|
+
"stroke-width": 1
|
|
708
|
+
}));
|
|
709
|
+
children.push(ctx.text(left - 10, py + 4, scale.labels[index], {
|
|
710
|
+
anchor: "end",
|
|
711
|
+
size: 12,
|
|
712
|
+
fill: ctx.palette.muted
|
|
713
|
+
}));
|
|
714
|
+
});
|
|
715
|
+
if (unit) children.push(ctx.text(left, 18, `${ctx.lang?.startsWith("en") ? "Unit: " : "单位:"}${unit}`, {
|
|
716
|
+
size: 12,
|
|
717
|
+
fill: ctx.palette.muted
|
|
718
|
+
}));
|
|
719
|
+
if (axisBreak) children.push(ctx.text(left, unit ? 34 : 18, `${ctx.lang?.startsWith("en") ? "Axis starts at " : "纵轴从 "}${ctx.format(baseline, field, false)}`, {
|
|
720
|
+
size: 12,
|
|
721
|
+
fill: ctx.palette.muted
|
|
722
|
+
}));
|
|
723
|
+
const colon = ctx.lang?.startsWith("en") ? ": " : ":";
|
|
724
|
+
stages.forEach((stage, index) => {
|
|
725
|
+
const x = left + slot * index;
|
|
726
|
+
const before = axisBreak && stage.total ? baseline : stage.before;
|
|
727
|
+
const yBefore = y(before);
|
|
728
|
+
const yAfter = y(stage.after);
|
|
729
|
+
const barWidth = Math.max(1, slot * .62);
|
|
730
|
+
const barX = x + (slot - barWidth) / 2;
|
|
731
|
+
const color = stage.total ? ctx.palette.ink : stage.delta >= 0 ? ctx.palette.accent : ctx.palette.tone5;
|
|
732
|
+
const meta = {
|
|
733
|
+
kind: stage.total ? "waterfall-total" : "waterfall-delta",
|
|
734
|
+
record: stage.record,
|
|
735
|
+
id: stage.id,
|
|
736
|
+
label: stage.label,
|
|
737
|
+
value: stage.value,
|
|
738
|
+
before: stage.before,
|
|
739
|
+
after: stage.after,
|
|
740
|
+
total: stage.total,
|
|
741
|
+
...axisBreak && stage.total ? { truncated: true } : {}
|
|
742
|
+
};
|
|
743
|
+
const bar = ctx.rect(barX, Math.min(yBefore, yAfter), barWidth, Math.max(1, Math.abs(yBefore - yAfter)), {
|
|
744
|
+
rx: 4,
|
|
745
|
+
fill: color,
|
|
746
|
+
stroke: ctx.palette.bg,
|
|
747
|
+
"stroke-width": 1,
|
|
748
|
+
meta
|
|
749
|
+
});
|
|
750
|
+
children.push(ctx.titled(bar, `${stage.label}${colon}${ctx.format(stage.value, field)}`));
|
|
751
|
+
const readout = readouts[index];
|
|
752
|
+
const half = readoutWidth[index] / 2;
|
|
753
|
+
const readoutX = Math.max(half + 2, Math.min(width - rightMargin - half, x + slot / 2));
|
|
754
|
+
const readoutY = head + 14 + lanes[index] * readoutPitch;
|
|
755
|
+
children.push(ctx.text(readoutX, readoutY, readout, {
|
|
756
|
+
anchor: "middle",
|
|
757
|
+
size: 12,
|
|
758
|
+
weight: stage.total ? 700 : 600,
|
|
759
|
+
fill: ctx.palette.fg,
|
|
760
|
+
meta
|
|
761
|
+
}));
|
|
762
|
+
children.push(ctx.textLines(x + slot / 2, bottom + 20, labelLines[index], {
|
|
763
|
+
anchor: "middle",
|
|
764
|
+
size: 12,
|
|
765
|
+
fill: ctx.palette.muted
|
|
766
|
+
}));
|
|
767
|
+
if (axisBreak && stage.total) {
|
|
768
|
+
const baseY = y(baseline) - 10;
|
|
769
|
+
for (const offset of [5, 12]) {
|
|
770
|
+
children.push(ctx.line(barX + offset, baseY, barX + offset + 6, baseY - 4, {
|
|
771
|
+
stroke: ctx.palette.bg,
|
|
772
|
+
"stroke-width": 4,
|
|
773
|
+
"stroke-linecap": "round"
|
|
774
|
+
}));
|
|
775
|
+
children.push(ctx.line(barX + offset, baseY, barX + offset + 6, baseY - 4, {
|
|
776
|
+
stroke: color,
|
|
777
|
+
"stroke-width": 1.5,
|
|
778
|
+
"stroke-linecap": "round",
|
|
779
|
+
meta: {
|
|
780
|
+
kind: "axis-break",
|
|
781
|
+
id: stage.id,
|
|
782
|
+
truncated: true
|
|
783
|
+
}
|
|
784
|
+
}));
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
if (index < stages.length - 1) children.push(ctx.line(x + slot * .81, yAfter, x + slot * 1.19, yAfter, {
|
|
788
|
+
stroke: ctx.palette.muted,
|
|
789
|
+
"stroke-width": 1,
|
|
790
|
+
"stroke-dasharray": "2 3",
|
|
791
|
+
class: "chart-connector"
|
|
792
|
+
}));
|
|
793
|
+
});
|
|
794
|
+
children.push(ctx.line(left, y(axisBreak ? baseline : 0), right, y(axisBreak ? baseline : 0), {
|
|
795
|
+
stroke: ctx.palette.axis,
|
|
796
|
+
"stroke-width": 1
|
|
797
|
+
}));
|
|
798
|
+
return {
|
|
799
|
+
width,
|
|
800
|
+
height: Math.ceil(bottom + 28 + labelDepth * 16),
|
|
801
|
+
children
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
if (spec.chart === "histogram") {
|
|
805
|
+
const rows = spec.data;
|
|
806
|
+
const density = rows.some((r) => Math.abs(r[e.upper] - r[e.lower] - (rows[0][e.upper] - rows[0][e.lower])) > 1e-9);
|
|
807
|
+
const values = rows.map((r) => density ? r[e.count] / (r[e.upper] - r[e.lower]) : r[e.count]);
|
|
808
|
+
const countDomain = ctx.field(e.count).domain;
|
|
809
|
+
const yScale = ctx.ticks(values, e.count, {
|
|
810
|
+
zero: true,
|
|
811
|
+
count: 4
|
|
812
|
+
});
|
|
813
|
+
if (density) yScale.labels = yScale.ticks.map((value) => ctx.format(value, e.count, false));
|
|
814
|
+
const english = /^en(?:-|$)/i.test(String(ctx.lang || ""));
|
|
815
|
+
const countMeta = ctx.field(e.count), binMeta = ctx.field(e.lower);
|
|
816
|
+
const countLabel = countMeta.label !== e.count ? countMeta.label : english ? "Count" : "数量";
|
|
817
|
+
const yAxisTitle = density ? `${countLabel} / ${binMeta.unit || (english ? "bin width" : "区间宽度")}(${english ? `area represents ${countLabel}` : `面积表示${countLabel}`})` : countLabel;
|
|
818
|
+
const yMin = Math.min(0, yScale.min, ...Array.isArray(countDomain) ? [countDomain[0]] : []);
|
|
819
|
+
const densityDomainMax = Array.isArray(countDomain) && density ? Math.max(...rows.map((row) => countDomain[1] / (row[e.upper] - row[e.lower]))) : null;
|
|
820
|
+
const maxValue = Math.max(yScale.max, ...values, ...Array.isArray(countDomain) ? [density ? densityDomainMax : countDomain[1]] : []);
|
|
821
|
+
const yLabelWidth = Math.max(...yScale.labels.map((label) => ctx.measure(label, 12, 400)));
|
|
822
|
+
const left = Math.max(40, Math.ceil(yLabelWidth + 18));
|
|
823
|
+
const xDomains = [ctx.field(e.lower).domain, ctx.field(e.upper).domain].filter(Array.isArray);
|
|
824
|
+
const binEdges = [...new Set(rows.flatMap((row) => [row[e.lower], row[e.upper]]))].sort((a, b) => a - b);
|
|
825
|
+
const edgeLabels = binEdges.map((value) => ctx.format(value, e.lower, false));
|
|
826
|
+
const edgeMinimum = ctx.measure(edgeLabels[0], 12, 400) + ctx.measure(edgeLabels.at(-1), 12, 400) + 8;
|
|
827
|
+
const sceneWidth = Math.max(ctx.width, left + edgeMinimum + 8);
|
|
828
|
+
const right = sceneWidth - 8;
|
|
829
|
+
const titleLines = ctx.wrap(yAxisTitle, Math.max(48, right - left), 12, 400);
|
|
830
|
+
const titleExtra = (titleLines.length - 1) * 16;
|
|
831
|
+
const xAnnotations = (spec.annotations || []).filter((a) => [e.lower, e.upper].includes(a.field) && a.kind !== "note");
|
|
832
|
+
const annotatedX = xAnnotations.flatMap((a) => a.kind === "rule" ? [a.value] : [a.from, a.to]);
|
|
833
|
+
const lo = Math.min(rows[0][e.lower], ...xDomains.map((domain) => domain[0]), ...annotatedX);
|
|
834
|
+
const hi = Math.max(rows.at(-1)[e.upper], ...xDomains.map((domain) => domain[1]), ...annotatedX);
|
|
835
|
+
const x = (v) => left + (v - lo) / (hi - lo || 1) * (right - left);
|
|
836
|
+
const lanes = labelLanes(ctx, xAnnotations.filter((a) => a.label).map((a) => ({
|
|
837
|
+
a,
|
|
838
|
+
text: a.label,
|
|
839
|
+
x: a.kind === "rule" ? x(a.value) : (x(a.from) + x(a.to)) / 2
|
|
840
|
+
})), {
|
|
841
|
+
left,
|
|
842
|
+
right
|
|
843
|
+
});
|
|
844
|
+
const annotationBand = lanes.height ? lanes.height + 4 : 0;
|
|
845
|
+
const top = 34 + titleExtra + annotationBand, bottom = 258 + titleExtra + annotationBand;
|
|
846
|
+
const y = (v) => bottom - (v - yMin) / (maxValue - yMin || 1) * (bottom - top);
|
|
847
|
+
yScale.ticks.forEach((v, i) => {
|
|
848
|
+
children.push(ctx.line(left, y(v), right, y(v), {
|
|
849
|
+
stroke: v === 0 ? ctx.palette.axis : ctx.palette.grid,
|
|
850
|
+
"stroke-width": 1
|
|
851
|
+
}));
|
|
852
|
+
addText(left - 8, y(v) + 4, yScale.labels[i], "end");
|
|
853
|
+
});
|
|
854
|
+
rows.forEach((row, i) => {
|
|
855
|
+
const value = values[i];
|
|
856
|
+
const barHeight = value > 0 ? Math.max(3, y(0) - y(value)) : Math.max(0, y(0) - y(value));
|
|
857
|
+
const bar = ctx.rect(x(row[e.lower]), y(0) - barHeight, Math.max(1, x(row[e.upper]) - x(row[e.lower])), barHeight, {
|
|
858
|
+
fill: ctx.palette.tone1,
|
|
859
|
+
stroke: ctx.palette.bg,
|
|
860
|
+
"stroke-width": 1,
|
|
861
|
+
rx: 2
|
|
862
|
+
});
|
|
863
|
+
const interval = `[${ctx.format(row[e.lower], e.lower, false)}, ${ctx.format(row[e.upper], e.upper, false)})`;
|
|
864
|
+
const count = `${ctx.format(row[e.count], e.count)}${countMeta.label && countMeta.label !== e.count ? ` ${countMeta.label}` : ""}`;
|
|
865
|
+
const densityText = `${ctx.format(value, e.count, false)}${countMeta.unit ? ` ${countMeta.unit}` : ""}${binMeta.unit ? `/${binMeta.unit}` : ""}`;
|
|
866
|
+
const title = english ? `${interval}${binMeta.unit ? ` ${binMeta.unit}` : ""} · ${count}${density ? ` · density ${densityText}` : ""}` : `${interval}${binMeta.unit ? ` ${binMeta.unit}` : ""} · ${count}${density ? ` · 密度 ${densityText}` : ""}`;
|
|
867
|
+
children.push(ctx.titled(bar, title));
|
|
868
|
+
});
|
|
869
|
+
const labelBottom = /* @__PURE__ */ new Map();
|
|
870
|
+
for (const item of lanes.items) {
|
|
871
|
+
const baseline = 30 + titleExtra + 12 + item.offset;
|
|
872
|
+
labelBottom.set(item.a, baseline + (item.lines.length - 1) * 16 + 4);
|
|
873
|
+
children.push(ctx.textLines(item.cx, baseline, item.lines, {
|
|
874
|
+
anchor: "middle",
|
|
875
|
+
fill: ctx.textColor("accent"),
|
|
876
|
+
"font-size": 12
|
|
877
|
+
}));
|
|
878
|
+
}
|
|
879
|
+
for (const a of xAnnotations) {
|
|
880
|
+
if (a.kind === "rule") children.push(ctx.line(x(a.value), (labelBottom.get(a) ?? top - 2) + 2, x(a.value), bottom, {
|
|
881
|
+
stroke: ctx.palette.accent,
|
|
882
|
+
"stroke-width": 1.5,
|
|
883
|
+
"stroke-dasharray": "4 4"
|
|
884
|
+
}));
|
|
885
|
+
if (a.kind === "band") children.push(ctx.rect(x(a.from), top, Math.max(0, x(a.to) - x(a.from)), bottom - top, {
|
|
886
|
+
fill: ctx.mix(ctx.palette.accent, ctx.palette.bg, .82),
|
|
887
|
+
opacity: .55
|
|
888
|
+
}));
|
|
889
|
+
}
|
|
890
|
+
const rawBinLabel = binMeta.label !== e.lower ? binMeta.label : "";
|
|
891
|
+
const subtitleLabel = String(spec.subtitle || "").split(/[||;;]/, 1)[0].trim();
|
|
892
|
+
const xAxisTitle = `${rawBinLabel && !/(?:lower|upper|下界|上界|区间下|区间上|区间.*(?:起点|终点)|^起点$|^终点$)/i.test(rawBinLabel) ? rawBinLabel : subtitleLabel && !/^纵轴/.test(subtitleLabel) ? subtitleLabel : english ? "Value" : "取值"}${binMeta.unit ? `(${binMeta.unit})` : ""}`;
|
|
893
|
+
children.push(ctx.textLines(left, 18, titleLines, {
|
|
894
|
+
fill: ctx.palette.muted,
|
|
895
|
+
"font-size": 12
|
|
896
|
+
}));
|
|
897
|
+
const edgePositions = binEdges.map((value) => x(value));
|
|
898
|
+
const edgeWidth = (index) => ctx.measure(edgeLabels[index], 12, 400);
|
|
899
|
+
const edgeLeft = (index) => edgePositions[index] - edgeWidth(index) * (index === 0 ? 0 : index === binEdges.length - 1 ? 1 : .5);
|
|
900
|
+
const edgeRight = (index) => edgeLeft(index) + edgeWidth(index);
|
|
901
|
+
const edgeGap = (index, prior) => edgeLeft(index) >= edgeRight(prior) + 8;
|
|
902
|
+
const shown = [0];
|
|
903
|
+
for (let index = 1; index < binEdges.length - 1; index += 1) if (shown.every((prior) => edgeGap(index, prior)) && edgeGap(binEdges.length - 1, index)) shown.push(index);
|
|
904
|
+
if (binEdges.length > 1) shown.push(binEdges.length - 1);
|
|
905
|
+
binEdges.forEach((value, index) => {
|
|
906
|
+
children.push(ctx.line(x(value), bottom, x(value), bottom + 4, {
|
|
907
|
+
stroke: ctx.palette.axis,
|
|
908
|
+
"stroke-width": 1
|
|
909
|
+
}));
|
|
910
|
+
if (!shown.includes(index)) return;
|
|
911
|
+
const anchor = index === 0 ? "start" : index === binEdges.length - 1 ? "end" : "middle";
|
|
912
|
+
children.push(ctx.text(x(value), bottom + 18, edgeLabels[index], {
|
|
913
|
+
anchor,
|
|
914
|
+
fill: ctx.palette.muted,
|
|
915
|
+
"font-size": 12
|
|
916
|
+
}));
|
|
917
|
+
});
|
|
918
|
+
const xTitleLines = ctx.wrap(xAxisTitle, Math.max(48, right - left), 12, 400);
|
|
919
|
+
children.push(ctx.textLines(right, bottom + 40, xTitleLines, {
|
|
920
|
+
anchor: "end",
|
|
921
|
+
fill: ctx.palette.muted,
|
|
922
|
+
"font-size": 12
|
|
923
|
+
}));
|
|
924
|
+
return {
|
|
925
|
+
width: sceneWidth,
|
|
926
|
+
height: bottom + 52 + (xTitleLines.length - 1) * 16,
|
|
927
|
+
children
|
|
928
|
+
};
|
|
929
|
+
}
|
|
930
|
+
return null;
|
|
931
|
+
}
|
|
932
|
+
function renderStatistics(spec, ctx) {
|
|
933
|
+
const layout = {
|
|
934
|
+
scatter: annotatedScene,
|
|
935
|
+
histogram: annotatedScene,
|
|
936
|
+
waterfall: annotatedScene,
|
|
937
|
+
heatmap,
|
|
938
|
+
sparkline,
|
|
939
|
+
"small-multiples": smallMultiples,
|
|
940
|
+
bullet: compare,
|
|
941
|
+
dumbbell: compare,
|
|
942
|
+
slope: compare,
|
|
943
|
+
boxplot: compare
|
|
944
|
+
}[spec.chart];
|
|
945
|
+
return createScene(spec, ctx, layout(spec, ctx));
|
|
946
|
+
}
|
|
947
|
+
function waffle(spec, ctx) {
|
|
948
|
+
const e = spec.encode, rows = spec.data, english = /^en(?:-|$)/i.test(String(ctx.lang || ""));
|
|
949
|
+
const values = rows.map((row) => row[e.value] ?? 0), total = values.reduce((sum, value) => sum + value, 0);
|
|
950
|
+
if (!(total > 0)) ctx.fit(english ? "waffle requires a positive total" : "华夫图合计必须大于0", english ? "Provide at least one positive value" : "为至少一个类别提供正值");
|
|
951
|
+
const counts = apportion(values, 100);
|
|
952
|
+
const pitch = Math.max(18, Math.min(24, Math.floor((ctx.width - 16) / 10))), radius = pitch * .4;
|
|
953
|
+
const gridSize = pitch * 10, width = Math.max(ctx.width, gridSize + 16), left = (width - gridSize) / 2;
|
|
954
|
+
const children = [];
|
|
955
|
+
let index = 0;
|
|
956
|
+
rows.forEach((row, category) => {
|
|
957
|
+
const label = String(row[e.category]), fill = ctx.palette[`tone${category % 6 + 1}`];
|
|
958
|
+
const share = ctx.percent(values[category] / total, 1);
|
|
959
|
+
const tip = english ? `${label}: ${share} (${counts[category]} dots)` : `${label}:${share}(${counts[category]} 点)`;
|
|
960
|
+
for (let k = 0; k < counts[category]; k += 1, index += 1) {
|
|
961
|
+
const cx = left + (index % 10 + .5) * pitch, cy = (Math.floor(index / 10) + .5) * pitch;
|
|
962
|
+
children.push(ctx.titled(ctx.circle(cx, cy, radius, {
|
|
963
|
+
fill,
|
|
964
|
+
meta: {
|
|
965
|
+
kind: "unit",
|
|
966
|
+
category: label,
|
|
967
|
+
value: row[e.value]
|
|
968
|
+
}
|
|
969
|
+
}), tip));
|
|
970
|
+
}
|
|
971
|
+
});
|
|
972
|
+
const note = english ? "100 dots make up the whole, about 1% each; dots are allocated by largest remainder, see the data table" : "100 点表示整体,每点约 1%;点数按最大余数法分配,原数见数据表";
|
|
973
|
+
const noteLines = ctx.wrap(note, gridSize, 12);
|
|
974
|
+
const noteY = gridSize + 22;
|
|
975
|
+
children.push(ctx.textLines(left, noteY, noteLines, { size: 12 }));
|
|
976
|
+
const legend = ctx.legend(rows.map((row, category) => ({
|
|
977
|
+
label: `${row[e.category]} · ${ctx.format(row[e.value], e.value)}`,
|
|
978
|
+
color: ctx.palette[`tone${category % 6 + 1}`],
|
|
979
|
+
meta: {
|
|
980
|
+
category: String(row[e.category]),
|
|
981
|
+
value: row[e.value]
|
|
982
|
+
}
|
|
983
|
+
})), gridSize);
|
|
984
|
+
const legendY = noteY + (noteLines.length - 1) * 16 + 16;
|
|
985
|
+
children.push(ctx.el("g", { transform: `translate(${left},${legendY})` }, legend.children));
|
|
986
|
+
return {
|
|
987
|
+
...ctx.scene(legendY + legend.height + 12, children, "waffle chart"),
|
|
988
|
+
width
|
|
989
|
+
};
|
|
990
|
+
}
|
|
991
|
+
function install({ register }) {
|
|
992
|
+
register("waffle", waffle);
|
|
993
|
+
register([
|
|
994
|
+
"sparkline",
|
|
995
|
+
"small-multiples",
|
|
996
|
+
"bullet",
|
|
997
|
+
"dumbbell",
|
|
998
|
+
"slope",
|
|
999
|
+
"waterfall",
|
|
1000
|
+
"histogram",
|
|
1001
|
+
"boxplot",
|
|
1002
|
+
"scatter",
|
|
1003
|
+
"heatmap"
|
|
1004
|
+
], renderStatistics);
|
|
1005
|
+
}
|
|
1006
|
+
install({ register });
|
|
1007
|
+
export { install, renderStatistics, statistics_exports, waffle };
|