@mui/x-charts
Version:
The community edition of MUI X Charts components.
53 lines (51 loc) • 2.27 kB
JavaScript
'use client';
import * as React from 'react';
import { getValueToPositionMapper, useYScale } from "../hooks/useScale.js";
import { isBandScale } from "../internals/isBandScale.js";
import { useSelector } from "../internals/store/useSelector.js";
import { useStore } from "../internals/store/useStore.js";
import { selectorChartsInteractionYAxisValue } from "../internals/plugins/featurePlugins/useChartCartesianAxis/index.js";
import { useDrawingArea } from "../hooks/index.js";
import { ChartsAxisHighlightPath } from "./ChartsAxisHighlightPath.js";
/**
* @ignore - internal component.
*/
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export default function ChartsYHighlight(props) {
const {
type,
classes
} = props;
const {
left,
width
} = useDrawingArea();
const yScale = useYScale();
const store = useStore();
const axisYValue = useSelector(store, selectorChartsInteractionYAxisValue);
const getYPosition = getValueToPositionMapper(yScale);
const isBandScaleY = type === 'band' && axisYValue !== null && isBandScale(yScale);
if (process.env.NODE_ENV !== 'production') {
const isError = isBandScaleY && yScale(axisYValue) === undefined;
if (isError) {
console.error([`MUI X Charts: The position value provided for the axis is not valid for the current scale.`, `This probably means something is wrong with the data passed to the chart.`, `The ChartsAxisHighlight component will not be displayed.`].join('\n'));
}
}
return /*#__PURE__*/_jsxs(React.Fragment, {
children: [isBandScaleY && yScale(axisYValue) !== undefined && /*#__PURE__*/_jsx(ChartsAxisHighlightPath, {
d: `M ${left} ${
// @ts-expect-error, yScale value is checked in the statement above
yScale(axisYValue) - (yScale.step() - yScale.bandwidth()) / 2} l 0 ${yScale.step()} l ${width} 0 l 0 ${-yScale.step()} Z`,
className: classes.root,
ownerState: {
axisHighlight: 'band'
}
}), type === 'line' && axisYValue !== null && /*#__PURE__*/_jsx(ChartsAxisHighlightPath, {
d: `M ${left} ${getYPosition(axisYValue)} L ${left + width} ${getYPosition(axisYValue)}`,
className: classes.root,
ownerState: {
axisHighlight: 'line'
}
})]
});
}