|
1 | 1 | import * as React from 'react'; |
| 2 | +import { unstable_composeClasses as composeClasses } from '@mui/utils'; |
| 3 | +import { useThemeProps, useTheme, Theme } from '@mui/material/styles'; |
2 | 4 | import { CartesianContext } from '../context/CartesianContextProvider'; |
3 | 5 | import { DrawingContext } from '../context/DrawingProvider'; |
4 | 6 | import useTicks from '../hooks/useTicks'; |
5 | 7 | import { YAxisProps } from '../models/axis'; |
| 8 | +import { Line, Tick, TickLabel, Label } from '../internals/components/AxisSharedComponents'; |
| 9 | +import { getAxisUtilityClass } from '../Axis/axisClasses'; |
6 | 10 |
|
7 | | -export function YAxis(props: YAxisProps) { |
| 11 | +const useUtilityClasses = (ownerState: YAxisProps & { theme: Theme }) => { |
| 12 | + const { classes, position } = ownerState; |
| 13 | + const slots = { |
| 14 | + root: ['root', 'directionY', position], |
| 15 | + line: ['line'], |
| 16 | + tickContainer: ['tickContainer'], |
| 17 | + tick: ['tick'], |
| 18 | + tickLabel: ['tickLabel'], |
| 19 | + label: ['label'], |
| 20 | + }; |
| 21 | + |
| 22 | + return composeClasses(slots, getAxisUtilityClass, classes); |
| 23 | +}; |
| 24 | + |
| 25 | +const defaultProps = { |
| 26 | + position: 'left', |
| 27 | + disableLine: false, |
| 28 | + disableTicks: false, |
| 29 | + tickFontSize: 10, |
| 30 | + labelFontSize: 14, |
| 31 | + tickSize: 6, |
| 32 | +} as const; |
| 33 | + |
| 34 | +export function YAxis(inProps: YAxisProps) { |
| 35 | + const props = useThemeProps({ props: { ...defaultProps, ...inProps }, name: 'MuiYAxis' }); |
8 | 36 | const { |
9 | 37 | yAxis: { |
10 | | - [props.axisId]: { scale: yScale, ...settings }, |
| 38 | + [props.axisId]: { scale: yScale, ticksNumber, ...settings }, |
11 | 39 | }, |
12 | 40 | } = React.useContext(CartesianContext); |
| 41 | + |
| 42 | + const defaultizedProps = { ...defaultProps, ...settings, ...props }; |
13 | 43 | const { |
14 | | - position = 'left', |
15 | | - disableLine = false, |
16 | | - disableTicks = false, |
17 | | - fill = 'currentColor', |
18 | | - fontSize = 10, |
| 44 | + position, |
| 45 | + disableLine, |
| 46 | + disableTicks, |
| 47 | + tickFontSize, |
19 | 48 | label, |
20 | | - labelFontSize = 14, |
21 | | - stroke = 'currentColor', |
22 | | - tickSize: tickSizeProp = 6, |
23 | | - } = { ...settings, ...props }; |
| 49 | + labelFontSize, |
| 50 | + tickSize: tickSizeProp, |
| 51 | + } = defaultizedProps; |
| 52 | + |
| 53 | + const theme = useTheme(); |
| 54 | + const classes = useUtilityClasses({ ...defaultizedProps, theme }); |
24 | 55 |
|
25 | 56 | const { left, top, width, height } = React.useContext(DrawingContext); |
26 | 57 |
|
27 | 58 | const tickSize = disableTicks ? 4 : tickSizeProp; |
28 | 59 |
|
29 | | - const yTicks = useTicks({ scale: yScale }); |
| 60 | + const yTicks = useTicks({ scale: yScale, ticksNumber }); |
30 | 61 |
|
31 | 62 | const positionSigne = position === 'right' ? 1 : -1; |
| 63 | + |
32 | 64 | return ( |
33 | | - <g transform={`translate(${position === 'right' ? left + width : left}, 0)`}> |
| 65 | + <g |
| 66 | + transform={`translate(${position === 'right' ? left + width : left}, 0)`} |
| 67 | + className={classes.root} |
| 68 | + > |
34 | 69 | {!disableLine && ( |
35 | | - <line |
36 | | - y1={yScale.range()[0]} |
37 | | - y2={yScale.range()[1]} |
38 | | - stroke={stroke} |
39 | | - shapeRendering="crispEdges" |
40 | | - /> |
| 70 | + <Line y1={yScale.range()[0]} y2={yScale.range()[1]} className={classes.line} /> |
41 | 71 | )} |
42 | 72 | {yTicks.map(({ value, offset }, index) => ( |
43 | | - <g key={index} transform={`translate(0, ${offset})`}> |
44 | | - {!disableTicks && ( |
45 | | - <line x2={positionSigne * tickSize} stroke={stroke} shapeRendering="crispEdges" /> |
46 | | - )} |
47 | | - <text |
48 | | - fill={fill} |
49 | | - transform={`translate(${positionSigne * (fontSize + tickSize + 2)}, 0)`} |
50 | | - textAnchor="middle" |
51 | | - fontSize={fontSize} |
| 73 | + <g key={index} transform={`translate(0, ${offset})`} className={classes.tickContainer}> |
| 74 | + {!disableTicks && <Tick x2={positionSigne * tickSize} className={classes.tick} />} |
| 75 | + <TickLabel |
| 76 | + transform={`translate(${positionSigne * (tickFontSize + tickSize + 2)}, 0)`} |
| 77 | + sx={{ |
| 78 | + fontSize: tickFontSize, |
| 79 | + }} |
| 80 | + className={classes.tickLabel} |
52 | 81 | > |
53 | 82 | {value} |
54 | | - </text> |
| 83 | + </TickLabel> |
55 | 84 | </g> |
56 | 85 | ))} |
57 | 86 | {label && ( |
58 | | - <text |
59 | | - fill={fill} |
60 | | - style={{}} |
61 | | - transform={`translate(${positionSigne * (fontSize + tickSize + 20)}, ${ |
| 87 | + <Label |
| 88 | + transform={`translate(${positionSigne * (tickFontSize + tickSize + 20)}, ${ |
62 | 89 | top + height / 2 |
63 | 90 | }) rotate(${positionSigne * 90})`} |
64 | | - fontSize={labelFontSize} |
65 | | - textAnchor="middle" |
| 91 | + sx={{ |
| 92 | + fontSize: labelFontSize, |
| 93 | + }} |
| 94 | + className={classes.label} |
66 | 95 | > |
67 | 96 | {label} |
68 | | - </text> |
| 97 | + </Label> |
69 | 98 | )} |
70 | 99 | </g> |
71 | 100 | ); |
|
0 commit comments