react-native-livechart 4.26.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.
Files changed (142) hide show
  1. package/dist/components/CustomReferenceLineOverlay.d.ts +8 -1
  2. package/dist/components/CustomReferenceLineOverlay.d.ts.map +1 -1
  3. package/dist/components/CustomTooltipOverlay.d.ts +3 -2
  4. package/dist/components/CustomTooltipOverlay.d.ts.map +1 -1
  5. package/dist/components/LiveChart.d.ts +1 -1
  6. package/dist/components/LiveChart.d.ts.map +1 -1
  7. package/dist/components/LiveChartSeries.d.ts.map +1 -1
  8. package/dist/components/MultiSeriesDots.d.ts +1 -1
  9. package/dist/components/MultiSeriesDots.d.ts.map +1 -1
  10. package/dist/components/MultiSeriesValueLabels.d.ts +1 -1
  11. package/dist/components/MultiSeriesValueLabels.d.ts.map +1 -1
  12. package/dist/components/MultiSeriesValueLines.d.ts +1 -1
  13. package/dist/components/MultiSeriesValueLines.d.ts.map +1 -1
  14. package/dist/components/ReferenceLineOverlay.d.ts +4 -0
  15. package/dist/components/ReferenceLineOverlay.d.ts.map +1 -1
  16. package/dist/components/ReferenceValueBadge.d.ts +15 -0
  17. package/dist/components/ReferenceValueBadge.d.ts.map +1 -0
  18. package/dist/components/XAxisOverlay.d.ts +17 -0
  19. package/dist/components/XAxisOverlay.d.ts.map +1 -1
  20. package/dist/core/liveChartEngineTick.d.ts +4 -0
  21. package/dist/core/liveChartEngineTick.d.ts.map +1 -1
  22. package/dist/core/liveChartSeriesEngineTick.d.ts +15 -2
  23. package/dist/core/liveChartSeriesEngineTick.d.ts.map +1 -1
  24. package/dist/core/resolveConfig.d.ts +28 -1
  25. package/dist/core/resolveConfig.d.ts.map +1 -1
  26. package/dist/core/useLiveChartEngine.d.ts +9 -0
  27. package/dist/core/useLiveChartEngine.d.ts.map +1 -1
  28. package/dist/core/useLiveChartSeriesEngine.d.ts +18 -4
  29. package/dist/core/useLiveChartSeriesEngine.d.ts.map +1 -1
  30. package/dist/draw/grid.d.ts +6 -4
  31. package/dist/draw/grid.d.ts.map +1 -1
  32. package/dist/draw/line.d.ts +21 -3
  33. package/dist/draw/line.d.ts.map +1 -1
  34. package/dist/hooks/crosshairSeries.d.ts +2 -2
  35. package/dist/hooks/crosshairSeries.d.ts.map +1 -1
  36. package/dist/hooks/crosshairShared.d.ts +16 -1
  37. package/dist/hooks/crosshairShared.d.ts.map +1 -1
  38. package/dist/hooks/referenceBadgeFont.d.ts +5 -0
  39. package/dist/hooks/referenceBadgeFont.d.ts.map +1 -0
  40. package/dist/hooks/resolveChartLayout.d.ts +18 -2
  41. package/dist/hooks/resolveChartLayout.d.ts.map +1 -1
  42. package/dist/hooks/useBadge.d.ts +16 -1
  43. package/dist/hooks/useBadge.d.ts.map +1 -1
  44. package/dist/hooks/useChartSkiaFont.d.ts +1 -0
  45. package/dist/hooks/useChartSkiaFont.d.ts.map +1 -1
  46. package/dist/hooks/useCrosshair.d.ts +7 -2
  47. package/dist/hooks/useCrosshair.d.ts.map +1 -1
  48. package/dist/hooks/useCrosshairSeries.d.ts +6 -1
  49. package/dist/hooks/useCrosshairSeries.d.ts.map +1 -1
  50. package/dist/hooks/useLatestCallback.d.ts +6 -0
  51. package/dist/hooks/useLatestCallback.d.ts.map +1 -0
  52. package/dist/hooks/useMarkers.d.ts.map +1 -1
  53. package/dist/hooks/useMultiSeriesDegen.d.ts +1 -1
  54. package/dist/hooks/useMultiSeriesDegen.d.ts.map +1 -1
  55. package/dist/hooks/useMultiSeriesLinePaths.d.ts.map +1 -1
  56. package/dist/hooks/usePanScroll.d.ts +3 -1
  57. package/dist/hooks/usePanScroll.d.ts.map +1 -1
  58. package/dist/hooks/usePinchZoom.d.ts +7 -4
  59. package/dist/hooks/usePinchZoom.d.ts.map +1 -1
  60. package/dist/hooks/useReferenceDrag.d.ts +20 -5
  61. package/dist/hooks/useReferenceDrag.d.ts.map +1 -1
  62. package/dist/hooks/useReferenceLine.d.ts +24 -12
  63. package/dist/hooks/useReferenceLine.d.ts.map +1 -1
  64. package/dist/hooks/useReferenceLinePress.d.ts +4 -2
  65. package/dist/hooks/useReferenceLinePress.d.ts.map +1 -1
  66. package/dist/hooks/useReferenceTagStack.d.ts +31 -0
  67. package/dist/hooks/useReferenceTagStack.d.ts.map +1 -0
  68. package/dist/hooks/useScrubMarkers.d.ts +12 -0
  69. package/dist/hooks/useScrubMarkers.d.ts.map +1 -0
  70. package/dist/hooks/useXAxis.d.ts +1 -1
  71. package/dist/hooks/useXAxis.d.ts.map +1 -1
  72. package/dist/hooks/useYAxis.d.ts +1 -1
  73. package/dist/hooks/useYAxis.d.ts.map +1 -1
  74. package/dist/hooks/useYAxisLabelWidth.d.ts +6 -0
  75. package/dist/hooks/useYAxisLabelWidth.d.ts.map +1 -0
  76. package/dist/hooks/useYAxisScaleGesture.d.ts +34 -0
  77. package/dist/hooks/useYAxisScaleGesture.d.ts.map +1 -0
  78. package/dist/index.d.ts +1 -1
  79. package/dist/index.d.ts.map +1 -1
  80. package/dist/math/axisBadgeLayout.d.ts +16 -0
  81. package/dist/math/axisBadgeLayout.d.ts.map +1 -0
  82. package/dist/math/markers.d.ts +3 -0
  83. package/dist/math/markers.d.ts.map +1 -1
  84. package/dist/math/range.d.ts +1 -1
  85. package/dist/math/range.d.ts.map +1 -1
  86. package/dist/math/referenceLines.d.ts.map +1 -1
  87. package/dist/math/referenceTagStack.d.ts +36 -0
  88. package/dist/math/referenceTagStack.d.ts.map +1 -0
  89. package/dist/math/scrubMarkers.d.ts +12 -0
  90. package/dist/math/scrubMarkers.d.ts.map +1 -0
  91. package/dist/types.d.ts +262 -37
  92. package/dist/types.d.ts.map +1 -1
  93. package/package.json +1 -1
  94. package/src/components/CustomReferenceLineOverlay.tsx +50 -8
  95. package/src/components/CustomTooltipOverlay.tsx +5 -1
  96. package/src/components/LiveChart.tsx +580 -138
  97. package/src/components/LiveChartSeries.tsx +139 -19
  98. package/src/components/MultiSeriesDots.tsx +4 -2
  99. package/src/components/MultiSeriesValueLabels.tsx +4 -2
  100. package/src/components/MultiSeriesValueLines.tsx +4 -2
  101. package/src/components/ReferenceLineOverlay.tsx +62 -26
  102. package/src/components/ReferenceValueBadge.tsx +102 -0
  103. package/src/components/XAxisOverlay.tsx +104 -1
  104. package/src/core/liveChartEngineTick.ts +49 -4
  105. package/src/core/liveChartSeriesEngineTick.ts +107 -14
  106. package/src/core/resolveConfig.ts +103 -7
  107. package/src/core/useLiveChartEngine.ts +45 -9
  108. package/src/core/useLiveChartSeriesEngine.ts +47 -6
  109. package/src/draw/grid.ts +17 -13
  110. package/src/draw/line.ts +46 -5
  111. package/src/hooks/crosshairSeries.ts +37 -5
  112. package/src/hooks/crosshairShared.ts +56 -1
  113. package/src/hooks/referenceBadgeFont.ts +35 -0
  114. package/src/hooks/resolveChartLayout.ts +51 -3
  115. package/src/hooks/useBadge.ts +44 -5
  116. package/src/hooks/useChartSkiaFont.ts +1 -1
  117. package/src/hooks/useCrosshair.ts +444 -310
  118. package/src/hooks/useCrosshairSeries.ts +57 -20
  119. package/src/hooks/useLatestCallback.ts +15 -0
  120. package/src/hooks/useMarkers.ts +51 -39
  121. package/src/hooks/useMultiSeriesDegen.ts +10 -5
  122. package/src/hooks/useMultiSeriesLinePaths.ts +2 -0
  123. package/src/hooks/usePanScroll.ts +155 -127
  124. package/src/hooks/usePinchZoom.ts +94 -66
  125. package/src/hooks/useReferenceDrag.ts +409 -156
  126. package/src/hooks/useReferenceLine.ts +453 -277
  127. package/src/hooks/useReferenceLineGrouping.ts +1 -1
  128. package/src/hooks/useReferenceLinePress.ts +121 -53
  129. package/src/hooks/useReferenceTagStack.ts +81 -0
  130. package/src/hooks/useScrubMarkers.ts +79 -0
  131. package/src/hooks/useXAxis.ts +11 -2
  132. package/src/hooks/useYAxis.ts +1 -1
  133. package/src/hooks/useYAxisLabelWidth.ts +26 -0
  134. package/src/hooks/useYAxisScaleGesture.ts +263 -0
  135. package/src/index.ts +11 -0
  136. package/src/math/axisBadgeLayout.ts +37 -0
  137. package/src/math/markers.ts +28 -3
  138. package/src/math/range.ts +35 -0
  139. package/src/math/referenceLines.ts +5 -0
  140. package/src/math/referenceTagStack.ts +112 -0
  141. package/src/math/scrubMarkers.ts +55 -0
  142. package/src/types.ts +263 -37
@@ -28,7 +28,7 @@ export function useReferenceLineGrouping({
28
28
  offAxisCustom: boolean[];
29
29
  dragValues: SharedValue<number[]>;
30
30
  }) {
31
- const hasOffAxisBadge = lines.map((line) => Boolean(line.badge || line.offAxisBadge));
31
+ const hasOffAxisBadge = lines.map((line) => Boolean(line.badge || line.offAxisBadge || line.valueBadge));
32
32
  const result = useDerivedValue<ReferenceGrouping>(() => {
33
33
  if (radius == null) return EMPTY_GROUPING;
34
34
  const canvasHeight = engine.canvasHeight.get();
@@ -1,3 +1,5 @@
1
+ import type { ReferenceTagStack } from "../math/referenceTagStack";
2
+ import { useMemo } from "react";
1
3
  import type { SkFont } from "@shopify/react-native-skia";
2
4
  import { Gesture } from "react-native-gesture-handler";
3
5
  import { useDerivedValue, type SharedValue } from "react-native-reanimated";
@@ -5,13 +7,19 @@ import { scheduleOnRN } from "react-native-worklets";
5
7
 
6
8
  import type { ChartEngineLayout } from "../core/useLiveChartEngine";
7
9
  import type { ChartPadding } from "../draw/line";
8
- import type { ReferenceLine } from "../types";
10
+ import type { AxisBadgeConfig } from "../math/axisBadgeLayout";
11
+ import { referenceBadgeFont } from "./referenceBadgeFont";
12
+ import { resolveReferenceBadge } from "../math/referenceLines";
13
+ import type { FontConfig, ReferenceLine } from "../types";
9
14
  import { pointInRect } from "./crosshairShared";
10
15
  import {
11
- computeReferenceBadgeRect,
16
+ computeReferenceLineLayout,
17
+ referenceBadgeRect,
12
18
  type ReferenceBadgeRect,
13
19
  } from "./useReferenceLine";
14
20
 
21
+ import { useLatestCallback } from "./useLatestCallback";
22
+
15
23
  /**
16
24
  * Builds a tap gesture that hit-tests a chart's reference-line **badges** (the
17
25
  * pill tags for working orders / alerts / targets) and fires `onPress(line,
@@ -36,71 +44,131 @@ export function useReferenceLinePress(
36
44
  /** Per-line live value overrides (dragged values) so a draggable line's hit-rect
37
45
  * tracks the drag, index-aligned with `lines`. */
38
46
  dragValues?: SharedValue<number[]>,
47
+ valueAxis?: AxisBadgeConfig,
48
+ fontProp?: FontConfig,
49
+ groupHidden?: SharedValue<boolean[]>,
50
+ custom?: boolean[],
51
+ offAxisCustom?: boolean[],
52
+ tagStack?: SharedValue<ReferenceTagStack>,
39
53
  ): {
40
54
  tapGesture: ReturnType<typeof Gesture.Tap>;
41
55
  hitTest: (x: number, y: number) => boolean;
42
56
  } {
43
- // Per-frame badge hit-rects, index-aligned with `lines` (null = no pressable
44
- // badge / off-screen / not laid out). Recomputed on the UI thread.
57
+ const fonts = useMemo(
58
+ () =>
59
+ lines.map((line) => ({
60
+ name: referenceBadgeFont(
61
+ font,
62
+ fontProp,
63
+ resolveReferenceBadge(line) ?? undefined,
64
+ ),
65
+ value: referenceBadgeFont(
66
+ font,
67
+ fontProp,
68
+ typeof line.valueBadge === "object" ? line.valueBadge : undefined,
69
+ ),
70
+ })),
71
+ [lines, font, fontProp],
72
+ );
45
73
  /* istanbul ignore next -- worklet runs on the UI thread, not in Jest */
46
- const rects = useDerivedValue<(ReferenceBadgeRect | null)[]>(() => {
74
+ const rects = useDerivedValue<(ReferenceBadgeRect | null)[][]>(() => {
47
75
  if (!active || lines.length === 0) return [];
48
- const w = engine.canvasWidth.value;
49
- const h = engine.canvasHeight.value;
50
- const dMin = engine.displayMin.value;
51
- const dMax = engine.displayMax.value;
52
- const out: (ReferenceBadgeRect | null)[] = [];
53
- const dv = dragValues?.value;
76
+ if (tagStack) {
77
+ const out: ReferenceBadgeRect[][] = [];
78
+ for (let i = 0; i < lines.length; i++) out.push([]);
79
+ for (const tag of tagStack.get().tags) {
80
+ if (lines[tag.index] && lines[tag.index].id === tag.lineId && tag.kind !== "custom" && (tag.kind === "value" || resolveReferenceBadge(lines[tag.index]))) {
81
+ out[tag.index]?.push(tag);
82
+ }
83
+ }
84
+ return out;
85
+ }
86
+ const out: (ReferenceBadgeRect | null)[][] = [];
54
87
  for (let i = 0; i < lines.length; i++) {
55
- out.push(
56
- computeReferenceBadgeRect(
57
- lines[i],
58
- w,
59
- h,
60
- padding,
61
- dMin,
62
- dMax,
63
- font,
64
- formatValue,
65
- dv ? dv[i] : undefined,
66
- ),
88
+ if (custom?.[i] || groupHidden?.get()[i]) {
89
+ out.push([]);
90
+ continue;
91
+ }
92
+ const layout = computeReferenceLineLayout(
93
+ engine.canvasWidth.get(),
94
+ engine.canvasHeight.get(),
95
+ padding,
96
+ lines[i],
97
+ formatValue,
98
+ fonts[i].name,
99
+ engine.displayMin.get(),
100
+ engine.displayMax.get(),
101
+ 0,
102
+ 1,
103
+ dragValues?.get()[i],
104
+ undefined,
105
+ undefined,
106
+ 0,
107
+ font,
108
+ fonts[i].value,
109
+ valueAxis,
67
110
  );
111
+ if (offAxisCustom?.[i] && layout.offAxis) {
112
+ out.push([]);
113
+ continue;
114
+ }
115
+ out.push([
116
+ referenceBadgeRect(layout, fonts[i].name, lines[i]),
117
+ layout.valueBadge,
118
+ ]);
68
119
  }
69
120
  return out;
70
121
  });
71
122
 
72
- // Topmost badge under (x, y), or -1. Last drawn = last in the array = topmost.
73
- /* istanbul ignore next -- worklet, runs on the UI thread */
74
- const indexAt = (x: number, y: number): number => {
75
- "worklet";
76
- const rs = rects.value;
77
- for (let i = rs.length - 1; i >= 0; i--) {
78
- const r = rs[i];
79
- if (r && pointInRect(x, y, r, hitSlop)) return i;
80
- }
81
- return -1;
82
- };
83
-
84
- /* istanbul ignore next -- worklet, runs on the UI thread */
85
- const hitTest = (x: number, y: number): boolean => {
86
- "worklet";
87
- return indexAt(x, y) >= 0;
88
- };
123
+ const emitPress = useLatestCallback(
124
+ (index: number, id: string | undefined) => {
125
+ const line = lines[index];
126
+ // A queued tap must not target a replacement at the same array index.
127
+ if (line && line.id === id) onPress?.(line, index);
128
+ },
129
+ );
130
+ const lineIds = useDerivedValue(() => {
131
+ const ids: (string | undefined)[] = [];
132
+ for (const line of lines) ids.push(line.id);
133
+ return ids;
134
+ });
89
135
 
90
- /* istanbul ignore next -- runs only via scheduleOnRN from the UI-thread tap */
91
- function emitPress(index: number) {
92
- onPress?.(lines[index], index);
93
- }
136
+ return useMemo(() => {
137
+ // Topmost badge under (x, y), or -1. Last drawn = last in the array = topmost.
138
+ /* istanbul ignore next -- worklet, runs on the UI thread */
139
+ const indexAt = (x: number, y: number): number => {
140
+ "worklet";
141
+ const rs = rects.get();
142
+ // Exact pill hits win over neighboring expanded touch targets.
143
+ if (tagStack) {
144
+ for (let i = rs.length - 1; i >= 0; i--) {
145
+ for (const r of rs[i]) if (r && pointInRect(x, y, r, 0)) return i;
146
+ }
147
+ }
148
+ for (let i = rs.length - 1; i >= 0; i--) {
149
+ for (const r of rs[i]) {
150
+ if (r && pointInRect(x, y, r, hitSlop)) return i;
151
+ }
152
+ }
153
+ return -1;
154
+ };
94
155
 
95
- /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
96
- const tapGesture = Gesture.Tap()
97
- .maxDuration(250)
98
- .onEnd((e, success) => {
156
+ /* istanbul ignore next -- worklet, runs on the UI thread */
157
+ const hitTest = (x: number, y: number): boolean => {
99
158
  "worklet";
100
- if (!active || !success) return;
101
- const i = indexAt(e.x, e.y);
102
- if (i >= 0) scheduleOnRN(emitPress, i);
103
- });
159
+ return indexAt(x, y) >= 0;
160
+ };
161
+
162
+ /* istanbul ignore next -- gesture worklet runs on the UI thread, not in Jest */
163
+ const tapGesture = Gesture.Tap()
164
+ .maxDuration(250)
165
+ .onEnd((e, success) => {
166
+ "worklet";
167
+ if (!active || !success) return;
168
+ const i = indexAt(e.x, e.y);
169
+ if (i >= 0) scheduleOnRN(emitPress, i, lineIds.get()[i]);
170
+ });
104
171
 
105
- return { tapGesture, hitTest };
172
+ return { tapGesture, hitTest };
173
+ }, [active, emitPress, hitSlop, lineIds, rects, tagStack]);
106
174
  }
@@ -0,0 +1,81 @@
1
+ import { useMemo, useRef } from "react";
2
+ import type { SkFont } from "@shopify/react-native-skia";
3
+ import { useAnimatedReaction, type SharedValue } from "react-native-reanimated";
4
+ import type { ChartEngineLayout } from "../core/useLiveChartEngine";
5
+ import type { ChartPadding } from "../draw/line";
6
+ import { measureFontTextWidth, type TextWidthCache } from "../lib/measureFontTextWidth";
7
+ import type { AxisBadgeConfig } from "../math/axisBadgeLayout";
8
+ import { classifyReferenceEdge, referenceLineForm, resolveReferenceBadge } from "../math/referenceLines";
9
+ import { EMPTY_TAG_STACK, stackReferenceTags, type ReferenceTag, type ReferenceTagStack, type TagRect } from "../math/referenceTagStack";
10
+ import type { FontConfig, ReferenceLine } from "../types";
11
+ import { computeScrubDotY } from "./crosshairShared";
12
+ import { referenceBadgeFont } from "./referenceBadgeFont";
13
+ import { computeReferenceLineLayout, referenceBadgeRect } from "./useReferenceLine";
14
+
15
+ export type CustomTagSizes = Record<string, { width: number; height: number }>;
16
+
17
+ /** One shared geometry result drives built-in/custom rendering and hit targets. */
18
+ export function useReferenceTagStack({
19
+ enabled, radius, engine, padding, lines, keys, custom, offAxisCustom,
20
+ customSizes, font, fontProp, formatValue, dragValues, valueAxis, obstacle, output,
21
+ }: {
22
+ enabled: boolean; radius: number; engine: ChartEngineLayout; padding: ChartPadding;
23
+ lines: ReferenceLine[]; keys: string[]; custom: boolean[]; offAxisCustom: boolean[];
24
+ customSizes: SharedValue<CustomTagSizes>; font: SkFont; fontProp?: FontConfig;
25
+ formatValue: (value: number) => string; dragValues: SharedValue<number[]>;
26
+ valueAxis: AxisBadgeConfig; obstacle: SharedValue<TagRect | null> | null;
27
+ output: SharedValue<ReferenceTagStack>;
28
+ }) {
29
+ const fonts = useMemo(() => enabled ? lines.map(line => ({
30
+ name: referenceBadgeFont(font, fontProp, resolveReferenceBadge(line) ?? undefined),
31
+ value: referenceBadgeFont(font, fontProp, typeof line.valueBadge === "object" ? line.valueBadge : undefined),
32
+ })) : [], [enabled, lines, font, fontProp]);
33
+ const caches = useRef<TextWidthCache[]>([]);
34
+ const valueCaches = useRef<TextWidthCache[]>([]);
35
+ useAnimatedReaction(() => {
36
+ if (!enabled) return EMPTY_TAG_STACK;
37
+ const w = engine.canvasWidth.get(), h = engine.canvasHeight.get();
38
+ const min = engine.displayMin.get(), max = engine.displayMax.get();
39
+ const tags: ReferenceTag[] = [];
40
+ const values = dragValues.get();
41
+ const sizes = customSizes.get();
42
+ for (let i = 0; i < lines.length; i++) {
43
+ const line = lines[i];
44
+ if (referenceLineForm(line) !== "line" || line.value === undefined) continue;
45
+ const value = values[i] ?? line.value;
46
+ const offAxis = classifyReferenceEdge(value, min, max) !== "in";
47
+ if (custom[i] || (offAxisCustom[i] && offAxis)) {
48
+ const size = sizes[keys[i]];
49
+ if (!size || w <= 0 || h - padding.top - padding.bottom <= 0) continue;
50
+ const anchor = resolveReferenceBadge(line)?.position ?? line.labelPosition ?? "left";
51
+ const y = Math.min(h - padding.bottom - 12, Math.max(padding.top + 12,
52
+ computeScrubDotY(value, min, max, h, padding.top, padding.bottom)));
53
+ const x = anchor === "right" ? w - padding.right - 2 - size.width
54
+ : anchor === "center" ? (padding.left + w - padding.right - size.width) / 2 : padding.left + 2;
55
+ tags.push({ index: i, lineId: line.id, key: keys[i] + ":custom", kind: "custom", x, y: y - size.height / 2, w: size.width, h: size.height, lineY: y });
56
+ continue;
57
+ }
58
+ const nameCache = caches.current[i] ?? (caches.current[i] = {});
59
+ const valueCache = valueCaches.current[i] ?? (valueCaches.current[i] = {});
60
+ const layout = computeReferenceLineLayout(w, h, padding, line, formatValue, fonts[i].name,
61
+ min, max, 0, 1, value, undefined, undefined, 0, font, fonts[i].value, valueAxis, nameCache, valueCache);
62
+ if (!layout.visible) continue;
63
+ let name = referenceBadgeRect(layout, fonts[i].name, line);
64
+ if (!name && layout.label) {
65
+ const fm = fonts[i].name.getMetrics();
66
+ name = { x: layout.labelX, y: layout.labelY + fm.ascent,
67
+ w: measureFontTextWidth(fonts[i].name, layout.label, nameCache), h: fm.descent - fm.ascent };
68
+ }
69
+ if (name) tags.push({ ...name, index: i, lineId: line.id, key: keys[i] + ":name", kind: "name", lineY: layout.y });
70
+ if (layout.valueBadge) tags.push({ ...layout.valueBadge, index: i, lineId: line.id, key: keys[i] + ":value", kind: "value", lineY: layout.y });
71
+ }
72
+ return stackReferenceTags(tags, padding.top, h - padding.bottom, radius, obstacle?.get() ?? null);
73
+ }, (next, previous) => {
74
+ // Avoid waking every tag mapper when the engine ticks but geometry is steady.
75
+ if (previous && next.tags.length === previous.tags.length && next.tags.every((t, i) => {
76
+ const p = previous.tags[i];
77
+ return t.key === p.key && t.index === p.index && t.x === p.x && t.y === p.y && t.w === p.w && t.h === p.h && t.offsetY === p.offsetY && t.lineY === p.lineY;
78
+ })) return;
79
+ output.set(next);
80
+ });
81
+ }
@@ -0,0 +1,79 @@
1
+ import {
2
+ useAnimatedReaction,
3
+ useSharedValue,
4
+ type SharedValue,
5
+ } from "react-native-reanimated";
6
+ import type { ResolvedScrubConfig } from "../core/resolveConfig";
7
+ import { matchScrubMarkers, scrubMarkerRange } from "../math/scrubMarkers";
8
+ import type { CandlePoint, ChartGap, Marker } from "../types";
9
+
10
+ export interface ScrubMarkerOptions {
11
+ markers: SharedValue<Marker[]>;
12
+ radius: number;
13
+ }
14
+
15
+ /** Adapt resolved config for both crosshair hooks. */
16
+ export function resolveScrubMarkerOptions(
17
+ config: ResolvedScrubConfig | null,
18
+ markers: SharedValue<Marker[]>,
19
+ ): ScrubMarkerOptions | undefined {
20
+ if (!config?.markers) return undefined;
21
+ return { markers, radius: config.markers.radius };
22
+ }
23
+
24
+ /** Selection changes notify tooltip consumers; movement within a match does not. */
25
+ export function useScrubMarkers(
26
+ options: ScrubMarkerOptions | undefined,
27
+ active: SharedValue<boolean>,
28
+ time: SharedValue<number>,
29
+ now: SharedValue<number>,
30
+ window: SharedValue<number>,
31
+ canvasWidth: SharedValue<number>,
32
+ horizontalPadding: number,
33
+ candleMode = false,
34
+ candle?: SharedValue<CandlePoint | null>,
35
+ candleWidth = 60,
36
+ gap?: SharedValue<ChartGap | null>,
37
+ ): SharedValue<Marker[]> {
38
+ const source = useSharedValue<Marker[]>([]);
39
+ const selected = useSharedValue<Marker[]>([]);
40
+ // A fresh shallow snapshot also detects .modify() edits where the caller
41
+ // retains the input array/objects. No metadata serialization or frame loop.
42
+ useAnimatedReaction(
43
+ () => options?.markers.get(),
44
+ (markers) => {
45
+ source.set(markers ? markers.slice() : []);
46
+ },
47
+ );
48
+ useAnimatedReaction(
49
+ () => {
50
+ if (!options || !active.get() || gap?.get()) return null;
51
+ return {
52
+ markers: source.get(),
53
+ range: scrubMarkerRange(
54
+ time.get(),
55
+ now.get(),
56
+ window.get(),
57
+ canvasWidth.get() - horizontalPadding,
58
+ options.radius,
59
+ candleMode,
60
+ candle?.get()?.time ?? null,
61
+ candleWidth,
62
+ ),
63
+ };
64
+ },
65
+ (input, previous) => {
66
+ const next = input ? matchScrubMarkers(input.markers, input.range) : [];
67
+ const current = selected.get();
68
+ // Refresh metadata when the input changes, but retain the result array
69
+ // through chart ticks / movement that select the same markers.
70
+ if (
71
+ next.length === current.length &&
72
+ (next.length === 0 || input?.markers === previous?.markers) &&
73
+ next.every((marker, index) => marker === current[index])
74
+ ) return;
75
+ selected.set(next);
76
+ },
77
+ );
78
+ return selected;
79
+ }
@@ -67,10 +67,12 @@ export function useXAxis(
67
67
  padding: ChartPadding,
68
68
  formatTime: (t: number) => string,
69
69
  font: SkFont,
70
+ minGap = 60,
70
71
  ) {
71
72
  const labelAlphas = useSharedValue<
72
73
  Record<number, { alpha: number; text: string }>
73
74
  >({});
75
+ const lastMinGap = useSharedValue(minGap);
74
76
 
75
77
  // A tick's key encodes its time, so its label is formatted once (below) and
76
78
  // never re-formatted per frame — an allocation optimization. That assumes
@@ -105,13 +107,18 @@ export function useXAxis(
105
107
  /* istanbul ignore next -- defensive; Reanimated test mock rarely executes this guard with non-zero w/h */
106
108
  if (chartW <= 0) return [] as XAxisEntry[];
107
109
 
110
+ // Config changes must settle in this run: a paused or parked engine may
111
+ // publish no more frames to finish fading the previous tick set.
112
+ const gapChanged = !Object.is(lastMinGap.get(), minGap);
113
+ if (gapChanged) lastMinGap.set(minGap);
114
+
108
115
  const fadeZone = 50;
109
116
 
110
117
  // Pick interval from the *target* window duration so the cadence is stable
111
118
  // and independent of the window we're animating from.
112
119
  const targetPxPerSec = chartW / targetWindow;
113
120
  let interval = niceTimeInterval(targetWindow);
114
- while (interval * targetPxPerSec < 60 && interval < targetWindow) {
121
+ while (interval * targetPxPerSec < minGap && interval < targetWindow) {
115
122
  interval *= 2;
116
123
  }
117
124
 
@@ -169,7 +176,9 @@ export function useXAxis(
169
176
 
170
177
  const isTarget = xAxisKeyIsTarget(key, targetKeys);
171
178
  const target = isTarget ? edgeAlpha : 0;
172
- let next = lerp(label.alpha, target, FADE, MS_PER_FRAME_60FPS);
179
+ let next = gapChanged
180
+ ? target
181
+ : lerp(label.alpha, target, FADE, MS_PER_FRAME_60FPS);
173
182
  if (Math.abs(next - target) < 0.02) next = target;
174
183
 
175
184
  // Only a key that left the target set is deleted once it has faded out. A
@@ -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
+ }