-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
ChartsYAxis.tsx
330 lines (303 loc) · 9.43 KB
/
ChartsYAxis.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import * as React from 'react';
import PropTypes from 'prop-types';
import { useSlotProps } from '@mui/base/utils';
import { unstable_composeClasses as composeClasses } from '@mui/utils';
import { useThemeProps, useTheme, Theme } from '@mui/material/styles';
import { useCartesianContext } from '../context/CartesianProvider';
import { useTicks } from '../hooks/useTicks';
import { useDrawingArea } from '../hooks/useDrawingArea';
import { ChartsYAxisProps } from '../models/axis';
import { AxisRoot } from '../internals/components/AxisSharedComponents';
import { ChartsText, ChartsTextProps } from '../ChartsText';
import { getAxisUtilityClass } from '../ChartsAxis/axisClasses';
const useUtilityClasses = (ownerState: ChartsYAxisProps & { theme: Theme }) => {
const { classes, position } = ownerState;
const slots = {
root: ['root', 'directionY', position],
line: ['line'],
tickContainer: ['tickContainer'],
tick: ['tick'],
tickLabel: ['tickLabel'],
label: ['label'],
};
return composeClasses(slots, getAxisUtilityClass, classes);
};
const defaultProps = {
position: 'left',
disableLine: false,
disableTicks: false,
tickFontSize: 12,
labelFontSize: 14,
tickSize: 6,
} as const;
/**
* Demos:
*
* - [Axis](https://mui.com/x/react-charts/axis/)
*
* API:
*
* - [ChartsYAxis API](https://mui.com/x/api/charts/charts-y-axis/)
*/
function ChartsYAxis(inProps: ChartsYAxisProps) {
const { yAxisIds, yAxis } = useCartesianContext();
const { scale: yScale, tickNumber, ...settings } = yAxis[inProps.axisId ?? yAxisIds[0]];
const themedProps = useThemeProps({ props: { ...settings, ...inProps }, name: 'MuiChartsYAxis' });
const defaultizedProps = {
...defaultProps,
...themedProps,
};
const {
position,
disableLine,
disableTicks,
tickFontSize,
label,
labelFontSize,
labelStyle,
tickLabelStyle,
tickSize: tickSizeProp,
valueFormatter,
slots,
slotProps,
tickPlacement,
tickLabelPlacement,
tickInterval,
tickLabelInterval,
} = defaultizedProps;
const theme = useTheme();
const isRTL = theme.direction === 'rtl';
const classes = useUtilityClasses({ ...defaultizedProps, theme });
const { left, top, width, height } = useDrawingArea();
const tickSize = disableTicks ? 4 : tickSizeProp;
const yTicks = useTicks({
scale: yScale,
tickNumber,
valueFormatter,
tickPlacement,
tickLabelPlacement,
tickInterval,
});
const positionSign = position === 'right' ? 1 : -1;
const labelRefPoint = {
x: positionSign * (tickFontSize + tickSize + 10),
y: top + height / 2,
};
const Line = slots?.axisLine ?? 'line';
const Tick = slots?.axisTick ?? 'line';
const TickLabel = slots?.axisTickLabel ?? ChartsText;
const Label = slots?.axisLabel ?? ChartsText;
const revertAnchor = (!isRTL && position === 'right') || (isRTL && position !== 'right');
const axisTickLabelProps = useSlotProps({
elementType: TickLabel,
externalSlotProps: slotProps?.axisTickLabel,
additionalProps: {
style: {
fontSize: tickFontSize,
textAnchor: revertAnchor ? 'start' : 'end',
dominantBaseline: 'central',
...tickLabelStyle,
},
} as Partial<ChartsTextProps>,
className: classes.tickLabel,
ownerState: {},
});
const axisLabelProps = useSlotProps({
elementType: Label,
externalSlotProps: slotProps?.axisLabel,
additionalProps: {
style: {
fontSize: labelFontSize,
angle: positionSign * 90,
textAnchor: 'middle',
dominantBaseline: 'auto',
...labelStyle,
} as Partial<ChartsTextProps>['style'],
} as Partial<ChartsTextProps>,
ownerState: {},
});
const lineSlotProps = useSlotProps({
elementType: Line,
externalSlotProps: slotProps?.axisLine,
additionalProps: {
strokeLinecap: 'square' as const,
},
ownerState: {},
});
const domain = yScale.domain();
if (domain.length === 0 || domain[0] === domain[1]) {
// Skip axis rendering if
// - the data is empty (for band and point axis)
// - No data is associated to the axis (other scale types)
return null;
}
return (
<AxisRoot
transform={`translate(${position === 'right' ? left + width : left}, 0)`}
className={classes.root}
>
{!disableLine && (
<Line y1={top} y2={top + height} className={classes.line} {...lineSlotProps} />
)}
{yTicks.map(({ formattedValue, offset, labelOffset, value }, index) => {
const xTickLabel = positionSign * (tickSize + 2);
const yTickLabel = labelOffset;
const skipLabel =
typeof tickLabelInterval === 'function' && !tickLabelInterval?.(value, index);
const showLabel = offset >= top - 1 && offset <= height + top + 1;
if (!showLabel) {
return null;
}
return (
<g key={index} transform={`translate(0, ${offset})`} className={classes.tickContainer}>
{!disableTicks && (
<Tick
x2={positionSign * tickSize}
className={classes.tick}
{...slotProps?.axisTick}
/>
)}
{formattedValue !== undefined && !skipLabel && (
<TickLabel
x={xTickLabel}
y={yTickLabel}
text={formattedValue.toString()}
{...axisTickLabelProps}
/>
)}
</g>
);
})}
{label && (
<g className={classes.label}>
<Label {...labelRefPoint} {...axisLabelProps} text={label} />
</g>
)}
</AxisRoot>
);
}
ChartsYAxis.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "pnpm proptypes" |
// ----------------------------------------------------------------------
/**
* The id of the axis to render.
* If undefined, it will be the first defined axis.
*/
axisId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
/**
* Override or extend the styles applied to the component.
*/
classes: PropTypes.object,
/**
* If true, the axis line is disabled.
* @default false
*/
disableLine: PropTypes.bool,
/**
* If true, the ticks are disabled.
* @default false
*/
disableTicks: PropTypes.bool,
/**
* The fill color of the axis text.
* @default 'currentColor'
*/
fill: PropTypes.string,
/**
* The label of the axis.
*/
label: PropTypes.string,
/**
* The font size of the axis label.
* @default 14
* @deprecated Consider using `labelStyle.fontSize` instead.
*/
labelFontSize: PropTypes.number,
/**
* The style applied to the axis label.
*/
labelStyle: PropTypes.object,
/**
* Position of the axis.
*/
position: PropTypes.oneOf(['left', 'right']),
/**
* The props used for each component slot.
* @default {}
*/
slotProps: PropTypes.object,
/**
* Overridable component slots.
* @default {}
*/
slots: PropTypes.object,
/**
* The stroke color of the axis line.
* @default 'currentColor'
*/
stroke: PropTypes.string,
/**
* The font size of the axis ticks text.
* @default 12
* @deprecated Consider using `tickLabelStyle.fontSize` instead.
*/
tickFontSize: PropTypes.number,
/**
* Defines which ticks are displayed.
* Its value can be:
* - 'auto' In such case the ticks are computed based on axis scale and other parameters.
* - a filtering function of the form `(value, index) => boolean` which is available only if the axis has "point" scale.
* - an array containing the values where ticks should be displayed.
* @see See {@link https://mui.com/x/react-charts/axis/#fixed-tick-positions}
* @default 'auto'
*/
tickInterval: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.array, PropTypes.func]),
/**
* Defines which ticks get its label displayed. Its value can be:
* - 'auto' In such case, labels are displayed if they do not overlap with the previous one.
* - a filtering function of the form (value, index) => boolean. Warning: the index is tick index, not data ones.
* @default 'auto'
*/
tickLabelInterval: PropTypes.oneOfType([PropTypes.oneOf(['auto']), PropTypes.func]),
/**
* The placement of ticks label. Can be the middle of the band, or the tick position.
* Only used if scale is 'band'.
* @default 'middle'
*/
tickLabelPlacement: PropTypes.oneOf(['middle', 'tick']),
/**
* The style applied to ticks text.
*/
tickLabelStyle: PropTypes.object,
/**
* Maximal step between two ticks.
* When using time data, the value is assumed to be in ms.
* Not supported by categorical axis (band, points).
*/
tickMaxStep: PropTypes.number,
/**
* Minimal step between two ticks.
* When using time data, the value is assumed to be in ms.
* Not supported by categorical axis (band, points).
*/
tickMinStep: PropTypes.number,
/**
* The number of ticks. This number is not guaranteed.
* Not supported by categorical axis (band, points).
*/
tickNumber: PropTypes.number,
/**
* The placement of ticks in regard to the band interval.
* Only used if scale is 'band'.
* @default 'extremities'
*/
tickPlacement: PropTypes.oneOf(['end', 'extremities', 'middle', 'start']),
/**
* The size of the ticks.
* @default 6
*/
tickSize: PropTypes.number,
} as any;
export { ChartsYAxis };