packages/core/src/features/viz/lib/point-series.tsx
This is the source snapshot used to build these API details. View this revision on GitHub.
1 import { resolveTexturePaint } from "@pibbl/core/internal";
2 import {
3 Path,
4 isSignal,
5 resolveSignalValue,
6 useCanvasContext,
7 } from "@pibbl/core";
8 import type { FillStyle, StrokeStyle } from "@pibbl/core";
9 import type {
10 PointMarkStyle,
11 PointSeriesProps,
12 PointSeriesStyle,
13 } from "./point-types.js";
14 import {
15 useSeriesPosition,
16 seriesAccessor,
17 mapSeriesPosition,
18 } from "./series-position.js";
19 import { pointData } from "./point-data.js";
20 import { appendPointTarget, pointPath } from "./point-geometry.js";
21 import { opacity, width } from "./paint.js";
22 const fields = ["radius", "fill", "stroke", "strokeWidth", "opacity", "cursor"];
23 function paint(
24 value: unknown,
25 name: string,
26 ): FillStyle | StrokeStyle | undefined {
27 if (
28 value === undefined ||
29 typeof value === "string" ||
30 value instanceof CanvasGradient ||
31 value instanceof CanvasPattern
32 )
33 return value;
34 throw new TypeError(`PointSeries: invalid ${name}.`);
35 }
36 function styleObject(input: unknown, callback: boolean): PointSeriesStyle {
37 if (input === null || typeof input !== "object" || Array.isArray(input))
38 throw new TypeError("PointSeries: style must be an object.");
39 const result: Record<string, unknown> = {};
40 for (const key of Object.keys(input)) {
41 if (!fields.includes(key) || (callback && key === "cursor"))
42 throw new TypeError(`PointSeries: unsupported style field ${key}.`);
43 const value = (input as Record<string, unknown>)[key];
44 if (callback && isSignal(value))
45 throw new TypeError(
46 "PointSeries: datum styles must contain plain values; read signals explicitly.",
47 );
48 result[key] = callback ? value : resolveSignalValue(value);
49 }
50 return result as PointSeriesStyle;
51 }
52 function normalized(style: PointSeriesStyle) {
53 const radius = width(style.radius ?? 3, "PointSeries radius");
54 if (style.cursor !== undefined && typeof style.cursor !== "string")
55 throw new TypeError("PointSeries: cursor must be a string.");
56 return {
57 radius,
58 fill: paint(style.fill ?? "#2563eb", "fill"),
59 stroke: paint(style.stroke, "stroke"),
60 strokeWidth: width(style.strokeWidth ?? 1, "PointSeries"),
61 opacity: opacity(style.opacity ?? 1, "PointSeries"),
62 cursor: style.cursor,
63 };
64 }
65 /**
66 * Batched circles in source paint order with one ordinary Pibbl target.
67 *
68 * @param props - Data, coordinate accessors, scales, and point styling. See
69 * {@link PointSeriesProps} .
70 * @returns Pibbl nodes drawing the point marks.
71 *
72 * @see {@link PointSeriesProps}
73 */
74 export function PointSeries<D>({
75 data: input,
76 x: xInput,
77 y: yInput,
78 xScale: xId,
79 yScale: yId,
80 defined,
81 inspection,
82 style: styleInput,
83 ...events
84 }: PointSeriesProps<D>) {
85 const xs = useSeriesPosition(xId),
86 ys = useSeriesPosition(yId);
87 const data = pointData(resolveSignalValue(input), "PointSeries");
88 const x = seriesAccessor(xInput),
89 y = seriesAccessor(yInput);
90 const specified = resolveSignalValue(styleInput);
91 const shared =
92 typeof specified === "function"
93 ? undefined
94 : normalized(styleObject(specified ?? {}, false));
95 const ctx = useCanvasContext();
96 const target = new Path2D();
97 const captured: import("../inspection/capture-types.js").CapturedNumericObservation<D>[] | undefined = inspection ? [] : undefined;
98 let run = 0;
99 if (inspection && (xs.type === "point" || ys.type === "point")) throw new TypeError("Numeric point inspection requires numeric continuous scales.");
100 for (let i = 0; i < data.length; i++) {
101 const datum = data[i];
102 if (defined && !defined(datum, i, data)) { run++; continue; }
103 const xv = x(datum, i, data),
104 yv = y(datum, i, data);
105 const mappedX = mapSeriesPosition(xs, xv),
106 mappedY = mapSeriesPosition(ys, yv);
107 if (mappedX === undefined || mappedY === undefined) { run++; continue; }
108 const px = mappedX,
109 py = mappedY;
110 const style =
111 shared ??
112 normalized(
113 styleObject(
114 (specified as (d: D, i: number, a: readonly D[]) => PointMarkStyle)(
115 datum,
116 i,
117 data,
118 ),
119 true,
120 ),
121 );
122 captured?.push(Object.freeze({datum,sourceIndex:i,domainX:xv as number,domainY:yv as number,anchorLocal:Object.freeze({x:px,y:py}),run,radiusLocal:style.radius,fill:!!style.fill,strokeWidthLocal:style.stroke ? style.strokeWidth : 0}));
123 if (style.radius === 0) continue;
124 appendPointTarget(
125 target,
126 px,
127 py,
128 style.radius,
129 !!style.fill,
130 style.stroke ? style.strokeWidth : 0,
131 );
132 const path = pointPath(px, py, style.radius);
133 ctx.save();
134 try {
135 ctx.globalAlpha *= style.opacity;
136 if (style.fill) {
137 ctx.fillStyle = resolveTexturePaint(style.fill, ctx, {
138 x: px - style.radius,
139 y: py - style.radius,
140 width: style.radius * 2,
141 height: style.radius * 2,
142 });
143 ctx.fill(path);
144 }
145 if (style.stroke && style.strokeWidth > 0) {
146 ctx.strokeStyle = resolveTexturePaint(style.stroke, ctx, {
147 x: px - style.radius,
148 y: py - style.radius,
149 width: style.radius * 2,
150 height: style.radius * 2,
151 });
152 ctx.lineWidth = style.strokeWidth;
153 ctx.stroke(path);
154 }
155 } finally {
156 ctx.restore();
157 }
158 }
159 if (inspection && xs.type !== "point" && ys.type !== "point") inspection.capture({observations:captured!,xScale:xs,yScale:ys});
160 return (
161 <Path
162 {...events}
163 style={{ d: target, fill: "transparent", cursor: shared?.cursor }}
164 />
165 );
166 }
167
Documentation version
Section titled “Documentation version”Documentation built with @pibbl/core 0.0.2, revision a1dcf8e. ALPHA — NOT FOR PRODUCTION USE.