UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

105 lines (101 loc) 2.92 kB
import _extends from "@babel/runtime/helpers/esm/extends"; import { useThemeProps } from '@mui/material/styles'; import { useDrawingArea } from "./useDrawingArea.mjs"; import { useXAxes, useYAxes } from "./useAxis.mjs"; import { defaultProps } from "../ChartsXAxis/utilities.mjs"; export function getXAxisCoordinates(drawingArea, computedAxis) { const { position, offset, height: axisHeight } = computedAxis; if (position === 'none') { return null; } let top; if (position === 'top') { top = drawingArea.top - axisHeight - offset; } else { top = drawingArea.top + drawingArea.height + offset; } const left = drawingArea.left; const bottom = top + axisHeight; const right = drawingArea.left + drawingArea.width; return { left, top, right, bottom }; } /** * Get the coordinates of the given X axis. The coordinates are relative to the SVG's origin. * @param axisId The id of the X axis. * @returns {AxisCoordinates | null} The coordinates of the X axis or null if the axis does not exist or has position: 'none'. */ export function useXAxisCoordinates(axisId) { const { xAxis: xAxes } = useXAxes(); const drawingArea = useDrawingArea(); const axis = xAxes[axisId]; // FIXME(v9): Remove // eslint-disable-next-line mui/material-ui-name-matches-component-name const themedProps = useThemeProps({ props: axis, name: 'MuiChartsXAxis' }); if (!axis) { return null; } const defaultizedProps = _extends({}, defaultProps, themedProps); return getXAxisCoordinates(drawingArea, defaultizedProps); } export function getYAxisCoordinates(drawingArea, computedAxis) { const { position, offset, width: axisWidth } = computedAxis; if (position === 'none') { return null; } let left; if (position === 'right') { left = drawingArea.left + drawingArea.width + offset; } else { left = drawingArea.left - axisWidth - offset; } const top = drawingArea.top; const bottom = drawingArea.top + drawingArea.height; const right = left + axisWidth; return { left, top, right, bottom }; } /** * Returns the coordinates of the given Y axis. The coordinates are relative to the SVG's origin. * @param axisId The id of the Y axis. * @returns {AxisCoordinates | null} The coordinates of the Y axis or null if the axis does not exist or has position: 'none'. */ export function useYAxisCoordinates(axisId) { const { yAxis: yAxes } = useYAxes(); const drawingArea = useDrawingArea(); const axis = yAxes[axisId]; // FIXME(v9): Remove // eslint-disable-next-line mui/material-ui-name-matches-component-name const themedProps = useThemeProps({ props: axis, name: 'MuiChartsYAxis' }); if (!axis) { return null; } const defaultizedProps = _extends({}, defaultProps, themedProps); return getYAxisCoordinates(drawingArea, defaultizedProps); }