react-native-livechart 4.25.0 → 4.27.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/CustomReferenceLineOverlay.d.ts +8 -1
- package/dist/components/CustomReferenceLineOverlay.d.ts.map +1 -1
- package/dist/components/CustomTooltipOverlay.d.ts +3 -2
- package/dist/components/CustomTooltipOverlay.d.ts.map +1 -1
- package/dist/components/LiveChart.d.ts +1 -1
- package/dist/components/LiveChart.d.ts.map +1 -1
- package/dist/components/LiveChartSeries.d.ts.map +1 -1
- package/dist/components/ReferenceLineOverlay.d.ts +4 -0
- package/dist/components/ReferenceLineOverlay.d.ts.map +1 -1
- package/dist/components/ReferenceValueBadge.d.ts +15 -0
- package/dist/components/ReferenceValueBadge.d.ts.map +1 -0
- package/dist/components/XAxisOverlay.d.ts +17 -0
- package/dist/components/XAxisOverlay.d.ts.map +1 -1
- package/dist/core/liveChartEngineTick.d.ts +6 -1
- package/dist/core/liveChartEngineTick.d.ts.map +1 -1
- package/dist/core/liveChartSeriesEngineTick.d.ts +4 -1
- package/dist/core/liveChartSeriesEngineTick.d.ts.map +1 -1
- package/dist/core/resolveConfig.d.ts +28 -1
- package/dist/core/resolveConfig.d.ts.map +1 -1
- package/dist/core/useLiveChartEngine.d.ts +10 -1
- package/dist/core/useLiveChartEngine.d.ts.map +1 -1
- package/dist/core/useLiveChartSeriesEngine.d.ts +5 -1
- package/dist/core/useLiveChartSeriesEngine.d.ts.map +1 -1
- package/dist/draw/grid.d.ts +2 -2
- package/dist/draw/line.d.ts +15 -1
- package/dist/draw/line.d.ts.map +1 -1
- package/dist/hooks/crosshairShared.d.ts +24 -1
- package/dist/hooks/crosshairShared.d.ts.map +1 -1
- package/dist/hooks/referenceBadgeFont.d.ts +5 -0
- package/dist/hooks/referenceBadgeFont.d.ts.map +1 -0
- package/dist/hooks/resolveChartLayout.d.ts +8 -0
- package/dist/hooks/resolveChartLayout.d.ts.map +1 -1
- package/dist/hooks/useBadge.d.ts +16 -1
- package/dist/hooks/useBadge.d.ts.map +1 -1
- package/dist/hooks/useChartSkiaFont.d.ts +1 -0
- package/dist/hooks/useChartSkiaFont.d.ts.map +1 -1
- package/dist/hooks/useCrosshair.d.ts +7 -2
- package/dist/hooks/useCrosshair.d.ts.map +1 -1
- package/dist/hooks/useCrosshairSeries.d.ts +4 -1
- package/dist/hooks/useCrosshairSeries.d.ts.map +1 -1
- package/dist/hooks/useLatestCallback.d.ts +6 -0
- package/dist/hooks/useLatestCallback.d.ts.map +1 -0
- package/dist/hooks/useMarkers.d.ts.map +1 -1
- package/dist/hooks/usePanScroll.d.ts +3 -1
- package/dist/hooks/usePanScroll.d.ts.map +1 -1
- package/dist/hooks/usePinchZoom.d.ts.map +1 -1
- package/dist/hooks/useReferenceDrag.d.ts +23 -7
- package/dist/hooks/useReferenceDrag.d.ts.map +1 -1
- package/dist/hooks/useReferenceLine.d.ts +24 -12
- package/dist/hooks/useReferenceLine.d.ts.map +1 -1
- package/dist/hooks/useReferenceLineGrouping.d.ts +19 -0
- package/dist/hooks/useReferenceLineGrouping.d.ts.map +1 -0
- package/dist/hooks/useReferenceLinePress.d.ts +4 -2
- package/dist/hooks/useReferenceLinePress.d.ts.map +1 -1
- package/dist/hooks/useReferenceTagStack.d.ts +31 -0
- package/dist/hooks/useReferenceTagStack.d.ts.map +1 -0
- package/dist/hooks/useScrubMarkers.d.ts +12 -0
- package/dist/hooks/useScrubMarkers.d.ts.map +1 -0
- package/dist/hooks/useXAxis.d.ts +1 -1
- package/dist/hooks/useXAxis.d.ts.map +1 -1
- package/dist/hooks/useYAxisScaleGesture.d.ts +34 -0
- package/dist/hooks/useYAxisScaleGesture.d.ts.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/math/axisBadgeLayout.d.ts +16 -0
- package/dist/math/axisBadgeLayout.d.ts.map +1 -0
- package/dist/math/markers.d.ts +3 -0
- package/dist/math/markers.d.ts.map +1 -1
- package/dist/math/range.d.ts +1 -1
- package/dist/math/range.d.ts.map +1 -1
- package/dist/math/rangeAnimation.d.ts +4 -0
- package/dist/math/rangeAnimation.d.ts.map +1 -0
- package/dist/math/referenceDrag.d.ts +17 -5
- package/dist/math/referenceDrag.d.ts.map +1 -1
- package/dist/math/referenceGroup.d.ts +2 -2
- package/dist/math/referenceTagStack.d.ts +36 -0
- package/dist/math/referenceTagStack.d.ts.map +1 -0
- package/dist/math/scrubMarkers.d.ts +12 -0
- package/dist/math/scrubMarkers.d.ts.map +1 -0
- package/dist/types.d.ts +240 -30
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/components/CustomReferenceLineOverlay.tsx +50 -8
- package/src/components/CustomTooltipOverlay.tsx +5 -1
- package/src/components/LiveChart.tsx +563 -200
- package/src/components/LiveChartSeries.tsx +117 -13
- package/src/components/ReferenceLineOverlay.tsx +62 -26
- package/src/components/ReferenceValueBadge.tsx +102 -0
- package/src/components/XAxisOverlay.tsx +104 -1
- package/src/core/liveChartEngineTick.ts +66 -10
- package/src/core/liveChartSeriesEngineTick.ts +55 -5
- package/src/core/resolveConfig.ts +95 -2
- package/src/core/useLiveChartEngine.ts +53 -11
- package/src/core/useLiveChartSeriesEngine.ts +11 -1
- package/src/draw/grid.ts +2 -2
- package/src/draw/line.ts +23 -1
- package/src/hooks/crosshairShared.ts +85 -1
- package/src/hooks/referenceBadgeFont.ts +35 -0
- package/src/hooks/resolveChartLayout.ts +17 -0
- package/src/hooks/useBadge.ts +44 -5
- package/src/hooks/useChartSkiaFont.ts +1 -1
- package/src/hooks/useCrosshair.ts +446 -300
- package/src/hooks/useCrosshairSeries.ts +29 -10
- package/src/hooks/useLatestCallback.ts +15 -0
- package/src/hooks/useMarkers.ts +51 -39
- package/src/hooks/usePanScroll.ts +155 -127
- package/src/hooks/usePinchZoom.ts +84 -63
- package/src/hooks/useReferenceDrag.ts +395 -146
- package/src/hooks/useReferenceLine.ts +453 -277
- package/src/hooks/useReferenceLineGrouping.ts +72 -0
- package/src/hooks/useReferenceLinePress.ts +121 -53
- package/src/hooks/useReferenceTagStack.ts +81 -0
- package/src/hooks/useScrubMarkers.ts +79 -0
- package/src/hooks/useXAxis.ts +11 -2
- package/src/hooks/useYAxisScaleGesture.ts +263 -0
- package/src/index.ts +11 -0
- package/src/math/axisBadgeLayout.ts +37 -0
- package/src/math/markers.ts +28 -3
- package/src/math/range.ts +35 -0
- package/src/math/rangeAnimation.ts +28 -0
- package/src/math/referenceDrag.ts +21 -4
- package/src/math/referenceGroup.ts +2 -2
- package/src/math/referenceTagStack.ts +112 -0
- package/src/math/scrubMarkers.ts +55 -0
- package/src/types.ts +246 -30
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { useDerivedValue, type SharedValue } from "react-native-reanimated";
|
|
2
|
+
|
|
3
|
+
import type { ChartEngineLayout } from "../core/useLiveChartEngine";
|
|
4
|
+
import type { ChartPadding } from "../draw/line";
|
|
5
|
+
import { groupReferenceLines, type ReferenceGrouping } from "../math/referenceGroup";
|
|
6
|
+
import { referenceLineForm } from "../math/referenceLines";
|
|
7
|
+
import type { ReferenceLine } from "../types";
|
|
8
|
+
import { pinnedPlotY } from "./crosshairShared";
|
|
9
|
+
|
|
10
|
+
/** Stable result while grouping is disabled. */
|
|
11
|
+
const EMPTY_GROUPING: ReferenceGrouping = { hidden: [], groups: [] };
|
|
12
|
+
|
|
13
|
+
/** Cluster the built-in tags that can actually render in the current plot. */
|
|
14
|
+
export function useReferenceLineGrouping({
|
|
15
|
+
radius,
|
|
16
|
+
engine,
|
|
17
|
+
padding,
|
|
18
|
+
lines,
|
|
19
|
+
custom,
|
|
20
|
+
offAxisCustom,
|
|
21
|
+
dragValues,
|
|
22
|
+
}: {
|
|
23
|
+
radius: number | null;
|
|
24
|
+
engine: ChartEngineLayout;
|
|
25
|
+
padding: ChartPadding;
|
|
26
|
+
lines: ReferenceLine[];
|
|
27
|
+
custom: boolean[];
|
|
28
|
+
offAxisCustom: boolean[];
|
|
29
|
+
dragValues: SharedValue<number[]>;
|
|
30
|
+
}) {
|
|
31
|
+
const hasOffAxisBadge = lines.map((line) => Boolean(line.badge || line.offAxisBadge || line.valueBadge));
|
|
32
|
+
const result = useDerivedValue<ReferenceGrouping>(() => {
|
|
33
|
+
if (radius == null) return EMPTY_GROUPING;
|
|
34
|
+
const canvasHeight = engine.canvasHeight.get();
|
|
35
|
+
const displayMin = engine.displayMin.get();
|
|
36
|
+
const displayMax = engine.displayMax.get();
|
|
37
|
+
const top = padding.top;
|
|
38
|
+
const yPositions: number[] = [];
|
|
39
|
+
for (let index = 0; index < lines.length; index++) {
|
|
40
|
+
const line = lines[index];
|
|
41
|
+
if (
|
|
42
|
+
referenceLineForm(line) !== "line" ||
|
|
43
|
+
line.value === undefined ||
|
|
44
|
+
custom[index] ||
|
|
45
|
+
offAxisCustom[index]
|
|
46
|
+
) {
|
|
47
|
+
yPositions.push(-1);
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
const value = dragValues.get()[index] ?? line.value;
|
|
51
|
+
// Plain off-axis lines are culled by useReferenceLine. Only a configured
|
|
52
|
+
// badge can contribute a visible tag to the edge's count pill.
|
|
53
|
+
if ((value < displayMin || value > displayMax) && !hasOffAxisBadge[index]) {
|
|
54
|
+
yPositions.push(-1);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
yPositions.push(
|
|
58
|
+
pinnedPlotY(
|
|
59
|
+
value,
|
|
60
|
+
displayMin,
|
|
61
|
+
displayMax,
|
|
62
|
+
canvasHeight,
|
|
63
|
+
top,
|
|
64
|
+
padding.bottom,
|
|
65
|
+
),
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
return groupReferenceLines(yPositions, radius);
|
|
69
|
+
});
|
|
70
|
+
const hidden = useDerivedValue<boolean[]>(() => result.get().hidden);
|
|
71
|
+
return { refGroupResult: result, groupHidden: hidden };
|
|
72
|
+
}
|
|
@@ -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 {
|
|
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
|
-
|
|
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
|
-
|
|
44
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
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
|
-
|
|
96
|
-
|
|
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
|
-
|
|
101
|
-
|
|
102
|
-
|
|
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
|
-
|
|
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
|
+
}
|
package/src/hooks/useXAxis.ts
CHANGED
|
@@ -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 <
|
|
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 =
|
|
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
|