@mui/x-charts
Version:
The community edition of MUI X Charts components.
36 lines (35 loc) • 1.49 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getPercentageValue = getPercentageValue;
var _formatErrorMessage2 = _interopRequireDefault(require("@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.
*/
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.' : (0, _formatErrorMessage2.default)(26, value));
}