Skip to content

packages/core/src/features/viz/lib/graph/icicle.tsx

Read as Markdown

This is the source snapshot used to build these API details. View this revision on GitHub.

Back to reference

1 import {
2   Group,
3   Rectangle,
4   resolveSignalValue,
5   type PibblNode,
6 } from "@pibbl/core";
7 import {
8   defaultGraphLabelStyle,
9   GraphLabel,
10   resolveMarkStyle,
11   useGraphFrame,
12 } from "./shared.js";
13 import { defaultPartitionCellStyle } from "./treemap.js";
14 import type {
15   IcicleOptions,
16   IcicleProps,
17   IcicleStyle,
18   PartitionCellGeometry,
19   PartitionCellProps,
20   PartitionLayout,
21 } from "./types.js";
22 import {
23   checkedBounds,
24   finiteGap,
25   hierarchy,
26   partitionLabel,
27   partitionLabelFits,
28   type HierarchyRecord,
29 } from "./hierarchy.js";
30 
31 /** Lay out hierarchy values in depth rows.
32  * @param root - Hierarchy root.
33  * @param options - Accessors, units, and finite bounds.
34  * @returns Immutable partition geometry. @see {@link PartitionLayout} */
35 export function layoutIcicle<D>(
36   root: D,
37   options: IcicleOptions<D>,
38 ): PartitionLayout<D> {
39   const bounds = checkedBounds(options.bounds, "layoutIcicle");
40   const gap = finiteGap(options.cellGap, 1, "layoutIcicle cellGap");
41   const levelGap = finiteGap(options.levelGap, 3, "layoutIcicle levelGap");
42   const tree = hierarchy(root, options, "layoutIcicle", options.value);
43   const depths: number[] = [];
44   const visit = (record: HierarchyRecord<D>) => {
45     depths[record.depth] = 1;
46     for (const child of record.children) visit(child);
47   };
48   visit(tree);
49   const visibleLevels = tree.isLeaf ? 1 : Math.max(1, depths.length - 1);
50   const levelHeight =
51     (bounds.height - Math.max(0, visibleLevels - 1) * levelGap) / visibleLevels;
52   if (levelHeight < 0)
53     throw new RangeError("layoutIcicle: bounds are too short for levelGap.");
54   const cells: PartitionCellGeometry<D>[] = [];
55   const add = (
56     record: HierarchyRecord<D>,
57     x: number,
58     width: number,
59     rootItem: boolean,
60     parentKey: PartitionCellGeometry<D>["parentKey"],
61   ) => {
62     const visibleDepth = rootItem ? 0 : record.depth - 1;
63     const y = bounds.y + visibleDepth * (levelHeight + levelGap);
64     const box = Object.freeze({
65       x: x + gap / 2,
66       y,
67       width: Math.max(0, width - gap),
68       height: levelHeight,
69     });
70     if (!rootItem || record.isLeaf)
71       cells.push(
72         Object.freeze({
73           key: record.key,
74           datum: record.datum,
75           parentKey,
76           depth: rootItem ? 0 : record.depth - 1,
77           value: record.value,
78           fraction: tree.value === 0 ? 0 : record.value / tree.value,
79           isLeaf: record.isLeaf,
80           collapsed: record.collapsed,
81           bounds: box,
82           label: (() => {
83             const label = partitionLabel(record, options, {
84               x: box.x + 12,
85               y: box.y + 8,
86               width: Math.max(0, box.width - 24),
87               height: Math.max(0, box.height - 16),
88             });
89             return partitionLabelFits(label, box) ? label : null;
90           })(),
91         }),
92       );
93     if (record.collapsed) return;
94     const total = record.children.reduce((sum, child) => sum + child.value, 0);
95     let cursor = x;
96     for (const child of record.children) {
97       const span =
98         total === 0
99           ? width / record.children.length
100           : (width * child.value) / total;
101       add(child, cursor, span, false, rootItem ? null : record.key);
102       cursor += span;
103     }
104   };
105   add(tree, bounds.x, bounds.width, true, null);
106   return Object.freeze({
107     bounds,
108     rootKey: tree.key,
109     total: tree.value,
110     unit: options.unit,
111     cells: Object.freeze(cells),
112   });
113 }
114 
115 /** Paint one icicle partition cell.
116  * @param props - Cell geometry, paint, events, and optional children.
117  * @returns Canvas composition. @see {@link PartitionCellGeometry} */
118 export function IcicleCell<D>(props: PartitionCellProps<D>): PibblNode {
119   const { bounds } = props.cell;
120   const { children, style, ...events } = props;
121   return (
122     <Group>
123       <Rectangle
124         {...events}
125         style={{
126           ...resolveSignalValue(style),
127           left: bounds.x,
128           top: bounds.y,
129           width: bounds.width,
130           height: bounds.height,
131         }}
132       />
133       {children}
134     </Group>
135   );
136 }
137 
138 /** Render hierarchy values as depth-aligned bands.
139  * @param props - Reactive hierarchy data and presentation.
140  * @returns Canvas composition. @see {@link layoutIcicle} */
141 export function Icicle<D>(props: IcicleProps<D>): PibblNode {
142   const frame = useGraphFrame<IcicleStyle>(props.style);
143   const root = resolveSignalValue(props.root);
144   if (frame.bounds.width === 0 || frame.bounds.height === 0)
145     return <Group style={frame.groupStyle}>{null}</Group>;
146   const layout = layoutIcicle(root, {
147     ...props,
148     bounds: frame.bounds,
149     cellGap: frame.style.cellGap,
150     levelGap: frame.style.levelGap,
151   });
152   const labelStyle = resolveSignalValue(props.labelStyle);
153   return (
154     <Group style={frame.groupStyle}>
155       {layout.cells.map((cell) =>
156         props.renderCell ? (
157           <Group
158             key={cell.key}
159             onClick={(event) => props.onCellClick?.(cell, event)}
160           >
161             {props.renderCell(cell)}
162           </Group>
163         ) : (
164           <IcicleCell
165             key={cell.key}
166             cell={cell}
167             style={{
168               ...defaultPartitionCellStyle(cell),
169               ...resolveMarkStyle(cell, props.cellStyle),
170             }}
171             onClick={(event) => props.onCellClick?.(cell, event)}
172           >
173             {cell.label && (
174               <GraphLabel
175                 label={cell.label}
176                 style={{ ...defaultGraphLabelStyle(cell.label), ...labelStyle }}
177               />
178             )}
179           </IcicleCell>
180         ),
181       )}
182     </Group>
183   );
184 }
185 

Documentation built with @pibbl/core 0.0.2, revision a1dcf8e. ALPHA — NOT FOR PRODUCTION USE.