@recharts/devtools 0.0.19 → 0.0.20
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 +7 -1
- package/dist/annotations/AnnotationsController.js +6 -5
- package/dist/annotations/RenderAnnotations.js +17 -4
- package/dist/annotations/annotationInteractionUtils.d.ts +12 -1
- package/dist/annotations/annotationInteractionUtils.js +19 -0
- package/dist/annotations/useAnnotationsManager.js +2 -2
- package/dist/annotations/useSnap.d.ts +16 -16
- package/dist/annotations/useSnap.js +69 -73
- package/dist/components/RechartsAnnotations.js +6 -1
- package/dist/theme/analyze.d.ts +11 -0
- package/dist/theme/analyze.js +7 -2
- package/package.json +7 -6
|
@@ -23,6 +23,12 @@ interface AnnotationsControllerProps {
|
|
|
23
23
|
isAdding: AnnotationType | null;
|
|
24
24
|
selectedAnnotationId: number | null;
|
|
25
25
|
snapMode: SnapMode;
|
|
26
|
+
/**
|
|
27
|
+
* Whether snapping to axis ticks is possible. Tick snapping needs rendered ticks on both axes,
|
|
28
|
+
* so it is unavailable on charts without a rendered <XAxis> and <YAxis>.
|
|
29
|
+
* Defaults to true.
|
|
30
|
+
*/
|
|
31
|
+
tickSnapAvailable?: boolean;
|
|
26
32
|
onStartAdding: (type: AnnotationType) => void;
|
|
27
33
|
onCancelAdding: () => void;
|
|
28
34
|
onDeleteAnnotation: (id: number) => void;
|
|
@@ -36,5 +42,5 @@ interface AnnotationsControllerProps {
|
|
|
36
42
|
* This component renders HTML elements and is meant to be rendered via portal
|
|
37
43
|
* outside the SVG chart context.
|
|
38
44
|
*/
|
|
39
|
-
export declare function AnnotationsController({ annotations, isAdding, selectedAnnotationId, snapMode, onStartAdding, onCancelAdding, onDeleteAnnotation, onUpdateAnnotation, onSelectAnnotation, onSnapModeChange, }: AnnotationsControllerProps): React.JSX.Element;
|
|
45
|
+
export declare function AnnotationsController({ annotations, isAdding, selectedAnnotationId, snapMode, tickSnapAvailable, onStartAdding, onCancelAdding, onDeleteAnnotation, onUpdateAnnotation, onSelectAnnotation, onSnapModeChange, }: AnnotationsControllerProps): React.JSX.Element;
|
|
40
46
|
export {};
|
|
@@ -58,7 +58,8 @@ const labelStyle = {
|
|
|
58
58
|
gap: '1ex',
|
|
59
59
|
marginRight: '2ex',
|
|
60
60
|
};
|
|
61
|
-
|
|
61
|
+
const TICK_SNAP_UNAVAILABLE_REASON = 'Snapping to axis ticks needs a rendered <XAxis> and <YAxis>. Annotations are placed unsnapped.';
|
|
62
|
+
function SnapModeRadioGroup({ snapMode, tickSnapAvailable, onSnapModeChange, }) {
|
|
62
63
|
const name = useId();
|
|
63
64
|
return (React.createElement("div", { style: { marginBottom: '12px' } },
|
|
64
65
|
React.createElement("div", { style: { marginBottom: '6px', fontWeight: 'bold' } }, "Snap to Data:"),
|
|
@@ -68,8 +69,8 @@ function SnapModeRadioGroup({ snapMode, onSnapModeChange, }) {
|
|
|
68
69
|
React.createElement("label", { style: labelStyle },
|
|
69
70
|
React.createElement("input", { type: "radio", name: name, value: "data", checked: snapMode === 'data', onChange: () => onSnapModeChange('data') }),
|
|
70
71
|
"Snap to data points"),
|
|
71
|
-
React.createElement("label", { style: labelStyle },
|
|
72
|
-
React.createElement("input", { type: "radio", name: name, value: "tick", checked: snapMode === 'tick', onChange: () => onSnapModeChange('tick') }),
|
|
72
|
+
React.createElement("label", { style: tickSnapAvailable ? labelStyle : { ...labelStyle, opacity: 0.5 }, title: tickSnapAvailable ? undefined : TICK_SNAP_UNAVAILABLE_REASON },
|
|
73
|
+
React.createElement("input", { type: "radio", name: name, value: "tick", checked: snapMode === 'tick', disabled: !tickSnapAvailable, onChange: () => onSnapModeChange('tick') }),
|
|
73
74
|
"Snap to axis ticks")));
|
|
74
75
|
}
|
|
75
76
|
function AnnotationEditForm({ annotation, handleStyleChange, onUpdateAnnotation, }) {
|
|
@@ -197,7 +198,7 @@ function AnnotationEditForm({ annotation, handleStyleChange, onUpdateAnnotation,
|
|
|
197
198
|
* This component renders HTML elements and is meant to be rendered via portal
|
|
198
199
|
* outside the SVG chart context.
|
|
199
200
|
*/
|
|
200
|
-
export function AnnotationsController({ annotations, isAdding, selectedAnnotationId, snapMode, onStartAdding, onCancelAdding, onDeleteAnnotation, onUpdateAnnotation, onSelectAnnotation, onSnapModeChange, }) {
|
|
201
|
+
export function AnnotationsController({ annotations, isAdding, selectedAnnotationId, snapMode, tickSnapAvailable = true, onStartAdding, onCancelAdding, onDeleteAnnotation, onUpdateAnnotation, onSelectAnnotation, onSnapModeChange, }) {
|
|
201
202
|
const [expandedAnnotationId, setExpandedAnnotationId] = useState(null);
|
|
202
203
|
const handleStyleChange = (annotation, styleKey, value) => {
|
|
203
204
|
onUpdateAnnotation({
|
|
@@ -219,7 +220,7 @@ export function AnnotationsController({ annotations, isAdding, selectedAnnotatio
|
|
|
219
220
|
"Click on the chart to place the annotation",
|
|
220
221
|
(isAdding === 'rectangle' || isAdding === 'freeformLine' || isAdding === 'circle') &&
|
|
221
222
|
' (click twice or drag to define dimensions)'))),
|
|
222
|
-
React.createElement(SnapModeRadioGroup, { snapMode: snapMode, onSnapModeChange: onSnapModeChange }),
|
|
223
|
+
React.createElement(SnapModeRadioGroup, { snapMode: snapMode, tickSnapAvailable: tickSnapAvailable, onSnapModeChange: onSnapModeChange }),
|
|
223
224
|
React.createElement("div", null,
|
|
224
225
|
React.createElement("div", { style: { marginBottom: '6px', fontWeight: 'bold' } },
|
|
225
226
|
"Annotations (",
|
|
@@ -19,7 +19,7 @@ import { Crosshair } from './Crosshair.js';
|
|
|
19
19
|
import { getAnnotationColor } from './annotationColors.js';
|
|
20
20
|
import { useSnap } from './useSnap.js';
|
|
21
21
|
import { DefaultZIndexes, ZIndexLayer } from 'recharts';
|
|
22
|
-
import { DEFAULT_CIRCLE_RADIUS, DEFAULT_RECT_WIDTH, DEFAULT_RECT_HEIGHT, applyAnchorOffset, isDragInteraction, } from './annotationInteractionUtils.js';
|
|
22
|
+
import { DEFAULT_CIRCLE_RADIUS, DEFAULT_RECT_WIDTH, DEFAULT_RECT_HEIGHT, applyAnchorOffset, getPositionType, isDragInteraction, } from './annotationInteractionUtils.js';
|
|
23
23
|
function renderAnnotation(annotation) {
|
|
24
24
|
switch (annotation.type) {
|
|
25
25
|
case 'horizontalLine':
|
|
@@ -78,7 +78,6 @@ function renderFollowerAnnotation(type, snapMode, position, annotationCount, fir
|
|
|
78
78
|
const baseAnnotation = {
|
|
79
79
|
type,
|
|
80
80
|
id: -1, // Temporary ID for follower
|
|
81
|
-
positionType: snapMode === 'none' ? 'pixel' : 'data',
|
|
82
81
|
style: baseStyle,
|
|
83
82
|
label: type === 'label' ? { value: 'Label' } : undefined,
|
|
84
83
|
};
|
|
@@ -102,6 +101,7 @@ function renderFollowerAnnotation(type, snapMode, position, annotationCount, fir
|
|
|
102
101
|
// radius from getDistance(pointA, pointB) using interactionCoordinate.
|
|
103
102
|
return renderAnnotation({
|
|
104
103
|
...baseAnnotation,
|
|
104
|
+
positionType: getPositionType(snapMode, type, firstClickPosition),
|
|
105
105
|
pointA: firstClickPosition,
|
|
106
106
|
pointB: syntheticPoint(fpx + DEFAULT_CIRCLE_RADIUS, fpy),
|
|
107
107
|
});
|
|
@@ -121,7 +121,12 @@ function renderFollowerAnnotation(type, snapMode, position, annotationCount, fir
|
|
|
121
121
|
});
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
|
-
return renderAnnotation({
|
|
124
|
+
return renderAnnotation({
|
|
125
|
+
...baseAnnotation,
|
|
126
|
+
positionType: getPositionType(snapMode, type, firstClickPosition, position),
|
|
127
|
+
pointA: firstClickPosition,
|
|
128
|
+
pointB: position,
|
|
129
|
+
});
|
|
125
130
|
}
|
|
126
131
|
// Before the first click: show a default-sized preview.
|
|
127
132
|
if (type === 'circle') {
|
|
@@ -129,6 +134,7 @@ function renderFollowerAnnotation(type, snapMode, position, annotationCount, fir
|
|
|
129
134
|
// Pixel mode: real cursor = pointB (edge); anchor offset left so radius = DEFAULT_CIRCLE_RADIUS.
|
|
130
135
|
return renderAnnotation({
|
|
131
136
|
...baseAnnotation,
|
|
137
|
+
positionType: 'pixel',
|
|
132
138
|
pointA: syntheticPoint(px - DEFAULT_CIRCLE_RADIUS, py),
|
|
133
139
|
pointB: position,
|
|
134
140
|
});
|
|
@@ -147,6 +153,7 @@ function renderFollowerAnnotation(type, snapMode, position, annotationCount, fir
|
|
|
147
153
|
// Pixel mode: real cursor = bottom-right corner (pointB); anchor = offset to top-left.
|
|
148
154
|
return renderAnnotation({
|
|
149
155
|
...baseAnnotation,
|
|
156
|
+
positionType: 'pixel',
|
|
150
157
|
pointA: syntheticPoint(px - DEFAULT_RECT_WIDTH, py - DEFAULT_RECT_HEIGHT),
|
|
151
158
|
pointB: position,
|
|
152
159
|
});
|
|
@@ -162,14 +169,20 @@ function renderFollowerAnnotation(type, snapMode, position, annotationCount, fir
|
|
|
162
169
|
}
|
|
163
170
|
if (type === 'freeformLine') {
|
|
164
171
|
// Default short angled stub so the line direction is hinted at.
|
|
172
|
+
// positionType follows pointA only, so the snapped case behaves as before.
|
|
165
173
|
return renderAnnotation({
|
|
166
174
|
...baseAnnotation,
|
|
175
|
+
positionType: getPositionType(snapMode, type, position),
|
|
167
176
|
pointA: position,
|
|
168
177
|
pointB: syntheticPoint(px + 30, py - 15),
|
|
169
178
|
});
|
|
170
179
|
}
|
|
171
180
|
// Single-click annotations (horizontalLine, verticalLine, crosshair, label).
|
|
172
|
-
return renderAnnotation({
|
|
181
|
+
return renderAnnotation({
|
|
182
|
+
...baseAnnotation,
|
|
183
|
+
positionType: getPositionType(snapMode, type, position),
|
|
184
|
+
pointA: position,
|
|
185
|
+
});
|
|
173
186
|
}
|
|
174
187
|
/**
|
|
175
188
|
* Component that renders all annotations and handles mouse events for adding new annotations.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SnapResult } from './useSnap.js';
|
|
2
|
-
import type { AnnotationType, SnapMode } from './types.js';
|
|
2
|
+
import type { AnnotationType, PositionType, SnapMode } from './types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Default dimensions for circle and rectangle pre-click follower previews (pixel mode only).
|
|
5
5
|
*
|
|
@@ -39,3 +39,14 @@ export declare function isDragInteraction(downPos: {
|
|
|
39
39
|
x: number;
|
|
40
40
|
y: number;
|
|
41
41
|
}): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Decides whether an annotation is positioned by data values or by pixels.
|
|
44
|
+
*
|
|
45
|
+
* Data positioning is used only when snapping is on AND the points actually snapped
|
|
46
|
+
* (carry a `dataPoint`). Snapping can fail even when it is on, for example in 'tick' mode
|
|
47
|
+
* on a chart without a rendered <XAxis>/<YAxis>; such points fall back to pixel positioning,
|
|
48
|
+
* otherwise the annotation components would render nothing.
|
|
49
|
+
*
|
|
50
|
+
* Circles only need the centre (pointA) to be snapped; the radius is always in pixels.
|
|
51
|
+
*/
|
|
52
|
+
export declare function getPositionType(snapMode: SnapMode, type: AnnotationType, pointA: SnapResult, pointB?: SnapResult): PositionType;
|
|
@@ -56,3 +56,22 @@ export function isDragInteraction(downPos, upPos) {
|
|
|
56
56
|
const dy = upPos.y - downPos.y;
|
|
57
57
|
return Math.sqrt(dx * dx + dy * dy) > DRAG_THRESHOLD;
|
|
58
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Decides whether an annotation is positioned by data values or by pixels.
|
|
61
|
+
*
|
|
62
|
+
* Data positioning is used only when snapping is on AND the points actually snapped
|
|
63
|
+
* (carry a `dataPoint`). Snapping can fail even when it is on, for example in 'tick' mode
|
|
64
|
+
* on a chart without a rendered <XAxis>/<YAxis>; such points fall back to pixel positioning,
|
|
65
|
+
* otherwise the annotation components would render nothing.
|
|
66
|
+
*
|
|
67
|
+
* Circles only need the centre (pointA) to be snapped; the radius is always in pixels.
|
|
68
|
+
*/
|
|
69
|
+
export function getPositionType(snapMode, type, pointA, pointB) {
|
|
70
|
+
if (snapMode === 'none' || pointA.dataPoint == null) {
|
|
71
|
+
return 'pixel';
|
|
72
|
+
}
|
|
73
|
+
if (type !== 'circle' && pointB != null && pointB.dataPoint == null) {
|
|
74
|
+
return 'pixel';
|
|
75
|
+
}
|
|
76
|
+
return 'data';
|
|
77
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useCallback, useState } from 'react';
|
|
2
2
|
import { DEFAULT_ANNOTATION_STYLE, getAnnotationColor } from './annotationColors.js';
|
|
3
3
|
import { getDistance } from './getDistance.js';
|
|
4
|
-
import { DEFAULT_CIRCLE_RADIUS, DEFAULT_RECT_HEIGHT, DEFAULT_RECT_WIDTH, } from './annotationInteractionUtils.js';
|
|
4
|
+
import { DEFAULT_CIRCLE_RADIUS, DEFAULT_RECT_HEIGHT, DEFAULT_RECT_WIDTH, getPositionType, } from './annotationInteractionUtils.js';
|
|
5
5
|
function annotationFactory(type, color, snapMode, pointA, pointB) {
|
|
6
6
|
const baseStyle = {
|
|
7
7
|
...DEFAULT_ANNOTATION_STYLE,
|
|
@@ -12,7 +12,7 @@ function annotationFactory(type, color, snapMode, pointA, pointB) {
|
|
|
12
12
|
return {
|
|
13
13
|
type,
|
|
14
14
|
id: Date.now(),
|
|
15
|
-
positionType: snapMode
|
|
15
|
+
positionType: getPositionType(snapMode, type, pointA, pointB),
|
|
16
16
|
style: baseStyle,
|
|
17
17
|
pointA,
|
|
18
18
|
pointB,
|
|
@@ -4,13 +4,12 @@
|
|
|
4
4
|
* This uses the Recharts scale hooks to convert between pixel and data coordinates,
|
|
5
5
|
* enabling "magnetic" annotation placement that snaps to actual data points.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* we keep the original coordinate for smooth freeform positioning.
|
|
7
|
+
* Each snap mode reads only the scales it needs: 'none' needs no scales, 'data' needs the
|
|
8
|
+
* data-snap inverse scales and 'tick' needs the tick-snap inverse scales (both plus the forward
|
|
9
|
+
* scales). When the scales a mode needs are unavailable (e.g. 'tick' on a chart without a
|
|
10
|
+
* rendered <XAxis>/<YAxis>, which has no rendered ticks) or the snapped point falls outside the
|
|
11
|
+
* axis, the hook returns the real interaction coordinate, unsnapped, with `snappedCoordinate`
|
|
12
|
+
* and `dataPoint` undefined. Annotations then fall back to pixel positioning.
|
|
14
13
|
*/
|
|
15
14
|
import { MouseEvent } from 'react';
|
|
16
15
|
import { Coordinate } from 'recharts';
|
|
@@ -24,20 +23,21 @@ export interface SnapResult {
|
|
|
24
23
|
} | undefined;
|
|
25
24
|
}
|
|
26
25
|
/**
|
|
27
|
-
* Hook that provides a function to snap
|
|
26
|
+
* Hook that provides a function to snap a mouse event to the nearest data point or axis tick.
|
|
27
|
+
*
|
|
28
|
+
* When snapping is enabled ('data' or 'tick'), this will:
|
|
29
|
+
* 1. Convert pixel coordinates to data values using the inverse snap scale
|
|
30
|
+
* 2. Convert the snapped data values back to pixel coordinates for display
|
|
28
31
|
*
|
|
29
|
-
*
|
|
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
|
|
32
|
+
* If snapping is disabled or not possible, the result carries only the real interaction coordinate.
|
|
33
33
|
*
|
|
34
34
|
* @example
|
|
35
35
|
* ```tsx
|
|
36
|
-
* const
|
|
36
|
+
* const snap = useSnap('data');
|
|
37
37
|
*
|
|
38
|
-
* const handleMouseMove = (
|
|
39
|
-
* const
|
|
40
|
-
* setPosition(
|
|
38
|
+
* const handleMouseMove = (e: React.MouseEvent<SVGGraphicsElement>) => {
|
|
39
|
+
* const { interactionCoordinate, snappedCoordinate } = snap(e);
|
|
40
|
+
* setPosition(snappedCoordinate ?? interactionCoordinate);
|
|
41
41
|
* };
|
|
42
42
|
* ```
|
|
43
43
|
*/
|
|
@@ -4,36 +4,60 @@
|
|
|
4
4
|
* This uses the Recharts scale hooks to convert between pixel and data coordinates,
|
|
5
5
|
* enabling "magnetic" annotation placement that snaps to actual data points.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* we keep the original coordinate for smooth freeform positioning.
|
|
7
|
+
* Each snap mode reads only the scales it needs: 'none' needs no scales, 'data' needs the
|
|
8
|
+
* data-snap inverse scales and 'tick' needs the tick-snap inverse scales (both plus the forward
|
|
9
|
+
* scales). When the scales a mode needs are unavailable (e.g. 'tick' on a chart without a
|
|
10
|
+
* rendered <XAxis>/<YAxis>, which has no rendered ticks) or the snapped point falls outside the
|
|
11
|
+
* axis, the hook returns the real interaction coordinate, unsnapped, with `snappedCoordinate`
|
|
12
|
+
* and `dataPoint` undefined. Annotations then fall back to pixel positioning.
|
|
14
13
|
*/
|
|
15
14
|
import { useCallback } from 'react';
|
|
16
|
-
import { getRelativeCoordinate, useXAxisInverseDataSnapScale, useYAxisInverseDataSnapScale, useXAxisInverseTickSnapScale, useYAxisInverseTickSnapScale, useXAxisScale, useYAxisScale, } from 'recharts';
|
|
15
|
+
import { getRelativeCoordinate, useXAxisInverseScale, useYAxisInverseScale, useXAxisInverseDataSnapScale, useYAxisInverseDataSnapScale, useXAxisInverseTickSnapScale, useYAxisInverseTickSnapScale, useXAxisScale, useYAxisScale, } from 'recharts';
|
|
16
|
+
/**
|
|
17
|
+
* Snaps one axis: pixel -> data value -> snapped pixel.
|
|
18
|
+
*
|
|
19
|
+
* `fallbackInverse` is tried when the snap scale gives a value the forward scale cannot map.
|
|
20
|
+
* This happens with data snapping on a category axis that has no `dataKey` (e.g. an implicit
|
|
21
|
+
* axis on a chart without <XAxis>): Recharts then offers the graphical items' values
|
|
22
|
+
* (e.g. the Line's `uv` values) as snap candidates, while the axis domain is the row indexes.
|
|
23
|
+
* The general inverse scale of a category axis returns the nearest category, which is a
|
|
24
|
+
* data point too.
|
|
25
|
+
*/
|
|
26
|
+
function snapAxis(pixel, inverse, forward, fallbackInverse) {
|
|
27
|
+
for (const candidateInverse of [inverse, fallbackInverse]) {
|
|
28
|
+
if (candidateInverse == null)
|
|
29
|
+
continue;
|
|
30
|
+
const value = candidateInverse(pixel);
|
|
31
|
+
const snappedPixel = forward(value);
|
|
32
|
+
if (snappedPixel != null) {
|
|
33
|
+
return { value, pixel: snappedPixel };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
17
38
|
/**
|
|
18
|
-
* Hook that provides a function to snap
|
|
39
|
+
* Hook that provides a function to snap a mouse event to the nearest data point or axis tick.
|
|
19
40
|
*
|
|
20
|
-
* When
|
|
21
|
-
* 1. Convert pixel coordinates to data values using inverse scale
|
|
22
|
-
* 2.
|
|
23
|
-
*
|
|
41
|
+
* When snapping is enabled ('data' or 'tick'), this will:
|
|
42
|
+
* 1. Convert pixel coordinates to data values using the inverse snap scale
|
|
43
|
+
* 2. Convert the snapped data values back to pixel coordinates for display
|
|
44
|
+
*
|
|
45
|
+
* If snapping is disabled or not possible, the result carries only the real interaction coordinate.
|
|
24
46
|
*
|
|
25
47
|
* @example
|
|
26
48
|
* ```tsx
|
|
27
|
-
* const
|
|
49
|
+
* const snap = useSnap('data');
|
|
28
50
|
*
|
|
29
|
-
* const handleMouseMove = (
|
|
30
|
-
* const
|
|
31
|
-
* setPosition(
|
|
51
|
+
* const handleMouseMove = (e: React.MouseEvent<SVGGraphicsElement>) => {
|
|
52
|
+
* const { interactionCoordinate, snappedCoordinate } = snap(e);
|
|
53
|
+
* setPosition(snappedCoordinate ?? interactionCoordinate);
|
|
32
54
|
* };
|
|
33
55
|
* ```
|
|
34
56
|
*/
|
|
35
57
|
export function useSnap(snapMode) {
|
|
36
58
|
// Get inverse scale functions for converting pixels -> data
|
|
59
|
+
const xInverseScale = useXAxisInverseScale();
|
|
60
|
+
const yInverseScale = useYAxisInverseScale();
|
|
37
61
|
const xInverseDataSnapScale = useXAxisInverseDataSnapScale();
|
|
38
62
|
const yInverseDataSnapScale = useYAxisInverseDataSnapScale();
|
|
39
63
|
const xInverseTickSnapScale = useXAxisInverseTickSnapScale();
|
|
@@ -42,81 +66,53 @@ export function useSnap(snapMode) {
|
|
|
42
66
|
const scaleX = useXAxisScale();
|
|
43
67
|
const scaleY = useYAxisScale();
|
|
44
68
|
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
69
|
/*
|
|
62
70
|
* Here we get pixel coordinates relative to the chart area (accounting for transforms)
|
|
63
71
|
* These are exact mouse interaction coordinates.
|
|
64
72
|
* Next, we need to "snap" these to the nearest data point if snapMode is enabled.
|
|
65
73
|
*/
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
snappedDataPoint = {
|
|
76
|
-
x: xInverseTickSnapScale(interactionCoordinate.relativeX),
|
|
77
|
-
y: yInverseTickSnapScale(interactionCoordinate.relativeY),
|
|
78
|
-
};
|
|
74
|
+
const relative = getRelativeCoordinate(e);
|
|
75
|
+
const interactionCoordinate = { x: relative.relativeX, y: relative.relativeY };
|
|
76
|
+
const unsnapped = {
|
|
77
|
+
interactionCoordinate,
|
|
78
|
+
snappedCoordinate: undefined,
|
|
79
|
+
dataPoint: undefined,
|
|
80
|
+
};
|
|
81
|
+
if (snapMode === 'none') {
|
|
82
|
+
return unsnapped;
|
|
79
83
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
84
|
+
/*
|
|
85
|
+
* Each snap mode needs only its own inverse scales plus the forward scales.
|
|
86
|
+
* Any of them can be missing: for example, charts without a rendered <XAxis>/<YAxis>
|
|
87
|
+
* have no rendered ticks, so the tick-snap scales are undefined.
|
|
88
|
+
* In that case we keep the real (unsnapped) interaction coordinate.
|
|
89
|
+
*/
|
|
90
|
+
const xInverse = snapMode === 'data' ? xInverseDataSnapScale : xInverseTickSnapScale;
|
|
91
|
+
const yInverse = snapMode === 'data' ? yInverseDataSnapScale : yInverseTickSnapScale;
|
|
92
|
+
if (xInverse == null || yInverse == null || scaleX == null || scaleY == null) {
|
|
93
|
+
return unsnapped;
|
|
90
94
|
}
|
|
91
95
|
/*
|
|
92
|
-
* Converts snapped data point back to pixel coordinates.
|
|
96
|
+
* Converts the snapped data point back to pixel coordinates.
|
|
93
97
|
* Some components will read the snapped data point to display values, while others will use the snapped pixel coordinates for positioning.
|
|
94
98
|
* We return here both the snapped data point (for display) and the snapped pixel coordinates (for positioning) for convenience.
|
|
95
99
|
* If you are using ReferenceLine/ReferenceDot, you can skip this step and use the snappedDataPoint directly.
|
|
96
100
|
*/
|
|
97
|
-
const snappedX =
|
|
98
|
-
const snappedY =
|
|
101
|
+
const snappedX = snapAxis(interactionCoordinate.x, xInverse, scaleX, snapMode === 'data' ? xInverseScale : undefined);
|
|
102
|
+
const snappedY = snapAxis(interactionCoordinate.y, yInverse, scaleY, snapMode === 'data' ? yInverseScale : undefined);
|
|
99
103
|
if (snappedX == null || snappedY == null) {
|
|
100
104
|
// 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
|
-
};
|
|
105
|
+
return unsnapped;
|
|
109
106
|
}
|
|
110
107
|
return {
|
|
111
|
-
interactionCoordinate
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
},
|
|
115
|
-
snappedCoordinate: { x: snappedX, y: snappedY },
|
|
116
|
-
dataPoint: snappedDataPoint,
|
|
108
|
+
interactionCoordinate,
|
|
109
|
+
snappedCoordinate: { x: snappedX.pixel, y: snappedY.pixel },
|
|
110
|
+
dataPoint: { x: snappedX.value, y: snappedY.value },
|
|
117
111
|
};
|
|
118
112
|
}, [
|
|
119
113
|
snapMode,
|
|
114
|
+
xInverseScale,
|
|
115
|
+
yInverseScale,
|
|
120
116
|
xInverseDataSnapScale,
|
|
121
117
|
yInverseDataSnapScale,
|
|
122
118
|
xInverseTickSnapScale,
|
|
@@ -17,6 +17,7 @@
|
|
|
17
17
|
*/
|
|
18
18
|
import React from 'react';
|
|
19
19
|
import { createPortal } from 'react-dom';
|
|
20
|
+
import { useXAxisInverseTickSnapScale, useYAxisInverseTickSnapScale } from 'recharts';
|
|
20
21
|
import { RECHARTS_ANNOTATIONS_PORTAL_ID } from '../constants.js';
|
|
21
22
|
import { useRechartsDevtoolsContext } from '../context/RechartsDevtoolsContext.js';
|
|
22
23
|
import { AnnotationsController, RenderAnnotations, useAnnotationsManager } from '../annotations/index.js';
|
|
@@ -26,6 +27,10 @@ export const RechartsAnnotations = () => {
|
|
|
26
27
|
const portalId = contextId ? `${contextId}-annotations` : RECHARTS_ANNOTATIONS_PORTAL_ID;
|
|
27
28
|
const [container, setContainer] = React.useState(null);
|
|
28
29
|
const annotationsManager = useAnnotationsManager();
|
|
30
|
+
// Tick snapping needs rendered ticks, which only a rendered <XAxis>/<YAxis> provides.
|
|
31
|
+
const xTickSnapScale = useXAxisInverseTickSnapScale();
|
|
32
|
+
const yTickSnapScale = useYAxisInverseTickSnapScale();
|
|
33
|
+
const tickSnapAvailable = xTickSnapScale != null && yTickSnapScale != null;
|
|
29
34
|
React.useEffect(() => {
|
|
30
35
|
const el = document.getElementById(portalId);
|
|
31
36
|
setContainer(el);
|
|
@@ -34,5 +39,5 @@ export const RechartsAnnotations = () => {
|
|
|
34
39
|
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
40
|
container &&
|
|
36
41
|
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)));
|
|
42
|
+
React.createElement(AnnotationsController, { annotations: annotationsManager.annotations, isAdding: annotationsManager.isAdding, selectedAnnotationId: annotationsManager.selectedAnnotationId, snapMode: annotationsManager.snapMode, tickSnapAvailable: tickSnapAvailable, onStartAdding: annotationsManager.startAddingAnnotation, onCancelAdding: annotationsManager.cancelAddingAnnotation, onDeleteAnnotation: annotationsManager.deleteAnnotation, onUpdateAnnotation: annotationsManager.updateAnnotation, onSelectAnnotation: annotationsManager.selectAnnotation, onSnapModeChange: annotationsManager.setSnapMode })), container)));
|
|
38
43
|
};
|
package/dist/theme/analyze.d.ts
CHANGED
|
@@ -6,6 +6,17 @@ import { type CriterionId } from './standards.js';
|
|
|
6
6
|
* - `stroke`: the series stroke, fully opaque. Lines, outlines, dots.
|
|
7
7
|
*/
|
|
8
8
|
export type Mark = 'fill' | 'stroke';
|
|
9
|
+
/**
|
|
10
|
+
* The analysis that produced a `CheckResult`:
|
|
11
|
+
* - `seriesOnBackground`: non-text contrast between each series mark and its background.
|
|
12
|
+
* - `seriesVsSeries`: CIEDE2000 color difference between each pair of series marks.
|
|
13
|
+
* - `seriesContrast`: non-text contrast between each pair of series marks.
|
|
14
|
+
* - `textOnBackground`: text contrast between the label color and its background.
|
|
15
|
+
* - `seriesAsText`: text contrast between each series stroke and its background.
|
|
16
|
+
* - `textOnSeries`: text contrast between the label color and each series fill.
|
|
17
|
+
*
|
|
18
|
+
* Checks run for each configured background and simulated vision where applicable.
|
|
19
|
+
*/
|
|
9
20
|
export type CheckId = 'seriesOnBackground' | 'seriesVsSeries' | 'seriesContrast' | 'textOnBackground' | 'seriesAsText' | 'textOnSeries';
|
|
10
21
|
export type Thresholds = {
|
|
11
22
|
/** Series against the page. Defaults to 3, from WCAG SC 1.4.11. */
|
package/dist/theme/analyze.js
CHANGED
|
@@ -10,10 +10,15 @@ const DEFAULT_THRESHOLDS = {
|
|
|
10
10
|
};
|
|
11
11
|
/** The checks that need `theme.typography.color`. */
|
|
12
12
|
const LABEL_CHECKS = ['textOnBackground', 'textOnSeries'];
|
|
13
|
-
/**
|
|
13
|
+
/**
|
|
14
|
+
* One entry per `graphicalItems` item, with the fill falling back to the stroke and the other way round.
|
|
15
|
+
* A fill object (gradient or pattern) counts as its solid `color`, the one recharts uses for Tooltip and labels.
|
|
16
|
+
* Without a `color` it has no color of its own, so the stroke is used, as recharts does.
|
|
17
|
+
*/
|
|
14
18
|
function readSeries(theme) {
|
|
15
19
|
return theme.graphicalItems.map((item, index) => {
|
|
16
|
-
const
|
|
20
|
+
const fill = typeof item.fill === 'string' ? item.fill : item.fill?.color;
|
|
21
|
+
const color = fill ?? item.stroke;
|
|
17
22
|
if (color == null) {
|
|
18
23
|
throw new Error(`graphicalItems[${index}] has no fill or stroke color`);
|
|
19
24
|
}
|
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.20",
|
|
8
8
|
"description": "Devtools and helper functions for Recharts",
|
|
9
9
|
"main": "dist/index.js",
|
|
10
10
|
"types": "dist/index.d.ts",
|
|
@@ -40,19 +40,20 @@
|
|
|
40
40
|
"author": "Recharts Group",
|
|
41
41
|
"license": "MIT",
|
|
42
42
|
"devDependencies": {
|
|
43
|
+
"@eslint/js": "^10.0.1",
|
|
43
44
|
"@testing-library/react": "^16.3.2",
|
|
44
45
|
"@types/culori": "^4.0.1",
|
|
45
46
|
"@types/node": "^26.1.0",
|
|
46
47
|
"@types/react": "^18.0.0",
|
|
47
48
|
"@types/react-dom": "^18.0.0",
|
|
48
|
-
"eslint": "^
|
|
49
|
-
"eslint-plugin-import": "^
|
|
49
|
+
"eslint": "^10.13.0",
|
|
50
|
+
"eslint-plugin-import-x": "^4.17.1",
|
|
50
51
|
"globals": "^17.6.0",
|
|
51
|
-
"jsdom": "^
|
|
52
|
+
"jsdom": "^31.0.0",
|
|
52
53
|
"prettier": "^3.0.0",
|
|
53
54
|
"react": "^19.0.0",
|
|
54
55
|
"react-dom": "^19.0.0",
|
|
55
|
-
"recharts": "^3.11.0
|
|
56
|
+
"recharts": "^3.11.0",
|
|
56
57
|
"typescript": "^5.0.0",
|
|
57
58
|
"typescript-eslint": "^8.0.0",
|
|
58
59
|
"vitest": "^5.0.2"
|
|
@@ -60,7 +61,7 @@
|
|
|
60
61
|
"peerDependencies": {
|
|
61
62
|
"react": "^18.0.0 || ^19.0.0",
|
|
62
63
|
"react-dom": "^18.0.0 || ^19.0.0",
|
|
63
|
-
"recharts": "^3.9.0
|
|
64
|
+
"recharts": "^3.9.0"
|
|
64
65
|
},
|
|
65
66
|
"peerDependenciesMeta": {
|
|
66
67
|
"react": {
|