@recharts/devtools 0.0.9 → 0.0.10
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/annotations/Blanket.d.ts +2 -0
- package/dist/annotations/Blanket.js +2 -2
- package/dist/annotations/RenderAnnotations.js +159 -14
- package/dist/annotations/annotationInteractionUtils.d.ts +41 -0
- package/dist/annotations/annotationInteractionUtils.js +58 -0
- package/dist/annotations/useAnnotationsManager.js +26 -2
- package/package.json +7 -5
|
@@ -13,6 +13,8 @@ export interface BlanketProps {
|
|
|
13
13
|
* when in annotation editing mode.
|
|
14
14
|
*/
|
|
15
15
|
fill?: string;
|
|
16
|
+
/** Optional CSS cursor value applied to the blanket rect. */
|
|
17
|
+
cursor?: string;
|
|
16
18
|
}
|
|
17
19
|
/**
|
|
18
20
|
* This component is used to create an invisible layer over the chart
|
|
@@ -13,7 +13,7 @@ import React from 'react';
|
|
|
13
13
|
* Instead, we render a transparent SVG rectangle that has pointerEvents disabled by default,
|
|
14
14
|
* and only enable them when the user is in the state where they are either adding, editing, or removing annotations.
|
|
15
15
|
*/
|
|
16
|
-
export const Blanket = React.memo(function Blanket({ onClick, onMouseMove, onMouseLeave, onMouseDown, onMouseUp, onMouseEnter, pointerEvents = 'none', fill = 'transparent', }) {
|
|
16
|
+
export const Blanket = React.memo(function Blanket({ onClick, onMouseMove, onMouseLeave, onMouseDown, onMouseUp, onMouseEnter, pointerEvents = 'none', fill = 'transparent', cursor, }) {
|
|
17
17
|
return (React.createElement(React.Fragment, null,
|
|
18
|
-
React.createElement("rect", { x: 0, y: 0, width: "100%", height: "100%", fill: fill, style: { pointerEvents }, onClick: onClick, onMouseMove: onMouseMove, onMouseLeave: onMouseLeave, onMouseDown: onMouseDown, onMouseUp: onMouseUp, onMouseEnter: onMouseEnter })));
|
|
18
|
+
React.createElement("rect", { x: 0, y: 0, width: "100%", height: "100%", fill: fill, style: { pointerEvents, cursor }, onClick: onClick, onMouseMove: onMouseMove, onMouseLeave: onMouseLeave, onMouseDown: onMouseDown, onMouseUp: onMouseUp, onMouseEnter: onMouseEnter })));
|
|
19
19
|
});
|
|
@@ -6,18 +6,20 @@
|
|
|
6
6
|
* follow the same pattern as this one. You may be able to reuse some of the hooks
|
|
7
7
|
* and components from the annotations/ folder.
|
|
8
8
|
*/
|
|
9
|
-
import React, { useCallback } from 'react';
|
|
9
|
+
import React, { useCallback, useRef } from 'react';
|
|
10
10
|
import { Blanket } from './Blanket.js';
|
|
11
11
|
import { HorizontalLine } from './HorizontalLine.js';
|
|
12
12
|
import { VerticalLine } from './VerticalLine.js';
|
|
13
13
|
import { CircleAnnotation } from './CircleAnnotation.js';
|
|
14
14
|
import { RectangleAnnotation } from './RectangleAnnotation.js';
|
|
15
|
+
import { getDistance } from './getDistance.js';
|
|
15
16
|
import { LabelAnnotation } from './LabelAnnotation.js';
|
|
16
17
|
import { FreeformLine } from './FreeformLine.js';
|
|
17
18
|
import { Crosshair } from './Crosshair.js';
|
|
18
19
|
import { getAnnotationColor } from './annotationColors.js';
|
|
19
20
|
import { useSnap } from './useSnap.js';
|
|
20
21
|
import { DefaultZIndexes, ZIndexLayer } from 'recharts';
|
|
22
|
+
import { DEFAULT_CIRCLE_RADIUS, DEFAULT_RECT_WIDTH, DEFAULT_RECT_HEIGHT, applyAnchorOffset, isDragInteraction, } from './annotationInteractionUtils.js';
|
|
21
23
|
function renderAnnotation(annotation) {
|
|
22
24
|
switch (annotation.type) {
|
|
23
25
|
case 'horizontalLine':
|
|
@@ -38,8 +40,29 @@ function renderAnnotation(annotation) {
|
|
|
38
40
|
return null;
|
|
39
41
|
}
|
|
40
42
|
}
|
|
43
|
+
/**
|
|
44
|
+
* Builds a synthetic SnapResult at the given pixel coordinates, with no snapping applied.
|
|
45
|
+
* Used for constructing default-sized follower previews before the user sets the first click point.
|
|
46
|
+
*/
|
|
47
|
+
function syntheticPoint(x, y) {
|
|
48
|
+
return { interactionCoordinate: { x, y }, snappedCoordinate: undefined, dataPoint: undefined };
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* A small crosshair drawn at the anchor point (circle center / rectangle top-left corner).
|
|
52
|
+
* Gives the user a visible reference for where the annotation origin is, since the real
|
|
53
|
+
* cursor is hidden and tracks pointB (the edge) instead.
|
|
54
|
+
*/
|
|
55
|
+
function FakeCursor({ x, y, color }) {
|
|
56
|
+
return (React.createElement("g", { pointerEvents: "none" },
|
|
57
|
+
React.createElement("line", { x1: x - 6, y1: y, x2: x + 6, y2: y, stroke: color, strokeWidth: 1.5 }),
|
|
58
|
+
React.createElement("line", { x1: x, y1: y - 6, x2: x, y2: y + 6, stroke: color, strokeWidth: 1.5 })));
|
|
59
|
+
}
|
|
41
60
|
/**
|
|
42
61
|
* Renders a preview of the annotation being added, following the mouse cursor.
|
|
62
|
+
*
|
|
63
|
+
* For circle and rectangle (pixel mode), the real cursor acts as pointB so the
|
|
64
|
+
* pre-click geometry matches the post-click geometry exactly.
|
|
65
|
+
* For other two-point annotations (freeformLine), a synthetic stub is used before the first click.
|
|
43
66
|
*/
|
|
44
67
|
function renderFollowerAnnotation(type, snapMode, position, annotationCount, firstClickPosition) {
|
|
45
68
|
const color = getAnnotationColor(annotationCount);
|
|
@@ -57,19 +80,96 @@ function renderFollowerAnnotation(type, snapMode, position, annotationCount, fir
|
|
|
57
80
|
id: -1, // Temporary ID for follower
|
|
58
81
|
positionType: snapMode === 'none' ? 'pixel' : 'data',
|
|
59
82
|
style: baseStyle,
|
|
60
|
-
label: undefined,
|
|
83
|
+
label: type === 'label' ? { value: 'Label' } : undefined,
|
|
61
84
|
};
|
|
62
|
-
|
|
85
|
+
// Use the snapped pixel position when available so the preview follows the nearest
|
|
86
|
+
// data/tick point rather than the raw cursor. Falls back to the real cursor in pixel mode.
|
|
87
|
+
const snapCoord = position.snappedCoordinate ?? position.interactionCoordinate;
|
|
88
|
+
const px = snapCoord.x;
|
|
89
|
+
const py = snapCoord.y;
|
|
90
|
+
// After the first click, render from the fixed anchor to the current cursor position.
|
|
91
|
+
if (firstClickPosition != null) {
|
|
92
|
+
// In data/tick modes the anchor has no pixel offset, so dist(anchor, cursor) = 0 right
|
|
93
|
+
// after the first click — the shape would collapse to invisible. Clamp to the same
|
|
94
|
+
// default size used for the pre-click preview so the transition is seamless (at exactly
|
|
95
|
+
// DEFAULT_CIRCLE_RADIUS the synthesised radius equals the real distance → no jump).
|
|
96
|
+
if (snapMode !== 'none') {
|
|
97
|
+
const dist = getDistance(firstClickPosition, position);
|
|
98
|
+
const fpx = firstClickPosition.interactionCoordinate.x;
|
|
99
|
+
const fpy = firstClickPosition.interactionCoordinate.y;
|
|
100
|
+
if (type === 'circle' && dist < DEFAULT_CIRCLE_RADIUS) {
|
|
101
|
+
// positionType stays 'data': CircleAnnotation renders centre from pointA.dataPoint,
|
|
102
|
+
// radius from getDistance(pointA, pointB) using interactionCoordinate.
|
|
103
|
+
return renderAnnotation({
|
|
104
|
+
...baseAnnotation,
|
|
105
|
+
pointA: firstClickPosition,
|
|
106
|
+
pointB: syntheticPoint(fpx + DEFAULT_CIRCLE_RADIUS, fpy),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if (type === 'rectangle' && dist < DEFAULT_CIRCLE_RADIUS) {
|
|
110
|
+
// RectangleAnnotation needs pointB.dataPoint in data mode; syntheticPoint has none,
|
|
111
|
+
// so fall back to pixel rendering. Use the snapped pixel as the anchor corner so
|
|
112
|
+
// the default-size rectangle sits at the same position as both the pre-click preview
|
|
113
|
+
// and the post-move data-mode rendering.
|
|
114
|
+
const anchorX = firstClickPosition.snappedCoordinate?.x ?? firstClickPosition.interactionCoordinate.x;
|
|
115
|
+
const anchorY = firstClickPosition.snappedCoordinate?.y ?? firstClickPosition.interactionCoordinate.y;
|
|
116
|
+
return renderAnnotation({
|
|
117
|
+
...baseAnnotation,
|
|
118
|
+
positionType: 'pixel',
|
|
119
|
+
pointA: syntheticPoint(anchorX, anchorY),
|
|
120
|
+
pointB: syntheticPoint(anchorX + DEFAULT_RECT_WIDTH, anchorY + DEFAULT_RECT_HEIGHT),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return renderAnnotation({ ...baseAnnotation, pointA: firstClickPosition, pointB: position });
|
|
125
|
+
}
|
|
126
|
+
// Before the first click: show a default-sized preview.
|
|
127
|
+
if (type === 'circle') {
|
|
128
|
+
if (snapMode === 'none') {
|
|
129
|
+
// Pixel mode: real cursor = pointB (edge); anchor offset left so radius = DEFAULT_CIRCLE_RADIUS.
|
|
130
|
+
return renderAnnotation({
|
|
131
|
+
...baseAnnotation,
|
|
132
|
+
pointA: syntheticPoint(px - DEFAULT_CIRCLE_RADIUS, py),
|
|
133
|
+
pointB: position,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
// Data/tick mode: cursor = centre; show pixel circle of default radius.
|
|
137
|
+
// We use positionType 'pixel' so CircleAnnotation can render without a dataPoint on pointB.
|
|
138
|
+
return renderAnnotation({
|
|
139
|
+
...baseAnnotation,
|
|
140
|
+
positionType: 'pixel',
|
|
141
|
+
pointA: syntheticPoint(px, py),
|
|
142
|
+
pointB: syntheticPoint(px + DEFAULT_CIRCLE_RADIUS, py),
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
if (type === 'rectangle') {
|
|
146
|
+
if (snapMode === 'none') {
|
|
147
|
+
// Pixel mode: real cursor = bottom-right corner (pointB); anchor = offset to top-left.
|
|
148
|
+
return renderAnnotation({
|
|
149
|
+
...baseAnnotation,
|
|
150
|
+
pointA: syntheticPoint(px - DEFAULT_RECT_WIDTH, py - DEFAULT_RECT_HEIGHT),
|
|
151
|
+
pointB: position,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
// Data/tick mode: cursor = top-left corner; show pixel rectangle of default size.
|
|
155
|
+
// We use positionType 'pixel' so RectangleAnnotation can render without dataPoints.
|
|
156
|
+
return renderAnnotation({
|
|
157
|
+
...baseAnnotation,
|
|
158
|
+
positionType: 'pixel',
|
|
159
|
+
pointA: syntheticPoint(px, py),
|
|
160
|
+
pointB: syntheticPoint(px + DEFAULT_RECT_WIDTH, py + DEFAULT_RECT_HEIGHT),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
if (type === 'freeformLine') {
|
|
164
|
+
// Default short angled stub so the line direction is hinted at.
|
|
63
165
|
return renderAnnotation({
|
|
64
166
|
...baseAnnotation,
|
|
65
167
|
pointA: position,
|
|
168
|
+
pointB: syntheticPoint(px + 30, py - 15),
|
|
66
169
|
});
|
|
67
170
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
pointA: firstClickPosition,
|
|
71
|
-
pointB: position,
|
|
72
|
-
});
|
|
171
|
+
// Single-click annotations (horizontalLine, verticalLine, crosshair, label).
|
|
172
|
+
return renderAnnotation({ ...baseAnnotation, pointA: position });
|
|
73
173
|
}
|
|
74
174
|
/**
|
|
75
175
|
* Component that renders all annotations and handles mouse events for adding new annotations.
|
|
@@ -79,24 +179,53 @@ function renderFollowerAnnotation(type, snapMode, position, annotationCount, fir
|
|
|
79
179
|
*/
|
|
80
180
|
export function RenderAnnotations({ annotations, isAdding, followerPosition, firstClickPosition, snapMode, onChartClick, onChartMouseDown, onChartMouseUp, onChartMouseMove, onChartMouseLeave, }) {
|
|
81
181
|
const snap = useSnap(snapMode);
|
|
182
|
+
// Circle and rectangle use a fake cursor in pixel mode only: the real cursor is hidden and
|
|
183
|
+
// the anchor crosshair is drawn at pointA (offset from the actual mouse position).
|
|
184
|
+
// In data/tick mode the cursor stays visible and snaps directly to data points.
|
|
185
|
+
const useFakeCursor = (isAdding === 'circle' || isAdding === 'rectangle') && snapMode === 'none';
|
|
186
|
+
// Tracks the real (non-offset) cursor position at the last mouseDown for circle/rectangle.
|
|
187
|
+
// Used to distinguish a click (tiny movement → two-click flow) from a drag (large movement
|
|
188
|
+
// → complete on mouseUp), because the offset anchor makes the naive dist > threshold check
|
|
189
|
+
// always true even for a stationary click.
|
|
190
|
+
const mouseDownRealPosRef = useRef(null);
|
|
82
191
|
const handleBlanketClick = useCallback((e) => {
|
|
83
192
|
const snapped = snap(e);
|
|
84
193
|
if (snapped) {
|
|
85
|
-
onChartClick(snapped);
|
|
194
|
+
onChartClick(applyAnchorOffset(snapped, isAdding, snapMode));
|
|
86
195
|
}
|
|
87
196
|
}, [onChartClick, snap]);
|
|
88
197
|
const handleBlanketMouseDown = useCallback((e) => {
|
|
89
198
|
const snapped = snap(e);
|
|
90
199
|
if (snapped) {
|
|
91
|
-
|
|
200
|
+
if (useFakeCursor) {
|
|
201
|
+
// Remember real cursor position so mouseUp can detect drag vs click.
|
|
202
|
+
mouseDownRealPosRef.current = snapped.interactionCoordinate;
|
|
203
|
+
}
|
|
204
|
+
// For circle/rectangle in pixel mode, record the anchor at the offset position
|
|
205
|
+
// (not the real cursor) so the initial size matches the pre-click preview exactly.
|
|
206
|
+
onChartMouseDown(applyAnchorOffset(snapped, isAdding, snapMode));
|
|
92
207
|
}
|
|
93
|
-
}, [onChartMouseDown, snap]);
|
|
208
|
+
}, [onChartMouseDown, snap, isAdding, snapMode, useFakeCursor]);
|
|
94
209
|
const handleBlanketMouseUp = useCallback((e) => {
|
|
95
210
|
const snapped = snap(e);
|
|
96
211
|
if (snapped) {
|
|
212
|
+
// For circle/rectangle in pixel mode, decide whether this mouseUp should confirm
|
|
213
|
+
// the annotation or just set the anchor for the two-click workflow.
|
|
214
|
+
if (useFakeCursor && !firstClickPosition && mouseDownRealPosRef.current) {
|
|
215
|
+
// This is the FIRST click (anchor not yet recorded).
|
|
216
|
+
// Only confirm via drag — a stationary click just sets the anchor so the user
|
|
217
|
+
// can move the mouse and click again to choose the final size.
|
|
218
|
+
const moved = isDragInteraction(mouseDownRealPosRef.current, snapped.interactionCoordinate);
|
|
219
|
+
mouseDownRealPosRef.current = null;
|
|
220
|
+
if (!moved)
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
mouseDownRealPosRef.current = null;
|
|
225
|
+
}
|
|
97
226
|
onChartMouseUp(snapped);
|
|
98
227
|
}
|
|
99
|
-
}, [onChartMouseUp, snap]);
|
|
228
|
+
}, [onChartMouseUp, snap, useFakeCursor, firstClickPosition]);
|
|
100
229
|
const handleBlanketMouseMove = useCallback((e) => {
|
|
101
230
|
e.preventDefault();
|
|
102
231
|
e.stopPropagation();
|
|
@@ -105,10 +234,26 @@ export function RenderAnnotations({ annotations, isAdding, followerPosition, fir
|
|
|
105
234
|
onChartMouseMove(snapped);
|
|
106
235
|
}
|
|
107
236
|
}, [onChartMouseMove, snap]);
|
|
237
|
+
// Compute the anchor position for the fake cursor crosshair.
|
|
238
|
+
let fakeCursorPos = null;
|
|
239
|
+
if (useFakeCursor && followerPosition) {
|
|
240
|
+
if (firstClickPosition) {
|
|
241
|
+
fakeCursorPos = firstClickPosition.interactionCoordinate;
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
const { x, y } = followerPosition.interactionCoordinate;
|
|
245
|
+
fakeCursorPos =
|
|
246
|
+
isAdding === 'circle'
|
|
247
|
+
? { x: x - DEFAULT_CIRCLE_RADIUS, y }
|
|
248
|
+
: { x: x - DEFAULT_RECT_WIDTH, y: y - DEFAULT_RECT_HEIGHT };
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
const followerColor = getAnnotationColor(annotations.length);
|
|
108
252
|
return (React.createElement(ZIndexLayer, { zIndex: DefaultZIndexes.label - 1 },
|
|
109
|
-
isAdding && (React.createElement(Blanket, { pointerEvents: "auto", onClick: handleBlanketClick, onMouseDown: handleBlanketMouseDown, onMouseUp: handleBlanketMouseUp, onMouseMove: handleBlanketMouseMove, onMouseLeave: onChartMouseLeave, onMouseEnter: handleBlanketMouseMove })),
|
|
253
|
+
isAdding && (React.createElement(Blanket, { pointerEvents: "auto", cursor: useFakeCursor ? 'none' : undefined, onClick: handleBlanketClick, onMouseDown: handleBlanketMouseDown, onMouseUp: handleBlanketMouseUp, onMouseMove: handleBlanketMouseMove, onMouseLeave: onChartMouseLeave, onMouseEnter: handleBlanketMouseMove })),
|
|
110
254
|
React.createElement("g", { style: { pointerEvents: isAdding ? 'none' : undefined } }, annotations.map(renderAnnotation)),
|
|
111
255
|
isAdding &&
|
|
112
256
|
followerPosition &&
|
|
113
|
-
renderFollowerAnnotation(isAdding, snapMode, followerPosition, annotations.length, firstClickPosition)
|
|
257
|
+
renderFollowerAnnotation(isAdding, snapMode, followerPosition, annotations.length, firstClickPosition),
|
|
258
|
+
fakeCursorPos && (React.createElement(FakeCursor, { x: fakeCursorPos.x, y: fakeCursorPos.y, color: followerColor }))));
|
|
114
259
|
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { SnapResult } from './useSnap.js';
|
|
2
|
+
import type { AnnotationType, SnapMode } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Default dimensions for circle and rectangle pre-click follower previews (pixel mode only).
|
|
5
|
+
*
|
|
6
|
+
* In pixel mode the real cursor acts as pointB (the edge / corner of the annotation)
|
|
7
|
+
* while pointA (the anchor) is offset from the cursor by these amounts. This means
|
|
8
|
+
* `getDistance(anchor, cursor) === DEFAULT_CIRCLE_RADIUS` (or the rect diagonal) at the
|
|
9
|
+
* exact moment of the first click, eliminating any visual size jump.
|
|
10
|
+
*/
|
|
11
|
+
export declare const DEFAULT_CIRCLE_RADIUS = 30;
|
|
12
|
+
export declare const DEFAULT_RECT_WIDTH = 60;
|
|
13
|
+
export declare const DEFAULT_RECT_HEIGHT = 40;
|
|
14
|
+
/**
|
|
15
|
+
* Minimum real-cursor travel (pixels) between mouseDown and mouseUp that counts as a drag.
|
|
16
|
+
* Below this threshold the interaction is treated as a stationary click, keeping the
|
|
17
|
+
* two-click workflow active instead of immediately confirming the annotation.
|
|
18
|
+
*/
|
|
19
|
+
export declare const DRAG_THRESHOLD = 5;
|
|
20
|
+
/**
|
|
21
|
+
* For circle and rectangle in pixel mode, shifts the recorded anchor (pointA /
|
|
22
|
+
* firstClickPosition) away from the real cursor by the default annotation size so that the
|
|
23
|
+
* real cursor becomes pointB. The distance between anchor and cursor then equals the
|
|
24
|
+
* default size at the exact moment of the first click, giving a seamless transition from
|
|
25
|
+
* the pre-click preview.
|
|
26
|
+
*
|
|
27
|
+
* Only applied in pixel mode (`snapMode === 'none'`) because data-snapped annotations
|
|
28
|
+
* require their anchor to correspond to an actual data point.
|
|
29
|
+
*/
|
|
30
|
+
export declare function applyAnchorOffset(snap: SnapResult, type: AnnotationType | null, snapMode: SnapMode): SnapResult;
|
|
31
|
+
/**
|
|
32
|
+
* Returns true when the real cursor travelled far enough between mouseDown and mouseUp
|
|
33
|
+
* to be classified as a drag (rather than a stationary click).
|
|
34
|
+
*/
|
|
35
|
+
export declare function isDragInteraction(downPos: {
|
|
36
|
+
x: number;
|
|
37
|
+
y: number;
|
|
38
|
+
}, upPos: {
|
|
39
|
+
x: number;
|
|
40
|
+
y: number;
|
|
41
|
+
}): boolean;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Default dimensions for circle and rectangle pre-click follower previews (pixel mode only).
|
|
3
|
+
*
|
|
4
|
+
* In pixel mode the real cursor acts as pointB (the edge / corner of the annotation)
|
|
5
|
+
* while pointA (the anchor) is offset from the cursor by these amounts. This means
|
|
6
|
+
* `getDistance(anchor, cursor) === DEFAULT_CIRCLE_RADIUS` (or the rect diagonal) at the
|
|
7
|
+
* exact moment of the first click, eliminating any visual size jump.
|
|
8
|
+
*/
|
|
9
|
+
export const DEFAULT_CIRCLE_RADIUS = 30;
|
|
10
|
+
export const DEFAULT_RECT_WIDTH = 60;
|
|
11
|
+
export const DEFAULT_RECT_HEIGHT = 40;
|
|
12
|
+
/**
|
|
13
|
+
* Minimum real-cursor travel (pixels) between mouseDown and mouseUp that counts as a drag.
|
|
14
|
+
* Below this threshold the interaction is treated as a stationary click, keeping the
|
|
15
|
+
* two-click workflow active instead of immediately confirming the annotation.
|
|
16
|
+
*/
|
|
17
|
+
export const DRAG_THRESHOLD = 5;
|
|
18
|
+
/**
|
|
19
|
+
* For circle and rectangle in pixel mode, shifts the recorded anchor (pointA /
|
|
20
|
+
* firstClickPosition) away from the real cursor by the default annotation size so that the
|
|
21
|
+
* real cursor becomes pointB. The distance between anchor and cursor then equals the
|
|
22
|
+
* default size at the exact moment of the first click, giving a seamless transition from
|
|
23
|
+
* the pre-click preview.
|
|
24
|
+
*
|
|
25
|
+
* Only applied in pixel mode (`snapMode === 'none'`) because data-snapped annotations
|
|
26
|
+
* require their anchor to correspond to an actual data point.
|
|
27
|
+
*/
|
|
28
|
+
export function applyAnchorOffset(snap, type, snapMode) {
|
|
29
|
+
if (snapMode !== 'none')
|
|
30
|
+
return snap;
|
|
31
|
+
const { x, y } = snap.interactionCoordinate;
|
|
32
|
+
if (type === 'circle') {
|
|
33
|
+
return {
|
|
34
|
+
...snap,
|
|
35
|
+
interactionCoordinate: { x: x - DEFAULT_CIRCLE_RADIUS, y },
|
|
36
|
+
snappedCoordinate: undefined,
|
|
37
|
+
dataPoint: undefined,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
if (type === 'rectangle') {
|
|
41
|
+
return {
|
|
42
|
+
...snap,
|
|
43
|
+
interactionCoordinate: { x: x - DEFAULT_RECT_WIDTH, y: y - DEFAULT_RECT_HEIGHT },
|
|
44
|
+
snappedCoordinate: undefined,
|
|
45
|
+
dataPoint: undefined,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
return snap;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Returns true when the real cursor travelled far enough between mouseDown and mouseUp
|
|
52
|
+
* to be classified as a drag (rather than a stationary click).
|
|
53
|
+
*/
|
|
54
|
+
export function isDragInteraction(downPos, upPos) {
|
|
55
|
+
const dx = upPos.x - downPos.x;
|
|
56
|
+
const dy = upPos.y - downPos.y;
|
|
57
|
+
return Math.sqrt(dx * dx + dy * dy) > DRAG_THRESHOLD;
|
|
58
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useCallback, useState } from 'react';
|
|
2
2
|
import { DEFAULT_ANNOTATION_STYLE, getAnnotationColor } from './annotationColors.js';
|
|
3
3
|
import { getDistance } from './getDistance.js';
|
|
4
|
+
import { DEFAULT_CIRCLE_RADIUS, DEFAULT_RECT_HEIGHT, DEFAULT_RECT_WIDTH, } from './annotationInteractionUtils.js';
|
|
4
5
|
function annotationFactory(type, color, snapMode, pointA, pointB) {
|
|
5
6
|
const baseStyle = {
|
|
6
7
|
...DEFAULT_ANNOTATION_STYLE,
|
|
@@ -15,7 +16,7 @@ function annotationFactory(type, color, snapMode, pointA, pointB) {
|
|
|
15
16
|
style: baseStyle,
|
|
16
17
|
pointA,
|
|
17
18
|
pointB,
|
|
18
|
-
label: undefined,
|
|
19
|
+
label: type === 'label' ? { value: 'Label' } : undefined,
|
|
19
20
|
};
|
|
20
21
|
}
|
|
21
22
|
/**
|
|
@@ -80,8 +81,31 @@ export const useAnnotationsManager = (props) => {
|
|
|
80
81
|
if (isAdding === 'rectangle' || isAdding === 'freeformLine' || isAdding === 'circle') {
|
|
81
82
|
if (firstClickPosition) {
|
|
82
83
|
const dist = getDistance(firstClickPosition, snap);
|
|
84
|
+
let expectedMinimumDistance = 3;
|
|
85
|
+
// In pixel mode the anchor is offset from the real cursor by the default annotation
|
|
86
|
+
// size, so we raise the threshold to avoid confirming on the first stationary click.
|
|
87
|
+
// In data/tick mode there is no offset, so the base 3px threshold is sufficient.
|
|
88
|
+
if (snapMode === 'none') {
|
|
89
|
+
switch (isAdding) {
|
|
90
|
+
case 'circle': {
|
|
91
|
+
expectedMinimumDistance += DEFAULT_CIRCLE_RADIUS;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case 'rectangle': {
|
|
95
|
+
expectedMinimumDistance += getDistance(snap, {
|
|
96
|
+
interactionCoordinate: {
|
|
97
|
+
x: snap.interactionCoordinate.x + DEFAULT_RECT_WIDTH,
|
|
98
|
+
y: snap.interactionCoordinate.y + DEFAULT_RECT_HEIGHT,
|
|
99
|
+
},
|
|
100
|
+
snappedCoordinate: undefined,
|
|
101
|
+
dataPoint: undefined,
|
|
102
|
+
});
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
83
107
|
// Threshold to distinguish between a simple click (to set anchor) and a drag/second click
|
|
84
|
-
if (dist >
|
|
108
|
+
if (dist > expectedMinimumDistance) {
|
|
85
109
|
const newAnnotation = createAnnotation(isAdding, snapMode, firstClickPosition, snap);
|
|
86
110
|
setAnnotations((prev) => [...prev, newAnnotation]);
|
|
87
111
|
props?.onAnnotationAdd?.(newAnnotation);
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
6
|
"type": "module",
|
|
7
|
-
"version": "0.0.
|
|
7
|
+
"version": "0.0.10",
|
|
8
8
|
"description": "Devtools and helper functions for Recharts",
|
|
9
9
|
"main": "dist/index.js",
|
|
10
10
|
"types": "dist/index.d.ts",
|
|
@@ -30,19 +30,21 @@
|
|
|
30
30
|
"author": "Recharts Group",
|
|
31
31
|
"license": "MIT",
|
|
32
32
|
"devDependencies": {
|
|
33
|
+
"@testing-library/react": "^16.3.2",
|
|
33
34
|
"@types/node": "^20.0.0",
|
|
34
35
|
"@types/react": "^18.0.0",
|
|
35
36
|
"@types/react-dom": "^18.0.0",
|
|
36
37
|
"eslint": "^9.0.0",
|
|
37
38
|
"eslint-plugin-import": "^2.31.0",
|
|
38
39
|
"globals": "^15.0.0",
|
|
40
|
+
"jsdom": "^28.1.0",
|
|
39
41
|
"prettier": "^3.0.0",
|
|
40
|
-
"typescript": "^5.0.0",
|
|
41
|
-
"typescript-eslint": "^8.0.0",
|
|
42
|
-
"vitest": "^1.0.0",
|
|
43
42
|
"react": "^19.0.0",
|
|
44
43
|
"react-dom": "^19.0.0",
|
|
45
|
-
"recharts": "^3.8.0"
|
|
44
|
+
"recharts": "^3.8.0",
|
|
45
|
+
"typescript": "^5.0.0",
|
|
46
|
+
"typescript-eslint": "^8.0.0",
|
|
47
|
+
"vitest": "^1.0.0"
|
|
46
48
|
},
|
|
47
49
|
"peerDependencies": {
|
|
48
50
|
"react": "^18.0.0 || ^19.0.0",
|