@mui/x-charts
Version:
The community edition of the charts components (MUI X).
105 lines • 4.81 kB
JavaScript
import _extends from "@babel/runtime/helpers/esm/extends";
import * as React from 'react';
import PropTypes from 'prop-types';
import composeClasses from '@mui/utils/composeClasses';
import generateUtilityClass from '@mui/utils/generateUtilityClass';
import generateUtilityClasses from '@mui/utils/generateUtilityClasses';
import { styled } from '@mui/material/styles';
import { InteractionContext } from '../context/InteractionProvider';
import { CartesianContext } from '../context/CartesianContextProvider';
import { getValueToPositionMapper } from '../hooks/useScale';
import { isBandScale } from '../internals/isBandScale';
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
export function getAxisHighlightUtilityClass(slot) {
return generateUtilityClass('MuiChartsAxisHighlight', slot);
}
export var chartsAxisHighlightClasses = generateUtilityClasses('MuiChartsAxisHighlight', ['root']);
var useUtilityClasses = function useUtilityClasses() {
var slots = {
root: ['root']
};
return composeClasses(slots, getAxisHighlightUtilityClass);
};
export var ChartsAxisHighlightPath = styled('path', {
name: 'MuiChartsAxisHighlight',
slot: 'Root',
overridesResolver: function overridesResolver(_, styles) {
return styles.root;
}
})(function (_ref) {
var ownerState = _ref.ownerState,
theme = _ref.theme;
return _extends({
pointerEvents: 'none'
}, ownerState.axisHighlight === 'band' && {
fill: theme.palette.mode === 'light' ? 'gray' : 'white',
fillOpacity: 0.1
}, ownerState.axisHighlight === 'line' && {
strokeDasharray: '5 2',
stroke: theme.palette.mode === 'light' ? '#000000' : '#ffffff'
});
});
/**
* Demos:
*
* - [Custom components](https://mui.com/x/react-charts/components/)
*
* API:
*
* - [ChartsAxisHighlight API](https://mui.com/x/api/charts/charts-axis-highlight/)
*/
function ChartsAxisHighlight(props) {
var xAxisHighlight = props.x,
yAxisHighlight = props.y;
var _React$useContext = React.useContext(CartesianContext),
xAxisIds = _React$useContext.xAxisIds,
xAxis = _React$useContext.xAxis,
yAxisIds = _React$useContext.yAxisIds,
yAxis = _React$useContext.yAxis;
var classes = useUtilityClasses();
var USED_X_AXIS_ID = xAxisIds[0];
var USED_Y_AXIS_ID = yAxisIds[0];
var xScale = xAxis[USED_X_AXIS_ID].scale;
var yScale = yAxis[USED_Y_AXIS_ID].scale;
var _React$useContext2 = React.useContext(InteractionContext),
axis = _React$useContext2.axis;
var getXPosition = getValueToPositionMapper(xScale);
var getYPosition = getValueToPositionMapper(yScale);
return /*#__PURE__*/_jsxs(React.Fragment, {
children: [xAxisHighlight === 'band' && axis.x !== null && isBandScale(xScale) && /*#__PURE__*/_jsx(ChartsAxisHighlightPath, {
d: "M ".concat(xScale(axis.x.value) - (xScale.step() - xScale.bandwidth()) / 2, " ").concat(yScale.range()[0], " l ").concat(xScale.step(), " 0 l 0 ").concat(yScale.range()[1] - yScale.range()[0], " l ").concat(-xScale.step(), " 0 Z"),
className: classes.root,
ownerState: {
axisHighlight: 'band'
}
}), yAxisHighlight === 'band' && axis.y !== null && isBandScale(yScale) && /*#__PURE__*/_jsx(ChartsAxisHighlightPath, {
d: "M ".concat(xScale.range()[0], " ").concat(yScale(axis.y.value) - (yScale.step() - yScale.bandwidth()) / 2, " l 0 ").concat(yScale.step(), " l ").concat(xScale.range()[1] - xScale.range()[0], " 0 l 0 ").concat(-yScale.step(), " Z"),
className: classes.root,
ownerState: {
axisHighlight: 'band'
}
}), xAxisHighlight === 'line' && axis.x !== null && /*#__PURE__*/_jsx(ChartsAxisHighlightPath, {
d: "M ".concat(getXPosition(axis.x.value), " ").concat(yScale.range()[0], " L ").concat(getXPosition(axis.x.value), " ").concat(yScale.range()[1]),
className: classes.root,
ownerState: {
axisHighlight: 'line'
}
}), yAxisHighlight === 'line' && axis.y !== null && /*#__PURE__*/_jsx(ChartsAxisHighlightPath, {
d: "M ".concat(xScale.range()[0], " ").concat(getYPosition(axis.y.value), " L ").concat(xScale.range()[1], " ").concat(getYPosition(axis.y.value)),
className: classes.root,
ownerState: {
axisHighlight: 'line'
}
})]
});
}
process.env.NODE_ENV !== "production" ? ChartsAxisHighlight.propTypes = {
// ----------------------------- Warning --------------------------------
// | These PropTypes are generated from the TypeScript type definitions |
// | To update them edit the TypeScript types and run "yarn proptypes" |
// ----------------------------------------------------------------------
x: PropTypes.oneOf(['band', 'line', 'none']),
y: PropTypes.oneOf(['band', 'line', 'none'])
} : void 0;
export { ChartsAxisHighlight };