react-native-livechart 4.28.2 → 4.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +1 -1
  2. package/dist/components/LiveChart.d.ts.map +1 -1
  3. package/dist/components/LiveChartSeries.d.ts.map +1 -1
  4. package/dist/components/LoadingOverlay.d.ts +6 -1
  5. package/dist/components/LoadingOverlay.d.ts.map +1 -1
  6. package/dist/constants.d.ts +6 -0
  7. package/dist/constants.d.ts.map +1 -1
  8. package/dist/core/liveChartEngineTick.d.ts +3 -1
  9. package/dist/core/liveChartEngineTick.d.ts.map +1 -1
  10. package/dist/core/resolveConfig.d.ts +4 -1
  11. package/dist/core/resolveConfig.d.ts.map +1 -1
  12. package/dist/core/useLiveChartEngine.d.ts +2 -0
  13. package/dist/core/useLiveChartEngine.d.ts.map +1 -1
  14. package/dist/draw/line.d.ts.map +1 -1
  15. package/dist/hooks/useBadge.d.ts.map +1 -1
  16. package/dist/hooks/useChartPaths.d.ts +3 -1
  17. package/dist/hooks/useChartPaths.d.ts.map +1 -1
  18. package/dist/hooks/useDemandFrameLoop.d.ts.map +1 -1
  19. package/dist/hooks/useLiveDot.d.ts +5 -2
  20. package/dist/hooks/useLiveDot.d.ts.map +1 -1
  21. package/dist/hooks/useReferenceLine.d.ts.map +1 -1
  22. package/dist/math/squiggly.d.ts +12 -0
  23. package/dist/math/squiggly.d.ts.map +1 -1
  24. package/dist/types.d.ts +44 -11
  25. package/dist/types.d.ts.map +1 -1
  26. package/package.json +2 -2
  27. package/src/components/LiveChart.tsx +14 -2
  28. package/src/components/LiveChartSeries.tsx +3 -0
  29. package/src/components/LoadingOverlay.tsx +44 -6
  30. package/src/constants.ts +7 -0
  31. package/src/core/liveChartEngineTick.ts +23 -2
  32. package/src/core/resolveConfig.ts +12 -2
  33. package/src/core/useLiveChartEngine.ts +8 -2
  34. package/src/draw/line.ts +13 -5
  35. package/src/hooks/useBadge.ts +18 -5
  36. package/src/hooks/useChartPaths.ts +6 -4
  37. package/src/hooks/useDemandFrameLoop.ts +7 -2
  38. package/src/hooks/useLiveDot.ts +26 -11
  39. package/src/hooks/useReferenceLine.ts +4 -1
  40. package/src/math/squiggly.ts +27 -0
  41. package/src/types.ts +44 -11
@@ -64,6 +64,8 @@ export function useChartPaths(
64
64
  simplifyTolerance = 0,
65
65
  /** Explicit empty intervals that split line and fill geometry. */
66
66
  lineGaps: CandleGap[] = [],
67
+ /** Controlled or buffered viewports must not fabricate prices in future space. */
68
+ shouldOmitTipBeyondData = false,
67
69
  ) {
68
70
  const lineBuilder = usePathBuilder();
69
71
  const fillBuilder = usePathBuilder();
@@ -148,10 +150,10 @@ export function useChartPaths(
148
150
  engine.canvasHeight.get(),
149
151
  padding,
150
152
  simplify > 0 ? cache.rawPts : buf,
151
- // Only a live-following chart may extend the line to the right edge; a
152
- // parked (scrolled-back / overscrolled) window ends at its last real
153
- // point rather than fabricating a flat line into dataless space.
154
- engine.viewEnd.get() != null,
153
+ // Only an unbuffered live-following chart may extend to the right edge;
154
+ // controlled or parked windows end at their last real point rather than
155
+ // fabricating a flat line into dataless space.
156
+ shouldOmitTipBeyondData || engine.viewEnd.get() != null,
155
157
  );
156
158
  const chartWidth = canvasWidth - padding.left - padding.right;
157
159
  const absoluteXOffset =
@@ -2,6 +2,8 @@ import { useCallback, useEffect } from "react";
2
2
  import { useAnimatedReaction, useSharedValue } from "react-native-reanimated";
3
3
  import { scheduleOnUI } from "react-native-worklets";
4
4
 
5
+ import { MAX_FRAME_DELTA_MS } from "../constants";
6
+
5
7
  /** Wait through several unchanged frames, rather than mistaking one quiet frame for rest. */
6
8
  export const IDLE_SETTLE_MS = 160;
7
9
 
@@ -10,7 +12,7 @@ function noReaction() { "worklet"; }
10
12
 
11
13
  export function nextQuietTime(previous: number, changed: boolean, dt: number): number {
12
14
  "worklet";
13
- return changed ? 0 : previous + Math.min(Math.max(dt, 0), 34);
15
+ return changed ? 0 : previous + Math.min(Math.max(dt, 0), MAX_FRAME_DELTA_MS);
14
16
  }
15
17
 
16
18
  /**
@@ -41,7 +43,10 @@ export function useDemandFrameLoop(
41
43
  "worklet";
42
44
  if (!mounted.get() || generation.get() !== token) return;
43
45
  // Resuming never integrates the entire sleeping interval as a single frame.
44
- const dt = previous === null ? 1000 / 60 : Math.min(timestamp - previous, 34);
46
+ const dt =
47
+ previous === null
48
+ ? 1000 / 60
49
+ : Math.min(timestamp - previous, MAX_FRAME_DELTA_MS);
45
50
  previous = timestamp;
46
51
  const quiet = nextQuietTime(quietTime.get(), tick(dt), dt);
47
52
  quietTime.set(quiet);
@@ -7,7 +7,7 @@ import type { ChartPadding } from "../draw/line";
7
7
 
8
8
  /**
9
9
  * Derive the live dot position (right edge of the chart, mapped to current value).
10
- * Returns `{ dotX, dotY }` as shared values. Coordinates are set to `-100`
10
+ * Returns `{ dotX, dotY, indicatorValue }` as shared values. Coordinates are set to `-100`
11
11
  * (off-screen sentinel) when canvas dimensions are unavailable.
12
12
  *
13
13
  * With `followViewEdge` + `edgeValue`, the dot (and the value line that shares
@@ -26,15 +26,34 @@ export function useLiveDot(
26
26
  edgeValue?: SharedValue<number>,
27
27
  followViewEdge = false,
28
28
  trackWhileParked = false,
29
+ /** Keep the dot on recorded geometry when the viewport includes future space. */
30
+ shouldOmitTipBeyondData = false,
29
31
  ) {
32
+ // A line with no synthetic tip ends at its recorded price. Share that value
33
+ // with the badge so its text, the value line, and the dot stay aligned even
34
+ // while the engine's live value is still easing toward a new tick.
35
+ const indicatorValue = useDerivedValue(() => {
36
+ if (followViewEdge && edgeValue) return edgeValue.value;
37
+ if (shouldOmitTipBeyondData) {
38
+ const data = engine.data.value;
39
+ const last = data[data.length - 1];
40
+ if (last && last.time <= engine.timestamp.value) return last.value;
41
+ }
42
+ return engine.displayValue.value;
43
+ });
44
+
30
45
  const dotX = useDerivedValue(() => {
31
46
  const w = engine.canvasWidth.value;
32
47
  if (w === 0) return -100;
33
48
  const right = w - padding.right;
34
- // While parked (scrolled back / overscrolled) the live point is not at the
35
- // plot edge — track its real x so a pan doesn't lose the dot, and hide it
36
- // only once the point leaves the window.
37
- if (trackWhileParked && !followViewEdge && engine.viewEnd.value != null) {
49
+ // Buffered or parked windows can put the last point before the plot edge.
50
+ // Track its real x so a pan doesn't lose the dot, and hide it only once
51
+ // the point leaves the window.
52
+ if (
53
+ !followViewEdge &&
54
+ (shouldOmitTipBeyondData ||
55
+ (trackWhileParked && engine.viewEnd.value != null))
56
+ ) {
38
57
  const data = engine.data.value;
39
58
  const last = data[data.length - 1];
40
59
  const win = engine.displayWindow.value;
@@ -56,12 +75,8 @@ export function useLiveDot(
56
75
  const dMax = engine.displayMax.value;
57
76
  const valRange = dMax - dMin;
58
77
  if (valRange === 0) return padding.top + chartH / 2;
59
- const v =
60
- followViewEdge && edgeValue
61
- ? edgeValue.value
62
- : engine.displayValue.value;
63
- return padding.top + ((dMax - v) / valRange) * chartH;
78
+ return padding.top + ((dMax - indicatorValue.value) / valRange) * chartH;
64
79
  });
65
80
 
66
- return { dotX, dotY } as const;
81
+ return { dotX, dotY, indicatorValue } as const;
67
82
  }
@@ -613,7 +613,10 @@ export function computeReferenceLineLayout(
613
613
  const x = bounds.left + (cfg.offsetX ?? 0);
614
614
  const y = base.y + (cfg.offsetY ?? 0);
615
615
  let textX = x + (pillW - contentW) / 2 + chevronW;
616
- // Match live-badge text alignment when its column mode is enabled.
616
+ // Match live-badge text alignment when its column mode is enabled. The
617
+ // price pill has no tail to give its left padding to, so it keeps both
618
+ // paddings and centers a value that doesn't fit them (a tight live badge
619
+ // aligns instead).
617
620
  if (
618
621
  cfg.position === "axis" &&
619
622
  valueAxis?.alignTextWithYAxis &&
@@ -1,3 +1,4 @@
1
+ import { MAX_FRAME_DELTA_MS } from "../constants";
1
2
  import type { ChartPadding } from "../draw/line";
2
3
 
3
4
  /**
@@ -39,6 +40,32 @@ export function squiggleClockSeconds(): number {
39
40
  return Date.now() / 1000;
40
41
  }
41
42
 
43
+ /**
44
+ * The squiggle's animated time (ms) after one more frame, toward
45
+ * `LoadingConfig.freezeAfterMs`. A frame counts its delta clamped to
46
+ * 0–{@link MAX_FRAME_DELTA_MS}, so the first frame back from the background (or
47
+ * after a stall) counts as one frame, not as the time away.
48
+ */
49
+ export function squiggleShownMsAfterFrame(
50
+ shownMs: number,
51
+ frameMs: number,
52
+ ): number {
53
+ "worklet";
54
+ return shownMs + Math.min(Math.max(frameMs, 0), MAX_FRAME_DELTA_MS);
55
+ }
56
+
57
+ /**
58
+ * Whether the squiggle has used up `freezeAfterMs` and is frozen. `undefined`
59
+ * never freezes; `0` or less freezes from the start.
60
+ */
61
+ export function squiggleFrozen(
62
+ shownMs: number,
63
+ freezeAfterMs: number | undefined,
64
+ ): boolean {
65
+ "worklet";
66
+ return freezeAfterMs !== undefined && shownMs >= freezeAfterMs;
67
+ }
68
+
42
69
  /**
43
70
  * Build a flat [x0,y0,x1,y1,...] point array for a standalone squiggly line
44
71
  * spanning the full chart area (used in loading / empty state).
package/src/types.ts CHANGED
@@ -758,11 +758,17 @@ export interface BadgeConfig extends BadgeStyleConfig {
758
758
  * column ({@link YAxisConfig.labelRightMargin}): it starts at the labels'
759
759
  * shared left X, and a value wider than every axis label ends at the
760
760
  * column's right edge. The pill keeps its shape and the value stays within
761
- * its `padX` padding (a value that doesn't fit there is centered), so the
762
- * room on each side follows the right inset and `metrics.badge`. The value
763
- * moves sideways when the column's width changes (a wider label entering or
764
- * leaving the axis). Centered without that column on the right side, while
765
- * the axis floats, and for a `"left"` badge. Default `"center"`.
761
+ * its `padX` padding, so the room on each side follows the right inset and
762
+ * `metrics.badge`. Next to the tail, whose taper keeps the value off the
763
+ * outline, a pill too narrow for both paddings gives up the left one first,
764
+ * then the right one, so the value moves continuously as the pill narrows:
765
+ * with room for its right padding it ends `padX` from the pill's end (the
766
+ * column's right edge when `marginEdge + padX` equals `labelRightMargin`).
767
+ * A value wider than a tailed pill's body, or one that doesn't fit both
768
+ * paddings of a pill without a tail, is centered. The value moves sideways
769
+ * when the column's width changes (a wider label entering or leaving the
770
+ * axis). Centered without that column on the right side, while the axis
771
+ * floats, and for a `"left"` badge. Default `"center"`.
766
772
  */
767
773
  textAlign?: "center" | "yAxisColumn";
768
774
  /**
@@ -1847,7 +1853,9 @@ export interface DotConfig {
1847
1853
  * dot must stay aligned with its badge. Single-series `LiveChart` only
1848
1854
  * (multi-series dots already ride each line's end while scrolled). Default
1849
1855
  * `false` (the dot stays pinned to the right edge with its pulse suppressed
1850
- * while scrolled back).
1856
+ * while scrolled back). With an external viewport or positive windowBuffer,
1857
+ * a line dot already tracks the recorded endpoint; this option still controls
1858
+ * its scroll-back visibility and pulse.
1851
1859
  *
1852
1860
  * @experimental
1853
1861
  */
@@ -2394,6 +2402,13 @@ export interface LoadingConfig {
2394
2402
  * squiggle with no placeholder ticks. Default `true`.
2395
2403
  */
2396
2404
  axisLabels?: boolean;
2405
+ /**
2406
+ * Freezes the squiggle once it has animated this long (ms) each time the
2407
+ * loading or empty shell appears; it moves again when the shell next appears.
2408
+ * Counted in frames drawn, so time in the background doesn't count. `0` or
2409
+ * less freezes it from the start. Default `undefined`: no limit.
2410
+ */
2411
+ freezeAfterMs?: number;
2397
2412
  }
2398
2413
 
2399
2414
  /** Y-range easing shared by both charts, independent of value/window tracking. */
@@ -2487,7 +2502,8 @@ export interface LiveChartCoreProps {
2487
2502
  * `true` shows the shell with the defaults; pass a {@link LoadingConfig} to
2488
2503
  * restyle it — `color` / `strokeWidth` for the squiggle + skeleton, `amplitude`
2489
2504
  * / `speed` for the breathing wave, `axisLabels` to toggle the skeleton
2490
- * placeholders. `false` / omitted is "not loading". Toggle between the config
2505
+ * placeholders, `freezeAfterMs` to freeze the wave after a while.
2506
+ * `false` / omitted is "not loading". Toggle between the config
2491
2507
  * and `false` as data loads (e.g. `loading={isLoading && cfg}`). The loading→live
2492
2508
  * reveal duration is governed separately by {@link TransitionConfig.reveal} (the
2493
2509
  * `transitions` prop).
@@ -2692,7 +2708,10 @@ export interface LiveChartCoreProps {
2692
2708
  /**
2693
2709
  * Right-edge buffer as a fraction of `timeWindow`. Pushes the live edge past the
2694
2710
  * current time so the latest point has breathing room; pass `0` to land the latest
2695
- * point exactly at the right edge. Default `0`.
2711
+ * point exactly at the right edge. In line-mode LiveChart, a positive buffer
2712
+ * stops the line/dot at the last recorded point; the badge and value line use
2713
+ * that recorded price. badge.followViewEdge retains its edge-pinned indicators.
2714
+ * Default `0`.
2696
2715
  */
2697
2716
  windowBuffer?: number;
2698
2717
  /**
@@ -2797,15 +2816,25 @@ export interface LiveChartCoreProps {
2797
2816
 
2798
2817
  /**
2799
2818
  * UI-thread scroll/zoom state adopted by a single-series {@link LiveChart}.
2800
- * The app owns leadership and timestamp mapping; gestures write these same values.
2819
+ * The app owns leadership and timestamp mapping; gestures write end/window.
2801
2820
  * @experimental
2802
2821
  */
2803
2822
  export interface ChartViewportControl {
2804
2823
  /** Right-edge unix timestamp in seconds; null follows this chart's live edge.
2805
- * Honors times past live. An end before the first retained point falls back to live. */
2824
+ * Honors times past live. Before the first retained point, falls back to live
2825
+ * unless a valid explicit range is supplied. */
2806
2826
  end: SharedValue<number | null>;
2807
2827
  /** Positive, finite visible width in seconds; null uses the timeWindow prop. */
2808
2828
  window: SharedValue<number | null>;
2829
+ /**
2830
+ * Exact price bounds while end is finite and non-null, including empty past or
2831
+ * future space. Both bounds and their difference must be finite, with min < max.
2832
+ * Applied immediately, overriding automatic fitting, range easing, nonNegative,
2833
+ * maxValue, minRange, and yRangeScale. Invalid/null/omitted bounds use auto-fit.
2834
+ * Ignored while end is null (including after resetZoom()); gestures and resets
2835
+ * do not write this optional shared value.
2836
+ */
2837
+ range?: SharedValue<Pick<ChartScale, "min" | "max"> | null>;
2809
2838
  }
2810
2839
 
2811
2840
  /** External viewport state and optional behavior settings for {@link LiveChart}.
@@ -2815,7 +2844,8 @@ export interface ChartViewportControl {
2815
2844
  export interface ViewportConfig extends ChartViewportControl {
2816
2845
  /** Ease non-null window overrides using the chart's smoothing. Default true.
2817
2846
  * Set false on followers copying a leader's drawn width to avoid easing twice.
2818
- * Null windows still ease back to timeWindow; Y-range/value smoothing is unchanged. */
2847
+ * Null windows still ease back to timeWindow; value and automatic Y-range
2848
+ * smoothing are unchanged. Explicit range bounds always apply immediately. */
2819
2849
  windowSmoothing?: boolean;
2820
2850
  }
2821
2851
 
@@ -2828,6 +2858,9 @@ export interface LiveChartProps extends LiveChartCoreProps {
2828
2858
  * and wake autoSleep charts. Single-series line/candle only.
2829
2859
  * Pass a ViewportConfig object to enable; false/omitted uses private state.
2830
2860
  * Both shared values are required, so this prop has no true shorthand.
2861
+ * Optional range supplies exact price bounds while end is non-null. In line
2862
+ * mode the line/dot stop at the last recorded point, with badge/value line at
2863
+ * that price; badge.followViewEdge keeps its edge-pinned indicators.
2831
2864
  * @experimental
2832
2865
  */
2833
2866
  viewport?: false | ViewportConfig;