packages/core/src/features/viz/lib/line-series.tsx
This is the source snapshot used to build these API details. View this revision on GitHub.
1 import { Path, resolveSignalValue } from "@pibbl/core";
2 import { useInternalValueMemoSlot } from "@pibbl/core/internal";
3 import type { LineSeriesProps } from "./types.js";
4 import {
5 appendCurveForward,
6 curveGeometry,
7 resolveSeriesCurve,
8 } from "./curve-definition.js";
9 import {
10 useSeriesPosition,
11 seriesAccessor,
12 mapSeriesPosition,
13 } from "./series-position.js";
14 import { width, opacity } from "./paint.js";
15 /**
16 * Draws a scaled line through defined data values, leaving gaps for undefined values.
17 *
18 * @param props - Data, coordinate accessors, scales, and line styling. See {@link LineSeriesProps}
19 * .
20 * @returns Pibbl nodes drawing the data as a line series.
21 *
22 * @see {@link LineSeriesProps}
23 */
24 export function LineSeries<D>({
25 data: dataInput,
26 x: xInput,
27 y: yInput,
28 xScale: xId,
29 yScale: yId,
30 defined,
31 inspection,
32 curve,
33 style: styleInput,
34 ...eventProps
35 }: LineSeriesProps<D>) {
36 const xScale = useSeriesPosition(xId);
37 const yScale = useSeriesPosition(yId);
38 const x = useInternalValueMemoSlot(
39 "vizXAccessor",
40 () => seriesAccessor(xInput),
41 [xInput],
42 );
43 const y = useInternalValueMemoSlot(
44 "vizYAccessor",
45 () => seriesAccessor(yInput),
46 [yInput],
47 );
48 const curveDefinition = resolveSeriesCurve(curve);
49 const data = resolveSignalValue(dataInput);
50 if (!Array.isArray(data))
51 throw new TypeError(
52 "LineSeries data must be an array or a signal containing an array.",
53 );
54 const style = resolveSignalValue(styleInput) ?? {};
55 const stroke = resolveSignalValue(style.stroke) ?? "#2563eb";
56 const strokeWidth = width(
57 resolveSignalValue(style.strokeWidth) ?? 2,
58 "LineSeries",
59 );
60 const alpha = opacity(resolveSignalValue(style.opacity) ?? 1, "LineSeries");
61 const cursor = resolveSignalValue(style.cursor);
62 const lineInteractionWidth = width(
63 resolveSignalValue(style.lineInteractionWidth) ?? strokeWidth,
64 "LineSeries",
65 );
66 const path = new Path2D();
67 const captured: import("../inspection/capture-types.js").CapturedNumericObservation<D>[] | undefined = inspection ? [] : undefined;
68 let runId = 0;
69 if (inspection && (xScale.type === "point" || yScale.type === "point")) throw new TypeError("Numeric line inspection requires numeric continuous scales.");
70 let run: (readonly [number, number])[] = [];
71 const flushRun = () => {
72 if (run.length >= 2) {
73 const geometry = curveGeometry(curveDefinition, run);
74 path.moveTo(geometry.start[0], geometry.start[1]);
75 appendCurveForward(path, geometry);
76 }
77 run = [];
78 };
79 for (let i = 0; i < data.length; i++) {
80 const datum = data[i];
81 if (defined && !defined(datum, i, data)) {
82 flushRun();
83 runId++;
84 continue;
85 }
86 const xv = x(datum, i, data),
87 yv = y(datum, i, data);
88 const px = mapSeriesPosition(xScale, xv),
89 py = mapSeriesPosition(yScale, yv);
90 if (px === undefined || py === undefined) {
91 flushRun();
92 runId++;
93 continue;
94 }
95 captured?.push(Object.freeze({datum,sourceIndex:i,domainX:xv as number,domainY:yv as number,anchorLocal:Object.freeze({x:px,y:py}),run:runId}));
96 const point = [px, py] as const;
97 run.push(point);
98 }
99 flushRun();
100 if (inspection && xScale.type !== "point" && yScale.type !== "point") inspection.capture({observations:captured!,xScale,yScale});
101 return (
102 <Path
103 {...eventProps}
104 style={{
105 d: path,
106 stroke,
107 strokeWidth,
108 opacity: alpha,
109 cursor,
110 lineInteractionWidth,
111 }}
112 />
113 );
114 }
115
Documentation version
Section titled “Documentation version”Documentation built with @pibbl/core 0.0.2, revision a1dcf8e. ALPHA — NOT FOR PRODUCTION USE.