react-native-livechart 4.28.2 → 4.30.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 (65) hide show
  1. package/README.md +1 -1
  2. package/dist/components/LiveChart.d.ts.map +1 -1
  3. package/dist/components/LiveChartSeries.d.ts.map +1 -1
  4. package/dist/components/LoadingOverlay.d.ts +6 -1
  5. package/dist/components/LoadingOverlay.d.ts.map +1 -1
  6. package/dist/components/ResetButtonOverlay.d.ts +25 -0
  7. package/dist/components/ResetButtonOverlay.d.ts.map +1 -0
  8. package/dist/constants.d.ts +6 -0
  9. package/dist/constants.d.ts.map +1 -1
  10. package/dist/core/gestureRangeOwnership.d.ts +13 -0
  11. package/dist/core/gestureRangeOwnership.d.ts.map +1 -0
  12. package/dist/core/liveChartEngineTick.d.ts +33 -1
  13. package/dist/core/liveChartEngineTick.d.ts.map +1 -1
  14. package/dist/core/resolveConfig.d.ts +39 -2
  15. package/dist/core/resolveConfig.d.ts.map +1 -1
  16. package/dist/core/useLiveChartEngine.d.ts +35 -2
  17. package/dist/core/useLiveChartEngine.d.ts.map +1 -1
  18. package/dist/draw/line.d.ts.map +1 -1
  19. package/dist/hooks/resetButtonLayout.d.ts +24 -0
  20. package/dist/hooks/resetButtonLayout.d.ts.map +1 -0
  21. package/dist/hooks/resetView.d.ts +31 -0
  22. package/dist/hooks/resetView.d.ts.map +1 -0
  23. package/dist/hooks/useBadge.d.ts.map +1 -1
  24. package/dist/hooks/useChartPaths.d.ts +3 -1
  25. package/dist/hooks/useChartPaths.d.ts.map +1 -1
  26. package/dist/hooks/useDemandFrameLoop.d.ts.map +1 -1
  27. package/dist/hooks/useLiveDot.d.ts +5 -2
  28. package/dist/hooks/useLiveDot.d.ts.map +1 -1
  29. package/dist/hooks/usePanScroll.d.ts +39 -3
  30. package/dist/hooks/usePanScroll.d.ts.map +1 -1
  31. package/dist/hooks/useReferenceLine.d.ts.map +1 -1
  32. package/dist/hooks/useResetButton.d.ts +41 -0
  33. package/dist/hooks/useResetButton.d.ts.map +1 -0
  34. package/dist/hooks/useYAxisScaleGesture.d.ts +14 -1
  35. package/dist/hooks/useYAxisScaleGesture.d.ts.map +1 -1
  36. package/dist/index.d.ts +1 -1
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/math/squiggly.d.ts +12 -0
  39. package/dist/math/squiggly.d.ts.map +1 -1
  40. package/dist/types.d.ts +167 -11
  41. package/dist/types.d.ts.map +1 -1
  42. package/package.json +2 -2
  43. package/src/components/LiveChart.tsx +75 -4
  44. package/src/components/LiveChartSeries.tsx +9 -1
  45. package/src/components/LoadingOverlay.tsx +44 -6
  46. package/src/components/ResetButtonOverlay.tsx +129 -0
  47. package/src/constants.ts +7 -0
  48. package/src/core/gestureRangeOwnership.ts +46 -0
  49. package/src/core/liveChartEngineTick.ts +103 -2
  50. package/src/core/resolveConfig.ts +88 -2
  51. package/src/core/useLiveChartEngine.ts +122 -4
  52. package/src/draw/line.ts +13 -5
  53. package/src/hooks/resetButtonLayout.ts +80 -0
  54. package/src/hooks/resetView.ts +92 -0
  55. package/src/hooks/useBadge.ts +18 -5
  56. package/src/hooks/useChartPaths.ts +6 -4
  57. package/src/hooks/useDemandFrameLoop.ts +7 -2
  58. package/src/hooks/useLiveDot.ts +26 -11
  59. package/src/hooks/usePanScroll.ts +177 -5
  60. package/src/hooks/useReferenceLine.ts +4 -1
  61. package/src/hooks/useResetButton.ts +181 -0
  62. package/src/hooks/useYAxisScaleGesture.ts +70 -13
  63. package/src/index.ts +3 -0
  64. package/src/math/squiggly.ts +27 -0
  65. package/src/types.ts +170 -11
@@ -1,4 +1,4 @@
1
- import { useMemo } from "react";
1
+ import { useMemo, useState } from "react";
2
2
  import { Gesture } from "react-native-gesture-handler";
3
3
  import {
4
4
  cancelAnimation,
@@ -8,8 +8,12 @@ import {
8
8
  withDecay,
9
9
  type SharedValue,
10
10
  } from "react-native-reanimated";
11
+ import { scheduleOnRN } from "react-native-worklets";
11
12
 
13
+ import type { ResolvedPriceScrollConfig } from "../core/resolveConfig";
14
+ import { writeGestureRange } from "../core/gestureRangeOwnership";
12
15
  import type { ChartPadding } from "../draw/line";
16
+ import type { ChartScale } from "../types";
13
17
 
14
18
  /** Minimum height (px) of the bottom "grab the time ruler" band in axis-drag mode. */
15
19
  export const AXIS_GRAB_MIN_PX = 44;
@@ -35,6 +39,17 @@ export interface PanScrollEngineRefs {
35
39
  canvasWidth: SharedValue<number>;
36
40
  /** Canvas height in px (for the axis-drag band hit-test). */
37
41
  canvasHeight: SharedValue<number>;
42
+ /** Price-pan state (see `priceScroll`). All or none; omitted on engines without it. */
43
+ viewRange?: SharedValue<Pick<ChartScale, "min" | "max"> | null>;
44
+ viewRangeGestureOwned?: SharedValue<boolean>;
45
+ /** Eased display range, snapshotted into `viewRange` when a price pan begins. */
46
+ displayMin?: SharedValue<number>;
47
+ displayMax?: SharedValue<number>;
48
+ /** Fitted range the pan may travel `maxOvershoot` spans past. */
49
+ fitMin?: SharedValue<number>;
50
+ fitMax?: SharedValue<number>;
51
+ /** Pinch-zoom override; a non-null width arms an `"afterScale"` price pan. */
52
+ viewWindow?: SharedValue<number | null>;
38
53
  }
39
54
 
40
55
  export interface UsePanScrollOptions {
@@ -93,6 +108,14 @@ export interface UsePanScrollOptions {
93
108
  fling?: boolean;
94
109
  /** Re-clamp parked state on config changes. False for an app-owned viewport. Default true. */
95
110
  clampOnOverscrollChange?: boolean;
111
+ /**
112
+ * Vertical price panning (`holdToScrub` mode only). Resolved from
113
+ * `priceScroll`; `null` keeps the classic horizontal-only pan that fails on
114
+ * vertical travel so a parent vertical scroll wins.
115
+ */
116
+ priceScroll?: ResolvedPriceScrollConfig | null;
117
+ /** Manual Y-range scale; `!== 1` arms an `"afterScale"` price pan. */
118
+ yRangeScale?: SharedValue<number>;
96
119
  }
97
120
 
98
121
  /**
@@ -206,12 +229,58 @@ export function axisBandTop(canvasHeight: number, padBottom: number): number {
206
229
  return canvasHeight - Math.max(padBottom, AXIS_GRAB_MIN_PX);
207
230
  }
208
231
 
232
+ /**
233
+ * Whether a vertical drag pans price. Mirrored into React state between
234
+ * gestures so the pan's native activation offsets claim vertical travel only
235
+ * while armed; an untouched chart still hands vertical drags to a parent
236
+ * ScrollView.
237
+ */
238
+ export function isPriceScrollArmed(
239
+ activation: "always" | "afterScale",
240
+ yRangeScale: number,
241
+ viewWindow: number | null,
242
+ hasGestureRange: boolean,
243
+ ): boolean {
244
+ "worklet";
245
+ if (activation === "always") return true;
246
+ return yRangeScale !== 1 || viewWindow !== null || hasGestureRange;
247
+ }
248
+
249
+ /**
250
+ * Next price bounds after dragging `changeY` px (drag down ⇒ the plot content
251
+ * moves down ⇒ higher prices come into view ⇒ both bounds rise), clamped so
252
+ * neither bound travels more than `maxOvershoot` spans past the fitted range.
253
+ * Returns `cur` unchanged when the span or plot height is degenerate.
254
+ */
255
+ export function nextPriceRange(
256
+ cur: Pick<ChartScale, "min" | "max">,
257
+ changeY: number,
258
+ plotHeight: number,
259
+ fitMin: number,
260
+ fitMax: number,
261
+ maxOvershoot: number,
262
+ ): Pick<ChartScale, "min" | "max"> {
263
+ "worklet";
264
+ const span = cur.max - cur.min;
265
+ if (!(span > 0) || !(plotHeight > 0) || !Number.isFinite(changeY)) return cur;
266
+ let shift = (changeY / plotHeight) * span;
267
+ if (Number.isFinite(fitMin) && Number.isFinite(fitMax) && fitMin < fitMax) {
268
+ const allowance = span * maxOvershoot;
269
+ const lo = fitMin - allowance;
270
+ const hi = fitMax + allowance;
271
+ if (cur.min + shift < lo) shift = lo - cur.min;
272
+ if (cur.max + shift > hi) shift = hi - cur.max;
273
+ }
274
+ return { min: cur.min + shift, max: cur.max + shift };
275
+ }
276
+
209
277
  /**
210
278
  * Horizontal pan that scrolls the chart back through time. Pick the activation
211
279
  * with `mode`:
212
280
  * - `"holdToScrub"` — a one-finger drag anywhere (activates on horizontal
213
- * travel; vertical falls through). Scrub must be press-and-hold so a quick
214
- * drag races past it (compose with `Gesture.Race`).
281
+ * travel; vertical falls through unless `priceScroll` is armed, then it
282
+ * pans price). Scrub must be press-and-hold so a quick drag races past it
283
+ * (compose with `Gesture.Race`).
215
284
  * - `"axisDrag"` — a one-finger drag starting in the bottom X-axis band, gated
216
285
  * via `manualActivation` (compose with `Gesture.Exclusive`, this gesture
217
286
  * first: outside the band it fails instantly so scrub runs).
@@ -232,17 +301,59 @@ export function usePanScroll({
232
301
  overscroll = 0,
233
302
  fling = true,
234
303
  clampOnOverscrollChange = true,
304
+ priceScroll = null,
305
+ yRangeScale,
235
306
  }: UsePanScrollOptions): ReturnType<typeof Gesture.Pan> {
236
- const { viewEnd, liveEdge, displayWindow, canvasWidth, canvasHeight } = engine;
307
+ const {
308
+ viewEnd,
309
+ liveEdge,
310
+ displayWindow,
311
+ canvasWidth,
312
+ canvasHeight,
313
+ viewRange,
314
+ viewRangeGestureOwned,
315
+ displayMin,
316
+ displayMax,
317
+ fitMin,
318
+ fitMax,
319
+ viewWindow,
320
+ } = engine;
237
321
  const padLeft = padding.left;
238
322
  const padRight = padding.right;
323
+ const padTop = padding.top;
239
324
  const padBottom = padding.bottom;
325
+ const priceActivation = priceScroll?.activation ?? null;
326
+ const maxOvershoot = priceScroll?.maxOvershoot ?? 1;
240
327
 
241
328
  // Axis-drag activation tracking (manualActivation). Created unconditionally so
242
329
  // the hook order is stable; unused in hold-to-scrub mode.
243
330
  const startX = useSharedValue(0);
244
331
  const startY = useSharedValue(0);
245
332
  const armed = useSharedValue(false);
333
+ // Set once an armed drag has shown vertical intent; only then does the pan
334
+ // take the price range, so a horizontal scroll keeps the Y auto-fit.
335
+ const priceEngaged = useSharedValue(false);
336
+ // Price-pan arming (holdToScrub + priceScroll). RNGH evaluates activation
337
+ // offsets natively per recognizer, so the arming is mirrored into React
338
+ // state and the pan is rebuilt with a vertical active offset while armed.
339
+ // It flips between gestures (after a scale, pinch, pan or reset), never
340
+ // mid-drag, and native activation beats a parent ScrollView's own slop the
341
+ // same way the horizontal offset does.
342
+ const [priceArmed, setPriceArmed] = useState(priceActivation === "always");
343
+ useAnimatedReaction(
344
+ () =>
345
+ priceActivation !== null &&
346
+ isPriceScrollArmed(
347
+ priceActivation,
348
+ yRangeScale?.get() ?? 1,
349
+ viewWindow?.get() ?? null,
350
+ (viewRangeGestureOwned?.get() ?? false) && (viewRange?.get() ?? null) !== null,
351
+ ),
352
+ /* istanbul ignore next -- UI-thread reaction; the pure predicate is unit-tested */
353
+ (next, prev) => {
354
+ if (next !== prev) scheduleOnRN(setPriceArmed, next);
355
+ },
356
+ );
246
357
  const overscrollSV = useDerivedValue(() => overscroll);
247
358
 
248
359
  // A runtime config change (the demo exposes Off / 50% / 90%) applies to an
@@ -298,9 +409,19 @@ export function usePanScroll({
298
409
 
299
410
  const onChange =
300
411
  /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
301
- (e: { changeX: number }) => {
412
+ (e: { changeX: number; changeY: number; translationY: number }) => {
302
413
  "worklet";
303
414
  if (scrubActive?.get()) return;
415
+ // Vertical travel engages the price pan at any point of the drag, so a
416
+ // drag that starts horizontal and turns diagonal moves both axes while
417
+ // a purely horizontal scroll keeps the Y auto-fit.
418
+ if (
419
+ priceArmed &&
420
+ !priceEngaged.get() &&
421
+ Math.abs(e.translationY) > AXIS_ACTIVATE_PX
422
+ ) {
423
+ priceEngaged.set(true);
424
+ }
304
425
  const win = displayWindow.get();
305
426
  const chartW = canvasWidth.get() - padLeft - padRight;
306
427
  if (chartW <= 0) return;
@@ -308,6 +429,27 @@ export function usePanScroll({
308
429
  const cur = viewEnd.get() ?? edge;
309
430
  const lo = panLowerBound(minTime.get(), win, edge, overscroll);
310
431
  viewEnd.set(nextViewEnd(cur, e.changeX, chartW, win, edge, lo, overscroll));
432
+ if (priceEngaged.get() && viewRange && viewRangeGestureOwned && displayMin && displayMax) {
433
+ let range = viewRange.get();
434
+ if (!(viewRangeGestureOwned.get() && range !== null)) {
435
+ // Freeze the auto-fit into an explicit range the moment the drag turns
436
+ // vertical, so the drag moves a fixed window; a range an earlier pan
437
+ // left behind carries on from where it is.
438
+ range = { min: displayMin.get(), max: displayMax.get() };
439
+ }
440
+ writeGestureRange(
441
+ viewRange,
442
+ viewRangeGestureOwned,
443
+ nextPriceRange(
444
+ range,
445
+ e.changeY,
446
+ canvasHeight.get() - padTop - padBottom,
447
+ fitMin?.get() ?? NaN,
448
+ fitMax?.get() ?? NaN,
449
+ maxOvershoot,
450
+ ),
451
+ );
452
+ }
311
453
  };
312
454
 
313
455
  const onEnd =
@@ -351,6 +493,7 @@ export function usePanScroll({
351
493
  () => {
352
494
  "worklet";
353
495
  scrollActive?.set(false);
496
+ priceEngaged.set(false);
354
497
  };
355
498
 
356
499
  if (mode === "axisDrag") {
@@ -398,6 +541,22 @@ export function usePanScroll({
398
541
  .onFinalize(onFinalize);
399
542
  }
400
543
 
544
+ if (priceActivation !== null && priceArmed) {
545
+ // holdToScrub + armed priceScroll: vertical travel activates the pan too,
546
+ // so a vertical drag pans price (a parent vertical scroll never sees it)
547
+ // and a diagonal drag moves both axes. Unarmed charts use the classic pan
548
+ // below, which fails on vertical travel.
549
+ return Gesture.Pan()
550
+ .enabled(enabled)
551
+ .maxPointers(1)
552
+ .activeOffsetX([-AXIS_ACTIVATE_PX, AXIS_ACTIVATE_PX])
553
+ .activeOffsetY([-AXIS_ACTIVATE_PX, AXIS_ACTIVATE_PX])
554
+ .onStart(onStart)
555
+ .onChange(onChange)
556
+ .onEnd(onEnd)
557
+ .onFinalize(onFinalize);
558
+ }
559
+
401
560
  // holdToScrub (default): a one-finger drag anywhere scrolls. Activate on
402
561
  // horizontal travel so a quick one-finger drag scrolls; a still press-hold
403
562
  // crosses no offset and falls through to the scrub gesture (which owns the
@@ -415,10 +574,15 @@ export function usePanScroll({
415
574
  armed,
416
575
  canvasHeight,
417
576
  canvasWidth,
577
+ displayMax,
578
+ displayMin,
418
579
  displayWindow,
419
580
  enabled,
581
+ fitMax,
582
+ fitMin,
420
583
  fling,
421
584
  liveEdge,
585
+ maxOvershoot,
422
586
  minTime,
423
587
  mode,
424
588
  onScrollStart,
@@ -426,11 +590,19 @@ export function usePanScroll({
426
590
  padBottom,
427
591
  padLeft,
428
592
  padRight,
593
+ padTop,
594
+ priceActivation,
595
+ priceArmed,
596
+ priceEngaged,
429
597
  scrollActive,
430
598
  scrubActive,
431
599
  startX,
432
600
  startY,
433
601
  viewEnd,
602
+ viewRange,
603
+ viewRangeGestureOwned,
604
+ viewWindow,
434
605
  wakeEngine,
606
+ yRangeScale,
435
607
  ]);
436
608
  }
@@ -613,7 +613,10 @@ export function computeReferenceLineLayout(
613
613
  const x = bounds.left + (cfg.offsetX ?? 0);
614
614
  const y = base.y + (cfg.offsetY ?? 0);
615
615
  let textX = x + (pillW - contentW) / 2 + chevronW;
616
- // Match live-badge text alignment when its column mode is enabled.
616
+ // Match live-badge text alignment when its column mode is enabled. The
617
+ // price pill has no tail to give its left padding to, so it keeps both
618
+ // paddings and centers a value that doesn't fit them (a tight live badge
619
+ // aligns instead).
617
620
  if (
618
621
  cfg.position === "axis" &&
619
622
  valueAxis?.alignTextWithYAxis &&
@@ -0,0 +1,181 @@
1
+ import { useCallback, useMemo } from "react";
2
+ import { Gesture, type GestureType } from "react-native-gesture-handler";
3
+ import {
4
+ useDerivedValue,
5
+ useSharedValue,
6
+ type SharedValue,
7
+ } from "react-native-reanimated";
8
+ import { scheduleOnRN, scheduleOnUI } from "react-native-worklets";
9
+
10
+ import type { ResolvedResetButtonConfig } from "../core/resolveConfig";
11
+ import type {
12
+ ChartEngineLayout,
13
+ ChartEngineRange,
14
+ ChartEngineReturnToLive,
15
+ ChartEngineScroll,
16
+ } from "../core/useLiveChartEngine";
17
+ import type { ChartPadding } from "../draw/line";
18
+ import {
19
+ isResetButtonHit,
20
+ isViewOffFitted,
21
+ resetButtonRect,
22
+ type ResetButtonRect,
23
+ } from "./resetButtonLayout";
24
+ import { resetChartView, type ResetViewRefs } from "./resetView";
25
+
26
+ /** Travel (px) and hold (ms) that still count as a tap on the button. */
27
+ const TAP_MAX_DISTANCE_PX = 12;
28
+ const TAP_MAX_DURATION_MS = 400;
29
+
30
+ interface Options {
31
+ /** `null` hides the button; `resetView()` on the handle still works. */
32
+ config: ResolvedResetButtonConfig | null;
33
+ engine: Pick<ChartEngineLayout, "canvasWidth" | "canvasHeight"> &
34
+ ChartEngineScroll &
35
+ ChartEngineRange &
36
+ ChartEngineReturnToLive & { wake?: () => void };
37
+ padding: ChartPadding;
38
+ yRangeScale: SharedValue<number>;
39
+ /** The button hides while the crosshair owns the touch. */
40
+ scrubActive: SharedValue<boolean>;
41
+ /** Resolved `timeScroll.returnToLive` duration (ms). */
42
+ returnToLiveMs: number;
43
+ onResetView?: () => void;
44
+ }
45
+
46
+ export interface ResetButtonState {
47
+ /** True while the view differs from the fitted live view (and no scrub). */
48
+ visible: SharedValue<boolean>;
49
+ /** Canvas-px rect the overlay positions itself at and the tap hit-tests. */
50
+ rect: SharedValue<ResetButtonRect>;
51
+ /** Written by the overlay's `onLayout`, so a custom visual of any size works. */
52
+ measuredWidth: SharedValue<number>;
53
+ measuredHeight: SharedValue<number>;
54
+ /** Tap on the button, or `null` when the button is off. Compose ahead of the plot graph. */
55
+ tapGesture: GestureType | null;
56
+ /** JS-thread reset (the handle's `resetView()` and accessibility activation). */
57
+ reset: () => void;
58
+ }
59
+
60
+ /**
61
+ * Visibility, placement and the tap of the floating reset-view button. The
62
+ * tap claims only touches that land on the visible button and fails everywhere
63
+ * else, so composed `Exclusive` ahead of the plot gestures it never delays them.
64
+ */
65
+ export function useResetButton({
66
+ config,
67
+ engine,
68
+ padding,
69
+ yRangeScale,
70
+ scrubActive,
71
+ returnToLiveMs,
72
+ onResetView,
73
+ }: Options): ResetButtonState {
74
+ const {
75
+ viewEnd,
76
+ viewWindow,
77
+ viewRange,
78
+ viewRangeGestureOwned,
79
+ canvasWidth,
80
+ canvasHeight,
81
+ returnT,
82
+ returnFrom,
83
+ wake,
84
+ } = engine;
85
+ const { left, right, top, bottom } = padding;
86
+ const enabled = config !== null;
87
+ const placement = config?.placement ?? "bottom-center";
88
+ const margin = config?.margin ?? 0;
89
+ const showYScale = config?.showFor.yScale ?? true;
90
+ const showPriceScroll = config?.showFor.priceScroll ?? true;
91
+ const showTimeScroll = config?.showFor.timeScroll ?? true;
92
+ const showZoom = config?.showFor.zoom ?? true;
93
+
94
+ const measuredWidth = useSharedValue(0);
95
+ const measuredHeight = useSharedValue(0);
96
+
97
+ const visible = useDerivedValue(() => {
98
+ if (!enabled || scrubActive.get()) return false;
99
+ return isViewOffFitted(
100
+ viewEnd.get(),
101
+ viewWindow.get(),
102
+ yRangeScale.get(),
103
+ viewRangeGestureOwned.get() && viewRange.get() !== null,
104
+ showTimeScroll,
105
+ showZoom,
106
+ showYScale,
107
+ showPriceScroll,
108
+ );
109
+ });
110
+
111
+ const rect = useDerivedValue(() =>
112
+ resetButtonRect(
113
+ canvasWidth.get(),
114
+ canvasHeight.get(),
115
+ { left, right, top, bottom },
116
+ placement,
117
+ margin,
118
+ measuredWidth.get(),
119
+ measuredHeight.get(),
120
+ ),
121
+ );
122
+
123
+ const refs = useMemo<ResetViewRefs>(
124
+ () => ({
125
+ viewEnd,
126
+ viewWindow,
127
+ viewRange,
128
+ viewRangeGestureOwned,
129
+ yRangeScale,
130
+ returnT,
131
+ returnFrom,
132
+ returnToLiveMs,
133
+ }),
134
+ [
135
+ returnFrom,
136
+ returnT,
137
+ returnToLiveMs,
138
+ viewEnd,
139
+ viewRange,
140
+ viewRangeGestureOwned,
141
+ viewWindow,
142
+ yRangeScale,
143
+ ],
144
+ );
145
+
146
+ const resetOnUI = useCallback(() => {
147
+ "worklet";
148
+ wake?.();
149
+ resetChartView(refs);
150
+ if (onResetView) scheduleOnRN(onResetView);
151
+ }, [onResetView, refs, wake]);
152
+ const reset = useCallback(() => {
153
+ scheduleOnUI(resetOnUI);
154
+ }, [resetOnUI]);
155
+
156
+ const tapGesture = useMemo(() => {
157
+ if (!enabled) return null;
158
+ return Gesture.Tap()
159
+ .maxDistance(TAP_MAX_DISTANCE_PX)
160
+ .maxDuration(TAP_MAX_DURATION_MS)
161
+ .onTouchesDown((event, manager) => {
162
+ "worklet";
163
+ const touch = event.allTouches[0];
164
+ if (
165
+ event.numberOfTouches !== 1 ||
166
+ !touch ||
167
+ !visible.get() ||
168
+ !isResetButtonHit(touch.x, touch.y, rect.get())
169
+ ) {
170
+ manager.fail();
171
+ }
172
+ })
173
+ .onEnd((_event, success) => {
174
+ "worklet";
175
+ if (!success) return;
176
+ resetOnUI();
177
+ });
178
+ }, [enabled, rect, resetOnUI, visible]);
179
+
180
+ return { visible, rect, measuredWidth, measuredHeight, tapGesture, reset };
181
+ }
@@ -12,9 +12,11 @@ import {
12
12
  type SharedValue,
13
13
  } from "react-native-reanimated";
14
14
  import type { ResolvedYAxisConfig } from "../core/resolveConfig";
15
+ import { writeGestureRange } from "../core/gestureRangeOwnership";
15
16
  import type { ChartEngineLayout } from "../core/useLiveChartEngine";
16
17
  import { rightAnchoredYAxisColumnLayout, type YAxisEntry } from "../draw/grid";
17
18
  import type { ChartPadding } from "../draw/line";
19
+ import type { ChartScale } from "../types";
18
20
 
19
21
  /** Preserve private scale state across gesture/axis toggles on a mounted chart. */
20
22
  export function useYAxisScaleValue(external?: SharedValue<number>) {
@@ -81,6 +83,24 @@ export function draggedYAxisScale(
81
83
  );
82
84
  }
83
85
 
86
+ /**
87
+ * Scale explicit price bounds about their midpoint by `factor` (> 1 widens).
88
+ * Returns `range` unchanged when the result would be degenerate or overflow.
89
+ */
90
+ export function scaledPriceRange(
91
+ range: Pick<ChartScale, "min" | "max">,
92
+ factor: number,
93
+ ): Pick<ChartScale, "min" | "max"> {
94
+ "worklet";
95
+ const mid = range.min / 2 + range.max / 2;
96
+ const half = ((range.max - range.min) / 2) * factor;
97
+ const min = mid - half;
98
+ const max = mid + half;
99
+ return Number.isFinite(min) && Number.isFinite(max) && min < max
100
+ ? { min, max }
101
+ : range;
102
+ }
103
+
84
104
  type Options = {
85
105
  engine: Pick<ChartEngineLayout, "canvasWidth" | "canvasHeight"> & {
86
106
  wake?: () => void;
@@ -97,6 +117,13 @@ type Options = {
97
117
  deferHit?: (x: number, y: number) => boolean;
98
118
  onStart?: () => void;
99
119
  active?: SharedValue<boolean>;
120
+ /**
121
+ * Explicit price bounds (see `priceScroll`). While gesture-owned, a drag
122
+ * scales these about their midpoint instead of `scale`, and a double-tap
123
+ * clears them along with the scale.
124
+ */
125
+ range?: SharedValue<Pick<ChartScale, "min" | "max"> | null>;
126
+ rangeGestureOwned?: SharedValue<boolean>;
100
127
  };
101
128
 
102
129
  /** Owns gutter touches at touch-down; fails immediately everywhere else. */
@@ -112,8 +139,12 @@ export function useYAxisScaleGesture({
112
139
  deferHit,
113
140
  onStart,
114
141
  active,
142
+ range,
143
+ rangeGestureOwned,
115
144
  }: Options) {
116
145
  const startScale = useSharedValue(1);
146
+ // Gesture-owned bounds at touch-down; null scales `scale` instead.
147
+ const startRange = useSharedValue<Pick<ChartScale, "min" | "max"> | null>(null);
117
148
  const startY = useSharedValue(0);
118
149
  const armed = useSharedValue(false);
119
150
  const { canvasWidth, canvasHeight, wake } = engine;
@@ -164,6 +195,7 @@ export function useYAxisScaleGesture({
164
195
  // the drag here so the first move cannot reuse a previous drag's scale.
165
196
  cancelAnimation(scale);
166
197
  startScale.set(scale.get());
198
+ startRange.set(range && rangeGestureOwned?.get() ? range.get() : null);
167
199
  startY.set(touch.y);
168
200
  armed.set(true);
169
201
  active?.set(true);
@@ -180,15 +212,29 @@ export function useYAxisScaleGesture({
180
212
  "worklet";
181
213
  const touch = event.allTouches[0];
182
214
  if (!armed.get() || !touch) return;
183
- scale.set(
184
- draggedYAxisScale(
185
- startScale.get(),
186
- touch.y - startY.get(),
187
- dragDistance,
188
- minScale,
189
- maxScale,
190
- ),
215
+ const fromScale = startScale.get();
216
+ const nextScale = draggedYAxisScale(
217
+ fromScale,
218
+ touch.y - startY.get(),
219
+ dragDistance,
220
+ minScale,
221
+ maxScale,
191
222
  );
223
+ const fromRange = startRange.get();
224
+ if (fromRange !== null && range && rangeGestureOwned?.get()) {
225
+ // A panned window scales in place by the same step the absolute scale
226
+ // takes, so repeated drags stay within [minScale, maxScale] overall
227
+ // instead of compounding. The tick ignores `scale` while the range is
228
+ // applied, so updating it only keeps that running total.
229
+ const validFrom =
230
+ Number.isFinite(fromScale) && fromScale > 0 ? fromScale : 1;
231
+ writeGestureRange(
232
+ range,
233
+ rangeGestureOwned,
234
+ scaledPriceRange(fromRange, nextScale / validFrom),
235
+ );
236
+ }
237
+ scale.set(nextScale);
192
238
  })
193
239
  .onTouchesUp((_event, manager) => {
194
240
  "worklet";
@@ -222,11 +268,19 @@ export function useYAxisScaleGesture({
222
268
  if (!success) return;
223
269
  wake?.();
224
270
  cancelAnimation(scale);
225
- scale.set(
226
- withTiming(Math.max(minScale, Math.min(maxScale, 1)), {
227
- duration: 240,
228
- }),
229
- );
271
+ if (range && rangeGestureOwned?.get()) {
272
+ // Land the scale in one step so the display range eases from the
273
+ // panned window to the fit (see `resetChartView`).
274
+ range.set(null);
275
+ rangeGestureOwned.set(false);
276
+ scale.set(Math.max(minScale, Math.min(maxScale, 1)));
277
+ } else {
278
+ scale.set(
279
+ withTiming(Math.max(minScale, Math.min(maxScale, 1)), {
280
+ duration: 240,
281
+ }),
282
+ );
283
+ }
230
284
  onStart?.();
231
285
  });
232
286
  return Gesture.Simultaneous(pan, reset);
@@ -245,6 +299,9 @@ export function useYAxisScaleGesture({
245
299
  font,
246
300
  deferHit,
247
301
  scale,
302
+ range,
303
+ rangeGestureOwned,
304
+ startRange,
248
305
  startScale,
249
306
  startY,
250
307
  armed,
package/src/index.ts CHANGED
@@ -100,6 +100,7 @@ export type {
100
100
  MotionMetrics,
101
101
  MultiSeriesDotConfig,
102
102
  PerSeriesTooltipConfig,
103
+ PriceScrollConfig,
103
104
  PulseConfig,
104
105
  RangeAnimationConfig,
105
106
  SeriesRangeAnimationConfig,
@@ -108,6 +109,8 @@ export type {
108
109
  ReferenceLineValueBadgeConfig,
109
110
  ReferenceLineGroupingConfig,
110
111
  ReferenceLineRenderProps,
112
+ ResetButtonConfig,
113
+ ResetButtonRenderProps,
111
114
  ReturnToLiveConfig,
112
115
  ScrubActionConfig,
113
116
  ScrubActionPoint,
@@ -1,3 +1,4 @@
1
+ import { MAX_FRAME_DELTA_MS } from "../constants";
1
2
  import type { ChartPadding } from "../draw/line";
2
3
 
3
4
  /**
@@ -39,6 +40,32 @@ export function squiggleClockSeconds(): number {
39
40
  return Date.now() / 1000;
40
41
  }
41
42
 
43
+ /**
44
+ * The squiggle's animated time (ms) after one more frame, toward
45
+ * `LoadingConfig.freezeAfterMs`. A frame counts its delta clamped to
46
+ * 0–{@link MAX_FRAME_DELTA_MS}, so the first frame back from the background (or
47
+ * after a stall) counts as one frame, not as the time away.
48
+ */
49
+ export function squiggleShownMsAfterFrame(
50
+ shownMs: number,
51
+ frameMs: number,
52
+ ): number {
53
+ "worklet";
54
+ return shownMs + Math.min(Math.max(frameMs, 0), MAX_FRAME_DELTA_MS);
55
+ }
56
+
57
+ /**
58
+ * Whether the squiggle has used up `freezeAfterMs` and is frozen. `undefined`
59
+ * never freezes; `0` or less freezes from the start.
60
+ */
61
+ export function squiggleFrozen(
62
+ shownMs: number,
63
+ freezeAfterMs: number | undefined,
64
+ ): boolean {
65
+ "worklet";
66
+ return freezeAfterMs !== undefined && shownMs >= freezeAfterMs;
67
+ }
68
+
42
69
  /**
43
70
  * Build a flat [x0,y0,x1,y1,...] point array for a standalone squiggly line
44
71
  * spanning the full chart area (used in loading / empty state).