@recharts/devtools 0.0.8 → 0.0.9
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 +31 -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 +114 -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/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 +136 -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 +3 -3
|
@@ -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,114 @@
|
|
|
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 } 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 { LabelAnnotation } from './LabelAnnotation.js';
|
|
16
|
+
import { FreeformLine } from './FreeformLine.js';
|
|
17
|
+
import { Crosshair } from './Crosshair.js';
|
|
18
|
+
import { getAnnotationColor } from './annotationColors.js';
|
|
19
|
+
import { useSnap } from './useSnap.js';
|
|
20
|
+
import { DefaultZIndexes, ZIndexLayer } from 'recharts';
|
|
21
|
+
function renderAnnotation(annotation) {
|
|
22
|
+
switch (annotation.type) {
|
|
23
|
+
case 'horizontalLine':
|
|
24
|
+
return React.createElement(HorizontalLine, { key: annotation.id, annotation: annotation });
|
|
25
|
+
case 'verticalLine':
|
|
26
|
+
return React.createElement(VerticalLine, { key: annotation.id, annotation: annotation });
|
|
27
|
+
case 'circle':
|
|
28
|
+
return React.createElement(CircleAnnotation, { key: annotation.id, annotation: annotation });
|
|
29
|
+
case 'rectangle':
|
|
30
|
+
return React.createElement(RectangleAnnotation, { key: annotation.id, annotation: annotation });
|
|
31
|
+
case 'label':
|
|
32
|
+
return React.createElement(LabelAnnotation, { key: annotation.id, annotation: annotation });
|
|
33
|
+
case 'freeformLine':
|
|
34
|
+
return React.createElement(FreeformLine, { key: annotation.id, annotation: annotation });
|
|
35
|
+
case 'crosshair':
|
|
36
|
+
return React.createElement(Crosshair, { key: annotation.id, annotation: annotation });
|
|
37
|
+
default:
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Renders a preview of the annotation being added, following the mouse cursor.
|
|
43
|
+
*/
|
|
44
|
+
function renderFollowerAnnotation(type, snapMode, position, annotationCount, firstClickPosition) {
|
|
45
|
+
const color = getAnnotationColor(annotationCount);
|
|
46
|
+
const baseStyle = {
|
|
47
|
+
color,
|
|
48
|
+
strokeWidth: 2,
|
|
49
|
+
strokeDasharray: '4',
|
|
50
|
+
fill: color,
|
|
51
|
+
opacity: 0.6,
|
|
52
|
+
fillOpacity: 0.1,
|
|
53
|
+
pointerEvents: 'none',
|
|
54
|
+
};
|
|
55
|
+
const baseAnnotation = {
|
|
56
|
+
type,
|
|
57
|
+
id: -1, // Temporary ID for follower
|
|
58
|
+
positionType: snapMode === 'none' ? 'pixel' : 'data',
|
|
59
|
+
style: baseStyle,
|
|
60
|
+
label: undefined,
|
|
61
|
+
};
|
|
62
|
+
if (firstClickPosition == null) {
|
|
63
|
+
return renderAnnotation({
|
|
64
|
+
...baseAnnotation,
|
|
65
|
+
pointA: position,
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return renderAnnotation({
|
|
69
|
+
...baseAnnotation,
|
|
70
|
+
pointA: firstClickPosition,
|
|
71
|
+
pointB: position,
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Component that renders all annotations and handles mouse events for adding new annotations.
|
|
76
|
+
*
|
|
77
|
+
* This component should be rendered inside a Recharts chart as a child,
|
|
78
|
+
* typically as the last child so it appears on top of other chart elements.
|
|
79
|
+
*/
|
|
80
|
+
export function RenderAnnotations({ annotations, isAdding, followerPosition, firstClickPosition, snapMode, onChartClick, onChartMouseDown, onChartMouseUp, onChartMouseMove, onChartMouseLeave, }) {
|
|
81
|
+
const snap = useSnap(snapMode);
|
|
82
|
+
const handleBlanketClick = useCallback((e) => {
|
|
83
|
+
const snapped = snap(e);
|
|
84
|
+
if (snapped) {
|
|
85
|
+
onChartClick(snapped);
|
|
86
|
+
}
|
|
87
|
+
}, [onChartClick, snap]);
|
|
88
|
+
const handleBlanketMouseDown = useCallback((e) => {
|
|
89
|
+
const snapped = snap(e);
|
|
90
|
+
if (snapped) {
|
|
91
|
+
onChartMouseDown(snapped);
|
|
92
|
+
}
|
|
93
|
+
}, [onChartMouseDown, snap]);
|
|
94
|
+
const handleBlanketMouseUp = useCallback((e) => {
|
|
95
|
+
const snapped = snap(e);
|
|
96
|
+
if (snapped) {
|
|
97
|
+
onChartMouseUp(snapped);
|
|
98
|
+
}
|
|
99
|
+
}, [onChartMouseUp, snap]);
|
|
100
|
+
const handleBlanketMouseMove = useCallback((e) => {
|
|
101
|
+
e.preventDefault();
|
|
102
|
+
e.stopPropagation();
|
|
103
|
+
const snapped = snap(e);
|
|
104
|
+
if (snapped) {
|
|
105
|
+
onChartMouseMove(snapped);
|
|
106
|
+
}
|
|
107
|
+
}, [onChartMouseMove, snap]);
|
|
108
|
+
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 })),
|
|
110
|
+
React.createElement("g", { style: { pointerEvents: isAdding ? 'none' : undefined } }, annotations.map(renderAnnotation)),
|
|
111
|
+
isAdding &&
|
|
112
|
+
followerPosition &&
|
|
113
|
+
renderFollowerAnnotation(isAdding, snapMode, followerPosition, annotations.length, firstClickPosition)));
|
|
114
|
+
}
|
|
@@ -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,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;
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react';
|
|
2
|
+
import { DEFAULT_ANNOTATION_STYLE, getAnnotationColor } from './annotationColors.js';
|
|
3
|
+
import { getDistance } from './getDistance.js';
|
|
4
|
+
function annotationFactory(type, color, snapMode, pointA, pointB) {
|
|
5
|
+
const baseStyle = {
|
|
6
|
+
...DEFAULT_ANNOTATION_STYLE,
|
|
7
|
+
color,
|
|
8
|
+
fill: color,
|
|
9
|
+
fillOpacity: 0.1,
|
|
10
|
+
};
|
|
11
|
+
return {
|
|
12
|
+
type,
|
|
13
|
+
id: Date.now(),
|
|
14
|
+
positionType: snapMode === 'none' ? 'pixel' : 'data',
|
|
15
|
+
style: baseStyle,
|
|
16
|
+
pointA,
|
|
17
|
+
pointB,
|
|
18
|
+
label: undefined,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Hook for managing annotation state and interactions.
|
|
23
|
+
*
|
|
24
|
+
* This hook provides state management for annotations including:
|
|
25
|
+
* - Adding, editing, and deleting annotations
|
|
26
|
+
* - Tracking mouse position for annotation placement
|
|
27
|
+
* - Selection state for editing
|
|
28
|
+
*/
|
|
29
|
+
export const useAnnotationsManager = (props) => {
|
|
30
|
+
const [annotations, setAnnotations] = useState([]);
|
|
31
|
+
const [isAdding, setIsAdding] = useState(null);
|
|
32
|
+
const [followerPosition, setFollowerPosition] = useState(null);
|
|
33
|
+
const [selectedAnnotationId, setSelectedAnnotationId] = useState(null);
|
|
34
|
+
const [snapMode, setSnapMode] = useState('none');
|
|
35
|
+
// Track click positions for multi-click annotations (rectangle, freeform line)
|
|
36
|
+
const [firstClickPosition, setFirstClickPosition] = useState(null);
|
|
37
|
+
const startAddingAnnotation = useCallback((type) => {
|
|
38
|
+
setIsAdding(type);
|
|
39
|
+
setFirstClickPosition(null);
|
|
40
|
+
setFollowerPosition(null);
|
|
41
|
+
}, []);
|
|
42
|
+
const cancelAddingAnnotation = useCallback(() => {
|
|
43
|
+
setIsAdding(null);
|
|
44
|
+
setFirstClickPosition(null);
|
|
45
|
+
setFollowerPosition(null);
|
|
46
|
+
}, []);
|
|
47
|
+
const deleteAnnotation = useCallback((id) => {
|
|
48
|
+
setAnnotations((prev) => prev.filter((a) => a.id !== id));
|
|
49
|
+
props?.onAnnotationDelete?.(id);
|
|
50
|
+
if (selectedAnnotationId === id) {
|
|
51
|
+
setSelectedAnnotationId(null);
|
|
52
|
+
}
|
|
53
|
+
}, [props, selectedAnnotationId]);
|
|
54
|
+
const updateAnnotation = useCallback((updatedAnnotation) => {
|
|
55
|
+
setAnnotations((prev) => prev.map((a) => (a.id === updatedAnnotation.id ? updatedAnnotation : a)));
|
|
56
|
+
props?.onAnnotationUpdate?.(updatedAnnotation);
|
|
57
|
+
}, [props]);
|
|
58
|
+
const selectAnnotation = useCallback((id) => {
|
|
59
|
+
setSelectedAnnotationId(id);
|
|
60
|
+
}, []);
|
|
61
|
+
const createAnnotation = useCallback((type, snapMode, pointA, pointB) => {
|
|
62
|
+
const color = getAnnotationColor(annotations.length);
|
|
63
|
+
return annotationFactory(type, color, snapMode, pointA, pointB);
|
|
64
|
+
}, [annotations.length]);
|
|
65
|
+
const onChartMouseDown = useCallback((snap) => {
|
|
66
|
+
if (!isAdding)
|
|
67
|
+
return;
|
|
68
|
+
// For rectangle and freeform line, MouseDown sets the first point (anchor)
|
|
69
|
+
if (isAdding === 'rectangle' || isAdding === 'freeformLine' || isAdding === 'circle') {
|
|
70
|
+
if (!firstClickPosition) {
|
|
71
|
+
setFirstClickPosition(snap);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}, [isAdding, firstClickPosition]);
|
|
75
|
+
const onChartMouseUp = useCallback((snap) => {
|
|
76
|
+
if (!isAdding)
|
|
77
|
+
return;
|
|
78
|
+
// For rectangle and freeform line, MouseUp can complete the annotation
|
|
79
|
+
// if it's a drag (distance > threshold) OR if it's the second click of a click-click flow
|
|
80
|
+
if (isAdding === 'rectangle' || isAdding === 'freeformLine' || isAdding === 'circle') {
|
|
81
|
+
if (firstClickPosition) {
|
|
82
|
+
const dist = getDistance(firstClickPosition, snap);
|
|
83
|
+
// Threshold to distinguish between a simple click (to set anchor) and a drag/second click
|
|
84
|
+
if (dist > 3) {
|
|
85
|
+
const newAnnotation = createAnnotation(isAdding, snapMode, firstClickPosition, snap);
|
|
86
|
+
setAnnotations((prev) => [...prev, newAnnotation]);
|
|
87
|
+
props?.onAnnotationAdd?.(newAnnotation);
|
|
88
|
+
setIsAdding(null);
|
|
89
|
+
setFirstClickPosition(null);
|
|
90
|
+
setFollowerPosition(null);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}, [isAdding, firstClickPosition, createAnnotation, props, snapMode]);
|
|
95
|
+
const onChartClick = useCallback((snap) => {
|
|
96
|
+
if (!isAdding)
|
|
97
|
+
return;
|
|
98
|
+
// Two-point annotations are now handled by MouseDown/MouseUp
|
|
99
|
+
if (isAdding === 'rectangle' || isAdding === 'freeformLine' || isAdding === 'circle') {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
// For other annotation types, single click creates the annotation
|
|
103
|
+
const newAnnotation = createAnnotation(isAdding, snapMode, snap);
|
|
104
|
+
setAnnotations((prev) => [...prev, newAnnotation]);
|
|
105
|
+
props?.onAnnotationAdd?.(newAnnotation);
|
|
106
|
+
setIsAdding(null);
|
|
107
|
+
setFollowerPosition(null);
|
|
108
|
+
}, [isAdding, createAnnotation, props, snapMode]);
|
|
109
|
+
const onChartMouseMove = useCallback((snap) => {
|
|
110
|
+
if (isAdding) {
|
|
111
|
+
setFollowerPosition(snap);
|
|
112
|
+
}
|
|
113
|
+
}, [isAdding]);
|
|
114
|
+
const onChartMouseLeave = useCallback(() => {
|
|
115
|
+
setFollowerPosition(null);
|
|
116
|
+
}, []);
|
|
117
|
+
return {
|
|
118
|
+
annotations,
|
|
119
|
+
isAdding,
|
|
120
|
+
followerPosition,
|
|
121
|
+
firstClickPosition,
|
|
122
|
+
selectedAnnotationId,
|
|
123
|
+
snapMode,
|
|
124
|
+
startAddingAnnotation,
|
|
125
|
+
cancelAddingAnnotation,
|
|
126
|
+
deleteAnnotation,
|
|
127
|
+
updateAnnotation,
|
|
128
|
+
selectAnnotation,
|
|
129
|
+
setSnapMode,
|
|
130
|
+
onChartClick,
|
|
131
|
+
onChartMouseDown,
|
|
132
|
+
onChartMouseUp,
|
|
133
|
+
onChartMouseMove,
|
|
134
|
+
onChartMouseLeave,
|
|
135
|
+
};
|
|
136
|
+
};
|
|
@@ -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;
|