react-native-livechart 4.29.0 → 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 (41) 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 +33 -2
  12. package/dist/core/useLiveChartEngine.d.ts.map +1 -1
  13. package/dist/hooks/resetButtonLayout.d.ts +24 -0
  14. package/dist/hooks/resetButtonLayout.d.ts.map +1 -0
  15. package/dist/hooks/resetView.d.ts +31 -0
  16. package/dist/hooks/resetView.d.ts.map +1 -0
  17. package/dist/hooks/usePanScroll.d.ts +39 -3
  18. package/dist/hooks/usePanScroll.d.ts.map +1 -1
  19. package/dist/hooks/useResetButton.d.ts +41 -0
  20. package/dist/hooks/useResetButton.d.ts.map +1 -0
  21. package/dist/hooks/useYAxisScaleGesture.d.ts +14 -1
  22. package/dist/hooks/useYAxisScaleGesture.d.ts.map +1 -1
  23. package/dist/index.d.ts +1 -1
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/types.d.ts +125 -2
  26. package/dist/types.d.ts.map +1 -1
  27. package/package.json +1 -1
  28. package/src/components/LiveChart.tsx +61 -2
  29. package/src/components/LiveChartSeries.tsx +6 -1
  30. package/src/components/ResetButtonOverlay.tsx +129 -0
  31. package/src/core/gestureRangeOwnership.ts +46 -0
  32. package/src/core/liveChartEngineTick.ts +84 -4
  33. package/src/core/resolveConfig.ts +76 -0
  34. package/src/core/useLiveChartEngine.ts +117 -5
  35. package/src/hooks/resetButtonLayout.ts +80 -0
  36. package/src/hooks/resetView.ts +92 -0
  37. package/src/hooks/usePanScroll.ts +177 -5
  38. package/src/hooks/useResetButton.ts +181 -0
  39. package/src/hooks/useYAxisScaleGesture.ts +70 -13
  40. package/src/index.ts +3 -0
  41. package/src/types.ts +128 -2
@@ -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
@@ -2315,6 +2315,16 @@ export interface LiveChartHandle {
2315
2315
  * ```
2316
2316
  */
2317
2317
  resetZoom(): void;
2318
+ /**
2319
+ * Return to the fitted live view: {@link resetZoom} plus `yRangeScale` → 1 and
2320
+ * a gesture-owned price range cleared, gliding the right edge back to live
2321
+ * with the `timeScroll.returnToLive` timing. What the `resetButton` runs.
2322
+ * Safe to call when already fitted. On LiveChartSeries, which has no price
2323
+ * pan, it is `resetZoom()` plus `yRangeScale` → 1.
2324
+ *
2325
+ * @experimental
2326
+ */
2327
+ resetView(): void;
2318
2328
  }
2319
2329
 
2320
2330
  /**
@@ -2831,8 +2841,11 @@ export interface ChartViewportControl {
2831
2841
  * future space. Both bounds and their difference must be finite, with min < max.
2832
2842
  * Applied immediately, overriding automatic fitting, range easing, nonNegative,
2833
2843
  * 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.
2844
+ * App-written bounds are ignored while end is null (including after resetZoom()).
2845
+ * With priceScroll, gestures write this range too: gesture-owned bounds stay
2846
+ * active while following live and obey the chart's hard bounds. A later app
2847
+ * write or replacement range SharedValue restores the app-owned contract;
2848
+ * resetView(), axis double-tap and framing changes preserve app-written bounds.
2836
2849
  */
2837
2850
  range?: SharedValue<Pick<ChartScale, "min" | "max"> | null>;
2838
2851
  }
@@ -2849,6 +2862,83 @@ export interface ViewportConfig extends ChartViewportControl {
2849
2862
  windowSmoothing?: boolean;
2850
2863
  }
2851
2864
 
2865
+ /**
2866
+ * Vertical price panning (see {@link LiveChartProps.priceScroll}).
2867
+ *
2868
+ * @experimental Prototype — gesture model and API may change.
2869
+ */
2870
+ export interface PriceScrollConfig {
2871
+ /**
2872
+ * When a vertical drag on the plot pans the price window:
2873
+ * - `"afterScale"` (default) — only once the range was manually changed (a
2874
+ * price-axis drag, a pinch, or an earlier price pan), so an untouched chart
2875
+ * still lets a parent ScrollView scroll.
2876
+ * - `"always"` — every vertical drag pans price.
2877
+ */
2878
+ activation?: "always" | "afterScale";
2879
+ /**
2880
+ * How far the window may travel past the fitted range, as a multiple of the
2881
+ * visible price span. Default `1`.
2882
+ */
2883
+ maxOvershoot?: number;
2884
+ }
2885
+
2886
+ /**
2887
+ * Floating reset-view button (see {@link LiveChartProps.resetButton}).
2888
+ *
2889
+ * @experimental
2890
+ */
2891
+ export interface ResetButtonConfig {
2892
+ /** Where the button sits inside the plot rect. Default `"bottom-center"`. */
2893
+ placement?: "bottom-center" | "bottom-right";
2894
+ /**
2895
+ * Which manual states show the button. Each defaults to `true`; set one to
2896
+ * `false` to ignore that state.
2897
+ */
2898
+ showFor?: {
2899
+ /** A scaled price axis (`yRangeScale !== 1`). */
2900
+ yScale?: boolean;
2901
+ /** A vertically panned price window (`priceScroll`). */
2902
+ priceScroll?: boolean;
2903
+ /** A window scrolled back from the live edge (`timeScroll`). */
2904
+ timeScroll?: boolean;
2905
+ /** A pinched window width (`zoom`). */
2906
+ zoom?: boolean;
2907
+ };
2908
+ /** Distance from the plot's bottom (and right) edge, in px. Default `12`. */
2909
+ margin?: number;
2910
+ /** Fade duration in ms when the button appears or hides. Default `160`. */
2911
+ fadeMs?: number;
2912
+ /**
2913
+ * Built-in button only: glyph rendered in the chart's font family, like
2914
+ * {@link ScrubActionConfig.icon}. Default `"↺"`.
2915
+ */
2916
+ icon?: string;
2917
+ /** Built-in button only: diameter in px. Default `36`. */
2918
+ size?: number;
2919
+ /** Built-in button only: fill color. Defaults to `palette.badgeBg`. */
2920
+ color?: string;
2921
+ /** Built-in button only: glyph color. Defaults to `palette.badgeText`. */
2922
+ iconColor?: string;
2923
+ /** Built-in button only: screen-reader label. The library ships no text of its own. */
2924
+ accessibilityLabel?: string;
2925
+ }
2926
+
2927
+ /**
2928
+ * Handed to {@link LiveChartProps.renderResetButton}.
2929
+ *
2930
+ * @experimental
2931
+ */
2932
+ export interface ResetButtonRenderProps {
2933
+ /** True while the view differs from the fitted live view. */
2934
+ visible: SharedValue<boolean>;
2935
+ /**
2936
+ * Runs `resetView()`. Wire to `onAccessibilityTap` on iOS and the `activate`
2937
+ * action through `accessibilityActions` / `onAccessibilityAction` on Android.
2938
+ */
2939
+ reset: () => void;
2940
+ }
2941
+
2852
2942
  /** Props for the single-series `LiveChart` component. */
2853
2943
  export interface LiveChartProps extends LiveChartCoreProps {
2854
2944
  /**
@@ -2864,6 +2954,42 @@ export interface LiveChartProps extends LiveChartCoreProps {
2864
2954
  * @experimental
2865
2955
  */
2866
2956
  viewport?: false | ViewportConfig;
2957
+ /**
2958
+ * Drag the plot vertically to move the price window — with `timeScroll` this
2959
+ * gives free X/Y panning. The pan writes the viewport's price `range` (the
2960
+ * supplied `viewport.range` shared value when one is given, private state
2961
+ * otherwise), which then holds while new data streams in. A later app write
2962
+ * or replacement range SharedValue relinquishes gesture ownership. A price-axis drag
2963
+ * scales that range, and a double-tap on the axis, `resetView()` or a
2964
+ * `snapKey` / mode change clears it. Rides on the `timeScroll` pan, so it
2965
+ * requires `timeScroll`. `true` = defaults, or pass a {@link PriceScrollConfig}.
2966
+ * Default `false`.
2967
+ *
2968
+ * @experimental Prototype — gesture model and API may change.
2969
+ */
2970
+ priceScroll?: boolean | PriceScrollConfig;
2971
+ /**
2972
+ * Floating button that appears once the user has left the fitted live view
2973
+ * (time pan, pinch, price-axis scale or price pan) and returns to it in one
2974
+ * tap (see {@link LiveChartHandle.resetView}). `true` = the built-in round
2975
+ * glyph button, or pass a {@link ResetButtonConfig}. Default `false`.
2976
+ *
2977
+ * @experimental
2978
+ */
2979
+ resetButton?: boolean | ResetButtonConfig;
2980
+ /**
2981
+ * Custom visual for `resetButton`. The chart owns placement, visibility, fade
2982
+ * and the tap, so return a plain view rather than a pressable; the wrapper is
2983
+ * measured, so any size works. Called on the JS thread once per render.
2984
+ *
2985
+ * @experimental
2986
+ */
2987
+ renderResetButton?: (props: ResetButtonRenderProps) => ReactElement;
2988
+ /**
2989
+ * Called on the JS thread after the reset overrides are applied on the UI
2990
+ * thread (button tap or `resetView()`). Return animations may still be running.
2991
+ */
2992
+ onResetView?: () => void;
2867
2993
  /** Area gradient fill under the line. `true` = defaults, or pass `GradientConfig`. Default `true`. */
2868
2994
  gradient?: boolean | GradientConfig;
2869
2995
  /**