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.
- package/dist/components/LiveChart.d.ts.map +1 -1
- package/dist/components/LiveChartSeries.d.ts.map +1 -1
- package/dist/components/ResetButtonOverlay.d.ts +25 -0
- package/dist/components/ResetButtonOverlay.d.ts.map +1 -0
- package/dist/core/gestureRangeOwnership.d.ts +13 -0
- package/dist/core/gestureRangeOwnership.d.ts.map +1 -0
- package/dist/core/liveChartEngineTick.d.ts +30 -0
- package/dist/core/liveChartEngineTick.d.ts.map +1 -1
- package/dist/core/resolveConfig.d.ts +35 -1
- package/dist/core/resolveConfig.d.ts.map +1 -1
- package/dist/core/useLiveChartEngine.d.ts +34 -3
- package/dist/core/useLiveChartEngine.d.ts.map +1 -1
- package/dist/hooks/crosshairShared.d.ts +2 -2
- package/dist/hooks/crosshairShared.d.ts.map +1 -1
- package/dist/hooks/resetButtonLayout.d.ts +24 -0
- package/dist/hooks/resetButtonLayout.d.ts.map +1 -0
- package/dist/hooks/resetView.d.ts +31 -0
- package/dist/hooks/resetView.d.ts.map +1 -0
- package/dist/hooks/useCrosshair.d.ts +3 -1
- package/dist/hooks/useCrosshair.d.ts.map +1 -1
- package/dist/hooks/useLiveDot.d.ts +6 -2
- package/dist/hooks/useLiveDot.d.ts.map +1 -1
- package/dist/hooks/usePanScroll.d.ts +39 -3
- package/dist/hooks/usePanScroll.d.ts.map +1 -1
- package/dist/hooks/useResetButton.d.ts +41 -0
- package/dist/hooks/useResetButton.d.ts.map +1 -0
- package/dist/hooks/useYAxisScaleGesture.d.ts +14 -1
- package/dist/hooks/useYAxisScaleGesture.d.ts.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/types.d.ts +138 -5
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/components/LiveChart.tsx +67 -6
- package/src/components/LiveChartSeries.tsx +6 -1
- package/src/components/ResetButtonOverlay.tsx +129 -0
- package/src/core/gestureRangeOwnership.ts +46 -0
- package/src/core/liveChartEngineTick.ts +84 -4
- package/src/core/resolveConfig.ts +76 -0
- package/src/core/useLiveChartEngine.ts +191 -22
- package/src/hooks/crosshairShared.ts +2 -2
- package/src/hooks/resetButtonLayout.ts +80 -0
- package/src/hooks/resetView.ts +92 -0
- package/src/hooks/useCrosshair.ts +50 -19
- package/src/hooks/useLiveDot.ts +41 -4
- package/src/hooks/usePanScroll.ts +177 -5
- package/src/hooks/useResetButton.ts +181 -0
- package/src/hooks/useYAxisScaleGesture.ts +70 -13
- package/src/index.ts +3 -0
- package/src/types.ts +141 -5
|
@@ -69,6 +69,8 @@ import {
|
|
|
69
69
|
resolveTradeStream,
|
|
70
70
|
resolveValueLine,
|
|
71
71
|
resolveVolume,
|
|
72
|
+
resolvePriceScroll,
|
|
73
|
+
resolveResetButton,
|
|
72
74
|
resolveViewport,
|
|
73
75
|
resolveZoom,
|
|
74
76
|
resolveXAxis,
|
|
@@ -124,6 +126,7 @@ import { useModeBlend } from "../hooks/useModeBlend";
|
|
|
124
126
|
import { resolveMomentumProp, useMomentum } from "../hooks/useMomentum";
|
|
125
127
|
import { AXIS_GRAB_MIN_PX, usePanScroll } from "../hooks/usePanScroll";
|
|
126
128
|
import { resetPinchZoom, usePinchZoom } from "../hooks/usePinchZoom";
|
|
129
|
+
import { useResetButton } from "../hooks/useResetButton";
|
|
127
130
|
import {
|
|
128
131
|
SERIES_INDICATOR_FADE_MS,
|
|
129
132
|
useSeriesIndicatorOpacity,
|
|
@@ -210,6 +213,7 @@ import { MultiSeriesTooltipStack } from "./MultiSeriesTooltipStack";
|
|
|
210
213
|
import { ValueTextOverlay } from "./ValueTextOverlay";
|
|
211
214
|
import { ReferenceLineGroupOverlay } from "./ReferenceLineGroupOverlay";
|
|
212
215
|
import { ReferenceLineOverlay } from "./ReferenceLineOverlay";
|
|
216
|
+
import { ResetButtonOverlay } from "./ResetButtonOverlay";
|
|
213
217
|
import { ScrubActionOverlay } from "./ScrubActionOverlay";
|
|
214
218
|
import { SegmentDividerOverlay } from "./SegmentDividerOverlay";
|
|
215
219
|
import { SegmentLineGradient } from "./SegmentLineGradient";
|
|
@@ -1510,6 +1514,10 @@ function useLiveChartController({
|
|
|
1510
1514
|
nowOverride,
|
|
1511
1515
|
timeScroll = false,
|
|
1512
1516
|
viewport,
|
|
1517
|
+
priceScroll = false,
|
|
1518
|
+
resetButton = false,
|
|
1519
|
+
renderResetButton,
|
|
1520
|
+
onResetView,
|
|
1513
1521
|
returnToLive,
|
|
1514
1522
|
zoom = false,
|
|
1515
1523
|
accessibilityLabel,
|
|
@@ -1755,6 +1763,8 @@ function useLiveChartController({
|
|
|
1755
1763
|
yAxisCfg,
|
|
1756
1764
|
zoom,
|
|
1757
1765
|
]);
|
|
1766
|
+
const priceScrollCfg = useMemo(() => resolvePriceScroll(priceScroll), [priceScroll]);
|
|
1767
|
+
const resetButtonCfg = useMemo(() => resolveResetButton(resetButton), [resetButton]);
|
|
1758
1768
|
|
|
1759
1769
|
const {
|
|
1760
1770
|
skiaFont,
|
|
@@ -2034,7 +2044,7 @@ function useLiveChartController({
|
|
|
2034
2044
|
palette,
|
|
2035
2045
|
);
|
|
2036
2046
|
|
|
2037
|
-
const { dotX, dotY, indicatorValue } = useLiveDot(
|
|
2047
|
+
const { dotX, dotY, indicatorValue, isFollowingViewEdge } = useLiveDot(
|
|
2038
2048
|
engine,
|
|
2039
2049
|
effectivePadding,
|
|
2040
2050
|
engine.edgeValue,
|
|
@@ -2053,7 +2063,7 @@ function useLiveChartController({
|
|
|
2053
2063
|
// result; while scrolled back, detect against the data prefix ending at the
|
|
2054
2064
|
// visible right-edge timestamp so incoming live ticks cannot change the pill.
|
|
2055
2065
|
const badgeMomentumSV = useDerivedValue(() =>
|
|
2056
|
-
|
|
2066
|
+
isFollowingViewEdge.value && engine.viewEnd.value !== null
|
|
2057
2067
|
? resolveMomentumProp(momentum, engine.data.value, engine.timestamp.value)
|
|
2058
2068
|
: momentumSV.value,
|
|
2059
2069
|
);
|
|
@@ -2213,6 +2223,7 @@ function useLiveChartController({
|
|
|
2213
2223
|
onScrubCandleChange,
|
|
2214
2224
|
resolveScrubMarkerOptions(scrubCfg, markersSV),
|
|
2215
2225
|
axisScaleActive,
|
|
2226
|
+
shouldOmitTipBeyondData,
|
|
2216
2227
|
);
|
|
2217
2228
|
|
|
2218
2229
|
// Capture only the shared value in the worklets below. Referencing
|
|
@@ -2255,6 +2266,9 @@ function useLiveChartController({
|
|
|
2255
2266
|
scrubActive: crosshairScrubActive,
|
|
2256
2267
|
// Clear any live crosshair when a scroll drag takes over.
|
|
2257
2268
|
onScrollStart: clearCrosshair,
|
|
2269
|
+
// Vertical drags pan the price window once armed (see `priceScroll`).
|
|
2270
|
+
priceScroll: priceScrollCfg,
|
|
2271
|
+
yRangeScale: effectiveYRangeScale,
|
|
2258
2272
|
});
|
|
2259
2273
|
|
|
2260
2274
|
// Pinch-to-zoom the visible window (two-finger). Anchors at the focal point and
|
|
@@ -2272,6 +2286,19 @@ function useLiveChartController({
|
|
|
2272
2286
|
onZoomStart: clearCrosshair,
|
|
2273
2287
|
});
|
|
2274
2288
|
|
|
2289
|
+
// Floating reset-view button: visibility and placement on the UI thread, a
|
|
2290
|
+
// tap composed ahead of the plot graph (see ChartView), and the JS-thread
|
|
2291
|
+
// `reset` that backs the handle's `resetView()`.
|
|
2292
|
+
const resetButtonState = useResetButton({
|
|
2293
|
+
config: resetButtonCfg,
|
|
2294
|
+
engine,
|
|
2295
|
+
padding: effectivePadding,
|
|
2296
|
+
yRangeScale: effectiveYRangeScale,
|
|
2297
|
+
scrubActive: crosshairScrubActive,
|
|
2298
|
+
returnToLiveMs,
|
|
2299
|
+
onResetView,
|
|
2300
|
+
});
|
|
2301
|
+
|
|
2275
2302
|
const axisAutoHideOpacity = useAxisAutoHide({
|
|
2276
2303
|
config: axisAutoHide,
|
|
2277
2304
|
scaleActive: axisScaleActive,
|
|
@@ -2398,6 +2425,9 @@ function useLiveChartController({
|
|
|
2398
2425
|
return {
|
|
2399
2426
|
effectiveYRangeScale,
|
|
2400
2427
|
axisScaleActive,
|
|
2428
|
+
resetButtonState,
|
|
2429
|
+
resetButtonCfg,
|
|
2430
|
+
renderResetButton,
|
|
2401
2431
|
deferAxisScaleHit: deferTapHit,
|
|
2402
2432
|
clearCrosshair,
|
|
2403
2433
|
// passthrough props the render needs
|
|
@@ -3870,8 +3900,9 @@ function ChartBadgeLayer({
|
|
|
3870
3900
|
metricsCfg.badge,
|
|
3871
3901
|
metricsCfg.motion.badgeColorSpeed,
|
|
3872
3902
|
yAxisFloat,
|
|
3873
|
-
|
|
3874
|
-
|
|
3903
|
+
// The controller already resolves recorded-tip versus view-edge pricing.
|
|
3904
|
+
undefined,
|
|
3905
|
+
false,
|
|
3875
3906
|
badgeCfg.radius,
|
|
3876
3907
|
badgeCfg.textColor,
|
|
3877
3908
|
labelColumn?.entries,
|
|
@@ -4229,6 +4260,10 @@ function ChartNativeOverlays({ model }: { model: LiveChartModel }) {
|
|
|
4229
4260
|
renderTooltip,
|
|
4230
4261
|
crosshair,
|
|
4231
4262
|
renderOverlay,
|
|
4263
|
+
resetButtonState,
|
|
4264
|
+
resetButtonCfg,
|
|
4265
|
+
renderResetButton,
|
|
4266
|
+
fontProp,
|
|
4232
4267
|
} = model;
|
|
4233
4268
|
const hasAnnotations =
|
|
4234
4269
|
(markersActive && renderMarker != null) ||
|
|
@@ -4281,6 +4316,19 @@ function ChartNativeOverlays({ model }: { model: LiveChartModel }) {
|
|
|
4281
4316
|
scrubDotY={crosshair.scrubDotY}
|
|
4282
4317
|
/>
|
|
4283
4318
|
) : null}
|
|
4319
|
+
{resetButtonCfg ? (
|
|
4320
|
+
<ResetButtonOverlay
|
|
4321
|
+
config={resetButtonCfg}
|
|
4322
|
+
visible={resetButtonState.visible}
|
|
4323
|
+
rect={resetButtonState.rect}
|
|
4324
|
+
measuredWidth={resetButtonState.measuredWidth}
|
|
4325
|
+
measuredHeight={resetButtonState.measuredHeight}
|
|
4326
|
+
reset={resetButtonState.reset}
|
|
4327
|
+
render={renderResetButton}
|
|
4328
|
+
palette={palette}
|
|
4329
|
+
fontFamily={fontProp?.fontFamily ?? MONO_FONT_FAMILY}
|
|
4330
|
+
/>
|
|
4331
|
+
) : null}
|
|
4284
4332
|
{renderOverlay ? <ChartCustomConsumerOverlay model={model} /> : null}
|
|
4285
4333
|
</>
|
|
4286
4334
|
);
|
|
@@ -4318,10 +4366,21 @@ function ChartView({
|
|
|
4318
4366
|
deferHit: model.deferAxisScaleHit,
|
|
4319
4367
|
onStart: model.clearCrosshair,
|
|
4320
4368
|
gesture: rootGesture,
|
|
4369
|
+
range: model.engine.viewRange,
|
|
4370
|
+
rangeGestureOwned: model.engine.viewRangeGestureOwned,
|
|
4321
4371
|
});
|
|
4372
|
+
// The reset tap goes first: it claims only touches on the visible button and
|
|
4373
|
+
// fails instantly elsewhere, so the plot graph never waits on it — while a
|
|
4374
|
+
// tap on the button can't be swallowed by the pan's activation offset.
|
|
4375
|
+
const resetTapGesture = model.resetButtonState.tapGesture;
|
|
4376
|
+
const gestureWithReset = useMemo(
|
|
4377
|
+
() =>
|
|
4378
|
+
resetTapGesture ? Gesture.Exclusive(resetTapGesture, gesture) : gesture,
|
|
4379
|
+
[gesture, resetTapGesture],
|
|
4380
|
+
);
|
|
4322
4381
|
|
|
4323
4382
|
return (
|
|
4324
|
-
<GestureDetector gesture={
|
|
4383
|
+
<GestureDetector gesture={gestureWithReset}>
|
|
4325
4384
|
<View
|
|
4326
4385
|
style={[{ flex: 1, backgroundColor }, style]}
|
|
4327
4386
|
onLayout={onLayout}
|
|
@@ -4347,12 +4406,14 @@ export const LiveChart = memo(
|
|
|
4347
4406
|
forwardRef<LiveChartHandle, LiveChartProps>(function LiveChart(props, ref) {
|
|
4348
4407
|
const model = useLiveChartController(props);
|
|
4349
4408
|
const { viewEnd, viewWindow } = model.engine;
|
|
4409
|
+
const resetView = model.resetButtonState.reset;
|
|
4350
4410
|
useImperativeHandle(
|
|
4351
4411
|
ref,
|
|
4352
4412
|
() => ({
|
|
4353
4413
|
resetZoom: () => scheduleOnUI(resetPinchZoom, { viewEnd, viewWindow }),
|
|
4414
|
+
resetView,
|
|
4354
4415
|
}),
|
|
4355
|
-
[viewEnd, viewWindow],
|
|
4416
|
+
[resetView, viewEnd, viewWindow],
|
|
4356
4417
|
);
|
|
4357
4418
|
if (model.xAxisCfg) {
|
|
4358
4419
|
return <ChartWithXAxis model={model} />;
|
|
@@ -72,6 +72,7 @@ import { useMultiSeriesDegen } from "../hooks/useMultiSeriesDegen";
|
|
|
72
72
|
import { useMultiSeriesLinePaths } from "../hooks/useMultiSeriesLinePaths";
|
|
73
73
|
import { AXIS_GRAB_MIN_PX, usePanScroll } from "../hooks/usePanScroll";
|
|
74
74
|
import { resetPinchZoom, usePinchZoom } from "../hooks/usePinchZoom";
|
|
75
|
+
import { resetSeriesView } from "../hooks/resetView";
|
|
75
76
|
import { useMultiSeriesReverseMorphInputs } from "../hooks/useReverseMorphEngineInputs";
|
|
76
77
|
import {
|
|
77
78
|
SERIES_INDICATOR_FADE_MS,
|
|
@@ -784,6 +785,7 @@ function useLiveChartSeriesController(props: LiveChartSeriesProps) {
|
|
|
784
785
|
);
|
|
785
786
|
|
|
786
787
|
return {
|
|
788
|
+
effectiveYRangeScale,
|
|
787
789
|
// passthrough props the render needs
|
|
788
790
|
series,
|
|
789
791
|
seriesOpacity: resolvedSeriesOpacity,
|
|
@@ -1490,12 +1492,15 @@ export const LiveChartSeries = forwardRef<
|
|
|
1490
1492
|
>(function LiveChartSeries(props, ref) {
|
|
1491
1493
|
const model = useLiveChartSeriesController(props);
|
|
1492
1494
|
const { viewEnd, viewWindow } = model.engine;
|
|
1495
|
+
const yRangeScale = model.effectiveYRangeScale;
|
|
1493
1496
|
useImperativeHandle(
|
|
1494
1497
|
ref,
|
|
1495
1498
|
() => ({
|
|
1496
1499
|
resetZoom: () => scheduleOnUI(resetPinchZoom, { viewEnd, viewWindow }),
|
|
1500
|
+
resetView: () =>
|
|
1501
|
+
scheduleOnUI(resetSeriesView, { viewEnd, viewWindow, yRangeScale }),
|
|
1497
1502
|
}),
|
|
1498
|
-
[viewEnd, viewWindow],
|
|
1503
|
+
[viewEnd, viewWindow, yRangeScale],
|
|
1499
1504
|
);
|
|
1500
1505
|
const {
|
|
1501
1506
|
rootGesture,
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { useCallback, useState, type ReactElement } from "react";
|
|
2
|
+
import { StyleSheet, Text, View, type AccessibilityActionEvent, type LayoutChangeEvent } from "react-native";
|
|
3
|
+
import Animated, {
|
|
4
|
+
useAnimatedReaction,
|
|
5
|
+
useAnimatedStyle,
|
|
6
|
+
useDerivedValue,
|
|
7
|
+
withTiming,
|
|
8
|
+
type SharedValue,
|
|
9
|
+
} from "react-native-reanimated";
|
|
10
|
+
import { scheduleOnRN } from "react-native-worklets";
|
|
11
|
+
|
|
12
|
+
import type { ResolvedResetButtonConfig } from "../core/resolveConfig";
|
|
13
|
+
import type { ResetButtonRect } from "../hooks/resetButtonLayout";
|
|
14
|
+
import type { LiveChartPalette, ResetButtonRenderProps } from "../types";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* React Native overlay (NOT Skia) for the floating reset-view button. The
|
|
18
|
+
* wrapper is positioned from the chart's rect on the UI thread and faded with
|
|
19
|
+
* `visible`; the tap itself is an RNGH gesture composed ahead of the plot graph
|
|
20
|
+
* (see `useResetButton`), so the content here is a plain view. Renders the
|
|
21
|
+
* consumer's `renderResetButton` element when given, otherwise a round glyph
|
|
22
|
+
* button in palette colors.
|
|
23
|
+
*/
|
|
24
|
+
export function ResetButtonOverlay({
|
|
25
|
+
config,
|
|
26
|
+
visible,
|
|
27
|
+
rect,
|
|
28
|
+
measuredWidth,
|
|
29
|
+
measuredHeight,
|
|
30
|
+
reset,
|
|
31
|
+
render,
|
|
32
|
+
palette,
|
|
33
|
+
fontFamily,
|
|
34
|
+
}: {
|
|
35
|
+
config: ResolvedResetButtonConfig;
|
|
36
|
+
visible: SharedValue<boolean>;
|
|
37
|
+
rect: SharedValue<ResetButtonRect>;
|
|
38
|
+
measuredWidth: SharedValue<number>;
|
|
39
|
+
measuredHeight: SharedValue<number>;
|
|
40
|
+
reset: () => void;
|
|
41
|
+
render: ((props: ResetButtonRenderProps) => ReactElement) | undefined;
|
|
42
|
+
palette: LiveChartPalette;
|
|
43
|
+
fontFamily: string;
|
|
44
|
+
}) {
|
|
45
|
+
const { fadeMs } = config;
|
|
46
|
+
const opacity = useDerivedValue(() =>
|
|
47
|
+
withTiming(visible.get() ? 1 : 0, { duration: fadeMs }),
|
|
48
|
+
);
|
|
49
|
+
const wrapperStyle = useAnimatedStyle(() => {
|
|
50
|
+
const r = rect.get();
|
|
51
|
+
return {
|
|
52
|
+
opacity: opacity.get(),
|
|
53
|
+
transform: [{ translateX: r.x }, { translateY: r.y }],
|
|
54
|
+
};
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// Mirrored to React so a hidden button is neither focusable by screen
|
|
58
|
+
// readers nor a target for native touches (the chart's RNGH gestures are
|
|
59
|
+
// unaffected either way: they live on the ancestor view).
|
|
60
|
+
const [isShown, setIsShown] = useState(false);
|
|
61
|
+
useAnimatedReaction(
|
|
62
|
+
() => visible.get(),
|
|
63
|
+
/* istanbul ignore next -- Reanimated reaction; state mirrored on the JS thread, not exercised under Jest */
|
|
64
|
+
(shown, previous) => {
|
|
65
|
+
if (shown !== previous) scheduleOnRN(setIsShown, shown);
|
|
66
|
+
},
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
const onLayout = useCallback(
|
|
70
|
+
(event: LayoutChangeEvent) => {
|
|
71
|
+
const { width, height } = event.nativeEvent.layout;
|
|
72
|
+
measuredWidth.set(width);
|
|
73
|
+
measuredHeight.set(height);
|
|
74
|
+
},
|
|
75
|
+
[measuredHeight, measuredWidth],
|
|
76
|
+
);
|
|
77
|
+
const onAccessibilityAction = useCallback(
|
|
78
|
+
(event: AccessibilityActionEvent) => {
|
|
79
|
+
if (event.nativeEvent.actionName === "activate") reset();
|
|
80
|
+
},
|
|
81
|
+
[reset],
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
return (
|
|
85
|
+
<Animated.View
|
|
86
|
+
style={[styles.wrapper, wrapperStyle]}
|
|
87
|
+
onLayout={onLayout}
|
|
88
|
+
pointerEvents={isShown ? "box-none" : "none"}
|
|
89
|
+
accessibilityElementsHidden={!isShown}
|
|
90
|
+
importantForAccessibility={isShown ? "auto" : "no-hide-descendants"}
|
|
91
|
+
>
|
|
92
|
+
{render ? (
|
|
93
|
+
render({ visible, reset })
|
|
94
|
+
) : (
|
|
95
|
+
<View
|
|
96
|
+
accessible
|
|
97
|
+
accessibilityRole="button"
|
|
98
|
+
accessibilityLabel={config.accessibilityLabel}
|
|
99
|
+
onAccessibilityTap={reset}
|
|
100
|
+
accessibilityActions={[{ name: "activate" }]}
|
|
101
|
+
onAccessibilityAction={onAccessibilityAction}
|
|
102
|
+
style={{
|
|
103
|
+
width: config.size,
|
|
104
|
+
height: config.size,
|
|
105
|
+
borderRadius: config.size / 2,
|
|
106
|
+
backgroundColor: config.color ?? palette.badgeBg,
|
|
107
|
+
alignItems: "center",
|
|
108
|
+
justifyContent: "center",
|
|
109
|
+
}}
|
|
110
|
+
>
|
|
111
|
+
<Text
|
|
112
|
+
allowFontScaling={false}
|
|
113
|
+
style={{
|
|
114
|
+
fontFamily,
|
|
115
|
+
fontSize: config.size / 2,
|
|
116
|
+
color: config.iconColor ?? palette.badgeText,
|
|
117
|
+
}}
|
|
118
|
+
>
|
|
119
|
+
{config.icon}
|
|
120
|
+
</Text>
|
|
121
|
+
</View>
|
|
122
|
+
)}
|
|
123
|
+
</Animated.View>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const styles = StyleSheet.create({
|
|
128
|
+
wrapper: { position: "absolute", left: 0, top: 0 },
|
|
129
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { useLayoutEffect, useState } from "react";
|
|
2
|
+
import { useSharedValue, type SharedValue } from "react-native-reanimated";
|
|
3
|
+
import { scheduleOnUI } from "react-native-worklets";
|
|
4
|
+
|
|
5
|
+
import type { ChartScale } from "../types";
|
|
6
|
+
|
|
7
|
+
type PriceRange = Pick<ChartScale, "min" | "max"> | null;
|
|
8
|
+
|
|
9
|
+
// Keep separate from Reanimated's positive mapper IDs and history listener IDs.
|
|
10
|
+
let nextRangeListenerId = -1_000_000;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* App writes relinquish ownership synchronously, including modify() calls and
|
|
14
|
+
* writes followed by a reset in the same worklet. Replacing the supplied range
|
|
15
|
+
* also relinquishes ownership before the new viewport is painted.
|
|
16
|
+
*/
|
|
17
|
+
export function useGestureRangeOwnership(range: SharedValue<PriceRange>) {
|
|
18
|
+
const owned = useSharedValue(false);
|
|
19
|
+
const [listenerId] = useState(() => nextRangeListenerId--);
|
|
20
|
+
useLayoutEffect(() => {
|
|
21
|
+
scheduleOnUI(() => {
|
|
22
|
+
"worklet";
|
|
23
|
+
owned.set(false);
|
|
24
|
+
range.addListener(listenerId, () => {
|
|
25
|
+
"worklet";
|
|
26
|
+
owned.set(false);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
return () => scheduleOnUI(() => {
|
|
30
|
+
"worklet";
|
|
31
|
+
range.removeListener(listenerId);
|
|
32
|
+
});
|
|
33
|
+
}, [range, owned, listenerId]);
|
|
34
|
+
return owned;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Mark only this write as gesture-owned, after synchronous listeners run. */
|
|
38
|
+
export function writeGestureRange(
|
|
39
|
+
range: SharedValue<PriceRange>,
|
|
40
|
+
owned: SharedValue<boolean>,
|
|
41
|
+
next: Exclude<PriceRange, null>,
|
|
42
|
+
): void {
|
|
43
|
+
"worklet";
|
|
44
|
+
range.value = next;
|
|
45
|
+
owned.value = true;
|
|
46
|
+
}
|
|
@@ -52,6 +52,22 @@ export interface EngineTickMutable {
|
|
|
52
52
|
extremaMaxValue: number;
|
|
53
53
|
extremaMinTime: number;
|
|
54
54
|
extremaMaxTime: number;
|
|
55
|
+
/**
|
|
56
|
+
* Fitted Y bounds this frame — the target the eased display range pursues,
|
|
57
|
+
* before an explicit {@link EngineTickInput.viewRange} overrides it. The
|
|
58
|
+
* price-pan gesture clamps its travel against these. Written every frame;
|
|
59
|
+
* optional so existing state literals stay valid.
|
|
60
|
+
*/
|
|
61
|
+
fitMin?: number;
|
|
62
|
+
fitMax?: number;
|
|
63
|
+
/**
|
|
64
|
+
* A gesture-owned {@link EngineTickInput.viewRange} after
|
|
65
|
+
* {@link clampGestureRange}, when the clamp changed it. The engine writes it
|
|
66
|
+
* back to the shared value so the next gesture frame starts from the clamped
|
|
67
|
+
* bounds. `NaN` when nothing was adjusted; written every frame.
|
|
68
|
+
*/
|
|
69
|
+
adjustedViewRangeMin?: number;
|
|
70
|
+
adjustedViewRangeMax?: number;
|
|
55
71
|
/** Previous frame's yRangeScale — detects an in-flight scale drag. */
|
|
56
72
|
lastYRangeScale?: number;
|
|
57
73
|
/** Canvas width observed by the previous measured tick. */
|
|
@@ -95,6 +111,13 @@ export interface EngineTickInput {
|
|
|
95
111
|
yRangeScale?: number;
|
|
96
112
|
/** Explicit finite price bounds for a freely positioned external viewport. */
|
|
97
113
|
viewRange?: Pick<ChartScale, "min" | "max"> | null;
|
|
114
|
+
/**
|
|
115
|
+
* Apply {@link viewRange} while following live too (`viewEnd` null). Set for
|
|
116
|
+
* a range written by the price-pan / axis-scale gestures, so the price window
|
|
117
|
+
* holds as new data streams in. An app-written range keeps the
|
|
118
|
+
* `viewEnd`-only rule. Default `false`.
|
|
119
|
+
*/
|
|
120
|
+
viewRangeFollowsLive?: boolean;
|
|
98
121
|
targetValue: number;
|
|
99
122
|
points: LiveChartPoint[];
|
|
100
123
|
/** Internal notification revision for the selected history; omit to disable reuse. */
|
|
@@ -202,6 +225,44 @@ export function advanceTimestampByPixel(
|
|
|
202
225
|
return elapsedSteps > 0 ? current + elapsedSteps * step : current;
|
|
203
226
|
}
|
|
204
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Keep a gesture-owned explicit range inside the chart's hard bounds, which
|
|
230
|
+
* the fit normally enforces: widen a span below `minRange` about its midpoint,
|
|
231
|
+
* then slide the window to respect `nonNegative` / `maxValue`. Returns the
|
|
232
|
+
* input object itself when nothing changes.
|
|
233
|
+
*/
|
|
234
|
+
export function clampGestureRange(
|
|
235
|
+
range: Pick<ChartScale, "min" | "max">,
|
|
236
|
+
nonNegative: boolean,
|
|
237
|
+
maxValue: number | undefined,
|
|
238
|
+
minRange: number | undefined,
|
|
239
|
+
): Pick<ChartScale, "min" | "max"> {
|
|
240
|
+
"worklet";
|
|
241
|
+
let min = range.min;
|
|
242
|
+
let max = range.max;
|
|
243
|
+
if (
|
|
244
|
+
minRange !== undefined &&
|
|
245
|
+
minRange > 0 &&
|
|
246
|
+
Number.isFinite(minRange) &&
|
|
247
|
+
max - min < minRange
|
|
248
|
+
) {
|
|
249
|
+
const mid = min / 2 + max / 2;
|
|
250
|
+
min = mid - minRange / 2;
|
|
251
|
+
max = mid + minRange / 2;
|
|
252
|
+
}
|
|
253
|
+
if (nonNegative && min < 0) {
|
|
254
|
+
max -= min;
|
|
255
|
+
min = 0;
|
|
256
|
+
}
|
|
257
|
+
if (maxValue !== undefined && max > maxValue) {
|
|
258
|
+
min -= max - maxValue;
|
|
259
|
+
max = maxValue;
|
|
260
|
+
if (nonNegative && min < 0) min = 0;
|
|
261
|
+
}
|
|
262
|
+
if (!(Number.isFinite(min) && Number.isFinite(max) && min < max)) return range;
|
|
263
|
+
return min === range.min && max === range.max ? range : { min, max };
|
|
264
|
+
}
|
|
265
|
+
|
|
205
266
|
/**
|
|
206
267
|
* One frame of the live chart engine (mirrors `useLiveChartEngine` worklet body).
|
|
207
268
|
* Mutates `state` in place for testability and reuse from the hook.
|
|
@@ -223,8 +284,8 @@ export function tickLiveChartEngineFrame(
|
|
|
223
284
|
const viewEnd = input.viewEnd;
|
|
224
285
|
const requestedRange = input.viewRange;
|
|
225
286
|
const viewRange =
|
|
226
|
-
viewEnd != null &&
|
|
227
|
-
|
|
287
|
+
((viewEnd != null && Number.isFinite(viewEnd)) ||
|
|
288
|
+
input.viewRangeFollowsLive === true) &&
|
|
228
289
|
requestedRange &&
|
|
229
290
|
Number.isFinite(requestedRange.min) &&
|
|
230
291
|
Number.isFinite(requestedRange.max) &&
|
|
@@ -544,6 +605,8 @@ export function tickLiveChartEngineFrame(
|
|
|
544
605
|
if (input.nonNegative && tMin < 0) tMin = 0;
|
|
545
606
|
const maxV = input.maxValue;
|
|
546
607
|
if (maxV !== undefined && tMax > maxV) tMax = maxV;
|
|
608
|
+
state.fitMin = tMin;
|
|
609
|
+
state.fitMax = tMax;
|
|
547
610
|
|
|
548
611
|
const disjoint = tMin >= state.displayMax || tMax <= state.displayMin;
|
|
549
612
|
// A subpixel settle must not jump across the other bound of a disjoint fit.
|
|
@@ -576,9 +639,26 @@ export function tickLiveChartEngineFrame(
|
|
|
576
639
|
}
|
|
577
640
|
|
|
578
641
|
// Explicit bounds keep a gesture's price coordinates stable, even in empty space.
|
|
642
|
+
state.adjustedViewRangeMin = NaN;
|
|
643
|
+
state.adjustedViewRangeMax = NaN;
|
|
579
644
|
if (viewRange) {
|
|
580
|
-
|
|
581
|
-
|
|
645
|
+
// An app-written range is applied verbatim (the app owns its bounds); a
|
|
646
|
+
// gesture-written one must still honor the chart's hard bounds.
|
|
647
|
+
const bounded =
|
|
648
|
+
input.viewRangeFollowsLive === true
|
|
649
|
+
? clampGestureRange(
|
|
650
|
+
viewRange,
|
|
651
|
+
input.nonNegative === true,
|
|
652
|
+
input.maxValue,
|
|
653
|
+
input.minRange,
|
|
654
|
+
)
|
|
655
|
+
: viewRange;
|
|
656
|
+
if (bounded !== viewRange) {
|
|
657
|
+
state.adjustedViewRangeMin = bounded.min;
|
|
658
|
+
state.adjustedViewRangeMax = bounded.max;
|
|
659
|
+
}
|
|
660
|
+
state.displayMin = bounded.min;
|
|
661
|
+
state.displayMax = bounded.max;
|
|
582
662
|
}
|
|
583
663
|
|
|
584
664
|
// Edge value: the price at the visible window's right edge. Following live →
|
|
@@ -33,6 +33,8 @@ import type {
|
|
|
33
33
|
ScrubActionConfig,
|
|
34
34
|
ScrubConfig,
|
|
35
35
|
PerSeriesTooltipConfig,
|
|
36
|
+
PriceScrollConfig,
|
|
37
|
+
ResetButtonConfig,
|
|
36
38
|
SelectionDotConfig,
|
|
37
39
|
SelectionDotProps,
|
|
38
40
|
SelectionDotRingConfig,
|
|
@@ -806,6 +808,80 @@ export function resolveZoom(
|
|
|
806
808
|
return resolveToggle(prop, ZOOM_DEFAULTS, false);
|
|
807
809
|
}
|
|
808
810
|
|
|
811
|
+
export interface ResolvedPriceScrollConfig {
|
|
812
|
+
activation: "always" | "afterScale";
|
|
813
|
+
/** Non-negative, finite multiple of the visible span. */
|
|
814
|
+
maxOvershoot: number;
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
const PRICE_SCROLL_DEFAULTS: ResolvedPriceScrollConfig = {
|
|
818
|
+
activation: "afterScale",
|
|
819
|
+
maxOvershoot: 1,
|
|
820
|
+
};
|
|
821
|
+
|
|
822
|
+
/**
|
|
823
|
+
* Resolves the `priceScroll` prop to a config or null (disabled). `true` →
|
|
824
|
+
* defaults, object → merged, falsy → null. A malformed `maxOvershoot` falls
|
|
825
|
+
* back to the default rather than unbounding the pan.
|
|
826
|
+
*/
|
|
827
|
+
export function resolvePriceScroll(
|
|
828
|
+
prop: boolean | PriceScrollConfig | undefined,
|
|
829
|
+
): ResolvedPriceScrollConfig | null {
|
|
830
|
+
const resolved = resolveToggle(prop, PRICE_SCROLL_DEFAULTS, false);
|
|
831
|
+
if (resolved === null) return null;
|
|
832
|
+
const validOvershoot =
|
|
833
|
+
Number.isFinite(resolved.maxOvershoot) && resolved.maxOvershoot >= 0;
|
|
834
|
+
return validOvershoot
|
|
835
|
+
? resolved
|
|
836
|
+
: { ...resolved, maxOvershoot: PRICE_SCROLL_DEFAULTS.maxOvershoot };
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
export interface ResolvedResetButtonConfig {
|
|
840
|
+
placement: "bottom-center" | "bottom-right";
|
|
841
|
+
showFor: {
|
|
842
|
+
yScale: boolean;
|
|
843
|
+
priceScroll: boolean;
|
|
844
|
+
timeScroll: boolean;
|
|
845
|
+
zoom: boolean;
|
|
846
|
+
};
|
|
847
|
+
margin: number;
|
|
848
|
+
fadeMs: number;
|
|
849
|
+
icon: string;
|
|
850
|
+
size: number;
|
|
851
|
+
/** undefined → palette.badgeBg at render time */
|
|
852
|
+
color: string | undefined;
|
|
853
|
+
/** undefined → palette.badgeText at render time */
|
|
854
|
+
iconColor: string | undefined;
|
|
855
|
+
accessibilityLabel: string | undefined;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
const RESET_BUTTON_DEFAULTS: ResolvedResetButtonConfig = {
|
|
859
|
+
placement: "bottom-center",
|
|
860
|
+
showFor: { yScale: true, priceScroll: true, timeScroll: true, zoom: true },
|
|
861
|
+
margin: 12,
|
|
862
|
+
fadeMs: 160,
|
|
863
|
+
icon: "↺",
|
|
864
|
+
size: 36,
|
|
865
|
+
color: undefined,
|
|
866
|
+
iconColor: undefined,
|
|
867
|
+
accessibilityLabel: undefined,
|
|
868
|
+
};
|
|
869
|
+
|
|
870
|
+
/**
|
|
871
|
+
* Resolves the `resetButton` prop to a config or null (disabled). `true` →
|
|
872
|
+
* defaults, object → merged (`showFor` merges per key), falsy → null.
|
|
873
|
+
*/
|
|
874
|
+
export function resolveResetButton(
|
|
875
|
+
prop: boolean | ResetButtonConfig | undefined,
|
|
876
|
+
): ResolvedResetButtonConfig | null {
|
|
877
|
+
const resolved = resolveToggle(prop, RESET_BUTTON_DEFAULTS, false);
|
|
878
|
+
if (resolved === null || typeof prop !== "object") return resolved;
|
|
879
|
+
return {
|
|
880
|
+
...resolved,
|
|
881
|
+
showFor: { ...RESET_BUTTON_DEFAULTS.showFor, ...prop.showFor },
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
|
|
809
885
|
/**
|
|
810
886
|
* Resolves `timeScroll.returnToLive` to the "return to live" glide duration in ms,
|
|
811
887
|
* where `0` means an instant snap (no animation):
|