UNPKG

@mui/x-charts

Version:

The community edition of MUI X Charts components.

29 lines 1.23 kB
import _formatErrorMessage from "@mui/x-internals/formatErrorMessage"; /** * Helper that converts values and percentages into values. * @param value The value provided by the developer. Can either be a number or a string with '%' or 'px'. * @param refValue The numerical value associated to 100%. * @returns The numerical value associated to the provided value. */ export function getPercentageValue(value, refValue) { if (typeof value === 'number') { return value; } if (value === '100%') { // Avoid potential rounding issues return refValue; } if (value.endsWith('%')) { const percentage = Number.parseFloat(value.slice(0, value.length - 1)); if (!Number.isNaN(percentage)) { return percentage * refValue / 100; } } if (value.endsWith('px')) { const val = Number.parseFloat(value.slice(0, value.length - 2)); if (!Number.isNaN(val)) { return val; } } throw new Error(process.env.NODE_ENV !== "production" ? `MUI X Charts: Received an unknown value "${value}". ` + 'Values must be a number, a string with a percentage (e.g., "50%"), or a string with pixels (e.g., "100px"). ' + 'Provide a valid number or string format.' : _formatErrorMessage(26, value)); }