react-native-livechart 4.28.0 → 4.28.2

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.
@@ -27,6 +27,8 @@ export function useMultiSeriesDegen(
27
27
  padding: ChartPadding,
28
28
  cfg: ResolvedDegenConfig | null,
29
29
  onShake?: (payload: DegenShakePayload) => void,
30
+ /** Optional stable transform owned by a chart whose particle layer mounts lazily. */
31
+ shakeOutput?: SharedValue<[{ translateX: number }, { translateY: number }]>,
30
32
  ): {
31
33
  pack: SharedValue<Float64Array<ArrayBuffer>>;
32
34
  packRevision: SharedValue<number>;
@@ -152,6 +154,7 @@ export function useMultiSeriesDegen(
152
154
  shakeStart.set(0);
153
155
  shakeX.set(0);
154
156
  shakeY.set(0);
157
+ shakeOutput?.set([{ translateX: 0 }, { translateY: 0 }]);
155
158
  return;
156
159
  }
157
160
 
@@ -240,6 +243,8 @@ export function useMultiSeriesDegen(
240
243
  shakeY.set(0);
241
244
  }
242
245
 
246
+ shakeOutput?.set([{ translateX: shakeX.get() }, { translateY: shakeY.get() }]);
247
+
243
248
  const activeCount = tickParticles(
244
249
  buf,
245
250
  slots,
@@ -257,13 +262,17 @@ export function useMultiSeriesDegen(
257
262
  },
258
263
  );
259
264
 
260
- const shakeTransform = useDerivedValue(() => {
261
- "worklet";
262
- return [{ translateX: shakeX.get() }, { translateY: shakeY.get() }] as [
263
- { translateX: number },
264
- { translateY: number },
265
- ];
266
- });
265
+ const shakeTransform = useDerivedValue(
266
+ shakeOutput
267
+ ? () => {
268
+ "worklet";
269
+ return [{ translateX: 0 }, { translateY: 0 }] as [{ translateX: number }, { translateY: number }];
270
+ }
271
+ : () => {
272
+ "worklet";
273
+ return [{ translateX: shakeX.get() }, { translateY: shakeY.get() }] as [{ translateX: number }, { translateY: number }];
274
+ },
275
+ );
267
276
 
268
- return { pack, packRevision, particleTimestamp, shakeTransform };
277
+ return { pack, packRevision, particleTimestamp, shakeTransform: shakeOutput ?? shakeTransform };
269
278
  }
@@ -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
- displays[i] ?? s[i].value,
78
+ displayValue,
75
79
  now,
76
80
  windowSecs,
77
81
  engine.displayMin.get(),
@@ -82,6 +86,7 @@ export function useMultiSeriesLinePaths(
82
86
  pool.ptsBuf,
83
87
  false,
84
88
  engine.tipTime?.get(),
89
+ liveTailValue,
85
90
  );
86
91
  const seriesTolerance = s[i].simplify ?? simplifyTolerance;
87
92
  const pts =
@@ -0,0 +1,133 @@
1
+ import { useCallback, useLayoutEffect, useRef, useState } from "react";
2
+ import { Platform } from "react-native";
3
+ import { useAnimatedReaction, useSharedValue, type SharedValue } from "react-native-reanimated";
4
+ import { executeOnUIRuntimeSync, scheduleOnRN } from "react-native-worklets";
5
+ import { lineColorsSignatureFromArray, lineStyleSignatureFromArray } from "../core/multiSeriesLayout";
6
+ import type { SeriesConfig } from "../types";
7
+ import { shouldResampleLayoutValue } from "./resolveChartLayout";
8
+
9
+ /** React only needs presentation fields; point histories stay on the UI runtime. */
10
+ export function seriesPresentationSignature(series: SeriesConfig[]): string {
11
+ "worklet";
12
+ let signature = lineColorsSignatureFromArray(series) + "\x1d" + lineStyleSignatureFromArray(series);
13
+ for (let i = 0; i < series.length; i++) {
14
+ const s = series[i];
15
+ signature += `\x1d${s.label ?? s.id}\x1f${s.visible === false ? 0 : 1}\x1f${s.kind ?? ""}\x1f${s.valueLabel ?? ""}`;
16
+ }
17
+ return signature;
18
+ }
19
+
20
+ export function seriesPresentationValue(series: SeriesConfig[]): number {
21
+ "worklet";
22
+ let sample = 0;
23
+ for (let i = 0; i < series.length; i++) {
24
+ const value = series[i].value;
25
+ if (Number.isFinite(value) && Math.abs(value) > Math.abs(sample)) sample = value;
26
+ }
27
+ return sample;
28
+ }
29
+
30
+ export function projectSeriesPresentation(series: SeriesConfig[]): SeriesConfig[] {
31
+ "worklet";
32
+ const snapshot: SeriesConfig[] = [];
33
+ for (let i = 0; i < series.length; i++) {
34
+ const s = series[i];
35
+ snapshot.push({
36
+ id: s.id, label: s.label, color: s.color, visible: s.visible,
37
+ style: s.style, intervals: s.intervals, strokeWidth: s.strokeWidth,
38
+ glow: s.glow, kind: s.kind, valueLabel: s.valueLabel,
39
+ value: s.value, data: [],
40
+ });
41
+ }
42
+ return snapshot;
43
+ }
44
+
45
+ interface Presentation {
46
+ epoch: number;
47
+ revision: number;
48
+ signature: string;
49
+ series: SeriesConfig[];
50
+ sample: number | undefined;
51
+ }
52
+
53
+ /** Bootstrap before paint, then publish config/layout changes together. */
54
+ export function useSeriesPresentation(series: SharedValue<SeriesConfig[]>) {
55
+ const currentSource = useRef<SharedValue<SeriesConfig[]> | null>(null);
56
+ const epochCounter = useRef(0);
57
+ const acceptedRevision = useRef(0);
58
+ const activeEpoch = useSharedValue(0);
59
+ const revision = useSharedValue(0);
60
+ const published = useSharedValue({ signature: "", sample: 0 });
61
+ // Read only compact metadata during lazy state initialization. This worklet
62
+ // has no writes or subscriptions, so an abandoned render has no UI effects.
63
+ const [presentation, setPresentation] = useState<Presentation>(() => {
64
+ const readInitial = () => {
65
+ "worklet";
66
+ const arr = series.get();
67
+ const value = seriesPresentationValue(arr);
68
+ return {
69
+ epoch: 1, revision: 0, signature: seriesPresentationSignature(arr),
70
+ series: projectSeriesPresentation(arr), sample: value !== 0 ? value : undefined,
71
+ };
72
+ };
73
+ return Platform.OS === "web" ? readInitial() : executeOnUIRuntimeSync(readInitial)();
74
+ });
75
+ const apply = useCallback((next: Presentation) => {
76
+ if (currentSource.current !== series || next.epoch !== epochCounter.current ||
77
+ next.revision <= acceptedRevision.current) return;
78
+ acceptedRevision.current = next.revision;
79
+ setPresentation(previous => previous.epoch === next.epoch &&
80
+ previous.signature === next.signature && previous.sample === next.sample ? previous : next);
81
+ }, [series]);
82
+
83
+ const refresh = useCallback(() => {
84
+ const epoch = epochCounter.current;
85
+ const read = () => {
86
+ "worklet";
87
+ const arr = series.get();
88
+ const signature = seriesPresentationSignature(arr);
89
+ const value = seriesPresentationValue(arr);
90
+ activeEpoch.set(epoch);
91
+ revision.set(revision.get() + 1);
92
+ published.set({ signature, sample: value });
93
+ return {
94
+ epoch, revision: revision.get(), signature,
95
+ series: projectSeriesPresentation(arr), sample: value !== 0 ? value : undefined,
96
+ };
97
+ };
98
+ // Web has one runtime and does not support executeOnUIRuntimeSync.
99
+ apply(Platform.OS === "web" ? read() : executeOnUIRuntimeSync(read)());
100
+ }, [series, activeEpoch, revision, published, apply]);
101
+
102
+ useLayoutEffect(() => {
103
+ currentSource.current = series;
104
+ epochCounter.current += 1;
105
+ refresh();
106
+ return () => { currentSource.current = null; };
107
+ }, [series, refresh]);
108
+
109
+ const epoch = presentation.epoch;
110
+ useAnimatedReaction(
111
+ () => {
112
+ const arr = series.get();
113
+ return { signature: seriesPresentationSignature(arr), value: seriesPresentationValue(arr) };
114
+ },
115
+ (current) => {
116
+ // Old mappers can finish after a prop change; neither their sample nor
117
+ // an already queued JS publication may overwrite the new source.
118
+ if (epoch !== activeEpoch.get()) return;
119
+ const previous = published.get();
120
+ const resample = shouldResampleLayoutValue(current.value, previous.sample);
121
+ if (current.signature === previous.signature && !resample) return;
122
+ const sample = resample ? current.value : previous.sample;
123
+ published.set({ signature: current.signature, sample });
124
+ revision.set(revision.get() + 1);
125
+ scheduleOnRN(apply, {
126
+ epoch, revision: revision.get(), signature: current.signature,
127
+ series: projectSeriesPresentation(series.get()), sample: sample !== 0 ? sample : undefined,
128
+ });
129
+ },
130
+ [series, epoch, activeEpoch, revision, published, apply],
131
+ );
132
+ return { snapshot: presentation.series, valueLayoutSample: presentation.sample, refresh };
133
+ }
package/src/types.ts CHANGED
@@ -1900,7 +1900,12 @@ export interface SeriesConfig {
1900
1900
  id: string;
1901
1901
  /** Array of data points for this series. */
1902
1902
  data: LiveChartPoint[];
1903
- /** Latest (live) value for smooth interpolation. */
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
+ */
1904
1909
  value: number;
1905
1910
  /** Line color. Defaults to the built-in series color palette. */
1906
1911
  color?: string;
@@ -2410,7 +2415,10 @@ export interface RangeAnimationConfig {
2410
2415
  contractionSmoothing?: number;
2411
2416
  }
2412
2417
 
2413
- /** Y-range easing and optional presentation transform for {@link LiveChartSeries}. */
2418
+ /**
2419
+ * Y-range easing and optional presentation transform for {@link LiveChartSeries}.
2420
+ * The first measured, visible data fits immediately; easing applies afterward.
2421
+ */
2414
2422
  export interface SeriesRangeAnimationConfig extends RangeAnimationConfig {
2415
2423
  /**
2416
2424
  * UI-thread worklet applied after normal fitting/easing. Receives untransformed
@@ -3066,6 +3074,8 @@ export interface LiveChartSeriesProps extends LiveChartCoreProps {
3066
3074
  * and values use the same cutoff, including callback payloads and tooltip
3067
3075
  * interpolation. Degen bursts originate at the presented dot. No React
3068
3076
  * render is required for clock updates.
3077
+ * An already-recorded value at this explicit head is presented immediately,
3078
+ * rather than receiving the ordinary live-feed tip easing a second time.
3069
3079
  */
3070
3080
  presentationTime?: SharedValue<number | undefined>;
3071
3081
  /**