react-native-livechart 4.29.0 → 4.30.1

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 (50) hide show
  1. package/dist/components/LiveChart.d.ts.map +1 -1
  2. package/dist/components/LiveChartSeries.d.ts.map +1 -1
  3. package/dist/components/ResetButtonOverlay.d.ts +25 -0
  4. package/dist/components/ResetButtonOverlay.d.ts.map +1 -0
  5. package/dist/core/gestureRangeOwnership.d.ts +13 -0
  6. package/dist/core/gestureRangeOwnership.d.ts.map +1 -0
  7. package/dist/core/liveChartEngineTick.d.ts +30 -0
  8. package/dist/core/liveChartEngineTick.d.ts.map +1 -1
  9. package/dist/core/resolveConfig.d.ts +35 -1
  10. package/dist/core/resolveConfig.d.ts.map +1 -1
  11. package/dist/core/useLiveChartEngine.d.ts +34 -3
  12. package/dist/core/useLiveChartEngine.d.ts.map +1 -1
  13. package/dist/hooks/crosshairShared.d.ts +2 -2
  14. package/dist/hooks/crosshairShared.d.ts.map +1 -1
  15. package/dist/hooks/resetButtonLayout.d.ts +24 -0
  16. package/dist/hooks/resetButtonLayout.d.ts.map +1 -0
  17. package/dist/hooks/resetView.d.ts +31 -0
  18. package/dist/hooks/resetView.d.ts.map +1 -0
  19. package/dist/hooks/useCrosshair.d.ts +3 -1
  20. package/dist/hooks/useCrosshair.d.ts.map +1 -1
  21. package/dist/hooks/useLiveDot.d.ts +6 -2
  22. package/dist/hooks/useLiveDot.d.ts.map +1 -1
  23. package/dist/hooks/usePanScroll.d.ts +39 -3
  24. package/dist/hooks/usePanScroll.d.ts.map +1 -1
  25. package/dist/hooks/useResetButton.d.ts +41 -0
  26. package/dist/hooks/useResetButton.d.ts.map +1 -0
  27. package/dist/hooks/useYAxisScaleGesture.d.ts +14 -1
  28. package/dist/hooks/useYAxisScaleGesture.d.ts.map +1 -1
  29. package/dist/index.d.ts +1 -1
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/types.d.ts +138 -5
  32. package/dist/types.d.ts.map +1 -1
  33. package/package.json +1 -1
  34. package/src/components/LiveChart.tsx +67 -6
  35. package/src/components/LiveChartSeries.tsx +6 -1
  36. package/src/components/ResetButtonOverlay.tsx +129 -0
  37. package/src/core/gestureRangeOwnership.ts +46 -0
  38. package/src/core/liveChartEngineTick.ts +84 -4
  39. package/src/core/resolveConfig.ts +76 -0
  40. package/src/core/useLiveChartEngine.ts +191 -22
  41. package/src/hooks/crosshairShared.ts +2 -2
  42. package/src/hooks/resetButtonLayout.ts +80 -0
  43. package/src/hooks/resetView.ts +92 -0
  44. package/src/hooks/useCrosshair.ts +50 -19
  45. package/src/hooks/useLiveDot.ts +41 -4
  46. package/src/hooks/usePanScroll.ts +177 -5
  47. package/src/hooks/useResetButton.ts +181 -0
  48. package/src/hooks/useYAxisScaleGesture.ts +70 -13
  49. package/src/index.ts +3 -0
  50. package/src/types.ts +141 -5
@@ -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,
package/src/types.ts CHANGED
@@ -776,7 +776,9 @@ export interface BadgeConfig extends BadgeStyleConfig {
776
776
  * indicators — the badge, the value line, and the live dot — to the price at
777
777
  * the visible window's right edge instead of the live price, so they track the
778
778
  * last visible price as you pan. The badge's momentum color is derived at that
779
- * same historical edge, so incoming live ticks do not recolor it. Default
779
+ * same historical edge, so incoming live ticks do not recolor it. Buffered or
780
+ * controlled lines track the latest recorded point while it is inside the
781
+ * visible time window, and follow the edge only after it leaves. Default
780
782
  * `false`.
781
783
  *
782
784
  * @experimental
@@ -2315,6 +2317,16 @@ export interface LiveChartHandle {
2315
2317
  * ```
2316
2318
  */
2317
2319
  resetZoom(): void;
2320
+ /**
2321
+ * Return to the fitted live view: {@link resetZoom} plus `yRangeScale` → 1 and
2322
+ * a gesture-owned price range cleared, gliding the right edge back to live
2323
+ * with the `timeScroll.returnToLive` timing. What the `resetButton` runs.
2324
+ * Safe to call when already fitted. On LiveChartSeries, which has no price
2325
+ * pan, it is `resetZoom()` plus `yRangeScale` → 1.
2326
+ *
2327
+ * @experimental
2328
+ */
2329
+ resetView(): void;
2318
2330
  }
2319
2331
 
2320
2332
  /**
@@ -2710,7 +2722,11 @@ export interface LiveChartCoreProps {
2710
2722
  * current time so the latest point has breathing room; pass `0` to land the latest
2711
2723
  * point exactly at the right edge. In line-mode LiveChart, a positive buffer
2712
2724
  * stops the line/dot at the last recorded point; the badge and value line use
2713
- * that recorded price. badge.followViewEdge retains its edge-pinned indicators.
2725
+ * that recorded price. Plain scrub also clamps to that latest recorded point.
2726
+ * If it leaves the window to the left, the scrub hides and onScrub clears
2727
+ * until recorded data re-enters the window.
2728
+ * badge.followViewEdge pins indicators to the edge only when the latest
2729
+ * recorded point leaves the visible time window.
2714
2730
  * Default `0`.
2715
2731
  */
2716
2732
  windowBuffer?: number;
@@ -2831,8 +2847,11 @@ export interface ChartViewportControl {
2831
2847
  * future space. Both bounds and their difference must be finite, with min < max.
2832
2848
  * Applied immediately, overriding automatic fitting, range easing, nonNegative,
2833
2849
  * maxValue, minRange, and yRangeScale. Invalid/null/omitted bounds use auto-fit.
2834
- * Ignored while end is null (including after resetZoom()); gestures and resets
2835
- * do not write this optional shared value.
2850
+ * App-written bounds are ignored while end is null (including after resetZoom()).
2851
+ * With priceScroll, gestures write this range too: gesture-owned bounds stay
2852
+ * active while following live and obey the chart's hard bounds. A later app
2853
+ * write or replacement range SharedValue restores the app-owned contract;
2854
+ * resetView(), axis double-tap and framing changes preserve app-written bounds.
2836
2855
  */
2837
2856
  range?: SharedValue<Pick<ChartScale, "min" | "max"> | null>;
2838
2857
  }
@@ -2849,6 +2868,83 @@ export interface ViewportConfig extends ChartViewportControl {
2849
2868
  windowSmoothing?: boolean;
2850
2869
  }
2851
2870
 
2871
+ /**
2872
+ * Vertical price panning (see {@link LiveChartProps.priceScroll}).
2873
+ *
2874
+ * @experimental Prototype — gesture model and API may change.
2875
+ */
2876
+ export interface PriceScrollConfig {
2877
+ /**
2878
+ * When a vertical drag on the plot pans the price window:
2879
+ * - `"afterScale"` (default) — only once the range was manually changed (a
2880
+ * price-axis drag, a pinch, or an earlier price pan), so an untouched chart
2881
+ * still lets a parent ScrollView scroll.
2882
+ * - `"always"` — every vertical drag pans price.
2883
+ */
2884
+ activation?: "always" | "afterScale";
2885
+ /**
2886
+ * How far the window may travel past the fitted range, as a multiple of the
2887
+ * visible price span. Default `1`.
2888
+ */
2889
+ maxOvershoot?: number;
2890
+ }
2891
+
2892
+ /**
2893
+ * Floating reset-view button (see {@link LiveChartProps.resetButton}).
2894
+ *
2895
+ * @experimental
2896
+ */
2897
+ export interface ResetButtonConfig {
2898
+ /** Where the button sits inside the plot rect. Default `"bottom-center"`. */
2899
+ placement?: "bottom-center" | "bottom-right";
2900
+ /**
2901
+ * Which manual states show the button. Each defaults to `true`; set one to
2902
+ * `false` to ignore that state.
2903
+ */
2904
+ showFor?: {
2905
+ /** A scaled price axis (`yRangeScale !== 1`). */
2906
+ yScale?: boolean;
2907
+ /** A vertically panned price window (`priceScroll`). */
2908
+ priceScroll?: boolean;
2909
+ /** A window scrolled back from the live edge (`timeScroll`). */
2910
+ timeScroll?: boolean;
2911
+ /** A pinched window width (`zoom`). */
2912
+ zoom?: boolean;
2913
+ };
2914
+ /** Distance from the plot's bottom (and right) edge, in px. Default `12`. */
2915
+ margin?: number;
2916
+ /** Fade duration in ms when the button appears or hides. Default `160`. */
2917
+ fadeMs?: number;
2918
+ /**
2919
+ * Built-in button only: glyph rendered in the chart's font family, like
2920
+ * {@link ScrubActionConfig.icon}. Default `"↺"`.
2921
+ */
2922
+ icon?: string;
2923
+ /** Built-in button only: diameter in px. Default `36`. */
2924
+ size?: number;
2925
+ /** Built-in button only: fill color. Defaults to `palette.badgeBg`. */
2926
+ color?: string;
2927
+ /** Built-in button only: glyph color. Defaults to `palette.badgeText`. */
2928
+ iconColor?: string;
2929
+ /** Built-in button only: screen-reader label. The library ships no text of its own. */
2930
+ accessibilityLabel?: string;
2931
+ }
2932
+
2933
+ /**
2934
+ * Handed to {@link LiveChartProps.renderResetButton}.
2935
+ *
2936
+ * @experimental
2937
+ */
2938
+ export interface ResetButtonRenderProps {
2939
+ /** True while the view differs from the fitted live view. */
2940
+ visible: SharedValue<boolean>;
2941
+ /**
2942
+ * Runs `resetView()`. Wire to `onAccessibilityTap` on iOS and the `activate`
2943
+ * action through `accessibilityActions` / `onAccessibilityAction` on Android.
2944
+ */
2945
+ reset: () => void;
2946
+ }
2947
+
2852
2948
  /** Props for the single-series `LiveChart` component. */
2853
2949
  export interface LiveChartProps extends LiveChartCoreProps {
2854
2950
  /**
@@ -2860,10 +2956,50 @@ export interface LiveChartProps extends LiveChartCoreProps {
2860
2956
  * Both shared values are required, so this prop has no true shorthand.
2861
2957
  * Optional range supplies exact price bounds while end is non-null. In line
2862
2958
  * mode the line/dot stop at the last recorded point, with badge/value line at
2863
- * that price; badge.followViewEdge keeps its edge-pinned indicators.
2959
+ * that price; plain scrub also clamps to that latest recorded point.
2960
+ * If it leaves the window to the left, the scrub hides and onScrub clears
2961
+ * until recorded data re-enters the window.
2962
+ * badge.followViewEdge pins indicators to the edge only when the latest
2963
+ * recorded point leaves the visible time window.
2864
2964
  * @experimental
2865
2965
  */
2866
2966
  viewport?: false | ViewportConfig;
2967
+ /**
2968
+ * Drag the plot vertically to move the price window — with `timeScroll` this
2969
+ * gives free X/Y panning. The pan writes the viewport's price `range` (the
2970
+ * supplied `viewport.range` shared value when one is given, private state
2971
+ * otherwise), which then holds while new data streams in. A later app write
2972
+ * or replacement range SharedValue relinquishes gesture ownership. A price-axis drag
2973
+ * scales that range, and a double-tap on the axis, `resetView()` or a
2974
+ * `snapKey` / mode change clears it. Rides on the `timeScroll` pan, so it
2975
+ * requires `timeScroll`. `true` = defaults, or pass a {@link PriceScrollConfig}.
2976
+ * Default `false`.
2977
+ *
2978
+ * @experimental Prototype — gesture model and API may change.
2979
+ */
2980
+ priceScroll?: boolean | PriceScrollConfig;
2981
+ /**
2982
+ * Floating button that appears once the user has left the fitted live view
2983
+ * (time pan, pinch, price-axis scale or price pan) and returns to it in one
2984
+ * tap (see {@link LiveChartHandle.resetView}). `true` = the built-in round
2985
+ * glyph button, or pass a {@link ResetButtonConfig}. Default `false`.
2986
+ *
2987
+ * @experimental
2988
+ */
2989
+ resetButton?: boolean | ResetButtonConfig;
2990
+ /**
2991
+ * Custom visual for `resetButton`. The chart owns placement, visibility, fade
2992
+ * and the tap, so return a plain view rather than a pressable; the wrapper is
2993
+ * measured, so any size works. Called on the JS thread once per render.
2994
+ *
2995
+ * @experimental
2996
+ */
2997
+ renderResetButton?: (props: ResetButtonRenderProps) => ReactElement;
2998
+ /**
2999
+ * Called on the JS thread after the reset overrides are applied on the UI
3000
+ * thread (button tap or `resetView()`). Return animations may still be running.
3001
+ */
3002
+ onResetView?: () => void;
2867
3003
  /** Area gradient fill under the line. `true` = defaults, or pass `GradientConfig`. Default `true`. */
2868
3004
  gradient?: boolean | GradientConfig;
2869
3005
  /**