@recharts/devtools 0.0.8 → 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.
Files changed (45) hide show
  1. package/dist/annotations/AnnotationsController.d.ts +40 -0
  2. package/dist/annotations/AnnotationsController.js +241 -0
  3. package/dist/annotations/Blanket.d.ts +33 -0
  4. package/dist/annotations/Blanket.js +19 -0
  5. package/dist/annotations/CircleAnnotation.d.ts +15 -0
  6. package/dist/annotations/CircleAnnotation.js +32 -0
  7. package/dist/annotations/Crosshair.d.ts +23 -0
  8. package/dist/annotations/Crosshair.js +42 -0
  9. package/dist/annotations/FreeformLine.d.ts +15 -0
  10. package/dist/annotations/FreeformLine.js +35 -0
  11. package/dist/annotations/HorizontalLine.d.ts +15 -0
  12. package/dist/annotations/HorizontalLine.js +23 -0
  13. package/dist/annotations/LabelAnnotation.d.ts +23 -0
  14. package/dist/annotations/LabelAnnotation.js +41 -0
  15. package/dist/annotations/RectangleAnnotation.d.ts +15 -0
  16. package/dist/annotations/RectangleAnnotation.js +36 -0
  17. package/dist/annotations/RenderAnnotations.d.ts +31 -0
  18. package/dist/annotations/RenderAnnotations.js +259 -0
  19. package/dist/annotations/VerticalLine.d.ts +15 -0
  20. package/dist/annotations/VerticalLine.js +23 -0
  21. package/dist/annotations/annotationColors.d.ts +20 -0
  22. package/dist/annotations/annotationColors.js +34 -0
  23. package/dist/annotations/annotationInteractionUtils.d.ts +41 -0
  24. package/dist/annotations/annotationInteractionUtils.js +58 -0
  25. package/dist/annotations/getDistance.d.ts +2 -0
  26. package/dist/annotations/getDistance.js +4 -0
  27. package/dist/annotations/index.d.ts +17 -0
  28. package/dist/annotations/index.js +17 -0
  29. package/dist/annotations/types.d.ts +38 -0
  30. package/dist/annotations/types.js +1 -0
  31. package/dist/annotations/useAnnotationsManager.d.ts +35 -0
  32. package/dist/annotations/useAnnotationsManager.js +160 -0
  33. package/dist/annotations/useSnap.d.ts +44 -0
  34. package/dist/annotations/useSnap.js +127 -0
  35. package/dist/components/RechartsAnnotations.d.ts +19 -0
  36. package/dist/components/RechartsAnnotations.js +38 -0
  37. package/dist/components/RechartsDevtools.d.ts +1 -1
  38. package/dist/components/RechartsDevtools.js +21 -15
  39. package/dist/constants.d.ts +1 -0
  40. package/dist/constants.js +1 -0
  41. package/dist/context/RechartsDevtoolsContext.d.ts +5 -0
  42. package/dist/context/RechartsDevtoolsContext.js +10 -1
  43. package/dist/index.d.ts +5 -2
  44. package/dist/index.js +4 -2
  45. package/package.json +8 -6
@@ -0,0 +1,36 @@
1
+ /**
2
+ * This annotation allows drawing a rectangle on a chart.
3
+ *
4
+ * It accepts either pixel coordinates or data values for positioning.
5
+ *
6
+ * In case it receives pixel coordinates, it uses a Rectangle component.
7
+ * If data values are provided, it uses a ReferenceArea component.
8
+ */
9
+ import React from 'react';
10
+ import { Rectangle, ReferenceArea } from 'recharts';
11
+ export function RectangleAnnotation({ annotation }) {
12
+ const { pointA, pointB, positionType, label, style } = annotation;
13
+ const { color = '#ccc', strokeWidth = 1, fill = '#ccc', opacity = 0.5, fillOpacity = 0.1, pointerEvents, } = style;
14
+ if (!pointB) {
15
+ return null;
16
+ }
17
+ if (positionType === 'data') {
18
+ // Use ReferenceArea for data-based positioning
19
+ if (!pointA.dataPoint || !pointB.dataPoint) {
20
+ return null;
21
+ }
22
+ return (React.createElement(ReferenceArea, { x1: String(pointA.dataPoint.x), x2: String(pointB.dataPoint.x), y1: String(pointA.dataPoint.y), y2: String(pointB.dataPoint.y), stroke: color, strokeWidth: strokeWidth, strokeOpacity: opacity, fill: fill, fillOpacity: fillOpacity, style: { pointerEvents }, label: label }));
23
+ }
24
+ // For pixel-based positioning, use Rectangle component
25
+ const x1Pixel = pointA.interactionCoordinate.x;
26
+ const x2Pixel = pointB.interactionCoordinate.x;
27
+ const y1Pixel = pointA.interactionCoordinate.y;
28
+ const y2Pixel = pointB.interactionCoordinate.y;
29
+ const x = Math.min(x1Pixel, x2Pixel);
30
+ const y = Math.min(y1Pixel, y2Pixel);
31
+ const width = Math.abs(x2Pixel - x1Pixel);
32
+ const height = Math.abs(y2Pixel - y1Pixel);
33
+ return (React.createElement("g", { style: { pointerEvents } },
34
+ React.createElement(Rectangle, { x: x, y: y, width: width, height: height, stroke: color, strokeWidth: strokeWidth, fill: fill, fillOpacity: fillOpacity }),
35
+ label && (React.createElement("text", { x: x + width / 2, y: y + height / 2, fill: label.style?.fill ?? color, fontSize: label.style?.fontSize ?? 10, fontWeight: label.style?.fontWeight ?? 'normal', textAnchor: "middle", dominantBaseline: "middle", pointerEvents: "none" }, label.value))));
36
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * @fileOverview Renders all annotations in the chart.
3
+ * Uses the Blanket to overlay the chart to capture mouse/touch events.
4
+ *
5
+ * It is likely that if you write your own Annotations component, you will want to
6
+ * follow the same pattern as this one. You may be able to reuse some of the hooks
7
+ * and components from the annotations/ folder.
8
+ */
9
+ import React from 'react';
10
+ import type { Annotation, AnnotationType, SnapMode } from './types.js';
11
+ import { SnapResult } from './useSnap.js';
12
+ interface RenderAnnotationsProps {
13
+ annotations: Annotation[];
14
+ isAdding: AnnotationType | null;
15
+ followerPosition: SnapResult | null;
16
+ firstClickPosition: SnapResult | null;
17
+ snapMode: SnapMode;
18
+ onChartClick: (snap: SnapResult) => void;
19
+ onChartMouseDown: (snap: SnapResult) => void;
20
+ onChartMouseUp: (snap: SnapResult) => void;
21
+ onChartMouseMove: (snap: SnapResult) => void;
22
+ onChartMouseLeave: () => void;
23
+ }
24
+ /**
25
+ * Component that renders all annotations and handles mouse events for adding new annotations.
26
+ *
27
+ * This component should be rendered inside a Recharts chart as a child,
28
+ * typically as the last child so it appears on top of other chart elements.
29
+ */
30
+ export declare function RenderAnnotations({ annotations, isAdding, followerPosition, firstClickPosition, snapMode, onChartClick, onChartMouseDown, onChartMouseUp, onChartMouseMove, onChartMouseLeave, }: RenderAnnotationsProps): React.JSX.Element;
31
+ export {};
@@ -0,0 +1,259 @@
1
+ /**
2
+ * @fileOverview Renders all annotations in the chart.
3
+ * Uses the Blanket to overlay the chart to capture mouse/touch events.
4
+ *
5
+ * It is likely that if you write your own Annotations component, you will want to
6
+ * follow the same pattern as this one. You may be able to reuse some of the hooks
7
+ * and components from the annotations/ folder.
8
+ */
9
+ import React, { useCallback, useRef } from 'react';
10
+ import { Blanket } from './Blanket.js';
11
+ import { HorizontalLine } from './HorizontalLine.js';
12
+ import { VerticalLine } from './VerticalLine.js';
13
+ import { CircleAnnotation } from './CircleAnnotation.js';
14
+ import { RectangleAnnotation } from './RectangleAnnotation.js';
15
+ import { getDistance } from './getDistance.js';
16
+ import { LabelAnnotation } from './LabelAnnotation.js';
17
+ import { FreeformLine } from './FreeformLine.js';
18
+ import { Crosshair } from './Crosshair.js';
19
+ import { getAnnotationColor } from './annotationColors.js';
20
+ import { useSnap } from './useSnap.js';
21
+ import { DefaultZIndexes, ZIndexLayer } from 'recharts';
22
+ import { DEFAULT_CIRCLE_RADIUS, DEFAULT_RECT_WIDTH, DEFAULT_RECT_HEIGHT, applyAnchorOffset, isDragInteraction, } from './annotationInteractionUtils.js';
23
+ function renderAnnotation(annotation) {
24
+ switch (annotation.type) {
25
+ case 'horizontalLine':
26
+ return React.createElement(HorizontalLine, { key: annotation.id, annotation: annotation });
27
+ case 'verticalLine':
28
+ return React.createElement(VerticalLine, { key: annotation.id, annotation: annotation });
29
+ case 'circle':
30
+ return React.createElement(CircleAnnotation, { key: annotation.id, annotation: annotation });
31
+ case 'rectangle':
32
+ return React.createElement(RectangleAnnotation, { key: annotation.id, annotation: annotation });
33
+ case 'label':
34
+ return React.createElement(LabelAnnotation, { key: annotation.id, annotation: annotation });
35
+ case 'freeformLine':
36
+ return React.createElement(FreeformLine, { key: annotation.id, annotation: annotation });
37
+ case 'crosshair':
38
+ return React.createElement(Crosshair, { key: annotation.id, annotation: annotation });
39
+ default:
40
+ return null;
41
+ }
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
+ }
60
+ /**
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.
66
+ */
67
+ function renderFollowerAnnotation(type, snapMode, position, annotationCount, firstClickPosition) {
68
+ const color = getAnnotationColor(annotationCount);
69
+ const baseStyle = {
70
+ color,
71
+ strokeWidth: 2,
72
+ strokeDasharray: '4',
73
+ fill: color,
74
+ opacity: 0.6,
75
+ fillOpacity: 0.1,
76
+ pointerEvents: 'none',
77
+ };
78
+ const baseAnnotation = {
79
+ type,
80
+ id: -1, // Temporary ID for follower
81
+ positionType: snapMode === 'none' ? 'pixel' : 'data',
82
+ style: baseStyle,
83
+ label: type === 'label' ? { value: 'Label' } : undefined,
84
+ };
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.
165
+ return renderAnnotation({
166
+ ...baseAnnotation,
167
+ pointA: position,
168
+ pointB: syntheticPoint(px + 30, py - 15),
169
+ });
170
+ }
171
+ // Single-click annotations (horizontalLine, verticalLine, crosshair, label).
172
+ return renderAnnotation({ ...baseAnnotation, pointA: position });
173
+ }
174
+ /**
175
+ * Component that renders all annotations and handles mouse events for adding new annotations.
176
+ *
177
+ * This component should be rendered inside a Recharts chart as a child,
178
+ * typically as the last child so it appears on top of other chart elements.
179
+ */
180
+ export function RenderAnnotations({ annotations, isAdding, followerPosition, firstClickPosition, snapMode, onChartClick, onChartMouseDown, onChartMouseUp, onChartMouseMove, onChartMouseLeave, }) {
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);
191
+ const handleBlanketClick = useCallback((e) => {
192
+ const snapped = snap(e);
193
+ if (snapped) {
194
+ onChartClick(applyAnchorOffset(snapped, isAdding, snapMode));
195
+ }
196
+ }, [onChartClick, snap]);
197
+ const handleBlanketMouseDown = useCallback((e) => {
198
+ const snapped = snap(e);
199
+ if (snapped) {
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));
207
+ }
208
+ }, [onChartMouseDown, snap, isAdding, snapMode, useFakeCursor]);
209
+ const handleBlanketMouseUp = useCallback((e) => {
210
+ const snapped = snap(e);
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
+ }
226
+ onChartMouseUp(snapped);
227
+ }
228
+ }, [onChartMouseUp, snap, useFakeCursor, firstClickPosition]);
229
+ const handleBlanketMouseMove = useCallback((e) => {
230
+ e.preventDefault();
231
+ e.stopPropagation();
232
+ const snapped = snap(e);
233
+ if (snapped) {
234
+ onChartMouseMove(snapped);
235
+ }
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);
252
+ return (React.createElement(ZIndexLayer, { zIndex: DefaultZIndexes.label - 1 },
253
+ isAdding && (React.createElement(Blanket, { pointerEvents: "auto", cursor: useFakeCursor ? 'none' : undefined, onClick: handleBlanketClick, onMouseDown: handleBlanketMouseDown, onMouseUp: handleBlanketMouseUp, onMouseMove: handleBlanketMouseMove, onMouseLeave: onChartMouseLeave, onMouseEnter: handleBlanketMouseMove })),
254
+ React.createElement("g", { style: { pointerEvents: isAdding ? 'none' : undefined } }, annotations.map(renderAnnotation)),
255
+ isAdding &&
256
+ followerPosition &&
257
+ renderFollowerAnnotation(isAdding, snapMode, followerPosition, annotations.length, firstClickPosition),
258
+ fakeCursorPos && (React.createElement(FakeCursor, { x: fakeCursorPos.x, y: fakeCursorPos.y, color: followerColor }))));
259
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ * This annotation allows drawing a vertical line on a chart.
3
+ *
4
+ * It accepts either pixel coordinates or data values for positioning.
5
+ *
6
+ * In case it receives pixel coordinates, it uses a line SVG element.
7
+ * If data values are provided, it uses a ReferenceLine component.
8
+ */
9
+ import React from 'react';
10
+ import { Annotation } from './types.js';
11
+ interface VerticalLineProps {
12
+ annotation: Annotation;
13
+ }
14
+ export declare function VerticalLine({ annotation }: VerticalLineProps): React.JSX.Element;
15
+ export {};
@@ -0,0 +1,23 @@
1
+ /**
2
+ * This annotation allows drawing a vertical line on a chart.
3
+ *
4
+ * It accepts either pixel coordinates or data values for positioning.
5
+ *
6
+ * In case it receives pixel coordinates, it uses a line SVG element.
7
+ * If data values are provided, it uses a ReferenceLine component.
8
+ */
9
+ import React from 'react';
10
+ import { ReferenceLine } from 'recharts';
11
+ export function VerticalLine({ annotation }) {
12
+ const { pointA, positionType, label, style } = annotation;
13
+ const { color = '#ccc', strokeWidth = 1, strokeDasharray, opacity = 1 } = style;
14
+ if (positionType === 'data') {
15
+ // Use ReferenceLine for data-based positioning
16
+ return (React.createElement(ReferenceLine, { x: String(pointA.dataPoint?.x), stroke: color, strokeWidth: strokeWidth, strokeDasharray: strokeDasharray, strokeOpacity: opacity, label: label }));
17
+ }
18
+ // For pixel-based positioning, use raw SVG line
19
+ const xPixel = pointA.interactionCoordinate.x;
20
+ return (React.createElement("g", null,
21
+ React.createElement("line", { x1: xPixel, y1: 0, x2: xPixel, y2: "100%", stroke: color, strokeWidth: strokeWidth, strokeDasharray: strokeDasharray, opacity: opacity, pointerEvents: "none" }),
22
+ label && (React.createElement("text", { x: xPixel + 5, y: 15, fill: label.style?.fill ?? color, fontSize: label.style?.fontSize ?? 10, fontWeight: label.style?.fontWeight ?? 'normal', pointerEvents: "none" }, label.value))));
23
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Array of bright, contrasting colors for annotations.
3
+ */
4
+ export declare const ANNOTATION_COLORS: string[];
5
+ /**
6
+ * Get a color for an annotation based on its index.
7
+ * Colors cycle through the palette.
8
+ */
9
+ export declare const getAnnotationColor: (index: number) => string;
10
+ /**
11
+ * Default annotation style values.
12
+ */
13
+ export declare const DEFAULT_ANNOTATION_STYLE: {
14
+ color: string;
15
+ strokeWidth: number;
16
+ strokeDasharray: string;
17
+ fill: string;
18
+ opacity: number;
19
+ fillOpacity: number;
20
+ };
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Array of bright, contrasting colors for annotations.
3
+ */
4
+ export const ANNOTATION_COLORS = [
5
+ '#FF0000', // Red
6
+ '#008000', // Dark Green
7
+ '#0000FF', // Blue
8
+ '#FF00FF', // Magenta
9
+ '#008B8B', // Dark Cyan
10
+ '#DAA520', // Goldenrod
11
+ '#FF8000', // Orange
12
+ '#8000FF', // Purple
13
+ '#FF0080', // Pink
14
+ '#228B22', // Forest Green
15
+ ];
16
+ /**
17
+ * Get a color for an annotation based on its index.
18
+ * Colors cycle through the palette.
19
+ */
20
+ export const getAnnotationColor = (index) => {
21
+ const normalized = ((index % ANNOTATION_COLORS.length) + ANNOTATION_COLORS.length) % ANNOTATION_COLORS.length;
22
+ return ANNOTATION_COLORS[normalized];
23
+ };
24
+ /**
25
+ * Default annotation style values.
26
+ */
27
+ export const DEFAULT_ANNOTATION_STYLE = {
28
+ color: '#FF0000',
29
+ strokeWidth: 2,
30
+ strokeDasharray: '4',
31
+ fill: '#FF0000',
32
+ opacity: 1,
33
+ fillOpacity: 0.1,
34
+ };
@@ -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
+ }
@@ -0,0 +1,2 @@
1
+ import type { SnapResult } from './useSnap.js';
2
+ export declare function getDistance(a: SnapResult, b: SnapResult): number;
@@ -0,0 +1,4 @@
1
+ export function getDistance(a, b) {
2
+ return Math.sqrt(Math.pow(a.interactionCoordinate.x - b.interactionCoordinate.x, 2) +
3
+ Math.pow(a.interactionCoordinate.y - b.interactionCoordinate.y, 2));
4
+ }
@@ -0,0 +1,17 @@
1
+ export type { Annotation, AnnotationType, AnnotationStyle, PositionType } from './types.js';
2
+ export { ANNOTATION_COLORS, getAnnotationColor, DEFAULT_ANNOTATION_STYLE, } from './annotationColors.js';
3
+ export { useAnnotationsManager } from './useAnnotationsManager.js';
4
+ export type { UseAnnotationsManagerProps, UseAnnotationsManagerReturn, } from './useAnnotationsManager.js';
5
+ export { useSnap } from './useSnap.js';
6
+ export type { SnapResult } from './useSnap.js';
7
+ export { Blanket } from './Blanket.js';
8
+ export type { BlanketProps } from './Blanket.js';
9
+ export { HorizontalLine } from './HorizontalLine.js';
10
+ export { VerticalLine } from './VerticalLine.js';
11
+ export { CircleAnnotation } from './CircleAnnotation.js';
12
+ export { RectangleAnnotation } from './RectangleAnnotation.js';
13
+ export { LabelAnnotation } from './LabelAnnotation.js';
14
+ export { FreeformLine } from './FreeformLine.js';
15
+ export { Crosshair } from './Crosshair.js';
16
+ export { RenderAnnotations } from './RenderAnnotations.js';
17
+ export { AnnotationsController } from './AnnotationsController.js';
@@ -0,0 +1,17 @@
1
+ // Colors and utilities
2
+ export { ANNOTATION_COLORS, getAnnotationColor, DEFAULT_ANNOTATION_STYLE, } from './annotationColors.js';
3
+ // Hooks
4
+ export { useAnnotationsManager } from './useAnnotationsManager.js';
5
+ export { useSnap } from './useSnap.js';
6
+ // Components - SVG annotations (render inside chart)
7
+ export { Blanket } from './Blanket.js';
8
+ export { HorizontalLine } from './HorizontalLine.js';
9
+ export { VerticalLine } from './VerticalLine.js';
10
+ export { CircleAnnotation } from './CircleAnnotation.js';
11
+ export { RectangleAnnotation } from './RectangleAnnotation.js';
12
+ export { LabelAnnotation } from './LabelAnnotation.js';
13
+ export { FreeformLine } from './FreeformLine.js';
14
+ export { Crosshair } from './Crosshair.js';
15
+ export { RenderAnnotations } from './RenderAnnotations.js';
16
+ // Components - HTML controls (render via portal)
17
+ export { AnnotationsController } from './AnnotationsController.js';
@@ -0,0 +1,38 @@
1
+ import { CSSProperties } from 'react';
2
+ import { SnapResult } from './useSnap.js';
3
+ import { LabelProps } from 'recharts';
4
+ /**
5
+ * Common style properties for annotations.
6
+ */
7
+ export interface AnnotationStyle {
8
+ color?: string;
9
+ strokeWidth?: number;
10
+ strokeDasharray?: string;
11
+ fill?: string;
12
+ opacity?: number;
13
+ fillOpacity?: number;
14
+ pointerEvents?: CSSProperties['pointerEvents'];
15
+ }
16
+ /**
17
+ * Position can be specified in pixels or data values.
18
+ * When using data values, the annotation will use Recharts Reference* components.
19
+ * When using pixel values, the annotation will use raw SVG elements.
20
+ */
21
+ export type PositionType = 'pixel' | 'data';
22
+ /**
23
+ * Union type of all annotation types.
24
+ */
25
+ export type Annotation = {
26
+ id: number;
27
+ type: 'horizontalLine' | 'verticalLine' | 'circle' | 'rectangle' | 'label' | 'freeformLine' | 'crosshair';
28
+ positionType: PositionType;
29
+ pointA: SnapResult;
30
+ pointB?: SnapResult;
31
+ label: LabelProps | undefined;
32
+ style: AnnotationStyle;
33
+ };
34
+ /**
35
+ * Type guard for annotation types.
36
+ */
37
+ export type AnnotationType = Annotation['type'];
38
+ export type SnapMode = 'none' | 'data' | 'tick';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,35 @@
1
+ import type { Annotation, AnnotationType, SnapMode } from './types.js';
2
+ import { SnapResult } from './useSnap.js';
3
+ export interface UseAnnotationsManagerProps {
4
+ onAnnotationAdd?: (annotation: Annotation) => void;
5
+ onAnnotationDelete?: (id: number) => void;
6
+ onAnnotationUpdate?: (annotation: Annotation) => void;
7
+ }
8
+ export interface UseAnnotationsManagerReturn {
9
+ annotations: Annotation[];
10
+ isAdding: AnnotationType | null;
11
+ followerPosition: SnapResult | null;
12
+ firstClickPosition: SnapResult | null;
13
+ selectedAnnotationId: number | null;
14
+ snapMode: SnapMode;
15
+ startAddingAnnotation: (type: AnnotationType) => void;
16
+ cancelAddingAnnotation: () => void;
17
+ deleteAnnotation: (id: number) => void;
18
+ updateAnnotation: (annotation: Annotation) => void;
19
+ selectAnnotation: (id: number | null) => void;
20
+ setSnapMode: (newSnapMode: SnapMode) => void;
21
+ onChartClick: (snap: SnapResult) => void;
22
+ onChartMouseDown: (snap: SnapResult) => void;
23
+ onChartMouseUp: (snap: SnapResult) => void;
24
+ onChartMouseMove: (snap: SnapResult) => void;
25
+ onChartMouseLeave: () => void;
26
+ }
27
+ /**
28
+ * Hook for managing annotation state and interactions.
29
+ *
30
+ * This hook provides state management for annotations including:
31
+ * - Adding, editing, and deleting annotations
32
+ * - Tracking mouse position for annotation placement
33
+ * - Selection state for editing
34
+ */
35
+ export declare const useAnnotationsManager: (props?: UseAnnotationsManagerProps) => UseAnnotationsManagerReturn;