@mui/x-charts
Version:
The community edition of MUI X Charts components.
133 lines (131 loc) • 4.64 kB
JavaScript
'use client';
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ChartsSingleYAxis = ChartsSingleYAxis;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var React = _interopRequireWildcard(require("react"));
var _useIsHydrated = require("../hooks/useIsHydrated");
var _domUtils = require("../internals/domUtils");
var _useTicks = require("../hooks/useTicks");
var _useDrawingArea = require("../hooks/useDrawingArea");
var _ChartProvider = require("../context/ChartProvider");
var _shortenLabels = require("./shortenLabels");
var _utilities = require("./utilities");
var _useAxisProps = require("./useAxisProps");
var _jsxRuntime = require("react/jsx-runtime");
/**
* @ignore - internal component.
*/function ChartsSingleYAxis(inProps) {
const {
yScale,
defaultizedProps,
tickNumber,
positionSign,
skipAxisRendering,
classes,
Line,
Tick,
TickLabel,
Label,
axisTickLabelProps,
axisLabelProps,
lineProps,
isRtl
} = (0, _useAxisProps.useAxisProps)(inProps);
const {
position,
disableLine,
disableTicks,
label,
tickSize: tickSizeProp,
valueFormatter,
slotProps,
tickPlacement,
tickLabelPlacement,
tickInterval,
tickLabelInterval,
sx,
offset,
width: axisWidth
} = defaultizedProps;
const drawingArea = (0, _useDrawingArea.useDrawingArea)();
const {
left,
top,
width,
height
} = drawingArea;
const {
instance
} = (0, _ChartProvider.useChartContext)();
const isHydrated = (0, _useIsHydrated.useIsHydrated)();
const tickSize = disableTicks ? 4 : tickSizeProp;
const yTicks = (0, _useTicks.useTicks)({
scale: yScale,
tickNumber,
valueFormatter,
tickPlacement,
tickLabelPlacement,
tickInterval,
direction: 'y'
});
// 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;
}
const labelRefPoint = {
x: positionSign * axisWidth,
y: top + height / 2
};
/* If there's an axis title, the tick labels have less space to render */
const tickLabelsMaxWidth = Math.max(0, axisWidth - (label ? (0, _domUtils.getStringSize)(label, axisLabelProps.style).height + _utilities.AXIS_LABEL_TICK_LABEL_GAP : 0) - tickSize - _utilities.TICK_LABEL_GAP);
const tickLabels = isHydrated ? (0, _shortenLabels.shortenLabels)(yTicks, drawingArea, tickLabelsMaxWidth, isRtl, axisTickLabelProps.style) : new Map(Array.from(yTicks).map(item => [item, item.formattedValue]));
return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_utilities.YAxisRoot, {
transform: `translate(${position === 'right' ? left + width + offset : left - offset}, 0)`,
className: classes.root,
sx: sx,
children: [!disableLine && /*#__PURE__*/(0, _jsxRuntime.jsx)(Line, (0, _extends2.default)({
y1: top,
y2: top + height,
className: classes.line
}, lineProps)), yTicks.map((item, index) => {
const {
offset: tickOffset,
labelOffset,
value
} = item;
const xTickLabel = positionSign * (tickSize + _utilities.TICK_LABEL_GAP);
const yTickLabel = labelOffset;
const skipLabel = typeof tickLabelInterval === 'function' && !tickLabelInterval?.(value, index);
const showLabel = instance.isYInside(tickOffset);
const tickLabel = tickLabels.get(item);
if (!showLabel) {
return null;
}
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("g", {
transform: `translate(0, ${tickOffset})`,
className: classes.tickContainer,
children: [!disableTicks && /*#__PURE__*/(0, _jsxRuntime.jsx)(Tick, (0, _extends2.default)({
x2: positionSign * tickSize,
className: classes.tick
}, slotProps?.axisTick)), tickLabel !== undefined && !skipLabel && /*#__PURE__*/(0, _jsxRuntime.jsx)(TickLabel, (0, _extends2.default)({
x: xTickLabel,
y: yTickLabel,
text: tickLabel
}, axisTickLabelProps))]
}, index);
}), label && isHydrated && /*#__PURE__*/(0, _jsxRuntime.jsx)("g", {
className: classes.label,
children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Label, (0, _extends2.default)({}, labelRefPoint, axisLabelProps, {
text: label
}))
})]
});
}
;