@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.
- package/dist/annotations/AnnotationsController.d.ts +40 -0
- package/dist/annotations/AnnotationsController.js +241 -0
- package/dist/annotations/Blanket.d.ts +33 -0
- package/dist/annotations/Blanket.js +19 -0
- package/dist/annotations/CircleAnnotation.d.ts +15 -0
- package/dist/annotations/CircleAnnotation.js +32 -0
- package/dist/annotations/Crosshair.d.ts +23 -0
- package/dist/annotations/Crosshair.js +42 -0
- package/dist/annotations/FreeformLine.d.ts +15 -0
- package/dist/annotations/FreeformLine.js +35 -0
- package/dist/annotations/HorizontalLine.d.ts +15 -0
- package/dist/annotations/HorizontalLine.js +23 -0
- package/dist/annotations/LabelAnnotation.d.ts +23 -0
- package/dist/annotations/LabelAnnotation.js +41 -0
- package/dist/annotations/RectangleAnnotation.d.ts +15 -0
- package/dist/annotations/RectangleAnnotation.js +36 -0
- package/dist/annotations/RenderAnnotations.d.ts +31 -0
- package/dist/annotations/RenderAnnotations.js +259 -0
- package/dist/annotations/VerticalLine.d.ts +15 -0
- package/dist/annotations/VerticalLine.js +23 -0
- package/dist/annotations/annotationColors.d.ts +20 -0
- package/dist/annotations/annotationColors.js +34 -0
- package/dist/annotations/annotationInteractionUtils.d.ts +41 -0
- package/dist/annotations/annotationInteractionUtils.js +58 -0
- package/dist/annotations/getDistance.d.ts +2 -0
- package/dist/annotations/getDistance.js +4 -0
- package/dist/annotations/index.d.ts +17 -0
- package/dist/annotations/index.js +17 -0
- package/dist/annotations/types.d.ts +38 -0
- package/dist/annotations/types.js +1 -0
- package/dist/annotations/useAnnotationsManager.d.ts +35 -0
- package/dist/annotations/useAnnotationsManager.js +160 -0
- package/dist/annotations/useSnap.d.ts +44 -0
- package/dist/annotations/useSnap.js +127 -0
- package/dist/components/RechartsAnnotations.d.ts +19 -0
- package/dist/components/RechartsAnnotations.js +38 -0
- package/dist/components/RechartsDevtools.d.ts +1 -1
- package/dist/components/RechartsDevtools.js +21 -15
- package/dist/constants.d.ts +1 -0
- package/dist/constants.js +1 -0
- package/dist/context/RechartsDevtoolsContext.d.ts +5 -0
- package/dist/context/RechartsDevtoolsContext.js +10 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.js +4 -2
- package/package.json +8 -6
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react';
|
|
2
|
+
import { DEFAULT_ANNOTATION_STYLE, getAnnotationColor } from './annotationColors.js';
|
|
3
|
+
import { getDistance } from './getDistance.js';
|
|
4
|
+
import { DEFAULT_CIRCLE_RADIUS, DEFAULT_RECT_HEIGHT, DEFAULT_RECT_WIDTH, } from './annotationInteractionUtils.js';
|
|
5
|
+
function annotationFactory(type, color, snapMode, pointA, pointB) {
|
|
6
|
+
const baseStyle = {
|
|
7
|
+
...DEFAULT_ANNOTATION_STYLE,
|
|
8
|
+
color,
|
|
9
|
+
fill: color,
|
|
10
|
+
fillOpacity: 0.1,
|
|
11
|
+
};
|
|
12
|
+
return {
|
|
13
|
+
type,
|
|
14
|
+
id: Date.now(),
|
|
15
|
+
positionType: snapMode === 'none' ? 'pixel' : 'data',
|
|
16
|
+
style: baseStyle,
|
|
17
|
+
pointA,
|
|
18
|
+
pointB,
|
|
19
|
+
label: type === 'label' ? { value: 'Label' } : undefined,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Hook for managing annotation state and interactions.
|
|
24
|
+
*
|
|
25
|
+
* This hook provides state management for annotations including:
|
|
26
|
+
* - Adding, editing, and deleting annotations
|
|
27
|
+
* - Tracking mouse position for annotation placement
|
|
28
|
+
* - Selection state for editing
|
|
29
|
+
*/
|
|
30
|
+
export const useAnnotationsManager = (props) => {
|
|
31
|
+
const [annotations, setAnnotations] = useState([]);
|
|
32
|
+
const [isAdding, setIsAdding] = useState(null);
|
|
33
|
+
const [followerPosition, setFollowerPosition] = useState(null);
|
|
34
|
+
const [selectedAnnotationId, setSelectedAnnotationId] = useState(null);
|
|
35
|
+
const [snapMode, setSnapMode] = useState('none');
|
|
36
|
+
// Track click positions for multi-click annotations (rectangle, freeform line)
|
|
37
|
+
const [firstClickPosition, setFirstClickPosition] = useState(null);
|
|
38
|
+
const startAddingAnnotation = useCallback((type) => {
|
|
39
|
+
setIsAdding(type);
|
|
40
|
+
setFirstClickPosition(null);
|
|
41
|
+
setFollowerPosition(null);
|
|
42
|
+
}, []);
|
|
43
|
+
const cancelAddingAnnotation = useCallback(() => {
|
|
44
|
+
setIsAdding(null);
|
|
45
|
+
setFirstClickPosition(null);
|
|
46
|
+
setFollowerPosition(null);
|
|
47
|
+
}, []);
|
|
48
|
+
const deleteAnnotation = useCallback((id) => {
|
|
49
|
+
setAnnotations((prev) => prev.filter((a) => a.id !== id));
|
|
50
|
+
props?.onAnnotationDelete?.(id);
|
|
51
|
+
if (selectedAnnotationId === id) {
|
|
52
|
+
setSelectedAnnotationId(null);
|
|
53
|
+
}
|
|
54
|
+
}, [props, selectedAnnotationId]);
|
|
55
|
+
const updateAnnotation = useCallback((updatedAnnotation) => {
|
|
56
|
+
setAnnotations((prev) => prev.map((a) => (a.id === updatedAnnotation.id ? updatedAnnotation : a)));
|
|
57
|
+
props?.onAnnotationUpdate?.(updatedAnnotation);
|
|
58
|
+
}, [props]);
|
|
59
|
+
const selectAnnotation = useCallback((id) => {
|
|
60
|
+
setSelectedAnnotationId(id);
|
|
61
|
+
}, []);
|
|
62
|
+
const createAnnotation = useCallback((type, snapMode, pointA, pointB) => {
|
|
63
|
+
const color = getAnnotationColor(annotations.length);
|
|
64
|
+
return annotationFactory(type, color, snapMode, pointA, pointB);
|
|
65
|
+
}, [annotations.length]);
|
|
66
|
+
const onChartMouseDown = useCallback((snap) => {
|
|
67
|
+
if (!isAdding)
|
|
68
|
+
return;
|
|
69
|
+
// For rectangle and freeform line, MouseDown sets the first point (anchor)
|
|
70
|
+
if (isAdding === 'rectangle' || isAdding === 'freeformLine' || isAdding === 'circle') {
|
|
71
|
+
if (!firstClickPosition) {
|
|
72
|
+
setFirstClickPosition(snap);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}, [isAdding, firstClickPosition]);
|
|
76
|
+
const onChartMouseUp = useCallback((snap) => {
|
|
77
|
+
if (!isAdding)
|
|
78
|
+
return;
|
|
79
|
+
// For rectangle and freeform line, MouseUp can complete the annotation
|
|
80
|
+
// if it's a drag (distance > threshold) OR if it's the second click of a click-click flow
|
|
81
|
+
if (isAdding === 'rectangle' || isAdding === 'freeformLine' || isAdding === 'circle') {
|
|
82
|
+
if (firstClickPosition) {
|
|
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
|
+
}
|
|
107
|
+
// Threshold to distinguish between a simple click (to set anchor) and a drag/second click
|
|
108
|
+
if (dist > expectedMinimumDistance) {
|
|
109
|
+
const newAnnotation = createAnnotation(isAdding, snapMode, firstClickPosition, snap);
|
|
110
|
+
setAnnotations((prev) => [...prev, newAnnotation]);
|
|
111
|
+
props?.onAnnotationAdd?.(newAnnotation);
|
|
112
|
+
setIsAdding(null);
|
|
113
|
+
setFirstClickPosition(null);
|
|
114
|
+
setFollowerPosition(null);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}, [isAdding, firstClickPosition, createAnnotation, props, snapMode]);
|
|
119
|
+
const onChartClick = useCallback((snap) => {
|
|
120
|
+
if (!isAdding)
|
|
121
|
+
return;
|
|
122
|
+
// Two-point annotations are now handled by MouseDown/MouseUp
|
|
123
|
+
if (isAdding === 'rectangle' || isAdding === 'freeformLine' || isAdding === 'circle') {
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
// For other annotation types, single click creates the annotation
|
|
127
|
+
const newAnnotation = createAnnotation(isAdding, snapMode, snap);
|
|
128
|
+
setAnnotations((prev) => [...prev, newAnnotation]);
|
|
129
|
+
props?.onAnnotationAdd?.(newAnnotation);
|
|
130
|
+
setIsAdding(null);
|
|
131
|
+
setFollowerPosition(null);
|
|
132
|
+
}, [isAdding, createAnnotation, props, snapMode]);
|
|
133
|
+
const onChartMouseMove = useCallback((snap) => {
|
|
134
|
+
if (isAdding) {
|
|
135
|
+
setFollowerPosition(snap);
|
|
136
|
+
}
|
|
137
|
+
}, [isAdding]);
|
|
138
|
+
const onChartMouseLeave = useCallback(() => {
|
|
139
|
+
setFollowerPosition(null);
|
|
140
|
+
}, []);
|
|
141
|
+
return {
|
|
142
|
+
annotations,
|
|
143
|
+
isAdding,
|
|
144
|
+
followerPosition,
|
|
145
|
+
firstClickPosition,
|
|
146
|
+
selectedAnnotationId,
|
|
147
|
+
snapMode,
|
|
148
|
+
startAddingAnnotation,
|
|
149
|
+
cancelAddingAnnotation,
|
|
150
|
+
deleteAnnotation,
|
|
151
|
+
updateAnnotation,
|
|
152
|
+
selectAnnotation,
|
|
153
|
+
setSnapMode,
|
|
154
|
+
onChartClick,
|
|
155
|
+
onChartMouseDown,
|
|
156
|
+
onChartMouseUp,
|
|
157
|
+
onChartMouseMove,
|
|
158
|
+
onChartMouseLeave,
|
|
159
|
+
};
|
|
160
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook for snapping pixel coordinates to the nearest data point.
|
|
3
|
+
*
|
|
4
|
+
* This uses the Recharts scale hooks to convert between pixel and data coordinates,
|
|
5
|
+
* enabling "magnetic" annotation placement that snaps to actual data points.
|
|
6
|
+
*
|
|
7
|
+
* NOTE: This imports from 'recharts' using hooks that are not yet published.
|
|
8
|
+
* TypeScript/ESLint errors are expected until recharts 3.8 is released.
|
|
9
|
+
*
|
|
10
|
+
* The hook detects this by checking if the inverse scale returns a value that differs
|
|
11
|
+
* significantly from the pixel position when converted back. If the roundtrip produces
|
|
12
|
+
* a very different value, it indicates snapping occurred, and we use that. Otherwise,
|
|
13
|
+
* we keep the original coordinate for smooth freeform positioning.
|
|
14
|
+
*/
|
|
15
|
+
import { MouseEvent } from 'react';
|
|
16
|
+
import { Coordinate } from 'recharts';
|
|
17
|
+
import { SnapMode } from './types.js';
|
|
18
|
+
export interface SnapResult {
|
|
19
|
+
interactionCoordinate: Coordinate;
|
|
20
|
+
snappedCoordinate: Coordinate | undefined;
|
|
21
|
+
dataPoint: {
|
|
22
|
+
x: unknown;
|
|
23
|
+
y: unknown;
|
|
24
|
+
} | undefined;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Hook that provides a function to snap pixel coordinates to the nearest data point.
|
|
28
|
+
*
|
|
29
|
+
* When snap-to-data is enabled, this will:
|
|
30
|
+
* 1. Convert pixel coordinates to data values using inverse scale
|
|
31
|
+
* 2. Find the closest data point in the domain (categories)
|
|
32
|
+
* 3. Convert back to pixel coordinates for display
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* const snapToData = useSnapToData({ enabled: true });
|
|
37
|
+
*
|
|
38
|
+
* const handleMouseMove = (x: number, y: number) => {
|
|
39
|
+
* const snapped = snapToData(x, y);
|
|
40
|
+
* setPosition({ x: snapped.x, y: snapped.y });
|
|
41
|
+
* };
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export declare function useSnap(snapMode: SnapMode): (e: MouseEvent<SVGGraphicsElement>) => SnapResult | undefined;
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook for snapping pixel coordinates to the nearest data point.
|
|
3
|
+
*
|
|
4
|
+
* This uses the Recharts scale hooks to convert between pixel and data coordinates,
|
|
5
|
+
* enabling "magnetic" annotation placement that snaps to actual data points.
|
|
6
|
+
*
|
|
7
|
+
* NOTE: This imports from 'recharts' using hooks that are not yet published.
|
|
8
|
+
* TypeScript/ESLint errors are expected until recharts 3.8 is released.
|
|
9
|
+
*
|
|
10
|
+
* The hook detects this by checking if the inverse scale returns a value that differs
|
|
11
|
+
* significantly from the pixel position when converted back. If the roundtrip produces
|
|
12
|
+
* a very different value, it indicates snapping occurred, and we use that. Otherwise,
|
|
13
|
+
* we keep the original coordinate for smooth freeform positioning.
|
|
14
|
+
*/
|
|
15
|
+
import { useCallback } from 'react';
|
|
16
|
+
import { getRelativeCoordinate, useXAxisInverseDataSnapScale, useYAxisInverseDataSnapScale, useXAxisInverseTickSnapScale, useYAxisInverseTickSnapScale, useXAxisScale, useYAxisScale, } from 'recharts';
|
|
17
|
+
/**
|
|
18
|
+
* Hook that provides a function to snap pixel coordinates to the nearest data point.
|
|
19
|
+
*
|
|
20
|
+
* When snap-to-data is enabled, this will:
|
|
21
|
+
* 1. Convert pixel coordinates to data values using inverse scale
|
|
22
|
+
* 2. Find the closest data point in the domain (categories)
|
|
23
|
+
* 3. Convert back to pixel coordinates for display
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```tsx
|
|
27
|
+
* const snapToData = useSnapToData({ enabled: true });
|
|
28
|
+
*
|
|
29
|
+
* const handleMouseMove = (x: number, y: number) => {
|
|
30
|
+
* const snapped = snapToData(x, y);
|
|
31
|
+
* setPosition({ x: snapped.x, y: snapped.y });
|
|
32
|
+
* };
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export function useSnap(snapMode) {
|
|
36
|
+
// Get inverse scale functions for converting pixels -> data
|
|
37
|
+
const xInverseDataSnapScale = useXAxisInverseDataSnapScale();
|
|
38
|
+
const yInverseDataSnapScale = useYAxisInverseDataSnapScale();
|
|
39
|
+
const xInverseTickSnapScale = useXAxisInverseTickSnapScale();
|
|
40
|
+
const yInverseTickSnapScale = useYAxisInverseTickSnapScale();
|
|
41
|
+
// And forward scales for data -> pixels
|
|
42
|
+
const scaleX = useXAxisScale();
|
|
43
|
+
const scaleY = useYAxisScale();
|
|
44
|
+
return useCallback((e) => {
|
|
45
|
+
if (xInverseDataSnapScale == null ||
|
|
46
|
+
yInverseDataSnapScale == null ||
|
|
47
|
+
xInverseTickSnapScale == null ||
|
|
48
|
+
yInverseTickSnapScale == null ||
|
|
49
|
+
scaleX == null ||
|
|
50
|
+
scaleY == null) {
|
|
51
|
+
/*
|
|
52
|
+
* Scales not available yet (e.g., outside of chart context, or chart not fully initialized)
|
|
53
|
+
* Just return zeroes to avoid errors
|
|
54
|
+
*/
|
|
55
|
+
return {
|
|
56
|
+
interactionCoordinate: { x: 0, y: 0 },
|
|
57
|
+
snappedCoordinate: undefined,
|
|
58
|
+
dataPoint: undefined,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/*
|
|
62
|
+
* Here we get pixel coordinates relative to the chart area (accounting for transforms)
|
|
63
|
+
* These are exact mouse interaction coordinates.
|
|
64
|
+
* Next, we need to "snap" these to the nearest data point if snapMode is enabled.
|
|
65
|
+
*/
|
|
66
|
+
const interactionCoordinate = getRelativeCoordinate(e);
|
|
67
|
+
let snappedDataPoint = undefined;
|
|
68
|
+
if (snapMode === 'data') {
|
|
69
|
+
snappedDataPoint = {
|
|
70
|
+
x: xInverseDataSnapScale(interactionCoordinate.relativeX),
|
|
71
|
+
y: yInverseDataSnapScale(interactionCoordinate.relativeY),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
else if (snapMode === 'tick') {
|
|
75
|
+
snappedDataPoint = {
|
|
76
|
+
x: xInverseTickSnapScale(interactionCoordinate.relativeX),
|
|
77
|
+
y: yInverseTickSnapScale(interactionCoordinate.relativeY),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
if (snappedDataPoint == null) {
|
|
81
|
+
// If snapping failed for some reason, just return the original interaction coordinates
|
|
82
|
+
return {
|
|
83
|
+
interactionCoordinate: {
|
|
84
|
+
x: interactionCoordinate.relativeX,
|
|
85
|
+
y: interactionCoordinate.relativeY,
|
|
86
|
+
},
|
|
87
|
+
snappedCoordinate: undefined,
|
|
88
|
+
dataPoint: undefined,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/*
|
|
92
|
+
* Converts snapped data point back to pixel coordinates.
|
|
93
|
+
* Some components will read the snapped data point to display values, while others will use the snapped pixel coordinates for positioning.
|
|
94
|
+
* We return here both the snapped data point (for display) and the snapped pixel coordinates (for positioning) for convenience.
|
|
95
|
+
* If you are using ReferenceLine/ReferenceDot, you can skip this step and use the snappedDataPoint directly.
|
|
96
|
+
*/
|
|
97
|
+
const snappedX = scaleX(snappedDataPoint.x);
|
|
98
|
+
const snappedY = scaleY(snappedDataPoint.y);
|
|
99
|
+
if (snappedX == null || snappedY == null) {
|
|
100
|
+
// If the snapped data point is outside the chart area, just return the original interaction coordinates
|
|
101
|
+
return {
|
|
102
|
+
interactionCoordinate: {
|
|
103
|
+
x: interactionCoordinate.relativeX,
|
|
104
|
+
y: interactionCoordinate.relativeY,
|
|
105
|
+
},
|
|
106
|
+
snappedCoordinate: undefined,
|
|
107
|
+
dataPoint: undefined,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
interactionCoordinate: {
|
|
112
|
+
x: interactionCoordinate.relativeX,
|
|
113
|
+
y: interactionCoordinate.relativeY,
|
|
114
|
+
},
|
|
115
|
+
snappedCoordinate: { x: snappedX, y: snappedY },
|
|
116
|
+
dataPoint: snappedDataPoint,
|
|
117
|
+
};
|
|
118
|
+
}, [
|
|
119
|
+
snapMode,
|
|
120
|
+
xInverseDataSnapScale,
|
|
121
|
+
yInverseDataSnapScale,
|
|
122
|
+
xInverseTickSnapScale,
|
|
123
|
+
yInverseTickSnapScale,
|
|
124
|
+
scaleX,
|
|
125
|
+
scaleY,
|
|
126
|
+
]);
|
|
127
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview RechartsAnnotations component for adding interactive annotations to charts.
|
|
3
|
+
*
|
|
4
|
+
* This component follows the same pattern as RechartsDevtools:
|
|
5
|
+
* - Renders SVG overlays directly in the chart context
|
|
6
|
+
* - Uses a portal to render the UI controls in an external container
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* ```tsx
|
|
10
|
+
* <AreaChart data={data}>
|
|
11
|
+
* <XAxis dataKey="name" />
|
|
12
|
+
* <YAxis />
|
|
13
|
+
* <Area dataKey="value" />
|
|
14
|
+
* <RechartsAnnotations />
|
|
15
|
+
* </AreaChart>
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
import React from 'react';
|
|
19
|
+
export declare const RechartsAnnotations: () => React.JSX.Element;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview RechartsAnnotations component for adding interactive annotations to charts.
|
|
3
|
+
*
|
|
4
|
+
* This component follows the same pattern as RechartsDevtools:
|
|
5
|
+
* - Renders SVG overlays directly in the chart context
|
|
6
|
+
* - Uses a portal to render the UI controls in an external container
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* ```tsx
|
|
10
|
+
* <AreaChart data={data}>
|
|
11
|
+
* <XAxis dataKey="name" />
|
|
12
|
+
* <YAxis />
|
|
13
|
+
* <Area dataKey="value" />
|
|
14
|
+
* <RechartsAnnotations />
|
|
15
|
+
* </AreaChart>
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
import React from 'react';
|
|
19
|
+
import { createPortal } from 'react-dom';
|
|
20
|
+
import { RECHARTS_ANNOTATIONS_PORTAL_ID } from '../constants.js';
|
|
21
|
+
import { useRechartsDevtoolsContext } from '../context/RechartsDevtoolsContext.js';
|
|
22
|
+
import { AnnotationsController, RenderAnnotations, useAnnotationsManager } from '../annotations/index.js';
|
|
23
|
+
export const RechartsAnnotations = () => {
|
|
24
|
+
const contextId = useRechartsDevtoolsContext();
|
|
25
|
+
// Use annotations-specific portal ID, falling back to global constant
|
|
26
|
+
const portalId = contextId ? `${contextId}-annotations` : RECHARTS_ANNOTATIONS_PORTAL_ID;
|
|
27
|
+
const [container, setContainer] = React.useState(null);
|
|
28
|
+
const annotationsManager = useAnnotationsManager();
|
|
29
|
+
React.useEffect(() => {
|
|
30
|
+
const el = document.getElementById(portalId);
|
|
31
|
+
setContainer(el);
|
|
32
|
+
}, [portalId]);
|
|
33
|
+
return (React.createElement(React.Fragment, null,
|
|
34
|
+
React.createElement(RenderAnnotations, { annotations: annotationsManager.annotations, isAdding: annotationsManager.isAdding, followerPosition: annotationsManager.followerPosition, firstClickPosition: annotationsManager.firstClickPosition, snapMode: annotationsManager.snapMode, onChartClick: annotationsManager.onChartClick, onChartMouseDown: annotationsManager.onChartMouseDown, onChartMouseUp: annotationsManager.onChartMouseUp, onChartMouseMove: annotationsManager.onChartMouseMove, onChartMouseLeave: annotationsManager.onChartMouseLeave }),
|
|
35
|
+
container &&
|
|
36
|
+
createPortal(React.createElement("div", { className: "recharts-annotations", style: { fontFamily: 'monospace', fontSize: '12px' } },
|
|
37
|
+
React.createElement(AnnotationsController, { annotations: annotationsManager.annotations, isAdding: annotationsManager.isAdding, selectedAnnotationId: annotationsManager.selectedAnnotationId, snapMode: annotationsManager.snapMode, onStartAdding: annotationsManager.startAddingAnnotation, onCancelAdding: annotationsManager.cancelAddingAnnotation, onDeleteAnnotation: annotationsManager.deleteAnnotation, onUpdateAnnotation: annotationsManager.updateAnnotation, onSelectAnnotation: annotationsManager.selectAnnotation, onSnapModeChange: annotationsManager.setSnapMode })), container)));
|
|
38
|
+
};
|
|
@@ -2,5 +2,5 @@ import React from 'react';
|
|
|
2
2
|
import { InspectorDef } from '../types.js';
|
|
3
3
|
declare const INSPECTORS: Record<string, InspectorDef>;
|
|
4
4
|
export type InspectorKey = keyof typeof INSPECTORS;
|
|
5
|
-
export declare const RechartsDevtools: () => React.JSX.Element
|
|
5
|
+
export declare const RechartsDevtools: () => React.JSX.Element;
|
|
6
6
|
export {};
|
|
@@ -11,6 +11,7 @@ import { YAxisDomainInspector } from '../inspectors/YAxisDomainInspector.js';
|
|
|
11
11
|
import { ActiveTooltipLabelInspector } from '../inspectors/ActiveTooltipLabelInspector.js';
|
|
12
12
|
import { useSessionStorageState } from '../hooks/useSessionStorageState.js';
|
|
13
13
|
import { useRechartsDevtoolsContext } from '../context/RechartsDevtoolsContext.js';
|
|
14
|
+
import { RechartsAnnotations } from './RechartsAnnotations.js';
|
|
14
15
|
const INSPECTORS = {
|
|
15
16
|
'useChartWidth | useChartHeight': ChartDimensionInspector,
|
|
16
17
|
useMargin: MarginInspector,
|
|
@@ -48,21 +49,26 @@ export const RechartsDevtools = () => {
|
|
|
48
49
|
}
|
|
49
50
|
}, [portalId, setSelectedInspectorId]);
|
|
50
51
|
const [isOverlayEnabled, setIsOverlayEnabled] = useSessionStorageState('rechartsDevtoolsOverlayEnabled', false);
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
const { Inspector, Overlay } = selectedInspector;
|
|
52
|
+
const Inspector = selectedInspector?.Inspector;
|
|
53
|
+
const Overlay = selectedInspector?.Overlay;
|
|
55
54
|
return (React.createElement(React.Fragment, null,
|
|
56
55
|
Overlay && isOverlayEnabled && React.createElement(Overlay, null),
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
56
|
+
container &&
|
|
57
|
+
selectedInspector &&
|
|
58
|
+
createPortal(React.createElement("div", { className: "recharts-devtools", style: { fontFamily: 'monospace', fontSize: '12px' } },
|
|
59
|
+
React.createElement("div", { style: {
|
|
60
|
+
marginBottom: '10px',
|
|
61
|
+
paddingBottom: '10px',
|
|
62
|
+
borderBottom: '1px solid #ccc',
|
|
63
|
+
} },
|
|
64
|
+
React.createElement("div", { style: { display: 'flex', alignItems: 'center', marginBottom: '4px', gap: '8px' } },
|
|
65
|
+
React.createElement("label", null, "Inspect Hook:"),
|
|
66
|
+
React.createElement("select", { value: selectedInspectorId, onChange: (e) => setSelectedInspectorId(e.target.value), style: { padding: '4px' } }, Object.keys(INSPECTORS).map((key) => (React.createElement("option", { key: key, value: key }, key)))),
|
|
67
|
+
Overlay && (React.createElement("div", { style: { marginTop: '4px' } },
|
|
68
|
+
React.createElement("label", { style: { cursor: 'pointer', display: 'flex', alignItems: 'center' } },
|
|
69
|
+
React.createElement("input", { type: "checkbox", checked: isOverlayEnabled, onChange: (e) => setIsOverlayEnabled(e.target.checked), style: { marginRight: '4px' } }),
|
|
70
|
+
"Show Overlay on chart"))))),
|
|
71
|
+
React.createElement("div", { style: { padding: '10px 0' } },
|
|
72
|
+
React.createElement(Inspector, null))), container),
|
|
73
|
+
React.createElement(RechartsAnnotations, null)));
|
|
68
74
|
};
|
package/dist/constants.d.ts
CHANGED
package/dist/constants.js
CHANGED
|
@@ -8,4 +8,9 @@ export declare const useRechartsDevtoolsContext: () => string | null;
|
|
|
8
8
|
export declare const RechartsDevtoolsPortal: React.FC<React.HTMLAttributes<HTMLDivElement> & {
|
|
9
9
|
initialTab?: InspectorKey;
|
|
10
10
|
}>;
|
|
11
|
+
/**
|
|
12
|
+
* Portal container for annotations UI.
|
|
13
|
+
* Place this component where you want the annotations controller to render.
|
|
14
|
+
*/
|
|
15
|
+
export declare const RechartsAnnotationsPortal: React.FC<React.HTMLAttributes<HTMLDivElement>>;
|
|
11
16
|
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import React, { createContext, useContext, useId } from 'react';
|
|
2
|
-
import { RECHARTS_DEVTOOLS_PORTAL_ID } from '../constants.js';
|
|
2
|
+
import { RECHARTS_DEVTOOLS_PORTAL_ID, RECHARTS_ANNOTATIONS_PORTAL_ID } from '../constants.js';
|
|
3
3
|
const Context = createContext(null);
|
|
4
4
|
export const RechartsDevtoolsContext = ({ children }) => {
|
|
5
5
|
// Generate a unique ID for this instance.
|
|
@@ -17,3 +17,12 @@ export const RechartsDevtoolsPortal = ({ initialTab, ...props }) => {
|
|
|
17
17
|
const id = contextId ?? RECHARTS_DEVTOOLS_PORTAL_ID;
|
|
18
18
|
return React.createElement("div", { id: id, "data-initial-tab": initialTab, ...props });
|
|
19
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* Portal container for annotations UI.
|
|
22
|
+
* Place this component where you want the annotations controller to render.
|
|
23
|
+
*/
|
|
24
|
+
export const RechartsAnnotationsPortal = (props) => {
|
|
25
|
+
const contextId = useRechartsDevtoolsContext();
|
|
26
|
+
const id = contextId ? `${contextId}-annotations` : RECHARTS_ANNOTATIONS_PORTAL_ID;
|
|
27
|
+
return React.createElement("div", { id: id, ...props });
|
|
28
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,9 @@ export { between, random } from './random.js';
|
|
|
2
2
|
export { generateMockData } from './generateMockData.js';
|
|
3
3
|
export { generateMockMarketData } from './generateMockMarketData.js';
|
|
4
4
|
export { RechartsDevtools } from './components/RechartsDevtools.js';
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
5
|
+
export { RechartsAnnotations } from './components/RechartsAnnotations.js';
|
|
6
|
+
export { RechartsDevtoolsPortal, RechartsAnnotationsPortal, RechartsDevtoolsContext, } from './context/RechartsDevtoolsContext.js';
|
|
7
|
+
export { RECHARTS_DEVTOOLS_PORTAL_ID, RECHARTS_ANNOTATIONS_PORTAL_ID } from './constants.js';
|
|
8
|
+
export type { Annotation, AnnotationType, AnnotationStyle, } from './annotations/types.js';
|
|
9
|
+
export { useAnnotationsManager } from './annotations/useAnnotationsManager.js';
|
|
7
10
|
export { useWhyDidYouRender } from './hooks/useWhyDidYouRender.js';
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,8 @@ export { between, random } from './random.js';
|
|
|
2
2
|
export { generateMockData } from './generateMockData.js';
|
|
3
3
|
export { generateMockMarketData } from './generateMockMarketData.js';
|
|
4
4
|
export { RechartsDevtools } from './components/RechartsDevtools.js';
|
|
5
|
-
export {
|
|
6
|
-
export {
|
|
5
|
+
export { RechartsAnnotations } from './components/RechartsAnnotations.js';
|
|
6
|
+
export { RechartsDevtoolsPortal, RechartsAnnotationsPortal, RechartsDevtoolsContext, } from './context/RechartsDevtoolsContext.js';
|
|
7
|
+
export { RECHARTS_DEVTOOLS_PORTAL_ID, RECHARTS_ANNOTATIONS_PORTAL_ID } from './constants.js';
|
|
8
|
+
export { useAnnotationsManager } from './annotations/useAnnotationsManager.js';
|
|
7
9
|
export { useWhyDidYouRender } from './hooks/useWhyDidYouRender.js';
|
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,23 +30,25 @@
|
|
|
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": "
|
|
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",
|
|
49
51
|
"react-dom": "^18.0.0 || ^19.0.0",
|
|
50
|
-
"recharts": "^3.
|
|
52
|
+
"recharts": "^3.8.0"
|
|
51
53
|
}
|
|
52
54
|
}
|