@mui/x-charts
Version:
The community edition of MUI X Charts components.
137 lines (134 loc) • 4.21 kB
JavaScript
'use client';
import _extends from "@babel/runtime/helpers/esm/extends";
import * as React from 'react';
import { useDrawingArea } from "../hooks/useDrawingArea.js";
import { isBandScale } from "../internals/isBandScale.js";
import { useChartContext } from "../context/ChartProvider/useChartContext.js";
import { TICK_LABEL_GAP, XAxisRoot } from "./utilities.js";
import { useTicksGrouped } from "../hooks/useTicksGrouped.js";
import { useAxisProps } from "./useAxisProps.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
const DEFAULT_GROUPING_CONFIG = {
tickSize: 6
};
const getGroupingConfig = (groups, groupIndex, tickSize) => {
const config = groups[groupIndex] ?? {};
const defaultTickSize = tickSize ?? DEFAULT_GROUPING_CONFIG.tickSize;
const calculatedTickSize = defaultTickSize * groupIndex * 2 + defaultTickSize;
return _extends({}, DEFAULT_GROUPING_CONFIG, config, {
tickSize: config.tickSize ?? calculatedTickSize
});
};
/**
* @ignore - internal component.
*/
function ChartsGroupedXAxis(inProps) {
const {
xScale,
defaultizedProps,
tickNumber,
positionSign,
skipAxisRendering,
classes,
Line,
Tick,
TickLabel,
Label,
axisTickLabelProps,
axisLabelProps
} = useAxisProps(inProps);
if (!isBandScale(xScale)) {
throw new Error('MUI X Charts: ChartsGroupedXAxis only supports the `band` and `point` scale types.');
}
const {
position,
disableLine,
disableTicks,
label,
tickSize,
valueFormatter,
slotProps,
tickInterval,
tickPlacement,
tickLabelPlacement,
sx,
offset,
height: axisHeight
} = defaultizedProps;
const groups = defaultizedProps.groups;
const drawingArea = useDrawingArea();
const {
left,
top,
width,
height
} = drawingArea;
const {
instance
} = useChartContext();
const labelRefPoint = {
x: left + width / 2,
y: positionSign * axisHeight
};
const xTicks = useTicksGrouped({
scale: xScale,
tickNumber,
valueFormatter,
tickInterval,
tickPlacement,
tickLabelPlacement,
direction: 'x',
groups
});
// Skip axis rendering if no data is available
// - The domain is an empty array for band/point scales.
// - The domains contains Infinity for continuous scales.
// - The position is set to 'none'.
if (skipAxisRendering) {
return null;
}
return /*#__PURE__*/_jsxs(XAxisRoot, {
transform: `translate(0, ${position === 'bottom' ? top + height + offset : top - offset})`,
className: classes.root,
sx: sx,
children: [!disableLine && /*#__PURE__*/_jsx(Line, _extends({
x1: left,
x2: left + width,
className: classes.line
}, slotProps?.axisLine)), xTicks.map((item, index) => {
const {
offset: tickOffset,
labelOffset
} = item;
const xTickLabel = labelOffset ?? 0;
const showTick = instance.isXInside(tickOffset);
const tickLabel = item.formattedValue;
const ignoreTick = item.ignoreTick ?? false;
const groupIndex = item.groupIndex ?? 0;
const groupConfig = getGroupingConfig(groups, groupIndex, tickSize);
const tickYSize = positionSign * groupConfig.tickSize;
const labelPositionY = positionSign * (groupConfig.tickSize + TICK_LABEL_GAP);
return /*#__PURE__*/_jsxs("g", {
transform: `translate(${tickOffset}, 0)`,
className: classes.tickContainer,
"data-group-index": groupIndex,
children: [!disableTicks && !ignoreTick && showTick && /*#__PURE__*/_jsx(Tick, _extends({
y2: tickYSize,
className: classes.tick
}, slotProps?.axisTick)), tickLabel !== undefined && /*#__PURE__*/_jsx(TickLabel, _extends({
x: xTickLabel,
y: labelPositionY
}, axisTickLabelProps, {
style: _extends({}, axisTickLabelProps.style, groupConfig.tickLabelStyle),
text: tickLabel
}))]
}, index);
}), label && /*#__PURE__*/_jsx("g", {
className: classes.label,
children: /*#__PURE__*/_jsx(Label, _extends({}, labelRefPoint, axisLabelProps, {
text: label
}))
})]
});
}
export { ChartsGroupedXAxis };