react-native-livechart 4.31.1 → 4.32.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/dist/components/CrosshairOverlay.d.ts +3 -1
  2. package/dist/components/CrosshairOverlay.d.ts.map +1 -1
  3. package/dist/components/CustomMarkerOverlay.d.ts +2 -2
  4. package/dist/components/CustomMarkerOverlay.d.ts.map +1 -1
  5. package/dist/components/FreeCrosshairOverlay.d.ts +25 -0
  6. package/dist/components/FreeCrosshairOverlay.d.ts.map +1 -0
  7. package/dist/components/LiveChart.d.ts.map +1 -1
  8. package/dist/components/LiveChartSeries.d.ts.map +1 -1
  9. package/dist/components/MarkerOverlay.d.ts +2 -2
  10. package/dist/components/MarkerOverlay.d.ts.map +1 -1
  11. package/dist/components/ScrubActionOverlay.d.ts +7 -1
  12. package/dist/components/ScrubActionOverlay.d.ts.map +1 -1
  13. package/dist/core/liveChartEngineTick.d.ts +2 -0
  14. package/dist/core/liveChartEngineTick.d.ts.map +1 -1
  15. package/dist/core/liveChartSeriesEngineTick.d.ts +6 -0
  16. package/dist/core/liveChartSeriesEngineTick.d.ts.map +1 -1
  17. package/dist/core/resolveConfig.d.ts +4 -0
  18. package/dist/core/resolveConfig.d.ts.map +1 -1
  19. package/dist/core/useLiveChartEngine.d.ts +7 -2
  20. package/dist/core/useLiveChartEngine.d.ts.map +1 -1
  21. package/dist/core/useLiveChartSeriesEngine.d.ts +9 -1
  22. package/dist/core/useLiveChartSeriesEngine.d.ts.map +1 -1
  23. package/dist/hooks/crosshairSeries.d.ts +1 -1
  24. package/dist/hooks/crosshairSeries.d.ts.map +1 -1
  25. package/dist/hooks/crosshairShared.d.ts +4 -2
  26. package/dist/hooks/crosshairShared.d.ts.map +1 -1
  27. package/dist/hooks/useCrosshair.d.ts +5 -1
  28. package/dist/hooks/useCrosshair.d.ts.map +1 -1
  29. package/dist/hooks/useCrosshairSeries.d.ts.map +1 -1
  30. package/dist/hooks/useMarkers.d.ts +2 -2
  31. package/dist/hooks/useMarkers.d.ts.map +1 -1
  32. package/dist/hooks/usePanScroll.d.ts +2 -0
  33. package/dist/hooks/usePanScroll.d.ts.map +1 -1
  34. package/dist/hooks/usePinchZoom.d.ts +2 -0
  35. package/dist/hooks/usePinchZoom.d.ts.map +1 -1
  36. package/dist/hooks/useReferenceLineSeries.d.ts +1 -1
  37. package/dist/hooks/useReferenceLineSeries.d.ts.map +1 -1
  38. package/dist/hooks/useScrubMarkers.d.ts +2 -1
  39. package/dist/hooks/useScrubMarkers.d.ts.map +1 -1
  40. package/dist/hooks/useVisibleRange.d.ts +8 -17
  41. package/dist/hooks/useVisibleRange.d.ts.map +1 -1
  42. package/dist/index.d.ts +1 -1
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/math/markers.d.ts +6 -0
  45. package/dist/math/markers.d.ts.map +1 -1
  46. package/dist/math/referenceLineSeries.d.ts +1 -1
  47. package/dist/math/referenceLineSeries.d.ts.map +1 -1
  48. package/dist/math/scrubMarkers.d.ts +1 -1
  49. package/dist/math/scrubMarkers.d.ts.map +1 -1
  50. package/dist/math/spline.d.ts +3 -2
  51. package/dist/math/spline.d.ts.map +1 -1
  52. package/dist/types.d.ts +47 -6
  53. package/dist/types.d.ts.map +1 -1
  54. package/package.json +1 -1
  55. package/src/components/CrosshairOverlay.tsx +7 -0
  56. package/src/components/CustomMarkerOverlay.tsx +5 -2
  57. package/src/components/FreeCrosshairOverlay.tsx +87 -0
  58. package/src/components/LiveChart.tsx +97 -73
  59. package/src/components/LiveChartSeries.tsx +33 -20
  60. package/src/components/MarkerOverlay.tsx +24 -14
  61. package/src/components/ScrubActionOverlay.tsx +24 -5
  62. package/src/core/liveChartEngineTick.ts +3 -0
  63. package/src/core/liveChartSeriesEngineTick.ts +25 -4
  64. package/src/core/resolveConfig.ts +13 -0
  65. package/src/core/useLiveChartEngine.ts +14 -2
  66. package/src/core/useLiveChartSeriesEngine.ts +44 -7
  67. package/src/hooks/crosshairSeries.ts +4 -4
  68. package/src/hooks/crosshairShared.ts +18 -12
  69. package/src/hooks/useCrosshair.ts +29 -5
  70. package/src/hooks/useCrosshairSeries.ts +10 -4
  71. package/src/hooks/useMarkers.ts +7 -4
  72. package/src/hooks/usePanScroll.ts +3 -1
  73. package/src/hooks/usePinchZoom.ts +8 -5
  74. package/src/hooks/useReferenceLineSeries.ts +8 -4
  75. package/src/hooks/useScrubMarkers.ts +5 -2
  76. package/src/hooks/useVisibleRange.ts +96 -66
  77. package/src/index.ts +1 -0
  78. package/src/math/markers.ts +25 -8
  79. package/src/math/referenceLineSeries.ts +4 -3
  80. package/src/math/scrubMarkers.ts +4 -1
  81. package/src/math/spline.ts +41 -25
  82. package/src/types.ts +45 -6
@@ -7,7 +7,7 @@ import {
7
7
  type SharedValue,
8
8
  } from "react-native-reanimated";
9
9
  import { runOnJS } from "react-native-worklets";
10
- import type { ChartEngineLayout } from "../core/useLiveChartEngine";
10
+ import type { MarkerEngineLayout } from "../core/useLiveChartEngine";
11
11
  import type { ChartPadding } from "../draw/line";
12
12
  import { projectMarkers, type ProjectedMarker } from "../math/markers";
13
13
  import { nearestMarkerIndex } from "../math/markers";
@@ -43,7 +43,7 @@ const ANCHORED_CLUSTER: ResolvedMarkerCluster = {
43
43
  * drawn (stacked / collapsed) positions.
44
44
  */
45
45
  export function useMarkers(
46
- engine: ChartEngineLayout,
46
+ engine: MarkerEngineLayout,
47
47
  padding: ChartPadding,
48
48
  markers: SharedValue<Marker[]>,
49
49
  active: boolean,
@@ -104,6 +104,9 @@ export function useMarkers(
104
104
  padLeft: padding.left,
105
105
  padRight: padding.right,
106
106
  timestamp: engine.timestamp.get(),
107
+ tipTime: engine.tipTime?.get(),
108
+ displaySeriesValues: engine.displaySeriesValues?.get(),
109
+ smoothRecordedTail: engine.smoothRecordedTail?.get(),
107
110
  displayWindow: engine.displayWindow.get(),
108
111
  displayMin: engine.displayMin.get(),
109
112
  displayMax: engine.displayMax.get(),
@@ -124,8 +127,8 @@ export function useMarkers(
124
127
  () => {
125
128
  if (!autoSleep || !autostart) return null;
126
129
  return [active, markers.get(), engine.canvasWidth.get(), engine.canvasHeight.get(),
127
- engine.timestamp.get(), engine.displayWindow.get(), engine.displayMin.get(),
128
- engine.displayMax.get(), seriesSV?.get(), lineData?.get(), isFrameLoopActive?.get()];
130
+ engine.timestamp.get(), engine.tipTime?.get(), engine.displayWindow.get(), engine.displayMin.get(),
131
+ engine.displayMax.get(), engine.displaySeriesValues?.get(), engine.smoothRecordedTail?.get(), seriesSV?.get(), lineData?.get(), isFrameLoopActive?.get()];
129
132
  },
130
133
  (inputs) => {
131
134
  if (inputs !== null) project();
@@ -35,6 +35,8 @@ export interface PanScrollEngineRefs {
35
35
  liveEdge: SharedValue<number>;
36
36
  /** Animating visible window width in seconds. */
37
37
  displayWindow: SharedValue<number>;
38
+ /** Ordinary window before any presentation-only future horizon. */
39
+ navigationWindow?: SharedValue<number>;
38
40
  /** Canvas width in px. */
39
41
  canvasWidth: SharedValue<number>;
40
42
  /** Canvas height in px (for the axis-drag band hit-test). */
@@ -307,7 +309,6 @@ export function usePanScroll({
307
309
  const {
308
310
  viewEnd,
309
311
  liveEdge,
310
- displayWindow,
311
312
  canvasWidth,
312
313
  canvasHeight,
313
314
  viewRange,
@@ -318,6 +319,7 @@ export function usePanScroll({
318
319
  fitMax,
319
320
  viewWindow,
320
321
  } = engine;
322
+ const displayWindow = engine.navigationWindow ?? engine.displayWindow;
321
323
  const padLeft = padding.left;
322
324
  const padRight = padding.right;
323
325
  const padTop = padding.top;
@@ -25,6 +25,8 @@ export interface PinchZoomEngineRefs {
25
25
  * `timeWindow` would otherwise lag a fast pinch and the focal anchor would drift).
26
26
  */
27
27
  displayWindow: SharedValue<number>;
28
+ /** Frame-published navigation width on multi-series charts, before the future horizon. */
29
+ navigationWindow?: SharedValue<number>;
28
30
  /** Canvas width in px. */
29
31
  canvasWidth: SharedValue<number>;
30
32
  }
@@ -169,7 +171,7 @@ export function usePinchZoom({
169
171
  onZoomStart,
170
172
  overscroll = 0,
171
173
  }: UsePinchZoomOptions): ReturnType<typeof Gesture.Pinch> {
172
- const { viewWindow, viewEnd, liveEdge, displayWindow, canvasWidth } = engine;
174
+ const { viewWindow, viewEnd, liveEdge, displayWindow, navigationWindow, canvasWidth } = engine;
173
175
  const padLeft = padding.left;
174
176
  const padRight = padding.right;
175
177
 
@@ -189,7 +191,7 @@ export function usePinchZoom({
189
191
  cancelAnimation(viewEnd);
190
192
  cancelAnimation(viewWindow);
191
193
  const edge = liveEdge.get();
192
- startWindow.set(displayWindow.get());
194
+ startWindow.set(navigationWindow ? viewWindow.get() ?? navigationWindow.get() : displayWindow.get());
193
195
  startViewEnd.set(viewEnd.get() ?? edge);
194
196
  onZoomStart?.();
195
197
  };
@@ -228,9 +230,9 @@ export function usePinchZoom({
228
230
  const hi = overscroll > 0 ? panUpperBound(newWin, edge, overscroll) : edge;
229
231
  if (newEnd > hi) newEnd = hi;
230
232
  viewWindow.set(newWin);
231
- // Track the displayed window 1:1 (bypass the frame-loop lerp lag) so the
232
- // focal anchor is pixel-accurate during the gesture.
233
- displayWindow.set(newWin);
233
+ // The multi-series frame publishes both viewport edges and widths together.
234
+ // The single-series engine uses this write to bypass its window easing.
235
+ if (!navigationWindow) displayWindow.set(newWin);
234
236
  // Unlike the pan (see nextViewEnd), snapping to `null` mid-gesture is safe
235
237
  // here: the pinch maps cumulative scale/focal from a gesture-START snapshot
236
238
  // (`startViewEnd`), not per-frame deltas, so a snap can't re-anchor it.
@@ -250,6 +252,7 @@ export function usePinchZoom({
250
252
  }, [
251
253
  canvasWidth,
252
254
  displayWindow,
255
+ navigationWindow,
253
256
  enabled,
254
257
  fullHistoryWindow,
255
258
  liveEdge,
@@ -17,7 +17,7 @@ export interface ReferenceLineSeriesGeometry {
17
17
  screenPts: SharedValue<number[]>;
18
18
  /** Whether any series segment crosses the visible plot. */
19
19
  visible: SharedValue<boolean>;
20
- /** Series value at the chart's current right edge. */
20
+ /** Series value at the presented data head. */
21
21
  currentValue: SharedValue<number>;
22
22
  /** Canvas Y coordinate of {@link currentValue}. */
23
23
  currentY: SharedValue<number>;
@@ -46,6 +46,7 @@ export function useReferenceLineSeries(
46
46
  cacheRef.current = { a: [], b: [], tick: false };
47
47
  }
48
48
 
49
+ const headTime = engine.tipTime ?? engine.timestamp;
49
50
  const screenPts = useDerivedValue(() => {
50
51
  const cache = cacheRef.current!;
51
52
  cache.tick = !cache.tick;
@@ -61,6 +62,7 @@ export function useReferenceLineSeries(
61
62
  extendToStart,
62
63
  extendToNow,
63
64
  cache.tick ? cache.a : cache.b,
65
+ headTime.get(),
64
66
  );
65
67
  });
66
68
 
@@ -74,7 +76,7 @@ export function useReferenceLineSeries(
74
76
  );
75
77
 
76
78
  const currentValue = useDerivedValue(
77
- () => interpolateAtTime(points, engine.timestamp.get()) ?? NaN,
79
+ () => interpolateAtTime(points, headTime.get()) ?? NaN,
78
80
  );
79
81
  const currentY = useDerivedValue(() =>
80
82
  thresholdLineY(
@@ -88,10 +90,12 @@ export function useReferenceLineSeries(
88
90
  );
89
91
  const currentVisible = useDerivedValue(() => {
90
92
  if (points.length === 0) return false;
91
- if (!extendToStart && points[0].time > engine.timestamp.get()) {
93
+ const head = headTime.get();
94
+ if (head < engine.timestamp.get() - engine.displayWindow.get()) return false;
95
+ if (!extendToStart && points[0].time > head) {
92
96
  return false;
93
97
  }
94
- if (!extendToNow && points[points.length - 1].time < engine.timestamp.get()) {
98
+ if (!extendToNow && points[points.length - 1].time < head) {
95
99
  return false;
96
100
  }
97
101
  return thresholdVisible(
@@ -10,15 +10,17 @@ import type { CandlePoint, ChartGap, Marker } from "../types";
10
10
  export interface ScrubMarkerOptions {
11
11
  markers: SharedValue<Marker[]>;
12
12
  radius: number;
13
+ tipTime?: SharedValue<number>;
13
14
  }
14
15
 
15
16
  /** Adapt resolved config for both crosshair hooks. */
16
17
  export function resolveScrubMarkerOptions(
17
18
  config: ResolvedScrubConfig | null,
18
19
  markers: SharedValue<Marker[]>,
20
+ tipTime?: SharedValue<number>,
19
21
  ): ScrubMarkerOptions | undefined {
20
22
  if (!config?.markers) return undefined;
21
- return { markers, radius: config.markers.radius };
23
+ return { markers, radius: config.markers.radius, tipTime };
22
24
  }
23
25
 
24
26
  /** Selection changes notify tooltip consumers; movement within a match does not. */
@@ -50,6 +52,7 @@ export function useScrubMarkers(
50
52
  if (!options || !active.get() || gap?.get()) return null;
51
53
  return {
52
54
  markers: source.get(),
55
+ tipTime: options.tipTime?.get(),
53
56
  range: scrubMarkerRange(
54
57
  time.get(),
55
58
  now.get(),
@@ -63,7 +66,7 @@ export function useScrubMarkers(
63
66
  };
64
67
  },
65
68
  (input, previous) => {
66
- const next = input ? matchScrubMarkers(input.markers, input.range) : [];
69
+ const next = input ? matchScrubMarkers(input.markers, input.range, input.tipTime) : [];
67
70
  const current = selected.get();
68
71
  // Refresh metadata when the input changes, but retain the result array
69
72
  // through chart ticks / movement that select the same markers.
@@ -1,11 +1,15 @@
1
1
  import { useCallback, useEffect, useRef } from "react";
2
2
  import {
3
+ makeMutable,
4
+ startMapper,
5
+ stopMapper,
3
6
  useAnimatedReaction,
4
7
  type SharedValue,
5
8
  } from "react-native-reanimated";
6
- import { scheduleOnRN } from "react-native-worklets";
9
+ import { scheduleOnRN, scheduleOnUI } from "react-native-worklets";
7
10
 
8
11
  import type { VisibleRange } from "../types";
12
+ import { useLatestCallback } from "./useLatestCallback";
9
13
 
10
14
  export type { VisibleRange };
11
15
 
@@ -13,13 +17,16 @@ export type { VisibleRange };
13
17
  export interface VisibleRangeEngineRefs {
14
18
  timestamp: SharedValue<number>;
15
19
  displayWindow: SharedValue<number>;
16
- viewEnd: SharedValue<number | null>;
20
+ following: SharedValue<boolean>;
21
+ /** Ordinary viewport before the multi-series future horizon is applied. */
22
+ navigationEnd?: SharedValue<number>;
23
+ navigationWindow?: SharedValue<number>;
17
24
  }
18
25
 
19
26
  export interface UseVisibleRangeOptions {
20
27
  engine: VisibleRangeEngineRefs;
21
- /** Earliest retained time (unix seconds) — used for the `onReachStart` trigger. */
22
- minTime: SharedValue<number>;
28
+ /** Earliest retained time in seconds, or null until the chart has a history bound. */
29
+ minTime: Pick<SharedValue<number | null>, "get">;
23
30
  /** Fires (throttled to ~1 Hz) when the visible window's edges change. */
24
31
  onVisibleRangeChange?: (range: VisibleRange) => void;
25
32
  /**
@@ -30,21 +37,6 @@ export interface UseVisibleRangeOptions {
30
37
  onReachStart?: () => void;
31
38
  }
32
39
 
33
- /**
34
- * Throttle key for a visible range: integer seconds of each edge plus the
35
- * near-start flag. The reaction emits only when this changes, so a live chart
36
- * (whose right edge slides every frame) notifies at most ~once per second rather
37
- * than per frame, and `onReachStart` edge-triggers off the trailing flag.
38
- */
39
- export function rangeSignature(
40
- startSec: number,
41
- endSec: number,
42
- near: boolean,
43
- ): string {
44
- "worklet";
45
- return `${Math.round(startSec)}|${Math.round(endSec)}|${near ? 1 : 0}`;
46
- }
47
-
48
40
  /** Whether the window's left edge is within `threshold` seconds of the oldest data. */
49
41
  export function isNearStart(
50
42
  winStart: number,
@@ -55,65 +47,103 @@ export function isNearStart(
55
47
  return winStart <= minTime + threshold;
56
48
  }
57
49
 
58
- /**
59
- * Bridges the UI-thread visible window to JS paging callbacks. A reaction watches
60
- * the window edges and `scheduleOnRN`s `onVisibleRangeChange` (throttled) and
61
- * `onReachStart` (edge-triggered near the oldest data). Inert when both callbacks
62
- * are absent. Used by both `LiveChart` and `LiveChartSeries`.
63
- */
50
+ // React Native supplies this monotonic clock. The library builds without DOM types.
51
+ declare const performance: { now(): number };
52
+
53
+ const RANGE_INTERVAL_MS = 1000;
54
+
55
+ /** Report the drawn viewport to JS paging callbacks on either chart. */
64
56
  export function useVisibleRange({
65
- engine,
57
+ engine: { timestamp, displayWindow, following, navigationEnd = timestamp, navigationWindow = displayWindow },
66
58
  minTime,
67
59
  onVisibleRangeChange,
68
60
  onReachStart,
69
61
  }: UseVisibleRangeOptions): void {
70
- // Latest callbacks behind refs so the (deps-stable) dispatchers below always
71
- // call the current prop without re-subscribing the reaction each render.
72
- const rangeCb = useRef(onVisibleRangeChange);
73
- const reachCb = useRef(onReachStart);
62
+ const readRange = useCallback((): VisibleRange => {
63
+ "worklet";
64
+ const endSec = timestamp.get();
65
+ return { startSec: endSec - displayWindow.get(), endSec, following: following.get() };
66
+ }, [timestamp, displayWindow, following]);
67
+ const lastReport = useRef<{ range: VisibleRange; time: number } | null>(null);
68
+ const emitRange = useLatestCallback((range: VisibleRange) => {
69
+ const previous = lastReport.current?.range;
70
+ if (!onVisibleRangeChange) return;
71
+ if (
72
+ previous?.startSec === range.startSec &&
73
+ previous.endSec === range.endSec &&
74
+ previous.following === range.following
75
+ ) return;
76
+ lastReport.current = { range, time: performance.now() };
77
+ onVisibleRangeChange(range);
78
+ });
79
+ const emitReachStart = useLatestCallback((range: VisibleRange) => {
80
+ if (!onReachStart) return;
81
+ // Paging loaders read the last reported range synchronously in onReachStart.
82
+ emitRange(range);
83
+ onReachStart();
84
+ });
85
+ const watchRange = onVisibleRangeChange != null;
86
+ const watchStart = onReachStart != null;
87
+
74
88
  useEffect(() => {
75
- rangeCb.current = onVisibleRangeChange;
76
- reachCb.current = onReachStart;
77
- }, [onVisibleRangeChange, onReachStart]);
89
+ if (!watchRange) return;
90
+ const pending = makeMutable(false);
91
+ let active = true;
92
+ let timeout: ReturnType<typeof setTimeout>;
78
93
 
79
- const emitRange = useCallback(
80
- /* istanbul ignore next -- dispatched via scheduleOnRN from the UI thread, not in Jest */
81
- (startSec: number, endSec: number, following: boolean) => {
82
- rangeCb.current?.({ startSec, endSec, following });
83
- },
84
- [],
85
- );
86
- const emitReachStart = useCallback(
87
- /* istanbul ignore next -- dispatched via scheduleOnRN from the UI thread, not in Jest */
88
- () => {
89
- reachCb.current?.();
90
- },
91
- [],
92
- );
94
+ function deliver(range: VisibleRange) {
95
+ if (!active) return;
96
+ emitRange(range);
97
+ }
98
+
99
+ function flush() {
100
+ // Sample all three fields on the same UI frame, including the final range
101
+ // when the chart has stopped producing frames during the throttle interval.
102
+ scheduleOnUI(() => {
103
+ "worklet";
104
+ pending.set(false);
105
+ scheduleOnRN(deliver, readRange());
106
+ });
107
+ }
108
+
109
+ function requestReport() {
110
+ if (!active) return;
111
+ const lastTime = lastReport.current?.time ?? -Infinity;
112
+ const remaining = RANGE_INTERVAL_MS - (performance.now() - lastTime);
113
+ // A paging callback can report a newer viewport while the timer is waiting.
114
+ if (remaining > 0) timeout = setTimeout(requestReport, Math.ceil(remaining));
115
+ else flush();
116
+ }
117
+
118
+ // Subscribe only to the viewport. Keep one JS request in flight while any
119
+ // number of pan, zoom, or horizon frames update the range underneath it.
120
+ const mapper = startMapper(() => {
121
+ "worklet";
122
+ if (!pending.get()) {
123
+ pending.set(true);
124
+ scheduleOnRN(requestReport);
125
+ }
126
+ }, [timestamp, displayWindow, following]);
93
127
 
94
- const active = onVisibleRangeChange != null || onReachStart != null;
128
+ return () => {
129
+ active = false;
130
+ lastReport.current = null;
131
+ stopMapper(mapper);
132
+ clearTimeout(timeout);
133
+ };
134
+ }, [watchRange, timestamp, displayWindow, following, readRange, emitRange]);
95
135
 
96
136
  useAnimatedReaction(
97
- /* istanbul ignore next -- Reanimated reaction worklet; runs on the UI thread, not in Jest */
98
137
  () => {
99
- if (!active) return ""; // inert — never changes, so the reaction never fires
100
- const end = engine.timestamp.value;
101
- const win = engine.displayWindow.value;
102
- const start = end - win;
103
- return rangeSignature(start, end, isNearStart(start, minTime.value, win));
138
+ if (!watchStart) return false;
139
+ const oldest = minTime.get();
140
+ if (oldest === null) return false;
141
+ const width = navigationWindow.get();
142
+ return isNearStart(navigationEnd.get() - width, oldest, width);
104
143
  },
105
- /* istanbul ignore next -- Reanimated reaction; dispatches on the JS thread, not exercised under Jest */
106
- (curr, prev) => {
107
- if (!active || curr === "" || curr === prev) return;
108
- const end = engine.timestamp.value;
109
- const win = engine.displayWindow.value;
110
- const start = end - win;
111
- const following = engine.viewEnd.value == null;
112
- const near = isNearStart(start, minTime.value, win);
113
- const wasNear = typeof prev === "string" && prev.endsWith("|1");
114
- scheduleOnRN(emitRange, start, end, following);
115
- if (near && !wasNear) scheduleOnRN(emitReachStart);
144
+ (near, wasNear) => {
145
+ if (near && !wasNear) scheduleOnRN(emitReachStart, readRange());
116
146
  },
117
- [active, emitRange, emitReachStart],
147
+ [watchStart, navigationEnd, navigationWindow, minTime, readRange, emitReachStart],
118
148
  );
119
149
  }
package/src/index.ts CHANGED
@@ -69,6 +69,7 @@ export type {
69
69
  DotRingConfig,
70
70
  EmptyStateMetrics,
71
71
  FontConfig,
72
+ FreeCrosshairConfig,
72
73
  FontWeight,
73
74
  GradientConfig,
74
75
  GridMetrics,
@@ -33,6 +33,10 @@ export interface ProjectMarkersOpts {
33
33
  displayWindow: number;
34
34
  displayMin: number;
35
35
  displayMax: number;
36
+ /** Last time with a presented series anchor; the viewport may extend beyond it. */
37
+ tipTime?: number;
38
+ displaySeriesValues?: number[];
39
+ smoothRecordedTail?: boolean;
36
40
  /** Multi-series data, used to anchor markers by `seriesId`. */
37
41
  series?: SeriesConfig[];
38
42
  /** Single-series line data; anchors markers that omit `value` (and `seriesId`). */
@@ -63,6 +67,17 @@ const CULL_MARGIN = 24;
63
67
  */
64
68
  const EDGE_SLOP = 0.5;
65
69
 
70
+ /** Absolute-value events can appear ahead of a series tip. */
71
+ export function isFutureSeriesMarker(
72
+ time: number,
73
+ value: number | undefined,
74
+ seriesId: string | undefined,
75
+ tipTime: number | undefined,
76
+ ): boolean {
77
+ "worklet";
78
+ return tipTime !== undefined && time > tipTime && value === undefined && seriesId !== undefined;
79
+ }
80
+
66
81
  /**
67
82
  * Project one marker (given its primitive anchor fields) into `target`,
68
83
  * without allocation. Primitives only, so per-glyph worklet closures don't
@@ -96,7 +111,8 @@ function projectInto(
96
111
  h === 0 ||
97
112
  chartW <= 0 ||
98
113
  opts.displayWindow <= 0 ||
99
- valRange <= 0
114
+ valRange <= 0 ||
115
+ isFutureSeriesMarker(time, value, seriesId, opts.tipTime)
100
116
  ) {
101
117
  target.visible = false;
102
118
  return;
@@ -107,13 +123,14 @@ function projectInto(
107
123
  } else if (seriesId !== undefined && opts.series) {
108
124
  for (let j = 0; j < opts.series.length; j++) {
109
125
  if (opts.series[j].id === seriesId) {
110
- // Anchor on the same interpolation the series is rendered with, so the
111
- // glyph sits exactly on the line: the linear chord for `curve: "linear"`,
112
- // the monotone spline otherwise.
113
- v =
114
- opts.series[j].curve === "linear"
115
- ? interpolateAtTime(opts.series[j].data, time)
116
- : splineValueAtTime(opts.series[j].data, time);
126
+ const series = opts.series[j];
127
+ const tip = opts.tipTime === undefined ? undefined : {
128
+ time: opts.tipTime,
129
+ value: opts.displaySeriesValues?.[j] ?? series.value,
130
+ };
131
+ const latest = series.data[series.data.length - 1];
132
+ v = splineValueAtTime(series.data, time, series.curve === "linear", tip,
133
+ opts.smoothRecordedTail && latest?.value === series.value);
117
134
  break;
118
135
  }
119
136
  }
@@ -13,7 +13,7 @@ import { thresholdLineY } from "./threshold";
13
13
  */
14
14
  export function buildReferenceLineSeriesPoints(
15
15
  points: LiveChartPoint[],
16
- now: number,
16
+ viewportEnd: number,
17
17
  windowSecs: number,
18
18
  displayMin: number,
19
19
  displayMax: number,
@@ -23,6 +23,7 @@ export function buildReferenceLineSeriesPoints(
23
23
  extendToStart: boolean,
24
24
  extendToNow: boolean,
25
25
  out?: number[],
26
+ headTime = viewportEnd,
26
27
  ): number[] {
27
28
  "worklet";
28
29
  const result = out ?? [];
@@ -43,13 +44,13 @@ export function buildReferenceLineSeriesPoints(
43
44
  return result;
44
45
  }
45
46
 
46
- const windowStart = now - windowSecs;
47
+ const windowStart = viewportEnd - windowSecs;
47
48
  const firstTime = points[0].time;
48
49
  const lastTime = points[points.length - 1].time;
49
50
  const startTime = extendToStart
50
51
  ? windowStart
51
52
  : Math.max(windowStart, firstTime);
52
- const endTime = extendToNow ? now : Math.min(now, lastTime);
53
+ const endTime = extendToNow ? headTime : Math.min(headTime, lastTime);
53
54
  if (endTime < startTime) return result;
54
55
 
55
56
  const startValue = interpolateAtTime(points, startTime);
@@ -1,4 +1,5 @@
1
1
  import type { Marker } from "../types";
2
+ import { isFutureSeriesMarker } from "./markers";
2
3
 
3
4
  export interface ScrubMarkerRange {
4
5
  from: number;
@@ -11,12 +12,14 @@ export interface ScrubMarkerRange {
11
12
  export function matchScrubMarkers(
12
13
  markers: Marker[],
13
14
  range: ScrubMarkerRange | null,
15
+ tipTime?: number,
14
16
  ): Marker[] {
15
17
  "worklet";
16
18
  if (!range) return [];
17
19
  return markers.filter(
18
20
  (marker) => marker.time >= range.from &&
19
- (range.exclusiveEnd ? marker.time < range.to : marker.time <= range.to),
21
+ (range.exclusiveEnd ? marker.time < range.to : marker.time <= range.to) &&
22
+ !isFutureSeriesMarker(marker.time, marker.value, marker.seriesId, tipTime),
20
23
  );
21
24
  }
22
25
 
@@ -146,7 +146,8 @@ export function drawSpline(
146
146
  * Value of the Fritsch–Carlson monotone cubic — the same curve {@link drawSpline}
147
147
  * renders — at `time`. Use this to anchor overlays (e.g. markers) exactly on the
148
148
  * rendered line instead of on the straight chord between data points. Returns
149
- * `null` for empty data; clamps to the endpoints outside the data range.
149
+ * `null` for empty data. A supplied tip replaces buffered future points and
150
+ * terminates the curve at its presented value. Linear series use the chord.
150
151
  *
151
152
  * The data→pixel mapping is affine, so evaluating the monotone cubic in data
152
153
  * space and then projecting matches the pixel-space spline. Tangents use the
@@ -157,46 +158,61 @@ export function drawSpline(
157
158
  export function splineValueAtTime(
158
159
  points: LiveChartPoint[],
159
160
  time: number,
161
+ linear = false,
162
+ tip?: LiveChartPoint,
163
+ smoothRecordedTail = false,
160
164
  ): number | null {
161
165
  "worklet";
162
166
  const n = points.length;
163
- if (n === 0) return null;
164
- if (n === 1 || time <= points[0].time) return points[0].value;
165
- if (time >= points[n - 1].time) return points[n - 1].value;
167
+ if (!n) return null;
168
+ const first = points[0];
169
+ const last = points[n - 1];
170
+ if (tip && (first.time > tip.time || time > tip.time)) return null;
171
+ if (tip && time === tip.time) return tip.value;
172
+ // buildLinePoints can ease the final recorded vertex together with the tip.
173
+ const tailValue = smoothRecordedTail && tip ? tip.value : last.value;
174
+ if (time <= first.time) return n === 1 ? tailValue : first.value;
175
+ if (!tip && time >= last.time) return last.value;
166
176
 
167
- // Segment [i, i+1] with points[i].time <= time < points[i+1].time.
177
+ // Find the recorded vertex at or before the marker. Resolve only its local
178
+ // neighbors, so clipping at the head never copies the series history.
168
179
  let lo = 0;
169
- let hi = n - 1;
180
+ let hi = n;
170
181
  while (hi - lo > 1) {
171
182
  const mid = (lo + hi) >> 1;
172
183
  if (points[mid].time <= time) lo = mid;
173
184
  else hi = mid;
174
185
  }
175
- const i = lo;
176
- const ti = points[i].time;
177
- const vi = points[i].value;
178
- const tj = points[i + 1].time;
179
- const vj = points[i + 1].value;
186
+ const left = points[lo];
187
+ const candidate = points[lo + 1];
188
+ const right = candidate && (!tip || candidate.time <= tip.time) ? candidate : tip!;
189
+ const ti = left.time;
190
+ const vi = left === last ? tailValue : left.value;
191
+ const tj = right.time;
192
+ const vj = right === last ? tailValue : right.value;
180
193
  const hSeg = tj - ti;
181
- /* istanbul ignore next -- guarded by the clamps above; defensive for duplicate times */
182
- if (hSeg <= 0) return vi;
194
+ if (hSeg <= 0 || time === ti) return vi;
183
195
 
184
196
  const deltaI = (vj - vi) / hSeg;
185
- if (n === 2) return vi + deltaI * (time - ti); // drawSpline draws a straight line for n === 2
197
+ if (linear) return vi + deltaI * (time - ti);
186
198
 
187
- // Tangents at the segment ends (monotone-safe averages of adjacent secants).
188
- let mi: number;
189
- if (i === 0) {
190
- mi = deltaI;
191
- } else {
192
- const dPrev = (vi - points[i - 1].value) / (ti - points[i - 1].time);
199
+ const before = points[lo - 1];
200
+ const next = points[lo + 2];
201
+ let after: LiveChartPoint | undefined = next;
202
+ if (tip && (!next || next.time > tip.time)) {
203
+ after = right === tip ? undefined : tip;
204
+ }
205
+ if (!before && !after) return vi + deltaI * (time - ti);
206
+
207
+ let mi = deltaI;
208
+ if (before) {
209
+ const dPrev = ti === before.time ? 0 : (vi - before.value) / (ti - before.time);
193
210
  mi = dPrev * deltaI <= 0 ? 0 : (dPrev + deltaI) / 2;
194
211
  }
195
- let mj: number;
196
- if (i + 1 === n - 1) {
197
- mj = deltaI;
198
- } else {
199
- const dNext = (points[i + 2].value - vj) / (points[i + 2].time - tj);
212
+ let mj = deltaI;
213
+ if (after) {
214
+ const afterValue = after === last ? tailValue : after.value;
215
+ const dNext = after.time === tj ? 0 : (afterValue - vj) / (after.time - tj);
200
216
  mj = deltaI * dNext <= 0 ? 0 : (deltaI + dNext) / 2;
201
217
  }
202
218