react-native-livechart 4.27.0 → 4.28.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/MultiSeriesDots.d.ts +1 -1
- package/dist/components/MultiSeriesDots.d.ts.map +1 -1
- package/dist/components/MultiSeriesValueLabels.d.ts +1 -1
- package/dist/components/MultiSeriesValueLabels.d.ts.map +1 -1
- package/dist/components/MultiSeriesValueLines.d.ts +1 -1
- package/dist/components/MultiSeriesValueLines.d.ts.map +1 -1
- package/dist/core/liveChartSeriesEngineTick.d.ts +15 -2
- package/dist/core/liveChartSeriesEngineTick.d.ts.map +1 -1
- package/dist/core/resolveConfig.d.ts.map +1 -1
- package/dist/core/useLiveChartEngine.d.ts +4 -0
- package/dist/core/useLiveChartEngine.d.ts.map +1 -1
- package/dist/core/useLiveChartSeriesEngine.d.ts +17 -4
- package/dist/core/useLiveChartSeriesEngine.d.ts.map +1 -1
- package/dist/draw/grid.d.ts +4 -2
- package/dist/draw/grid.d.ts.map +1 -1
- package/dist/draw/line.d.ts +8 -2
- package/dist/draw/line.d.ts.map +1 -1
- package/dist/hooks/crosshairSeries.d.ts +2 -2
- package/dist/hooks/crosshairSeries.d.ts.map +1 -1
- package/dist/hooks/resolveChartLayout.d.ts +10 -2
- package/dist/hooks/resolveChartLayout.d.ts.map +1 -1
- package/dist/hooks/useCrosshairSeries.d.ts +3 -1
- package/dist/hooks/useCrosshairSeries.d.ts.map +1 -1
- package/dist/hooks/useMultiSeriesDegen.d.ts +1 -1
- package/dist/hooks/useMultiSeriesDegen.d.ts.map +1 -1
- package/dist/hooks/useMultiSeriesLinePaths.d.ts.map +1 -1
- package/dist/hooks/usePinchZoom.d.ts +7 -4
- package/dist/hooks/usePinchZoom.d.ts.map +1 -1
- package/dist/hooks/useReferenceDrag.d.ts.map +1 -1
- package/dist/hooks/useYAxis.d.ts +1 -1
- package/dist/hooks/useYAxis.d.ts.map +1 -1
- package/dist/hooks/useYAxisLabelWidth.d.ts +6 -0
- package/dist/hooks/useYAxisLabelWidth.d.ts.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/math/referenceLines.d.ts.map +1 -1
- package/dist/types.d.ts +69 -7
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/components/LiveChart.tsx +20 -0
- package/src/components/LiveChartSeries.tsx +26 -6
- package/src/components/MultiSeriesDots.tsx +4 -2
- package/src/components/MultiSeriesValueLabels.tsx +4 -2
- package/src/components/MultiSeriesValueLines.tsx +4 -2
- package/src/core/liveChartSeriesEngineTick.ts +74 -14
- package/src/core/resolveConfig.ts +8 -5
- package/src/core/useLiveChartEngine.ts +4 -0
- package/src/core/useLiveChartSeriesEngine.ts +47 -6
- package/src/draw/grid.ts +15 -11
- package/src/draw/line.ts +36 -11
- package/src/hooks/crosshairSeries.ts +37 -5
- package/src/hooks/resolveChartLayout.ts +34 -3
- package/src/hooks/useCrosshairSeries.ts +28 -10
- package/src/hooks/useMultiSeriesDegen.ts +10 -5
- package/src/hooks/useMultiSeriesLinePaths.ts +8 -1
- package/src/hooks/usePinchZoom.ts +11 -4
- package/src/hooks/useReferenceDrag.ts +21 -3
- package/src/hooks/useYAxis.ts +1 -1
- package/src/hooks/useYAxisLabelWidth.ts +26 -0
- package/src/index.ts +1 -0
- package/src/math/referenceLines.ts +5 -0
- package/src/types.ts +65 -7
|
@@ -15,21 +15,51 @@ const TOOLTIP_SIDE_GAP = 8;
|
|
|
15
15
|
const TOOLTIP_STACK_GAP = 4;
|
|
16
16
|
const TOOLTIP_TOP_GAP = 4;
|
|
17
17
|
|
|
18
|
-
function valueAtTime(
|
|
18
|
+
function valueAtTime(
|
|
19
|
+
series: SeriesConfig,
|
|
20
|
+
time: number,
|
|
21
|
+
maxTime?: number,
|
|
22
|
+
tipValue?: number,
|
|
23
|
+
): number | null {
|
|
19
24
|
"worklet";
|
|
20
|
-
return interpolateAtTime(series.data, time);
|
|
25
|
+
if (maxTime === undefined) return interpolateAtTime(series.data, time);
|
|
26
|
+
const points = series.data;
|
|
27
|
+
if (points.length === 0 || points[0].time > maxTime) return null;
|
|
28
|
+
const target = Math.min(time, maxTime);
|
|
29
|
+
if (target >= maxTime && tipValue !== undefined) return tipValue;
|
|
30
|
+
if (target <= points[0].time) return points[0].value;
|
|
31
|
+
let lo = 0;
|
|
32
|
+
let hi = points.length;
|
|
33
|
+
while (lo < hi) {
|
|
34
|
+
const mid = (lo + hi) >> 1;
|
|
35
|
+
if (points[mid].time <= target) lo = mid + 1;
|
|
36
|
+
else hi = mid;
|
|
37
|
+
}
|
|
38
|
+
const before = points[lo - 1];
|
|
39
|
+
const next = points[lo];
|
|
40
|
+
// Interpolate only through presented history and the synthetic endpoint,
|
|
41
|
+
// matching the line builder rather than blending toward a buffered sample.
|
|
42
|
+
const endTime = next && next.time <= maxTime ? next.time : maxTime;
|
|
43
|
+
const endValue = next && next.time <= maxTime
|
|
44
|
+
? next.value
|
|
45
|
+
: (tipValue ?? before.value);
|
|
46
|
+
if (endTime <= before.time) return before.value;
|
|
47
|
+
return before.value + (endValue - before.value) *
|
|
48
|
+
((target - before.time) / (endTime - before.time));
|
|
21
49
|
}
|
|
22
50
|
|
|
23
51
|
export function interpolateSeriesAtTime(
|
|
24
52
|
series: SeriesConfig[],
|
|
25
53
|
time: number,
|
|
54
|
+
maxTime?: number,
|
|
55
|
+
displaySeriesValues?: number[],
|
|
26
56
|
): { primary: number | null; seriesValues: ScrubSeriesValue[] } {
|
|
27
57
|
"worklet";
|
|
28
58
|
const seriesValues: ScrubSeriesValue[] = [];
|
|
29
59
|
let primary: number | null = null;
|
|
30
60
|
for (let i = 0; i < series.length; i++) {
|
|
31
61
|
if (series[i].visible === false) continue;
|
|
32
|
-
const v = valueAtTime(series[i], time);
|
|
62
|
+
const v = valueAtTime(series[i], time, maxTime, displaySeriesValues?.[i]);
|
|
33
63
|
if (v === null) continue;
|
|
34
64
|
seriesValues.push({
|
|
35
65
|
id: series[i].id,
|
|
@@ -127,7 +157,7 @@ export function computePerSeriesTooltipLayout(
|
|
|
127
157
|
if (item.visible === false || item.data.length === 0) continue;
|
|
128
158
|
const value = pinned
|
|
129
159
|
? (displaySeriesValues[i] ?? item.value)
|
|
130
|
-
: valueAtTime(item, activeTime);
|
|
160
|
+
: valueAtTime(item, activeTime, maxTime, displaySeriesValues[i]);
|
|
131
161
|
if (value === null || !Number.isFinite(value)) continue;
|
|
132
162
|
|
|
133
163
|
const label = truncateSeriesTooltipLabel(
|
|
@@ -300,8 +330,10 @@ export function deriveScrubValueSeries(
|
|
|
300
330
|
scrubActive: boolean,
|
|
301
331
|
scrubTime: number,
|
|
302
332
|
series: SeriesConfig[],
|
|
333
|
+
maxTime?: number,
|
|
334
|
+
displaySeriesValues?: number[],
|
|
303
335
|
): number | null {
|
|
304
336
|
"worklet";
|
|
305
337
|
if (!scrubActive || scrubTime < 0) return null;
|
|
306
|
-
return interpolateSeriesAtTime(series, scrubTime).primary;
|
|
338
|
+
return interpolateSeriesAtTime(series, scrubTime, maxTime, displaySeriesValues).primary;
|
|
307
339
|
}
|
|
@@ -10,13 +10,19 @@ import {
|
|
|
10
10
|
resolvePadding,
|
|
11
11
|
type ChartPadding,
|
|
12
12
|
} from "../draw/line";
|
|
13
|
-
import type { BadgeMetrics, ChartInsets, LiveChartPalette } from "../types";
|
|
13
|
+
import type { BadgeMetrics, ChartInsets, LiveChartPalette, YAxisConfig } from "../types";
|
|
14
14
|
|
|
15
15
|
export interface ChartLayoutConfig {
|
|
16
16
|
palette: LiveChartPalette;
|
|
17
17
|
lineWidthOverride?: number;
|
|
18
18
|
insetsOverride?: ChartInsets;
|
|
19
19
|
yAxis: boolean;
|
|
20
|
+
/** Explicit canvas-edge margin for a right-anchored tick column. */
|
|
21
|
+
labelRightMargin?: number;
|
|
22
|
+
/** Widest actual tick string measured on the UI thread; undefined uses a sample. */
|
|
23
|
+
yAxisLabelWidth?: number;
|
|
24
|
+
/** Outer dot/ring radius, before optional pulse/glow. */
|
|
25
|
+
dotRadius?: number;
|
|
20
26
|
/** Float the y-axis over a full-width plot (no reserved right gutter). */
|
|
21
27
|
yAxisFloat?: boolean;
|
|
22
28
|
badge: boolean;
|
|
@@ -39,7 +45,7 @@ export interface ChartLayoutConfig {
|
|
|
39
45
|
/** Skia font for measuring label width. When provided with formatValue + currentValue, padding auto-sizes. */
|
|
40
46
|
font?: SkFont;
|
|
41
47
|
/** Worklet formatter — called on JS thread here to measure label width */
|
|
42
|
-
formatValue?: (v: number) => string;
|
|
48
|
+
formatValue?: (v: number, tickStep?: number) => string;
|
|
43
49
|
/** Current value read from SharedValue on JS thread — used to produce a sample label for measurement */
|
|
44
50
|
currentValue?: number;
|
|
45
51
|
/**
|
|
@@ -65,6 +71,16 @@ export interface ChartLayoutConfig {
|
|
|
65
71
|
multiSeriesMaxLabelWidth?: number;
|
|
66
72
|
}
|
|
67
73
|
|
|
74
|
+
/** Only auto-size a gutter for a non-floating, right-anchored tick column. */
|
|
75
|
+
export function shouldMeasureYAxisLabels(
|
|
76
|
+
axis: Pick<YAxisConfig, "side" | "labelRightMargin" | "float"> | null | undefined,
|
|
77
|
+
rightInset: number | undefined,
|
|
78
|
+
floating = axis?.float ?? false,
|
|
79
|
+
): boolean {
|
|
80
|
+
return axis?.side !== "left" && axis?.labelRightMargin != null &&
|
|
81
|
+
!floating && rightInset == null;
|
|
82
|
+
}
|
|
83
|
+
|
|
68
84
|
export interface ChartLayoutResult {
|
|
69
85
|
strokeWidth: number;
|
|
70
86
|
padding: ChartPadding;
|
|
@@ -111,7 +127,7 @@ export function resolveChartLayout(
|
|
|
111
127
|
(badgeUsesRightGutter || !config.badge || config.yAxis)
|
|
112
128
|
) {
|
|
113
129
|
const v = config.currentValue;
|
|
114
|
-
const samples = [v, v / 10, v / 100, v * 10].map(config.formatValue);
|
|
130
|
+
const samples = [v, v / 10, v / 100, v * 10].map(value => config.formatValue!(value));
|
|
115
131
|
measuredYAxisLabelWidth = Math.max(
|
|
116
132
|
...samples.map((s) => measureFontTextWidth(config.font!, s)),
|
|
117
133
|
);
|
|
@@ -168,6 +184,21 @@ export function resolveChartLayout(
|
|
|
168
184
|
);
|
|
169
185
|
}
|
|
170
186
|
|
|
187
|
+
// An edge-anchored column needs one label width and one radial clearance,
|
|
188
|
+
// unlike centered labels whose gutter reserves radial space on both sides.
|
|
189
|
+
if (config.yAxis && config.labelRightMargin != null && !config.yAxisFloat &&
|
|
190
|
+
config.insetsOverride?.right == null) {
|
|
191
|
+
const labelW = config.yAxisLabelWidth ??
|
|
192
|
+
((measuredYAxisLabelWidth ?? 49) + (config.font?.getSize() ?? 12) / 2);
|
|
193
|
+
const dotR = config.dotRadius ?? config.multiSeriesDotRadius ?? 0;
|
|
194
|
+
const radialClearance = Math.max(dotR, dotEffectOutset) + 6;
|
|
195
|
+
const clearance = config.multiSeriesValueLabel
|
|
196
|
+
? Math.max(radialClearance, dotR + 8 + (config.multiSeriesMaxLabelWidth ?? 0) + 8)
|
|
197
|
+
: radialClearance;
|
|
198
|
+
const axisPad = Math.ceil(config.labelRightMargin + labelW + clearance);
|
|
199
|
+
rightPad = badgeUsesRightGutter ? Math.max(rightPad, axisPad) : axisPad;
|
|
200
|
+
}
|
|
201
|
+
|
|
171
202
|
// ── Left inset ───────────────────────────────────────────────────────────
|
|
172
203
|
let leftPad: number;
|
|
173
204
|
if (config.insetsOverride?.left != null) {
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
import { scheduleOnRN } from "react-native-worklets";
|
|
11
11
|
import type { ResolvedPerSeriesTooltipConfig } from "../core/resolveConfig";
|
|
12
12
|
import type { MultiEngineState } from "../core/useLiveChartEngine";
|
|
13
|
-
import { type ChartPadding } from "../draw/line";
|
|
13
|
+
import { lineTipX, type ChartPadding } from "../draw/line";
|
|
14
14
|
import type { ScrubPointMulti } from "../types";
|
|
15
15
|
import {
|
|
16
16
|
computePerSeriesTooltipLayout,
|
|
@@ -77,9 +77,20 @@ export function useCrosshairSeries(
|
|
|
77
77
|
markerOptions?: ScrubMarkerOptions,
|
|
78
78
|
/** Price-axis ownership also guards against stale native long-press events. */
|
|
79
79
|
axisScaleActive?: SharedValue<boolean>,
|
|
80
|
+
/** Bottom strip reserved for the time-axis drag gesture, in layout points. */
|
|
81
|
+
scrubBottomExclude = 0,
|
|
80
82
|
): CrosshairState {
|
|
81
|
-
const
|
|
83
|
+
const pointerX = useSharedValue(-1);
|
|
82
84
|
const scrubActive = useSharedValue(false);
|
|
85
|
+
const scrubX = useDerivedValue(() => {
|
|
86
|
+
const x = pointerX.get();
|
|
87
|
+
const head = engine.tipTime?.get();
|
|
88
|
+
if (x < 0 || head === undefined) return x;
|
|
89
|
+
const end = engine.timestamp.get();
|
|
90
|
+
const window = engine.displayWindow.get();
|
|
91
|
+
if (head < end - window) return -1;
|
|
92
|
+
return Math.min(x, lineTipX(end, window, head, engine.canvasWidth.get(), padding));
|
|
93
|
+
});
|
|
83
94
|
// Tracks whether the active scrub phase actually began, so a tap that never
|
|
84
95
|
// activates doesn't emit a spurious onGestureEnd.
|
|
85
96
|
const gestureStarted = useSharedValue(false);
|
|
@@ -100,22 +111,26 @@ export function useCrosshairSeries(
|
|
|
100
111
|
const noScrollActive = useSharedValue(false);
|
|
101
112
|
const scrollActiveSV = scrollActive ?? noScrollActive;
|
|
102
113
|
|
|
103
|
-
const scrubTime = useDerivedValue(() =>
|
|
104
|
-
|
|
114
|
+
const scrubTime = useDerivedValue(() => {
|
|
115
|
+
const head = engine.tipTime?.get();
|
|
116
|
+
if (head !== undefined && head < engine.timestamp.get() - engine.displayWindow.get()) return -1;
|
|
117
|
+
return Math.min(head ?? Infinity, computeScrubTime(
|
|
105
118
|
scrubActive.get(),
|
|
106
119
|
scrubX.get(),
|
|
107
120
|
padding,
|
|
108
121
|
engine.canvasWidth.get(),
|
|
109
122
|
engine.timestamp.get(),
|
|
110
123
|
engine.displayWindow.get(),
|
|
111
|
-
)
|
|
112
|
-
);
|
|
124
|
+
));
|
|
125
|
+
});
|
|
113
126
|
|
|
114
127
|
const scrubValue = useDerivedValue(() =>
|
|
115
128
|
deriveScrubValueSeries(
|
|
116
129
|
scrubActive.get(),
|
|
117
130
|
scrubTime.get(),
|
|
118
131
|
engine.series.get(),
|
|
132
|
+
engine.tipTime?.get(),
|
|
133
|
+
engine.displaySeriesValues.get(),
|
|
119
134
|
),
|
|
120
135
|
);
|
|
121
136
|
|
|
@@ -190,7 +205,10 @@ export function useCrosshairSeries(
|
|
|
190
205
|
const x = scrubX.get();
|
|
191
206
|
const chartW = engine.canvasWidth.get() - padding.left - padding.right;
|
|
192
207
|
if (chartW <= 0) return "__pending__";
|
|
193
|
-
|
|
208
|
+
if (time < 0) return "__pending__";
|
|
209
|
+
const r = interpolateSeriesAtTime(
|
|
210
|
+
engine.series.get(), time, engine.tipTime?.get(), engine.displaySeriesValues.get(),
|
|
211
|
+
);
|
|
194
212
|
if (r.primary === null) return "__pending__";
|
|
195
213
|
const dotY = computeScrubDotY(
|
|
196
214
|
r.primary,
|
|
@@ -320,7 +338,7 @@ export function useCrosshairSeries(
|
|
|
320
338
|
padding,
|
|
321
339
|
engine.canvasWidth.get(),
|
|
322
340
|
clampToPlot,
|
|
323
|
-
|
|
341
|
+
pointerX,
|
|
324
342
|
scrubActive,
|
|
325
343
|
gestureStarted,
|
|
326
344
|
);
|
|
@@ -337,7 +355,7 @@ export function useCrosshairSeries(
|
|
|
337
355
|
padding,
|
|
338
356
|
engine.canvasWidth.get(),
|
|
339
357
|
clampToPlot,
|
|
340
|
-
|
|
358
|
+
pointerX,
|
|
341
359
|
scrubActive,
|
|
342
360
|
);
|
|
343
361
|
},
|
|
@@ -368,7 +386,7 @@ export function useCrosshairSeries(
|
|
|
368
386
|
// Gate by touch-down position so outside starts fail before activation and
|
|
369
387
|
// remain available to competing/parent gestures. Accepted pans keep tracking
|
|
370
388
|
// outside because `shouldCancelWhenOutside(false)` is set above.
|
|
371
|
-
const scrubHitSlop = resolveScrubHitSlop(padding, clampToPlot);
|
|
389
|
+
const scrubHitSlop = resolveScrubHitSlop(padding, clampToPlot, scrubBottomExclude);
|
|
372
390
|
if (scrubHitSlop) gesture = gesture.hitSlop(scrubHitSlop);
|
|
373
391
|
|
|
374
392
|
return {
|
|
@@ -10,7 +10,7 @@ import { runOnJS } from "react-native-worklets";
|
|
|
10
10
|
import { DEGEN_STRIDE } from "../constants";
|
|
11
11
|
import type { ResolvedDegenConfig } from "../core/resolveConfig";
|
|
12
12
|
import type { MultiEngineState } from "../core/useLiveChartEngine";
|
|
13
|
-
import type
|
|
13
|
+
import { lineTipX, type ChartPadding } from "../draw/line";
|
|
14
14
|
import { computeShake, spawnBurst, tickParticles } from "../math/degenTick";
|
|
15
15
|
import { detectMomentum } from "../math/momentum";
|
|
16
16
|
import type { DegenShakePayload } from "../types";
|
|
@@ -164,19 +164,24 @@ export function useMultiSeriesDegen(
|
|
|
164
164
|
|
|
165
165
|
const cw = engine.canvasWidth.get();
|
|
166
166
|
const ch = engine.canvasHeight.get();
|
|
167
|
-
const
|
|
167
|
+
const end = engine.timestamp.get();
|
|
168
|
+
const window = engine.displayWindow.get();
|
|
169
|
+
const head = engine.tipTime?.get() ?? end;
|
|
170
|
+
const ox = lineTipX(end, window, head, cw, padding);
|
|
171
|
+
const canSpawn = cw >= 1 && ch >= 1 && window > 0 &&
|
|
172
|
+
ox >= padding.left && ox <= cw - padding.right;
|
|
168
173
|
const dMin = engine.displayMin.get();
|
|
169
174
|
const dMax = engine.displayMax.get();
|
|
170
175
|
const valRange = dMax - dMin;
|
|
171
176
|
const chartH = ch - padding.top - padding.bottom;
|
|
172
|
-
const ox = cw - padding.right;
|
|
173
177
|
const allowDown = downSV.get() > 0.5;
|
|
174
178
|
|
|
175
179
|
// Each visible series fires a burst off its own dot on a fresh swing.
|
|
176
180
|
let firedDir = 0; // 0 none, 1 up, 2 down
|
|
177
181
|
for (let i = 0; i < n; i++) {
|
|
178
|
-
const visible = (ops[i] ?? 0) >= 0.5
|
|
179
|
-
|
|
182
|
+
const visible = (ops[i] ?? 0) >= 0.5 &&
|
|
183
|
+
s[i].data.length > 0 && s[i].data[0].time <= head;
|
|
184
|
+
const m = visible ? detectMomentum(s[i].data, 20, 0.12, head) : "flat";
|
|
180
185
|
const code = m === "up" ? 1 : m === "down" ? 2 : 0;
|
|
181
186
|
const prev = moms[i];
|
|
182
187
|
moms[i] = code;
|
|
@@ -69,9 +69,13 @@ export function useMultiSeriesLinePaths(
|
|
|
69
69
|
const absoluteXOffset =
|
|
70
70
|
windowSecs > 0 ? (now - windowSecs) * (chartWidth / windowSecs) : 0;
|
|
71
71
|
for (let i = 0; i < count; i++) {
|
|
72
|
+
const displayValue = displays[i] ?? s[i].value;
|
|
73
|
+
const latest = s[i].data[s[i].data.length - 1];
|
|
74
|
+
const liveTailValue = engine.smoothRecordedTail?.get() && latest?.value === s[i].value
|
|
75
|
+
? displayValue : undefined;
|
|
72
76
|
const rawPts = buildLinePoints(
|
|
73
77
|
s[i].data,
|
|
74
|
-
|
|
78
|
+
displayValue,
|
|
75
79
|
now,
|
|
76
80
|
windowSecs,
|
|
77
81
|
engine.displayMin.get(),
|
|
@@ -80,6 +84,9 @@ export function useMultiSeriesLinePaths(
|
|
|
80
84
|
engine.canvasHeight.get(),
|
|
81
85
|
padding,
|
|
82
86
|
pool.ptsBuf,
|
|
87
|
+
false,
|
|
88
|
+
engine.tipTime?.get(),
|
|
89
|
+
liveTailValue,
|
|
83
90
|
);
|
|
84
91
|
const seriesTolerance = s[i].simplify ?? simplifyTolerance;
|
|
85
92
|
const pts =
|
|
@@ -60,6 +60,8 @@ export interface UsePinchZoomOptions {
|
|
|
60
60
|
minTime: SharedValue<number>;
|
|
61
61
|
/** The configured `timeWindow` prop (seconds) — the anchor for the zoom bounds. */
|
|
62
62
|
timeWindow: number;
|
|
63
|
+
/** Current unzoomed history span, including breathing room; independent of retained samples. */
|
|
64
|
+
fullHistoryWindow?: SharedValue<number | null>;
|
|
63
65
|
/** Master switch. When false the gesture is disabled. */
|
|
64
66
|
enabled: boolean;
|
|
65
67
|
/** Tightest window (max zoom-in), seconds. Default `timeWindow / 8`. */
|
|
@@ -91,8 +93,9 @@ export function clampWindow(
|
|
|
91
93
|
|
|
92
94
|
/**
|
|
93
95
|
* Resolve the zoom bounds for a gesture. `maxWin` defaults to the data span but
|
|
94
|
-
*
|
|
95
|
-
* starting view); `minWin` defaults
|
|
96
|
+
* includes the configured window and authoritative full-history span (so you can
|
|
97
|
+
* restore the starting view). Explicit maxima take precedence; `minWin` defaults
|
|
98
|
+
* to an 8× zoom-in of the configured window.
|
|
96
99
|
* Guards against an inverted range (min ≤ max).
|
|
97
100
|
*/
|
|
98
101
|
export function zoomWindowBounds(
|
|
@@ -100,9 +103,10 @@ export function zoomWindowBounds(
|
|
|
100
103
|
span: number,
|
|
101
104
|
minCfg: number | undefined,
|
|
102
105
|
maxCfg: number | undefined,
|
|
106
|
+
fullHistoryWindow?: number | null,
|
|
103
107
|
): [number, number] {
|
|
104
108
|
"worklet";
|
|
105
|
-
const maxWin = Math.max(
|
|
109
|
+
const maxWin = maxCfg ?? Math.max(span, configWindow, fullHistoryWindow ?? 0);
|
|
106
110
|
let minWin = minCfg ?? configWindow / 8;
|
|
107
111
|
if (minWin > maxWin) minWin = maxWin;
|
|
108
112
|
return [minWin, maxWin];
|
|
@@ -158,6 +162,7 @@ export function usePinchZoom({
|
|
|
158
162
|
padding,
|
|
159
163
|
minTime,
|
|
160
164
|
timeWindow,
|
|
165
|
+
fullHistoryWindow,
|
|
161
166
|
enabled,
|
|
162
167
|
minTimeWindow,
|
|
163
168
|
maxTimeWindow,
|
|
@@ -184,7 +189,7 @@ export function usePinchZoom({
|
|
|
184
189
|
cancelAnimation(viewEnd);
|
|
185
190
|
cancelAnimation(viewWindow);
|
|
186
191
|
const edge = liveEdge.get();
|
|
187
|
-
startWindow.set(
|
|
192
|
+
startWindow.set(displayWindow.get());
|
|
188
193
|
startViewEnd.set(viewEnd.get() ?? edge);
|
|
189
194
|
onZoomStart?.();
|
|
190
195
|
};
|
|
@@ -210,6 +215,7 @@ export function usePinchZoom({
|
|
|
210
215
|
span,
|
|
211
216
|
minTimeWindow,
|
|
212
217
|
maxTimeWindow,
|
|
218
|
+
fullHistoryWindow?.get(),
|
|
213
219
|
);
|
|
214
220
|
// Pinch out (scale > 1) ⇒ narrower window ⇒ zoom in.
|
|
215
221
|
const newWin = clampWindow(sWin / e.scale, minWin, maxWin);
|
|
@@ -245,6 +251,7 @@ export function usePinchZoom({
|
|
|
245
251
|
canvasWidth,
|
|
246
252
|
displayWindow,
|
|
247
253
|
enabled,
|
|
254
|
+
fullHistoryWindow,
|
|
248
255
|
liveEdge,
|
|
249
256
|
maxTimeWindow,
|
|
250
257
|
minTime,
|
|
@@ -2,6 +2,7 @@ import { stackedTagAt, type ReferenceTagStack } from "../math/referenceTagStack"
|
|
|
2
2
|
import { useCallback, useMemo } from "react";
|
|
3
3
|
import { Gesture } from "react-native-gesture-handler";
|
|
4
4
|
import {
|
|
5
|
+
isSharedValue,
|
|
5
6
|
useAnimatedReaction,
|
|
6
7
|
useDerivedValue,
|
|
7
8
|
useSharedValue,
|
|
@@ -40,7 +41,8 @@ const EMPTY: never[] = [];
|
|
|
40
41
|
|
|
41
42
|
/**
|
|
42
43
|
* The value a drag at canvas `y` gives `line` against the current range: the
|
|
43
|
-
* price at that Y, snapped to `line.snap` and clamped to `
|
|
44
|
+
* price at that Y, snapped to `line.snap` and clamped to `bounds` (the line's
|
|
45
|
+
* `bounds`, read from its SharedValue when it has one). Null
|
|
44
46
|
* before the canvas is laid out. `atFinger`: the value the finger sets (what the
|
|
45
47
|
* range fits and `onChange` reports) — the nearest increment, rounded outward
|
|
46
48
|
* once the finger is past the plot's edge so even a coarse `snap` can widen the
|
|
@@ -49,6 +51,7 @@ const EMPTY: never[] = [];
|
|
|
49
51
|
*/
|
|
50
52
|
function dragValueAtY(
|
|
51
53
|
line: ReferenceLine,
|
|
54
|
+
bounds: [number, number] | undefined,
|
|
52
55
|
y: number,
|
|
53
56
|
displayMin: number,
|
|
54
57
|
displayMax: number,
|
|
@@ -74,7 +77,7 @@ function dragValueAtY(
|
|
|
74
77
|
y < padTop ? 1 : y > canvasHeight - padBottom ? -1 : 0,
|
|
75
78
|
)
|
|
76
79
|
: snapPriceWithin(raw, line.snap, displayMin, displayMax);
|
|
77
|
-
return clampToBounds(snapped,
|
|
80
|
+
return clampToBounds(snapped, bounds);
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
/**
|
|
@@ -177,6 +180,17 @@ export function useReferenceDrag(
|
|
|
177
180
|
return ranges;
|
|
178
181
|
});
|
|
179
182
|
|
|
183
|
+
// Per-line drag bounds (`ReferenceLine.bounds`), index-aligned with `lines`.
|
|
184
|
+
// The worklet captures the bounds themselves rather than `linesSV`, so a
|
|
185
|
+
// SharedValue bound is one of its inputs: moving it re-clamps the drag and
|
|
186
|
+
// re-checks onDragIn / onDragOut on the UI thread, without a render.
|
|
187
|
+
const boundsInputs = useMemo(() => lines.map((l) => l.bounds), [lines]);
|
|
188
|
+
const bounds = useDerivedValue(() => {
|
|
189
|
+
const out: ([number, number] | undefined)[] = [];
|
|
190
|
+
for (const b of boundsInputs) out.push(isSharedValue<[number, number]>(b) ? b.get() : b);
|
|
191
|
+
return out;
|
|
192
|
+
});
|
|
193
|
+
|
|
180
194
|
const dragIndex = useSharedValue(-1);
|
|
181
195
|
const startX = useSharedValue(0);
|
|
182
196
|
const startY = useSharedValue(0);
|
|
@@ -205,6 +219,7 @@ export function useReferenceDrag(
|
|
|
205
219
|
if (l === undefined || l.id !== grabbedId.get()) return values;
|
|
206
220
|
const v = dragValueAtY(
|
|
207
221
|
l,
|
|
222
|
+
bounds.get()[i],
|
|
208
223
|
lastY.get(),
|
|
209
224
|
displayMin.get(),
|
|
210
225
|
displayMax.get(),
|
|
@@ -309,7 +324,7 @@ export function useReferenceDrag(
|
|
|
309
324
|
continue;
|
|
310
325
|
}
|
|
311
326
|
const v = drawnValues.get()[i] ?? l.value;
|
|
312
|
-
out.push(referenceValueOut(v, dMin, dMax,
|
|
327
|
+
out.push(referenceValueOut(v, dMin, dMax, bounds.get()[i]));
|
|
313
328
|
}
|
|
314
329
|
return out;
|
|
315
330
|
},
|
|
@@ -412,6 +427,7 @@ export function useReferenceDrag(
|
|
|
412
427
|
}
|
|
413
428
|
const v = dragValueAtY(
|
|
414
429
|
l,
|
|
430
|
+
bounds.get()[i],
|
|
415
431
|
e.y - pointerOffsetY.get(),
|
|
416
432
|
displayMin.get(),
|
|
417
433
|
displayMax.get(),
|
|
@@ -442,6 +458,7 @@ export function useReferenceDrag(
|
|
|
442
458
|
const v =
|
|
443
459
|
dragValueAtY(
|
|
444
460
|
l,
|
|
461
|
+
bounds.get()[i],
|
|
445
462
|
lastY.get(),
|
|
446
463
|
displayMin.get(),
|
|
447
464
|
displayMax.get(),
|
|
@@ -511,6 +528,7 @@ export function useReferenceDrag(
|
|
|
511
528
|
}, [
|
|
512
529
|
activated,
|
|
513
530
|
anyDraggable,
|
|
531
|
+
bounds,
|
|
514
532
|
canvasHeight,
|
|
515
533
|
clearDrag,
|
|
516
534
|
displayMax,
|
package/src/hooks/useYAxis.ts
CHANGED
|
@@ -15,7 +15,7 @@ import type { GridMetrics } from "../types";
|
|
|
15
15
|
export function useYAxis(
|
|
16
16
|
engine: ChartEngineLayout,
|
|
17
17
|
padding: ChartPadding,
|
|
18
|
-
formatValue: (v: number) => string,
|
|
18
|
+
formatValue: (v: number, tickStep?: number) => string,
|
|
19
19
|
font: SkFont,
|
|
20
20
|
minGap = 36,
|
|
21
21
|
gridMetrics: GridMetrics = GRID_METRICS_DEFAULTS,
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { SkFont } from "@shopify/react-native-skia";
|
|
2
|
+
import { useAnimatedReaction, type SharedValue } from "react-native-reanimated";
|
|
3
|
+
import { scheduleOnRN } from "react-native-worklets";
|
|
4
|
+
import { widestYAxisLabelWidth, type YAxisEntry } from "../draw/grid";
|
|
5
|
+
|
|
6
|
+
/** Report the painted column width only when it changes, avoiding per-frame React work. */
|
|
7
|
+
export function useYAxisLabelWidth(
|
|
8
|
+
entries: SharedValue<YAxisEntry[]>,
|
|
9
|
+
font: SkFont,
|
|
10
|
+
enabled: boolean,
|
|
11
|
+
onWidthChange: (width: number | undefined) => void,
|
|
12
|
+
) {
|
|
13
|
+
useAnimatedReaction(
|
|
14
|
+
() => {
|
|
15
|
+
if (!enabled) return undefined;
|
|
16
|
+
const labels = entries.get();
|
|
17
|
+
return labels.length > 0 ? Math.ceil(widestYAxisLabelWidth(labels, font)) : undefined;
|
|
18
|
+
},
|
|
19
|
+
(width, previous) => {
|
|
20
|
+
if (width !== previous && (width !== undefined || previous != null)) {
|
|
21
|
+
scheduleOnRN(onWidthChange, width);
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
[entries, font, enabled, onWidthChange],
|
|
25
|
+
);
|
|
26
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { isSharedValue } from "react-native-reanimated";
|
|
2
|
+
|
|
1
3
|
import type {
|
|
2
4
|
FontWeight,
|
|
3
5
|
ReferenceLine,
|
|
@@ -94,6 +96,9 @@ export function referenceLineReactKeys(
|
|
|
94
96
|
|
|
95
97
|
/** Serializes render-relevant config without callback identity churn. */
|
|
96
98
|
function stableReferenceLineSignature(value: unknown): string {
|
|
99
|
+
// A SharedValue (e.g. live `bounds`) holds UI-thread data, not identity, and
|
|
100
|
+
// reading it here would read `.value` during render.
|
|
101
|
+
if (isSharedValue(value)) return "shared";
|
|
97
102
|
if (Array.isArray(value)) {
|
|
98
103
|
return `[${value.map(stableReferenceLineSignature).join(",")}]`;
|
|
99
104
|
}
|
package/src/types.ts
CHANGED
|
@@ -228,8 +228,13 @@ export interface ReferenceLine {
|
|
|
228
228
|
* can't be dragged past either end. Omit for unbounded (clamped only to the
|
|
229
229
|
* visible range). Reaching a bound fires {@link onDragOut}. Applies only while
|
|
230
230
|
* {@link draggable}.
|
|
231
|
+
*
|
|
232
|
+
* Pass a `SharedValue<[number, number]>` for a bound that follows a live value
|
|
233
|
+
* (e.g. a stop that can't cross the current price): the clamp and
|
|
234
|
+
* {@link onDragIn} / {@link onDragOut} read it on the UI thread, so moving it
|
|
235
|
+
* doesn't need a new `referenceLines` array or a re-render.
|
|
231
236
|
*/
|
|
232
|
-
bounds?: [number, number]
|
|
237
|
+
bounds?: [number, number] | SharedValue<[number, number]>;
|
|
233
238
|
/**
|
|
234
239
|
* Fired on the JS thread *while* dragging, each time the finger moves the line
|
|
235
240
|
* to a new (snapped, clamped) value. De-duplicated to value changes — not every
|
|
@@ -848,7 +853,9 @@ export interface YAxisConfig {
|
|
|
848
853
|
* Place price labels in a shared left-aligned column whose right edge sits
|
|
849
854
|
* this many pixels from the canvas edge. When omitted, labels keep the
|
|
850
855
|
* default centered-gutter placement. To line the live badge's value up with
|
|
851
|
-
* the column, set {@link BadgeConfig.textAlign} to `"yAxisColumn"`.
|
|
856
|
+
* the column, set {@link BadgeConfig.textAlign} to `"yAxisColumn"`. Automatic
|
|
857
|
+
* gutters use actual tick widths, this margin, dot-effect outset and 6px
|
|
858
|
+
* clearance. Explicit right insets and badge reservations retain priority.
|
|
852
859
|
*/
|
|
853
860
|
labelRightMargin?: number;
|
|
854
861
|
/**
|
|
@@ -1893,7 +1900,12 @@ export interface SeriesConfig {
|
|
|
1893
1900
|
id: string;
|
|
1894
1901
|
/** Array of data points for this series. */
|
|
1895
1902
|
data: LiveChartPoint[];
|
|
1896
|
-
/**
|
|
1903
|
+
/**
|
|
1904
|
+
* Latest live value. Ordinary live updates ease the dot and newest line sample
|
|
1905
|
+
* together with `smoothing`, even when this value is already in `data`.
|
|
1906
|
+
* Source history stays unchanged; an explicit presentationTime uses an
|
|
1907
|
+
* already-recorded presented value immediately.
|
|
1908
|
+
*/
|
|
1897
1909
|
value: number;
|
|
1898
1910
|
/** Line color. Defaults to the built-in series color palette. */
|
|
1899
1911
|
color?: string;
|
|
@@ -2272,7 +2284,9 @@ export interface ZoomConfig {
|
|
|
2272
2284
|
minTimeWindow?: number;
|
|
2273
2285
|
/**
|
|
2274
2286
|
* Widest visible window in seconds (max zoom-out). Defaults to the full data
|
|
2275
|
-
* span (you can zoom out to all retained history), never below `timeWindow
|
|
2287
|
+
* span (you can zoom out to all retained history), never below `timeWindow`
|
|
2288
|
+
* unless explicitly set. LiveChartSeries also includes
|
|
2289
|
+
* its anchored full-history span when historyStartTime is supplied.
|
|
2276
2290
|
*/
|
|
2277
2291
|
maxTimeWindow?: number;
|
|
2278
2292
|
}
|
|
@@ -2280,7 +2294,8 @@ export interface ZoomConfig {
|
|
|
2280
2294
|
/** Imperative methods exposed by `LiveChart` and `LiveChartSeries`. */
|
|
2281
2295
|
export interface LiveChartHandle {
|
|
2282
2296
|
/**
|
|
2283
|
-
* Reset built-in pinch/time-window zoom to the configured `timeWindow`
|
|
2297
|
+
* Reset built-in pinch/time-window zoom to the configured `timeWindow`
|
|
2298
|
+
* (or LiveChartSeries historyStartTime span) and
|
|
2284
2299
|
* clear the focal-point offset so the chart follows the live edge again.
|
|
2285
2300
|
* On LiveChart with viewport, clears the supplied end/window shared values.
|
|
2286
2301
|
* Safe to call when already reset. A paused chart remains paused.
|
|
@@ -2400,6 +2415,21 @@ export interface RangeAnimationConfig {
|
|
|
2400
2415
|
contractionSmoothing?: number;
|
|
2401
2416
|
}
|
|
2402
2417
|
|
|
2418
|
+
/** Y-range easing and optional presentation transform for {@link LiveChartSeries}. */
|
|
2419
|
+
export interface SeriesRangeAnimationConfig extends RangeAnimationConfig {
|
|
2420
|
+
/**
|
|
2421
|
+
* UI-thread worklet applied after normal fitting/easing. Receives untransformed
|
|
2422
|
+
* display bounds and the visible Unix-second interval; may read SharedValues.
|
|
2423
|
+
* Returned finite, increasing bounds affect presentation only and never feed
|
|
2424
|
+
* back into fitting. Invalid results are ignored. Removing it restores the
|
|
2425
|
+
* untransformed range. Hard bounds are applied to the fit before this transform.
|
|
2426
|
+
*/
|
|
2427
|
+
transform?: (range: { min: number; max: number; from: number; to: number }) => {
|
|
2428
|
+
min: number;
|
|
2429
|
+
max: number;
|
|
2430
|
+
};
|
|
2431
|
+
}
|
|
2432
|
+
|
|
2403
2433
|
/** Props shared between `LiveChart` and `LiveChartSeries`. */
|
|
2404
2434
|
export interface LiveChartCoreProps {
|
|
2405
2435
|
/** Color scheme. Default `"dark"`. */
|
|
@@ -2546,8 +2576,12 @@ export interface LiveChartCoreProps {
|
|
|
2546
2576
|
* points or committed candles. Default `"No data"`.
|
|
2547
2577
|
*/
|
|
2548
2578
|
emptyText?: string;
|
|
2549
|
-
/**
|
|
2550
|
-
|
|
2579
|
+
/**
|
|
2580
|
+
* Worklet formatter for value labels. Y-axis ticks pass their interval in
|
|
2581
|
+
* source units as `tickStep`; badges, tooltips and layout samples omit it.
|
|
2582
|
+
* Existing one-argument formatters remain valid. Default `v => v.toFixed(2)`.
|
|
2583
|
+
*/
|
|
2584
|
+
formatValue?: (v: number, tickStep?: number) => string;
|
|
2551
2585
|
/** Custom formatter for time labels. Default renders `HH:MM:SS`. */
|
|
2552
2586
|
formatTime?: (t: number) => string;
|
|
2553
2587
|
/** Y-axis grid lines + labels. `true` = defaults, `false` = hidden, or pass `YAxisConfig`. Default `true`. */
|
|
@@ -3029,6 +3063,30 @@ export interface LiveChartProps extends LiveChartCoreProps {
|
|
|
3029
3063
|
|
|
3030
3064
|
/** Props for the multi-series `LiveChartSeries` component. */
|
|
3031
3065
|
export interface LiveChartSeriesProps extends LiveChartCoreProps {
|
|
3066
|
+
/**
|
|
3067
|
+
* UI-thread presentation clock in Unix seconds. Each frame reads this head;
|
|
3068
|
+
* undefined falls back to nowOverride or wall time. Paths/dots end at the
|
|
3069
|
+
* earlier of the viewport edge and head, leaving windowBuffer empty. Samples
|
|
3070
|
+
* after this head are excluded from paths and range fitting. Scrub time limits
|
|
3071
|
+
* and values use the same cutoff, including callback payloads and tooltip
|
|
3072
|
+
* interpolation. Degen bursts originate at the presented dot. No React
|
|
3073
|
+
* render is required for clock updates.
|
|
3074
|
+
* An already-recorded value at this explicit head is presented immediately,
|
|
3075
|
+
* rather than receiving the ordinary live-feed tip easing a second time.
|
|
3076
|
+
*/
|
|
3077
|
+
presentationTime?: SharedValue<number | undefined>;
|
|
3078
|
+
/**
|
|
3079
|
+
* Authoritative history start (Unix seconds). While following without a zoom
|
|
3080
|
+
* override, grows the window to keep this time at the left edge, including
|
|
3081
|
+
* `windowBuffer` (must be < 1). Pan/zoom overrides remain usable; resetZoom()
|
|
3082
|
+
* restores following. A non-finite or future start falls back to timeWindow.
|
|
3083
|
+
*/
|
|
3084
|
+
historyStartTime?: number;
|
|
3085
|
+
/**
|
|
3086
|
+
* Shared-range easing plus an optional reversible UI-thread presentation
|
|
3087
|
+
* transform. See {@link SeriesRangeAnimationConfig}; recorded values remain unchanged.
|
|
3088
|
+
*/
|
|
3089
|
+
rangeAnimation?: SeriesRangeAnimationConfig;
|
|
3032
3090
|
/** Array of series definitions. Must be a SharedValue for UI-thread reads. */
|
|
3033
3091
|
series: SharedValue<SeriesConfig[]>;
|
|
3034
3092
|
/**
|