packages/core/src/features/viz/lib/scale-adjust.tsx
This is the source snapshot used to build these API details. View this revision on GitHub.
1 import { Group, resolveSignalValue } from "@pibbl/core";
2 import type { ScaleAdjustProps, ResolvedScale, BandCategory } from "./types.js";
3 import { lookupScale } from "./scale-context.js";
4 /**
5 * Adjusts descendant placement using configured x and y scales.
6 *
7 * @param props - Scale identity and domain or range adjustments for descendants. See
8 * {@link ScaleAdjustProps} .
9 * @returns Child content evaluated with the adjusted scale.
10 *
11 * @see {@link ScaleAdjustProps}
12 */
13 export function ScaleAdjust(props: ScaleAdjustProps) {
14 const x = lookupScale(props.xScale, "ScaleAdjust");
15 const y = lookupScale(props.yScale, "ScaleAdjust");
16 if (
17 (props.x === undefined) !== (props.xScale === undefined) ||
18 (props.y === undefined) !== (props.yScale === undefined)
19 )
20 throw new TypeError("ScaleAdjust coordinates require matching scale IDs.");
21 const coordinate = (scale: ResolvedScale, value: BandCategory) => {
22 if (scale.type === "band") throw new TypeError("ScaleAdjust requires continuous or point scales.");
23 if (scale.type !== "point" && typeof value !== "number") throw new TypeError("ScaleAdjust continuous coordinates must be numbers.");
24 const result = scale.type === "point" ? scale.map(value) : scale.map(value as number);
25 if (result === undefined) throw new RangeError("ScaleAdjust point category is not in the domain.");
26 return result;
27 };
28 return (
29 <Group
30 style={{
31 translateX: x ? coordinate(x.get(), resolveSignalValue(props.x!)) : 0,
32 translateY: y ? coordinate(y.get(), resolveSignalValue(props.y!)) : 0,
33 }}
34 >
35 {props.children}
36 </Group>
37 );
38 }
39
Documentation version
Section titled “Documentation version”Documentation built with @pibbl/core 0.0.2, revision 2dccb19. ALPHA — NOT FOR PRODUCTION USE.