packages/core/src/features/viz/lib/pie-layout.tsx
This is the source snapshot used to build these API details. View this revision on GitHub.
1 import {
2 Group,
3 Path,
4 Wedge,
5 definePrimitive,
6 Line,
7 Rectangle,
8 resolveSignalValue,
9 useComputed,
10 useCanvasContext,
11 useLayoutBox,
12 } from "@pibbl/core";
13 import { resolveTexturePaint, withHooksForbidden } from "@pibbl/core/internal";
14 import type {
15 PieLayoutProps,
16 PieLayoutSlice,
17 PieSliceProps,
18 PieLabelProps,
19 PieLabelLayout,
20 } from "./pie-types.js";
21 import { pieSlices } from "./pie-slices.js";
22 import { layoutPie } from "./layout-pie.js";
23 function finite(value: number, name: string, nonnegative = false) {
24 if (!Number.isFinite(value) || (nonnegative && value < 0))
25 throw new RangeError(
26 `PieLayout: ${name} must be finite${nonnegative ? " and nonnegative" : ""}.`,
27 );
28 return value;
29 }
30 /**
31 * Computes pie geometry and label placement, then renders children for each resolved slice.
32 *
33 * @param props - Data, pie geometry, label policy, and child renderer. See {@link PieLayoutProps}.
34 * @returns Pibbl content arranged as pie slices.
35 *
36 * @see {@link PieLayoutProps}
37 */
38 export function PieLayout<D>(props: PieLayoutProps<D>) {
39 const allocation = useLayoutBox();
40 const context = useCanvasContext();
41 const style = resolveSignalValue(props.style) ?? {};
42 const bounds = resolveSignalValue(props.bounds) ?? {
43 x: 0,
44 y: 0,
45 width: allocation.width,
46 height: allocation.height,
47 };
48 const bx = finite(bounds.x, "bounds.x"),
49 by = finite(bounds.y, "bounds.y");
50 const bw = finite(bounds.width, "bounds.width", true),
51 bh = finite(bounds.height, "bounds.height", true);
52 finite(bx + bw, "bounds right");
53 finite(by + bh, "bounds bottom");
54 const cx = finite(resolveSignalValue(style.cx) ?? bx + bw / 2, "cx");
55 const cy = finite(resolveSignalValue(style.cy) ?? by + bh / 2, "cy");
56 const radius = finite(
57 resolveSignalValue(style.radius) ?? Math.min(bw * 0.28, bh * 0.4),
58 "radius",
59 true,
60 );
61 const innerRadius = finite(
62 resolveSignalValue(style.innerRadius) ?? 0,
63 "innerRadius",
64 true,
65 );
66 if (radius === 0 ? innerRadius !== 0 : innerRadius >= radius)
67 throw new RangeError(
68 "PieLayout: innerRadius must be zero when radius is zero, or less than radius.",
69 );
70 const gap = finite(resolveSignalValue(style.labelGap) ?? 8, "labelGap", true);
71 finite(cx + radius + gap, "right label edge");
72 finite(cx - radius - gap, "left label edge");
73 finite(cy + radius, "bottom");
74 finite(cy - radius, "top");
75 const font = resolveSignalValue(style.labelFont) ?? "14px sans-serif";
76 if (typeof font !== "string" || !font.trim())
77 throw new TypeError("PieLayout: labelFont must be a Canvas font string.");
78 const placement = resolveSignalValue(props.placement) ?? "outside";
79 if (!["inside", "outside", "auto"].includes(placement))
80 throw new TypeError(
81 "PieLayout: placement must be inside, outside, or auto.",
82 );
83 if (typeof props.children !== "function")
84 throw new TypeError(
85 "PieLayout: children must be a function returning Pibbl elements.",
86 );
87 const revision =
88 props.revision === undefined ? undefined : resolveSignalValue(props.revision);
89 if (
90 revision !== undefined &&
91 (!Number.isSafeInteger(revision) || revision < 0)
92 )
93 throw new RangeError(
94 "PieLayout: revision must be a finite nonnegative safe integer.",
95 );
96 // An omitted revision deliberately replaces this token on every evaluation.
97 // Stable callback identities alone are not evidence of stable results.
98 const cacheKey = revision === undefined ? {} : revision;
99 const slices = useComputed(
100 () =>
101 pieSlices(resolveSignalValue(props.data), {
102 value: props.value,
103 keyBy: props.keyBy,
104 }),
105 [cacheKey, props.data, props.value, props.keyBy],
106 );
107 const layout = useComputed(() => {
108 context.save();
109 try {
110 // Canvas silently ignores malformed fonts. Two distinct sentinels detect
111 // that without depending on the caller's inherited font or CSS spelling.
112 context.font = "10px serif";
113 context.font = font;
114 const normalized = context.font;
115 context.font = "11px monospace";
116 context.font = font;
117 if (context.font !== normalized)
118 throw new TypeError("PieLayout: labelFont must be a valid Canvas font.");
119 context.textAlign = "left";
120 context.textBaseline = "alphabetic";
121 return layoutPie(
122 slices.get(),
123 cx,
124 cy,
125 radius,
126 innerRadius,
127 { x: bx, y: by, width: bw, height: bh },
128 context.font,
129 gap,
130 placement,
131 props.label,
132 (text) => {
133 const m = context.measureText(text);
134 const width = Math.max(
135 m.width,
136 m.actualBoundingBoxLeft + m.actualBoundingBoxRight,
137 );
138 const ascent = Math.max(0, m.actualBoundingBoxAscent),
139 descent = Math.max(0, m.actualBoundingBoxDescent);
140 return {
141 width: finite(width, "label width", true),
142 height: finite(Math.max(1, ascent + descent), "label height", true),
143 left: Math.max(0, m.actualBoundingBoxLeft),
144 ascent,
145 };
146 },
147 );
148 } finally {
149 context.restore();
150 }
151 }, [
152 slices,
153 cx,
154 cy,
155 radius,
156 innerRadius,
157 bx,
158 by,
159 bw,
160 bh,
161 font,
162 gap,
163 placement,
164 props.label,
165 context,
166 ]);
167 return layout
168 .get()
169 .map((slice) => (
170 <Group key={slice.key}>
171 {withHooksForbidden(
172 "PieLayout children cannot call hooks; return a component instead.",
173 () => props.children(slice),
174 )}
175 </Group>
176 ));
177 }
178 /**
179 * Draws a resolved pie slice using wedge geometry and the supplied paint style.
180 *
181 * @param props - Slice geometry, appearance, handlers, and children. See {@link PieSliceProps}.
182 * @returns Pibbl nodes drawing one pie slice and its children.
183 *
184 * @see {@link PieSliceProps}
185 */
186 export function PieSlice<D>({
187 slice,
188 style: input,
189 children,
190 ...events
191 }: PieSliceProps<D>) {
192 const style = resolveSignalValue(input) ?? {};
193 if (slice.innerRadius === 0) {
194 return (
195 <Group>
196 <Wedge
197 {...events}
198 style={{
199 ...style,
200 cx: slice.cx,
201 cy: slice.cy,
202 radius: slice.radius,
203 startAngle: slice.startAngle,
204 endAngle: slice.endAngle,
205 }}
206 />
207 {children}
208 </Group>
209 );
210 }
211 const sweep = resolveClockwiseSweep(slice.startAngle, slice.endAngle);
212 const path = new Path2D();
213 path.arc(
214 slice.cx,
215 slice.cy,
216 slice.radius,
217 sweep.startAngle,
218 sweep.endAngle,
219 );
220 if (slice.innerRadius > 0)
221 path.arc(
222 slice.cx,
223 slice.cy,
224 slice.innerRadius,
225 sweep.endAngle,
226 sweep.startAngle,
227 true,
228 );
229 else path.lineTo(slice.cx, slice.cy);
230 path.closePath();
231 return (
232 <Group>
233 <Path
234 {...events}
235 keyboardNavigationBounds={
236 events.keyboardNavigationBounds ??
237 annularSliceBounds(slice, sweep)
238 }
239 style={{
240 ...style,
241 d: path,
242 }}
243 />
244 {children}
245 </Group>
246 );
247 }
248
249 /** The outer and inner arcs both contribute to an annular path's focus box. */
250 function annularSliceBounds(
251 slice: PieLayoutSlice<unknown>,
252 sweep: Readonly<ClockwiseSweep>,
253 ) {
254 if (sweep.sweep === 0) return undefined;
255 if (sweep.fullCircle)
256 return {
257 x: slice.cx - slice.radius,
258 y: slice.cy - slice.radius,
259 width: slice.radius * 2,
260 height: slice.radius * 2,
261 };
262 const angles = [sweep.startAngle, sweep.endAngle];
263 for (let cardinalIndex = 0; cardinalIndex < 4; cardinalIndex++) {
264 const cardinal = cardinalIndex * (Math.PI / 2);
265 const angle = cardinal < sweep.startAngle ? cardinal + fullTurn : cardinal;
266 if (angle <= sweep.endAngle) angles.push(angle);
267 }
268 const xs: number[] = [];
269 const ys: number[] = [];
270 for (const angle of angles) {
271 const cosine = Math.cos(angle);
272 const sine = Math.sin(angle);
273 xs.push(
274 slice.cx + slice.radius * cosine,
275 slice.cx + slice.innerRadius * cosine,
276 );
277 ys.push(
278 slice.cy + slice.radius * sine,
279 slice.cy + slice.innerRadius * sine,
280 );
281 }
282 const left = Math.min(...xs);
283 const right = Math.max(...xs);
284 const top = Math.min(...ys);
285 const bottom = Math.max(...ys);
286 return { x: left, y: top, width: right - left, height: bottom - top };
287 }
288
289 const fullTurn = Math.PI * 2;
290
291 interface ClockwiseSweep {
292 readonly startAngle: number;
293 readonly endAngle: number;
294 readonly sweep: number;
295 readonly fullCircle: boolean;
296 }
297
298 /** Matches Canvas's bounded clockwise arc rule without retaining unbounded turns. */
299 function resolveClockwiseSweep(
300 startAngle: number,
301 endAngle: number,
302 ): ClockwiseSweep {
303 if (!Number.isFinite(startAngle) || !Number.isFinite(endAngle))
304 throw new RangeError("PieSlice: slice angles must be finite.");
305 const start = positiveModulo(startAngle, fullTurn);
306 if (endAngle - startAngle >= fullTurn)
307 return {
308 startAngle: start,
309 endAngle: start + fullTurn,
310 sweep: fullTurn,
311 fullCircle: true,
312 };
313 const sweep = positiveModulo(
314 positiveModulo(endAngle, fullTurn) - start,
315 fullTurn,
316 );
317 return {
318 startAngle: start,
319 endAngle: start + sweep,
320 sweep,
321 fullCircle: false,
322 };
323 }
324
325 function positiveModulo(value: number, divisor: number) {
326 const result = value % divisor;
327 if (result === 0) return 0;
328 return result < 0 ? result + divisor : result;
329 }
330
331 const LabelText = definePrimitive<
332 { label: Extract<PieLabelLayout, { status: "placed" }> },
333 { readonly custom?: never; fill: import("@pibbl/core").FillStyle }
334 >(({ label }, style, context) => {
335 context.font = label.font;
336 context.textAlign = "left";
337 context.textBaseline = "alphabetic";
338 context.fillStyle = resolveTexturePaint(style.fill, context, { x: label.x, y: label.y, width: label.width, height: label.height });
339 context.fillText(label.text, label.textX, label.textY);
340 });
341
342 /**
343 * Draws a resolved slice label and optional leader line; decorative labels ignore pointer
344 * targeting
345 * by default.
346 *
347 * @param props - Slice geometry, label appearance, and interaction options. See
348 * {@link PieLabelProps} .
349 * @returns Pibbl content drawing the slice label, or null when the label was not placed.
350 *
351 * @see {@link PieLabelProps}
352 */
353 export function PieLabel<D>({
354 slice,
355 style: input,
356 pointerEvents = "none",
357 ...events
358 }: PieLabelProps<D>) {
359 const style = resolveSignalValue(input) ?? {};
360 const label = slice.label;
361 if (!label || label.status !== "placed") return null;
362 return (
363 <Group {...events}>
364 {label.leader && (
365 <Line
366 pointerEvents="none"
367 style={{
368 coords: label.leader.map((p) => [p[0], p[1]]),
369 stroke: resolveSignalValue(style.leaderStroke) ?? "#94a3b8",
370 strokeWidth: resolveSignalValue(style.leaderWidth) ?? 1,
371 }}
372 />
373 )}
374 <Rectangle
375 pointerEvents={pointerEvents}
376 style={{
377 left: label.x,
378 top: label.y,
379 width: label.width,
380 height: label.height,
381 fill: "transparent",
382 }}
383 />
384 <LabelText
385 label={label}
386 style={{ fill: resolveSignalValue(style.fill) ?? "#182c42" }}
387 />
388 </Group>
389 );
390 }
391
Documentation version
Section titled “Documentation version”Documentation built with @pibbl/core 0.0.2, revision a1dcf8e. ALPHA — NOT FOR PRODUCTION USE.