react-native-livechart 4.27.0 → 4.28.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.
- 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 +13 -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 +2 -0
- package/dist/core/useLiveChartEngine.d.ts.map +1 -1
- package/dist/core/useLiveChartSeriesEngine.d.ts +16 -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 +6 -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 +61 -6
- 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 +70 -14
- package/src/core/resolveConfig.ts +8 -5
- package/src/core/useLiveChartEngine.ts +2 -0
- package/src/core/useLiveChartSeriesEngine.ts +42 -6
- package/src/draw/grid.ts +15 -11
- package/src/draw/line.ts +23 -4
- 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 +2 -0
- 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 +57 -6
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { MOTION_METRICS_DEFAULTS } from "../constants";
|
|
2
2
|
import { lerp } from "../math/lerp";
|
|
3
3
|
import { rangeAnimationSpeed } from "../math/rangeAnimation";
|
|
4
|
-
import type {
|
|
4
|
+
import type { SeriesRangeAnimationConfig, SeriesConfig } from "../types";
|
|
5
5
|
|
|
6
6
|
export interface MultiEngineTickMutable {
|
|
7
7
|
displayMin: number;
|
|
8
8
|
displayMax: number;
|
|
9
|
+
/** Current clock before breathing-room buffer. */
|
|
10
|
+
currentTime?: number;
|
|
11
|
+
/** Unzoomed authoritative-history span, including buffer. */
|
|
12
|
+
fullHistoryWindow?: number | null;
|
|
13
|
+
/** Untransformed eased bounds, kept separate from presentation output. */
|
|
14
|
+
fittedMin?: number;
|
|
15
|
+
fittedMax?: number;
|
|
9
16
|
displayWindow: number;
|
|
10
17
|
timestamp: number;
|
|
11
18
|
/**
|
|
@@ -35,8 +42,10 @@ export interface MultiEngineTickInput {
|
|
|
35
42
|
canvasWidth: number;
|
|
36
43
|
canvasHeight: number;
|
|
37
44
|
timeWindow: number;
|
|
45
|
+
/** Authoritative Unix-second start for an expanding live history. */
|
|
46
|
+
historyStartTime?: number;
|
|
38
47
|
smoothing: number;
|
|
39
|
-
rangeAnimation?:
|
|
48
|
+
rangeAnimation?: SeriesRangeAnimationConfig;
|
|
40
49
|
exaggerate: boolean;
|
|
41
50
|
/** Extra catch-up speed added to `smoothing` when a series tip lags. Default `0.12`. */
|
|
42
51
|
adaptiveSpeedBoost?: number;
|
|
@@ -55,6 +64,8 @@ export interface MultiEngineTickInput {
|
|
|
55
64
|
nowSeconds?: number;
|
|
56
65
|
/** Override the engine's "now" (unix seconds). */
|
|
57
66
|
nowOverride?: number;
|
|
67
|
+
/** UI-thread presentation head, with precedence over nowOverride/wall time. */
|
|
68
|
+
presentationTime?: number;
|
|
58
69
|
/** Right-edge buffer as a fraction of the time window. */
|
|
59
70
|
windowBuffer?: number;
|
|
60
71
|
paused?: boolean;
|
|
@@ -110,8 +121,15 @@ export function tickLiveChartSeriesEngineFrame(
|
|
|
110
121
|
input: MultiEngineTickInput,
|
|
111
122
|
): void {
|
|
112
123
|
"worklet";
|
|
113
|
-
const baseNow = input.nowOverride ?? input.nowSeconds ?? Date.now() / 1000;
|
|
114
|
-
const
|
|
124
|
+
const baseNow = input.presentationTime ?? input.nowOverride ?? input.nowSeconds ?? Date.now() / 1000;
|
|
125
|
+
const buffer = input.windowBuffer ?? 0;
|
|
126
|
+
const start = input.historyStartTime;
|
|
127
|
+
const fullWindow = start != null && Number.isFinite(start) && start < baseNow &&
|
|
128
|
+
buffer >= 0 && buffer < 1 ? (baseNow - start) / (1 - buffer) : null;
|
|
129
|
+
state.currentTime = baseNow;
|
|
130
|
+
state.fullHistoryWindow = fullWindow;
|
|
131
|
+
const liveWindow = input.viewWindow ?? fullWindow ?? input.timeWindow;
|
|
132
|
+
const liveEdge = baseNow + buffer * liveWindow;
|
|
115
133
|
state.liveEdge = liveEdge;
|
|
116
134
|
const viewEnd = input.viewEnd;
|
|
117
135
|
// Earliest first-point time across visible series — the floor a frozen edge
|
|
@@ -123,6 +141,7 @@ export function tickLiveChartSeriesEngineFrame(
|
|
|
123
141
|
const d = input.series[i].data;
|
|
124
142
|
if (d.length > 0 && d[0].time < firstDataTime) firstDataTime = d[0].time;
|
|
125
143
|
}
|
|
144
|
+
if (fullWindow != null) firstDataTime = Math.min(firstDataTime, start!);
|
|
126
145
|
if (firstDataTime === Infinity) firstDataTime = -Infinity;
|
|
127
146
|
// Freeze the right edge while the gesture has parked `viewEnd` behind the live
|
|
128
147
|
// edge AND that edge still sits within the data; a stranded edge falls through
|
|
@@ -151,6 +170,11 @@ export function tickLiveChartSeriesEngineFrame(
|
|
|
151
170
|
|
|
152
171
|
if (input.canvasWidth === 0 || input.canvasHeight === 0) return;
|
|
153
172
|
|
|
173
|
+
// Never ease/fit against the previous frame's transformed output. Retain these
|
|
174
|
+
// even after removing the callback, so its expansion disappears in one frame.
|
|
175
|
+
state.displayMin = state.fittedMin ?? state.displayMin;
|
|
176
|
+
state.displayMax = state.fittedMax ?? state.displayMax;
|
|
177
|
+
|
|
154
178
|
const speed = input.smoothing;
|
|
155
179
|
// One-shot settle (snapKey change): collapse this frame's easing so the
|
|
156
180
|
// window / range / tips land on target instantly. See `MultiEngineTickInput.snap`.
|
|
@@ -170,23 +194,26 @@ export function tickLiveChartSeriesEngineFrame(
|
|
|
170
194
|
|
|
171
195
|
// Pinch-zoom: ease toward the zoom override when set, else the configured
|
|
172
196
|
// window (mirrors the single-series tick).
|
|
173
|
-
const
|
|
174
|
-
|
|
197
|
+
const anchoredFollowing = fullWindow != null && input.viewWindow == null &&
|
|
198
|
+
!scrolledBack && !input.paused && (input.returnT == null || input.returnT >= 1);
|
|
199
|
+
// Park the unzoomed width with a frozen edge. Resuming live restores the exact
|
|
200
|
+
// anchored span; easing a growing target would continuously lose the start.
|
|
201
|
+
const targetWindow = input.viewWindow ??
|
|
202
|
+
(fullWindow != null && (scrolledBack || input.paused) ? state.displayWindow : liveWindow);
|
|
203
|
+
state.displayWindow = snap || anchoredFollowing
|
|
175
204
|
? targetWindow
|
|
176
205
|
: lerp(state.displayWindow, targetWindow, speed, input.dt);
|
|
177
206
|
|
|
178
207
|
const winStart = state.timestamp - state.displayWindow;
|
|
179
208
|
const range = state.displayMax - state.displayMin;
|
|
180
209
|
|
|
181
|
-
//
|
|
182
|
-
//
|
|
183
|
-
//
|
|
184
|
-
// price while the line ends in the past. Mirrors single-series `edgeValue`.
|
|
185
|
-
// Reuses the gated `scrolledBack` computed above.
|
|
210
|
+
// Historical edges use recorded values. An edge parked in or beyond the
|
|
211
|
+
// future buffer still ends at the presentation head, using its presented
|
|
212
|
+
// value; samples after that head must never enter the tip or fitted range.
|
|
186
213
|
|
|
187
214
|
for (let i = 0; i < n; i++) {
|
|
188
215
|
let target = series[i].value;
|
|
189
|
-
if (scrolledBack) {
|
|
216
|
+
if (scrolledBack && state.timestamp < baseNow) {
|
|
190
217
|
const pts = series[i].data;
|
|
191
218
|
let elo = 0;
|
|
192
219
|
let ehi = pts.length;
|
|
@@ -205,7 +232,14 @@ export function tickLiveChartSeriesEngineFrame(
|
|
|
205
232
|
(1 - gapRatio) *
|
|
206
233
|
(input.adaptiveSpeedBoost ??
|
|
207
234
|
MOTION_METRICS_DEFAULTS.adaptiveSpeedBoost);
|
|
208
|
-
|
|
235
|
+
// History already owns the authoritative target at this timestamp. Easing
|
|
236
|
+
// it again would put the synthetic tip/dot behind the newest recorded point.
|
|
237
|
+
// Unrecorded prices and historical viewport edges retain normal smoothing.
|
|
238
|
+
const pts = series[i].data;
|
|
239
|
+
const latest = pts[pts.length - 1];
|
|
240
|
+
const targetIsRecorded = !scrolledBack && latest != null &&
|
|
241
|
+
latest.time <= Math.min(state.timestamp, baseNow) && latest.value === target;
|
|
242
|
+
state.displayValues[i] = snap || targetIsRecorded
|
|
209
243
|
? target
|
|
210
244
|
: lerp(cur, target, adaptiveSpeed, input.dt);
|
|
211
245
|
|
|
@@ -239,7 +273,8 @@ export function tickLiveChartSeriesEngineFrame(
|
|
|
239
273
|
// already track the edge value, so they stay in-range). Following live,
|
|
240
274
|
// keep the tail inclusive — feed timestamps can run slightly ahead of
|
|
241
275
|
// the local clock and must not flicker out of the range.
|
|
242
|
-
if (scrolledBack && points[j].time > state.timestamp)
|
|
276
|
+
if ((scrolledBack && points[j].time > state.timestamp) ||
|
|
277
|
+
(input.presentationTime != null && points[j].time > baseNow)) break;
|
|
243
278
|
const v = points[j].value;
|
|
244
279
|
/* istanbul ignore next -- trivial min/max */
|
|
245
280
|
if (v < tMin) tMin = v;
|
|
@@ -394,4 +429,25 @@ export function tickLiveChartSeriesEngineFrame(
|
|
|
394
429
|
);
|
|
395
430
|
}
|
|
396
431
|
}
|
|
432
|
+
|
|
433
|
+
state.fittedMin = state.displayMin;
|
|
434
|
+
state.fittedMax = state.displayMax;
|
|
435
|
+
const transform = input.rangeAnimation?.transform;
|
|
436
|
+
if (transform) {
|
|
437
|
+
const result = transform({
|
|
438
|
+
min: state.fittedMin,
|
|
439
|
+
max: state.fittedMax,
|
|
440
|
+
from: winStart,
|
|
441
|
+
to: state.timestamp,
|
|
442
|
+
});
|
|
443
|
+
if (
|
|
444
|
+
result &&
|
|
445
|
+
Number.isFinite(result.min) &&
|
|
446
|
+
Number.isFinite(result.max) &&
|
|
447
|
+
result.min < result.max
|
|
448
|
+
) {
|
|
449
|
+
state.displayMin = result.min;
|
|
450
|
+
state.displayMax = result.max;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
397
453
|
}
|
|
@@ -1069,12 +1069,15 @@ function resolvePerSeriesTooltip(
|
|
|
1069
1069
|
if (dash !== undefined) {
|
|
1070
1070
|
resolved.guideDashPattern = dash === true ? [3, 3] : dash || undefined;
|
|
1071
1071
|
}
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1072
|
+
// Boolean defaults may already be serialized/frozen by an earlier chart's
|
|
1073
|
+
// worklet. Normalize a fresh object rather than mutating that shared config.
|
|
1074
|
+
return {
|
|
1075
|
+
...resolved,
|
|
1076
|
+
maxLabelChars: Math.max(1, Math.floor(resolved.maxLabelChars)),
|
|
1077
|
+
bucketSeconds: resolved.bucketSeconds !== undefined && resolved.bucketSeconds > 0
|
|
1075
1078
|
? resolved.bucketSeconds
|
|
1076
|
-
: undefined
|
|
1077
|
-
|
|
1079
|
+
: undefined,
|
|
1080
|
+
};
|
|
1078
1081
|
}
|
|
1079
1082
|
|
|
1080
1083
|
/**
|
|
@@ -227,6 +227,8 @@ export interface MultiEngineState
|
|
|
227
227
|
series: SharedValue<SeriesConfig[]>;
|
|
228
228
|
displaySeriesValues: SharedValue<number[]>;
|
|
229
229
|
seriesOpacities: SharedValue<number[]>;
|
|
230
|
+
/** Presented tip time, clamped to the viewport edge; absent uses timestamp. */
|
|
231
|
+
tipTime?: SharedValue<number>;
|
|
230
232
|
}
|
|
231
233
|
|
|
232
234
|
export type EngineState = SingleEngineState | MultiEngineState;
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
type SharedValue,
|
|
11
11
|
} from "react-native-reanimated";
|
|
12
12
|
import { MS_PER_FRAME_60FPS, RETURN_TO_LIVE_MS } from "../constants";
|
|
13
|
-
import type { LiveChartPoint,
|
|
13
|
+
import type { LiveChartPoint, SeriesRangeAnimationConfig, SeriesConfig } from "../types";
|
|
14
14
|
import {
|
|
15
15
|
tickLiveChartSeriesEngineFrame,
|
|
16
16
|
type MultiEngineTickInput,
|
|
@@ -21,8 +21,9 @@ import type { ChartEngineScroll, MultiEngineState } from "./useLiveChartEngine";
|
|
|
21
21
|
export interface MultiSeriesEngineConfig {
|
|
22
22
|
series: SharedValue<SeriesConfig[]>;
|
|
23
23
|
timeWindow: number;
|
|
24
|
+
historyStartTime?: number;
|
|
24
25
|
smoothing: number;
|
|
25
|
-
rangeAnimation?:
|
|
26
|
+
rangeAnimation?: SeriesRangeAnimationConfig;
|
|
26
27
|
/** Extra catch-up speed added to `smoothing` when a series tip lags. */
|
|
27
28
|
adaptiveSpeedBoost?: number;
|
|
28
29
|
exaggerate?: boolean;
|
|
@@ -34,6 +35,7 @@ export interface MultiSeriesEngineConfig {
|
|
|
34
35
|
/** Positive, finite live Y-range multiplier (1 = auto-fit); read each frame. */
|
|
35
36
|
yRangeScale?: SharedValue<number>;
|
|
36
37
|
nowOverride?: number;
|
|
38
|
+
presentationTime?: SharedValue<number | undefined>;
|
|
37
39
|
windowBuffer?: number;
|
|
38
40
|
paused?: boolean;
|
|
39
41
|
/**
|
|
@@ -72,13 +74,16 @@ export interface MultiEngineFrameRefs {
|
|
|
72
74
|
seriesOpacities: SharedValue<number[]>;
|
|
73
75
|
displayMin: SharedValue<number>;
|
|
74
76
|
displayMax: SharedValue<number>;
|
|
77
|
+
/** Persist untransformed fitted bounds even without a scratch container. */
|
|
78
|
+
fittedMinSV?: SharedValue<number | undefined>;
|
|
79
|
+
fittedMaxSV?: SharedValue<number | undefined>;
|
|
75
80
|
displayWindow: SharedValue<number>;
|
|
76
81
|
timestamp: SharedValue<number>;
|
|
77
82
|
canvasWidth: SharedValue<number>;
|
|
78
83
|
canvasHeight: SharedValue<number>;
|
|
79
84
|
timeWindow: SharedValue<number>;
|
|
80
85
|
smoothing: SharedValue<number>;
|
|
81
|
-
rangeAnimationSV?: SharedValue<
|
|
86
|
+
rangeAnimationSV?: SharedValue<SeriesRangeAnimationConfig | undefined>;
|
|
82
87
|
adaptiveSpeedBoostSV?: SharedValue<number | undefined>;
|
|
83
88
|
exaggerateSV: SharedValue<boolean>;
|
|
84
89
|
referenceValue: SharedValue<number | undefined>;
|
|
@@ -88,6 +93,10 @@ export interface MultiEngineFrameRefs {
|
|
|
88
93
|
minRangeSV?: SharedValue<number | undefined>;
|
|
89
94
|
yRangeScaleSV?: SharedValue<number>;
|
|
90
95
|
nowOverrideSV?: SharedValue<number | undefined>;
|
|
96
|
+
presentationTimeSV?: SharedValue<number | undefined>;
|
|
97
|
+
historyStartTimeSV?: SharedValue<number | undefined>;
|
|
98
|
+
currentTimeSV?: SharedValue<number>;
|
|
99
|
+
fullHistoryWindowSV?: SharedValue<number | null>;
|
|
91
100
|
windowBufferSV?: SharedValue<number>;
|
|
92
101
|
pausedSV: SharedValue<boolean>;
|
|
93
102
|
/** Pan-scroll right-edge override (null = follow live). Optional for callers/tests. */
|
|
@@ -219,6 +228,8 @@ export function applyLiveChartSeriesEngineFrame(
|
|
|
219
228
|
};
|
|
220
229
|
state.displayMin = sv.displayMin.value;
|
|
221
230
|
state.displayMax = sv.displayMax.value;
|
|
231
|
+
state.fittedMin = sv.fittedMinSV?.value ?? state.fittedMin;
|
|
232
|
+
state.fittedMax = sv.fittedMaxSV?.value ?? state.fittedMax;
|
|
222
233
|
state.displayWindow = sv.displayWindow.value;
|
|
223
234
|
state.timestamp = sv.timestamp.value;
|
|
224
235
|
state.liveEdge = sv.liveEdgeSV?.value ?? 0;
|
|
@@ -254,6 +265,8 @@ export function applyLiveChartSeriesEngineFrame(
|
|
|
254
265
|
input.minRange = sv.minRangeSV?.value;
|
|
255
266
|
input.yRangeScale = sv.yRangeScaleSV?.value ?? 1;
|
|
256
267
|
input.nowOverride = sv.nowOverrideSV?.value;
|
|
268
|
+
input.presentationTime = sv.presentationTimeSV?.value;
|
|
269
|
+
input.historyStartTime = sv.historyStartTimeSV?.value;
|
|
257
270
|
input.windowBuffer = sv.windowBufferSV?.value ?? 0;
|
|
258
271
|
input.series = seriesSnap;
|
|
259
272
|
input.nowSeconds = Date.now() / 1000;
|
|
@@ -267,9 +280,13 @@ export function applyLiveChartSeriesEngineFrame(
|
|
|
267
280
|
tickLiveChartSeriesEngineFrame(state, input);
|
|
268
281
|
sv.displayMin.value = state.displayMin;
|
|
269
282
|
sv.displayMax.value = state.displayMax;
|
|
283
|
+
if (sv.fittedMinSV) sv.fittedMinSV.value = state.fittedMin;
|
|
284
|
+
if (sv.fittedMaxSV) sv.fittedMaxSV.value = state.fittedMax;
|
|
270
285
|
sv.displayWindow.value = state.displayWindow;
|
|
271
286
|
sv.timestamp.value = state.timestamp;
|
|
272
287
|
if (sv.liveEdgeSV) sv.liveEdgeSV.value = state.liveEdge;
|
|
288
|
+
if (sv.currentTimeSV) sv.currentTimeSV.value = state.currentTime!;
|
|
289
|
+
if (sv.fullHistoryWindowSV) sv.fullHistoryWindowSV.value = state.fullHistoryWindow ?? null;
|
|
273
290
|
sv.displaySeriesValues.value = state.displayValues;
|
|
274
291
|
sv.seriesOpacities.value = state.opacities;
|
|
275
292
|
sv.extremaMinValue.value = state.extremaMinValue;
|
|
@@ -301,11 +318,17 @@ export function applyLiveChartSeriesEngineFrame(
|
|
|
301
318
|
*/
|
|
302
319
|
export function useLiveChartSeriesEngine(
|
|
303
320
|
config: MultiSeriesEngineConfig,
|
|
304
|
-
): MultiEngineState & ChartEngineScroll {
|
|
321
|
+
): MultiEngineState & ChartEngineScroll & {
|
|
322
|
+
currentTime: SharedValue<number>;
|
|
323
|
+
fullHistoryWindow: SharedValue<number | null>;
|
|
324
|
+
} {
|
|
305
325
|
// Pinch-zoom window-width override (null = follow the configured window).
|
|
306
326
|
// Declared first so `timeWindow` folds it in — see useLiveChartEngine.
|
|
307
327
|
const viewWindow = useSharedValue<number | null>(null);
|
|
308
|
-
const
|
|
328
|
+
const fullHistoryWindow = useSharedValue<number | null>(null);
|
|
329
|
+
const timeWindow = useDerivedValue(() => viewWindow.value ?? fullHistoryWindow.value ?? config.timeWindow);
|
|
330
|
+
const configuredTimeWindow = useDerivedValue(() => config.timeWindow);
|
|
331
|
+
const historyStartTimeSV = useDerivedValue(() => config.historyStartTime);
|
|
309
332
|
const smoothing = useDerivedValue(() => config.smoothing);
|
|
310
333
|
const rangeAnimationSV = useDerivedValue(() => config.rangeAnimation);
|
|
311
334
|
const adaptiveSpeedBoostSV = useDerivedValue(() => config.adaptiveSpeedBoost);
|
|
@@ -332,12 +355,16 @@ export function useLiveChartSeriesEngine(
|
|
|
332
355
|
|
|
333
356
|
const displayMin = useSharedValue(0);
|
|
334
357
|
const displayMax = useSharedValue(1);
|
|
358
|
+
const fittedMinSV = useSharedValue<number | undefined>(undefined);
|
|
359
|
+
const fittedMaxSV = useSharedValue<number | undefined>(undefined);
|
|
335
360
|
const displayWindow = useSharedValue(config.timeWindow);
|
|
336
361
|
const canvasWidth = useSharedValue(0);
|
|
337
362
|
const canvasHeight = useSharedValue(0);
|
|
338
363
|
// Seed once; overwritten by the frame callback on the first tick.
|
|
339
364
|
const [initialTimestamp] = useState(() => Date.now() / 1000);
|
|
340
365
|
const timestamp = useSharedValue(initialTimestamp);
|
|
366
|
+
const currentTime = useSharedValue(initialTimestamp);
|
|
367
|
+
const tipTime = useDerivedValue(() => Math.min(timestamp.value, currentTime.value));
|
|
341
368
|
|
|
342
369
|
// Pan-scroll state (see useLiveChartEngine). Defaults to following live.
|
|
343
370
|
const viewEnd = useSharedValue<number | null>(null);
|
|
@@ -390,11 +417,15 @@ export function useLiveChartSeriesEngine(
|
|
|
390
417
|
seriesOpacities,
|
|
391
418
|
displayMin,
|
|
392
419
|
displayMax,
|
|
420
|
+
fittedMinSV,
|
|
421
|
+
fittedMaxSV,
|
|
393
422
|
displayWindow,
|
|
394
423
|
timestamp,
|
|
395
424
|
canvasWidth,
|
|
396
425
|
canvasHeight,
|
|
397
|
-
timeWindow,
|
|
426
|
+
timeWindow: configuredTimeWindow,
|
|
427
|
+
historyStartTimeSV,
|
|
428
|
+
fullHistoryWindowSV: fullHistoryWindow,
|
|
398
429
|
smoothing,
|
|
399
430
|
rangeAnimationSV,
|
|
400
431
|
adaptiveSpeedBoostSV,
|
|
@@ -406,6 +437,8 @@ export function useLiveChartSeriesEngine(
|
|
|
406
437
|
minRangeSV,
|
|
407
438
|
yRangeScaleSV: config.yRangeScale,
|
|
408
439
|
nowOverrideSV,
|
|
440
|
+
presentationTimeSV: config.presentationTime,
|
|
441
|
+
currentTimeSV: currentTime,
|
|
409
442
|
windowBufferSV,
|
|
410
443
|
pausedSV,
|
|
411
444
|
viewEndSV: viewEnd,
|
|
@@ -473,6 +506,9 @@ export function useLiveChartSeriesEngine(
|
|
|
473
506
|
viewEnd,
|
|
474
507
|
viewWindow,
|
|
475
508
|
liveEdge,
|
|
509
|
+
currentTime,
|
|
510
|
+
tipTime,
|
|
511
|
+
fullHistoryWindow,
|
|
476
512
|
series: renderedSeries,
|
|
477
513
|
displaySeriesValues,
|
|
478
514
|
seriesOpacities,
|
package/src/draw/grid.ts
CHANGED
|
@@ -17,6 +17,16 @@ export interface RightAnchoredYAxisColumnLayout {
|
|
|
17
17
|
gridEndX: number;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
/** Width used by the painted right-anchored column, including fading entries. */
|
|
21
|
+
export function widestYAxisLabelWidth(entries: readonly YAxisEntry[], font: SkFont): number {
|
|
22
|
+
"worklet";
|
|
23
|
+
let width = 0;
|
|
24
|
+
for (let i = 0; i < entries.length; i++) {
|
|
25
|
+
width = Math.max(width, measureFontTextWidth(font, entries[i].label));
|
|
26
|
+
}
|
|
27
|
+
return width;
|
|
28
|
+
}
|
|
29
|
+
|
|
20
30
|
/**
|
|
21
31
|
* Measure the widest Y-axis label and anchor the shared left-aligned column to
|
|
22
32
|
* a fixed canvas-edge margin. Shared by the axis, reference-line and live-badge
|
|
@@ -30,13 +40,7 @@ export function rightAnchoredYAxisColumnLayout(
|
|
|
30
40
|
gridEndGap = 0,
|
|
31
41
|
): RightAnchoredYAxisColumnLayout {
|
|
32
42
|
"worklet";
|
|
33
|
-
|
|
34
|
-
for (let i = 0; i < entries.length; i++) {
|
|
35
|
-
maxTextW = Math.max(
|
|
36
|
-
maxTextW,
|
|
37
|
-
measureFontTextWidth(font, entries[i].label),
|
|
38
|
-
);
|
|
39
|
-
}
|
|
43
|
+
const maxTextW = widestYAxisLabelWidth(entries, font);
|
|
40
44
|
const labelX = canvasWidth - labelRightMargin - maxTextW;
|
|
41
45
|
return { labelX, gridEndX: labelX - gridEndGap };
|
|
42
46
|
}
|
|
@@ -59,7 +63,7 @@ export function fixedGridEntries(
|
|
|
59
63
|
padTop: number,
|
|
60
64
|
count: number,
|
|
61
65
|
minGap: number,
|
|
62
|
-
formatValue: (v: number) => string,
|
|
66
|
+
formatValue: (v: number, tickStep?: number) => string,
|
|
63
67
|
): YAxisEntry[] {
|
|
64
68
|
"worklet";
|
|
65
69
|
// minGap floor: at most `floor(chartH / minGap)` gaps fit ⇒ that many + 1 labels.
|
|
@@ -73,7 +77,7 @@ export function fixedGridEntries(
|
|
|
73
77
|
for (let i = 0; i < n; i++) {
|
|
74
78
|
const y = padTop + i * stepPx;
|
|
75
79
|
const val = displayMax - i * stepVal;
|
|
76
|
-
entries.push({ y, label: formatValue(val), alpha: 1 });
|
|
80
|
+
entries.push({ y, label: formatValue(val, stepVal), alpha: 1 });
|
|
77
81
|
}
|
|
78
82
|
return entries;
|
|
79
83
|
}
|
|
@@ -164,7 +168,7 @@ export function computeGridEntries(
|
|
|
164
168
|
padBottom: number,
|
|
165
169
|
prevInterval: number,
|
|
166
170
|
labelAlphas: Record<number, number>,
|
|
167
|
-
formatValue: (v: number) => string,
|
|
171
|
+
formatValue: (v: number, tickStep?: number) => string,
|
|
168
172
|
dt: number,
|
|
169
173
|
minGap = 36,
|
|
170
174
|
grid: GridMetrics = GRID_METRICS_DEFAULTS,
|
|
@@ -299,7 +303,7 @@ export function computeGridEntries(
|
|
|
299
303
|
const y = gridValueToY(val, displayMax, valRange, padTop, chartH);
|
|
300
304
|
/* istanbul ignore next -- vertical clip; y stays in-band when val comes from tracked keys */
|
|
301
305
|
if (y < padTop - 10 || y > canvasHeight - padBottom + 10) continue;
|
|
302
|
-
entries.push({ y, label: formatValue(val), alpha });
|
|
306
|
+
entries.push({ y, label: formatValue(val, fine), alpha });
|
|
303
307
|
}
|
|
304
308
|
|
|
305
309
|
return { entries, interval };
|
package/src/draw/line.ts
CHANGED
|
@@ -237,10 +237,24 @@ export function seriesPlotClip(
|
|
|
237
237
|
};
|
|
238
238
|
}
|
|
239
239
|
|
|
240
|
+
/** Project a presented head, clamped to the viewport edge, into the plot. */
|
|
241
|
+
export function lineTipX(
|
|
242
|
+
viewportEnd: number,
|
|
243
|
+
windowSecs: number,
|
|
244
|
+
tipTime: number,
|
|
245
|
+
canvasWidth: number,
|
|
246
|
+
padding: ChartPadding,
|
|
247
|
+
): number {
|
|
248
|
+
"worklet";
|
|
249
|
+
const chartW = canvasWidth - padding.left - padding.right;
|
|
250
|
+
if (windowSecs <= 0 || chartW <= 0) return -100;
|
|
251
|
+
return padding.left + ((Math.min(viewportEnd, tipTime) - (viewportEnd - windowSecs)) / windowSecs) * chartW;
|
|
252
|
+
}
|
|
253
|
+
|
|
240
254
|
/**
|
|
241
255
|
* Build screen-space points as a flat number array [x0, y0, x1, y1, ...].
|
|
242
256
|
* Includes one point before the window for smooth left-edge entry,
|
|
243
|
-
* and appends a live tip at (now, displayValue).
|
|
257
|
+
* and appends a live tip at (min(now, tipTime), displayValue).
|
|
244
258
|
*
|
|
245
259
|
* Flat layout avoids ~150 tuple object allocations per frame.
|
|
246
260
|
*
|
|
@@ -263,6 +277,8 @@ export function buildLinePoints(
|
|
|
263
277
|
padding: ChartPadding,
|
|
264
278
|
out?: number[],
|
|
265
279
|
omitTipBeyondData = false,
|
|
280
|
+
/** Optional presentation head; viewport end still determines the projection. */
|
|
281
|
+
tipTime = now,
|
|
266
282
|
): number[] {
|
|
267
283
|
"worklet";
|
|
268
284
|
const pts: number[] = out ?? [];
|
|
@@ -275,6 +291,8 @@ export function buildLinePoints(
|
|
|
275
291
|
return pts;
|
|
276
292
|
|
|
277
293
|
const winStart = now - windowSecs;
|
|
294
|
+
const head = Math.min(now, tipTime);
|
|
295
|
+
if (head < winStart || windowSecs <= 0) return pts;
|
|
278
296
|
|
|
279
297
|
// Binary search for first point >= winStart
|
|
280
298
|
let lo = 0;
|
|
@@ -291,10 +309,11 @@ export function buildLinePoints(
|
|
|
291
309
|
let ehi = data.length;
|
|
292
310
|
while (elo < ehi) {
|
|
293
311
|
const emid = (elo + ehi) >> 1;
|
|
294
|
-
if (data[emid].time <=
|
|
312
|
+
if (data[emid].time <= head) elo = emid + 1;
|
|
295
313
|
else ehi = emid;
|
|
296
314
|
}
|
|
297
315
|
const endIdx = elo;
|
|
316
|
+
if (endIdx <= startIdx) return pts;
|
|
298
317
|
|
|
299
318
|
const xScale = chartW / windowSecs;
|
|
300
319
|
const yScale = chartH / valRange;
|
|
@@ -372,14 +391,14 @@ export function buildLinePoints(
|
|
|
372
391
|
if (
|
|
373
392
|
omitTipBeyondData &&
|
|
374
393
|
endIdx === data.length &&
|
|
375
|
-
data[endIdx - 1].time <
|
|
394
|
+
data[endIdx - 1].time < head
|
|
376
395
|
) {
|
|
377
396
|
return pts;
|
|
378
397
|
}
|
|
379
398
|
|
|
380
399
|
// Live tip at current time with smoothed value
|
|
381
400
|
pts.push(
|
|
382
|
-
|
|
401
|
+
lineTipX(now, windowSecs, head, canvasWidth, padding),
|
|
383
402
|
padding.top + ((displayMax - displayValue) / valRange) * chartH,
|
|
384
403
|
);
|
|
385
404
|
|
|
@@ -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) {
|