UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

98 lines (96 loc) 3.4 kB
'use client'; import _extends from "@babel/runtime/helpers/esm/extends"; import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose"; const _excluded = ["scale", "tickNumber", "reverse"]; import useSlotProps from '@mui/utils/useSlotProps'; import { useThemeProps, useTheme } from '@mui/material/styles'; import { useRtl } from '@mui/system/RtlProvider'; import { ChartsText } from "../ChartsText/index.js"; import { useXAxes } from "../hooks/useAxis.js"; import { getDefaultBaseline, getDefaultTextAnchor } from "../ChartsText/defaultTextPlacement.js"; import { invertTextAnchor } from "../internals/invertTextAnchor.js"; import { defaultProps, useUtilityClasses } from "./utilities.js"; import { isBandScale } from "../internals/isBandScale.js"; import { isInfinity } from "../internals/isInfinity.js"; export const useAxisProps = inProps => { const { xAxis, xAxisIds } = useXAxes(); const _xAxis = xAxis[inProps.axisId ?? xAxisIds[0]], { scale: xScale, tickNumber, reverse } = _xAxis, settings = _objectWithoutPropertiesLoose(_xAxis, _excluded); // eslint-disable-next-line material-ui/mui-name-matches-component-name const themedProps = useThemeProps({ props: _extends({}, settings, inProps), name: 'MuiChartsXAxis' }); const defaultizedProps = _extends({}, defaultProps, themedProps); const { position, tickLabelStyle, labelStyle, slots, slotProps } = defaultizedProps; const theme = useTheme(); const isRtl = useRtl(); const classes = useUtilityClasses(defaultizedProps); const positionSign = position === 'bottom' ? 1 : -1; const Line = slots?.axisLine ?? 'line'; const Tick = slots?.axisTick ?? 'line'; const TickLabel = slots?.axisTickLabel ?? ChartsText; const Label = slots?.axisLabel ?? ChartsText; const defaultTextAnchor = getDefaultTextAnchor((position === 'bottom' ? 0 : 180) - (tickLabelStyle?.angle ?? 0)); const defaultDominantBaseline = getDefaultBaseline((position === 'bottom' ? 0 : 180) - (tickLabelStyle?.angle ?? 0)); const axisTickLabelProps = useSlotProps({ elementType: TickLabel, externalSlotProps: slotProps?.axisTickLabel, additionalProps: { style: _extends({}, theme.typography.caption, { fontSize: 12, lineHeight: 1.25, textAnchor: isRtl ? invertTextAnchor(defaultTextAnchor) : defaultTextAnchor, dominantBaseline: defaultDominantBaseline }, tickLabelStyle) }, className: classes.tickLabel, ownerState: {} }); const axisLabelProps = useSlotProps({ elementType: Label, externalSlotProps: slotProps?.axisLabel, additionalProps: { style: _extends({}, theme.typography.body1, { lineHeight: 1, fontSize: 14, textAnchor: 'middle', dominantBaseline: position === 'bottom' ? 'text-after-edge' : 'text-before-edge' }, labelStyle) }, ownerState: {} }); const domain = xScale.domain(); const isScaleBand = isBandScale(xScale); const skipAxisRendering = isScaleBand && domain.length === 0 || !isScaleBand && domain.some(isInfinity) || position === 'none'; return { xScale, defaultizedProps, tickNumber, positionSign, skipAxisRendering, classes, Line, Tick, TickLabel, Label, axisTickLabelProps, axisLabelProps, reverse, isRtl }; };