-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ChartsAxisTooltipContent.tsx
185 lines (169 loc) · 6 KB
/
ChartsAxisTooltipContent.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
import * as React from 'react';
import PropTypes from 'prop-types';
import { SxProps, Theme } from '@mui/material/styles';
import { useSlotProps } from '@mui/base/utils';
import { AxisInteractionData } from '../context/InteractionProvider';
import { useCartesianContext } from '../context/CartesianProvider';
import { ChartSeriesDefaultized, ChartSeriesType } from '../models/seriesType/config';
import { AxisDefaultized } from '../models/axis';
import { ChartsTooltipClasses } from './chartsTooltipClasses';
import { DefaultChartsAxisTooltipContent } from './DefaultChartsAxisTooltipContent';
import { ZAxisContext } from '../context/ZAxisContextProvider';
import { useColorProcessor } from '../hooks/useColor';
import { isCartesianSeriesType } from '../internals/isCartesian';
import { useSeries } from '../hooks/useSeries';
type ChartSeriesDefaultizedWithColorGetter = ChartSeriesDefaultized<ChartSeriesType> & {
getColor: (dataIndex: number) => string;
};
export type ChartsAxisContentProps = {
/**
* Data identifying the triggered axis.
*/
axisData: AxisInteractionData;
/**
* The series linked to the triggered axis.
*/
series: ChartSeriesDefaultizedWithColorGetter[];
/**
* The properties of the triggered axis.
*/
axis: AxisDefaultized;
/**
* The index of the data item triggered.
*/
dataIndex?: null | number;
/**
* The value associated to the current mouse position.
*/
axisValue: string | number | Date | null;
/**
* Override or extend the styles applied to the component.
*/
classes: ChartsTooltipClasses;
sx?: SxProps<Theme>;
};
function ChartsAxisTooltipContent(props: {
axisData: AxisInteractionData;
content?: React.ElementType<ChartsAxisContentProps>;
contentProps?: Partial<ChartsAxisContentProps>;
sx?: SxProps<Theme>;
classes: ChartsAxisContentProps['classes'];
}) {
const { content, contentProps, axisData, sx, classes } = props;
const isXaxis = (axisData.x && axisData.x.index) !== undefined;
const dataIndex = isXaxis ? axisData.x && axisData.x.index : axisData.y && axisData.y.index;
const axisValue = isXaxis ? axisData.x && axisData.x.value : axisData.y && axisData.y.value;
const { xAxisIds, xAxis, yAxisIds, yAxis } = useCartesianContext();
const { zAxisIds, zAxis } = React.useContext(ZAxisContext);
const series = useSeries();
const colorProcessors = useColorProcessor();
const USED_AXIS_ID = isXaxis ? xAxisIds[0] : yAxisIds[0];
const relevantSeries = React.useMemo(() => {
const rep: any[] = [];
Object.keys(series)
.filter(isCartesianSeriesType)
.forEach((seriesType) => {
series[seriesType]!.seriesOrder.forEach((seriesId) => {
const item = series[seriesType]!.series[seriesId];
const axisKey = isXaxis ? item.xAxisKey : item.yAxisKey;
if (axisKey === undefined || axisKey === USED_AXIS_ID) {
const seriesToAdd = series[seriesType]!.series[seriesId];
const zAxisKey = (seriesToAdd as any).zAxisKey ?? zAxisIds[0];
const getColor =
colorProcessors[seriesType]?.(
seriesToAdd as any,
xAxis[seriesToAdd.xAxisKey ?? xAxisIds[0]],
yAxis[seriesToAdd.yAxisKey ?? yAxisIds[0]],
zAxisKey && zAxis[zAxisKey],
) ?? (() => '');
rep.push({ ...seriesToAdd, getColor });
}
});
});
return rep;
}, [
USED_AXIS_ID,
colorProcessors,
isXaxis,
series,
xAxis,
xAxisIds,
yAxis,
yAxisIds,
zAxis,
zAxisIds,
]);
const relevantAxis = React.useMemo(() => {
return isXaxis ? xAxis[USED_AXIS_ID] : yAxis[USED_AXIS_ID];
}, [USED_AXIS_ID, isXaxis, xAxis, yAxis]);
const Content = content ?? DefaultChartsAxisTooltipContent;
const chartTooltipContentProps = useSlotProps({
elementType: Content,
externalSlotProps: contentProps,
additionalProps: {
axisData,
series: relevantSeries,
axis: relevantAxis,
dataIndex,
axisValue,
sx,
classes,
},
ownerState: {},
});
return <Content {...chartTooltipContentProps} />;
}
ChartsAxisTooltipContent.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "pnpm proptypes" |
// ----------------------------------------------------------------------
axisData: PropTypes.shape({
x: PropTypes.shape({
index: PropTypes.number,
value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string])
.isRequired,
}),
y: PropTypes.shape({
index: PropTypes.number,
value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string])
.isRequired,
}),
}).isRequired,
classes: PropTypes.object.isRequired,
content: PropTypes.elementType,
contentProps: PropTypes.shape({
axis: PropTypes.object,
axisData: PropTypes.shape({
x: PropTypes.shape({
index: PropTypes.number,
value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string])
.isRequired,
}),
y: PropTypes.shape({
index: PropTypes.number,
value: PropTypes.oneOfType([PropTypes.instanceOf(Date), PropTypes.number, PropTypes.string])
.isRequired,
}),
}),
axisValue: PropTypes.oneOfType([
PropTypes.instanceOf(Date),
PropTypes.number,
PropTypes.string,
]),
classes: PropTypes.object,
dataIndex: PropTypes.number,
series: PropTypes.arrayOf(PropTypes.object),
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
}),
sx: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.func, PropTypes.object, PropTypes.bool])),
PropTypes.func,
PropTypes.object,
]),
} as any;
export { ChartsAxisTooltipContent };