-
Notifications
You must be signed in to change notification settings - Fork 698
/
XYChart.tsx
216 lines (203 loc) · 7.38 KB
/
XYChart.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* eslint jsx-a11y/mouse-events-have-key-events: 'off', @typescript-eslint/no-explicit-any: 'off' */
import React, { useContext, useEffect } from 'react';
import ParentSize from '@visx/responsive/lib/components/ParentSize';
import { ResizeObserverPolyfill } from '@visx/responsive/lib/types';
import { AxisScaleOutput } from '@visx/axis';
import { ScaleConfig } from '@visx/scale';
import DataContext from '../context/DataContext';
import { Margin, EventHandlerParams } from '../types';
import useEventEmitter from '../hooks/useEventEmitter';
import EventEmitterProvider from '../providers/EventEmitterProvider';
import TooltipContext from '../context/TooltipContext';
import TooltipProvider from '../providers/TooltipProvider';
import DataProvider, { DataProviderProps } from '../providers/DataProvider';
import useEventEmitters from '../hooks/useEventEmitters';
import { XYCHART_EVENT_SOURCE } from '../constants';
import useEventHandlers, {
POINTER_EVENTS_ALL,
POINTER_EVENTS_NEAREST,
} from '../hooks/useEventHandlers';
const DEFAULT_MARGIN = { top: 50, right: 50, bottom: 50, left: 50 };
export type XYChartProps<
XScaleConfig extends ScaleConfig<AxisScaleOutput, any, any>,
YScaleConfig extends ScaleConfig<AxisScaleOutput, any, any>,
Datum extends object,
> = {
/** aria-label for the chart svg element. */
accessibilityLabel?: string;
/** Whether to capture and dispatch pointer events to EventEmitter context (which e.g., Series subscribe to). */
captureEvents?: boolean;
/** Total width of the desired chart svg, including margin. */
width?: number;
/** Total height of the desired chart svg, including margin. */
height?: number;
/** Margin to apply around the outside. */
margin?: Margin;
/** XYChart children (Series, Tooltip, etc.). */
children: React.ReactNode;
/** If DataContext is not available, XYChart will wrap itself in a DataProvider and set this as the theme. */
theme?: DataProviderProps<XScaleConfig, YScaleConfig>['theme'];
/** If DataContext is not available, XYChart will wrap itself in a DataProvider and set this as the xScale config. */
xScale?: DataProviderProps<XScaleConfig, YScaleConfig>['xScale'];
/** If DataContext is not available, XYChart will wrap itself in a DataProvider and set this as the yScale config. */
yScale?: DataProviderProps<XScaleConfig, YScaleConfig>['yScale'];
/* If DataContext is not available, XYChart will wrap itself in a DataProvider and set this as horizontal. Determines whether Series will be plotted horizontally (e.g., horizontal bars). By default this will try to be inferred based on scale types. */
horizontal?: boolean | 'auto';
/** Callback invoked for onPointerMove events for the nearest Datum to the PointerEvent _for each Series with pointerEvents={true}_. */
onPointerMove?: ({
datum,
distanceX,
distanceY,
event,
index,
key,
svgPoint,
}: EventHandlerParams<Datum>) => void;
/** Callback invoked for onPointerOut events for the nearest Datum to the PointerEvent _for each Series with pointerEvents={true}_. */
onPointerOut?: (
/** The PointerEvent. */
event: React.PointerEvent,
) => void;
/** Callback invoked for onPointerUp events for the nearest Datum to the PointerEvent _for each Series with pointerEvents={true}_. */
onPointerUp?: ({
datum,
distanceX,
distanceY,
event,
index,
key,
svgPoint,
}: EventHandlerParams<Datum>) => void;
/** Callback invoked for onPointerDown events for the nearest Datum to the PointerEvent _for each Series with pointerEvents={true}_. */
onPointerDown?: ({
datum,
distanceX,
distanceY,
event,
index,
key,
svgPoint,
}: EventHandlerParams<Datum>) => void;
/** Whether to invoke PointerEvent handlers for all dataKeys, or the nearest dataKey. */
pointerEventsDataKey?: 'all' | 'nearest';
/**
* Responsive charts, <Tooltip />, and <AnnotationLabel /> depend on ResizeObserver
* which may be polyfilled globally, passed to individual components or injected once
* into this component.
*/
resizeObserverPolyfill?: ResizeObserverPolyfill;
};
const allowedEventSources = [XYCHART_EVENT_SOURCE];
export default function XYChart<
XScaleConfig extends ScaleConfig<AxisScaleOutput, any, any>,
YScaleConfig extends ScaleConfig<AxisScaleOutput, any, any>,
Datum extends object,
>(props: XYChartProps<XScaleConfig, YScaleConfig, Datum>) {
const {
accessibilityLabel = 'XYChart',
captureEvents = true,
children,
height,
horizontal,
margin = DEFAULT_MARGIN,
onPointerMove,
onPointerOut,
onPointerUp,
onPointerDown,
pointerEventsDataKey = 'nearest',
theme,
width,
xScale,
yScale,
// pass prop to context so it can be used anywhere
resizeObserverPolyfill: resizeObserverPolyfillProp,
} = props;
const { setDimensions, resizeObserverPolyfill } = useContext(DataContext);
const tooltipContext = useContext(TooltipContext);
const emit = useEventEmitter();
// update dimensions in context
useEffect(() => {
if (setDimensions && width != null && height != null && width > 0 && height > 0) {
setDimensions({ width, height, margin });
}
}, [setDimensions, width, height, margin]);
const eventEmitters = useEventEmitters({ source: XYCHART_EVENT_SOURCE });
useEventHandlers({
dataKey: pointerEventsDataKey === 'nearest' ? POINTER_EVENTS_NEAREST : POINTER_EVENTS_ALL,
onPointerMove,
onPointerOut,
onPointerUp,
onPointerDown,
allowedSources: allowedEventSources,
});
// if Context or dimensions are not available, wrap self in the needed providers
if (!setDimensions) {
if (!xScale || !yScale) {
console.warn(
'[@visx/xychart] XYChart: When no DataProvider is available in context, you must pass xScale & yScale config to XYChart.',
);
return null;
}
return (
<DataProvider
xScale={xScale}
yScale={yScale}
theme={theme}
initialDimensions={{ width, height, margin }}
horizontal={horizontal}
resizeObserverPolyfill={resizeObserverPolyfillProp}
>
<XYChart {...props} />
</DataProvider>
);
}
if (width == null || height == null) {
return (
<ParentSize resizeObserverPolyfill={resizeObserverPolyfill}>
{(dims) => (
<XYChart
{...props}
width={props.width == null ? dims.width : props.width}
height={props.height == null ? dims.height : props.height}
/>
)}
</ParentSize>
);
}
if (tooltipContext == null) {
return (
<TooltipProvider>
<XYChart {...props} />
</TooltipProvider>
);
}
// EventEmitterProvider should be the last wrapper so we do not duplicate handlers
if (emit == null) {
return (
<EventEmitterProvider>
<XYChart {...props} />
</EventEmitterProvider>
);
}
if (width <= 0 || height <= 0) {
if (process.env.NODE_ENV === 'development') {
console.info('XYChart has a zero width or height, bailing', { width, height });
}
return null;
}
return (
<svg width={width} height={height} aria-label={accessibilityLabel}>
{children}
{captureEvents && (
<rect
x={margin.left}
y={margin.top}
width={width - margin.left - margin.right}
height={height - margin.top - margin.bottom}
fill="transparent"
{...eventEmitters}
/>
)}
</svg>
);
}