Skip to content

packages/core/src/features/viz/lib/step-curves.ts

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   createSeriesCurve,
3   type SeriesCurve,
4   type SeriesCurvePoint,
5 } from "./curve-definition.js";
6 
7 function segment(end: SeriesCurvePoint) {
8   return Object.freeze({ end });
9 }
10 
11 function stepBeforeSegments(points: readonly SeriesCurvePoint[]) {
12   const segments = [];
13   for (let i = 1; i < points.length; i++) {
14     const previous = points[i - 1],
15       next = points[i];
16     segments.push(segment([previous[0], next[1]]), segment(next));
17   }
18   return segments;
19 }
20 
21 function stepAfterSegments(points: readonly SeriesCurvePoint[]) {
22   const segments = [];
23   for (let i = 1; i < points.length; i++) {
24     const previous = points[i - 1],
25       next = points[i];
26     segments.push(segment([next[0], previous[1]]), segment(next));
27   }
28   return segments;
29 }
30 
31 function stepMidpointSegments(points: readonly SeriesCurvePoint[]) {
32   const segments = [];
33   for (let i = 1; i < points.length; i++) {
34     const previous = points[i - 1],
35       next = points[i];
36     const midpoint = previous[0] / 2 + next[0] / 2;
37     segments.push(
38       segment([midpoint, previous[1]]),
39       segment([midpoint, next[1]]),
40       segment(next),
41     );
42   }
43   return segments;
44 }
45 
46 const before = /* @__PURE__ */ createSeriesCurve(stepBeforeSegments);
47 const after = /* @__PURE__ */ createSeriesCurve(stepAfterSegments);
48 const midpoint = /* @__PURE__ */ createSeriesCurve(stepMidpointSegments);
49 
50 /**
51  * Returns the immutable definition that jumps to the next value at the prior x coordinate.
52  *
53  * @returns The named {@link SeriesCurve} definition.
54  */
55 export function stepBefore(): SeriesCurve {
56   return before;
57 }
58 
59 /**
60  * Returns the immutable definition that holds the prior value until the next x coordinate.
61  *
62  * @returns The named {@link SeriesCurve} definition.
63  */
64 export function stepAfter(): SeriesCurve {
65   return after;
66 }
67 
68 /**
69  * Returns the immutable definition that places each value transition halfway between x values.
70  *
71  * @returns The named {@link SeriesCurve} definition.
72  */
73 export function stepMidpoint(): SeriesCurve {
74   return midpoint;
75 }
76 

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