@mui/x-charts
Version:
The community edition of MUI X Charts components.
23 lines (21 loc) • 808 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getDataIndexForOrdinalScaleValue = getDataIndexForOrdinalScaleValue;
exports.invertScale = invertScale;
var _scaleGuards = require("./scaleGuards");
function invertScale(scale, data, value) {
if ((0, _scaleGuards.isOrdinalScale)(scale)) {
const dataIndex = getDataIndexForOrdinalScaleValue(scale, value);
return data[dataIndex];
}
return scale.invert(value);
}
/**
* Get the data index for a given value on an ordinal scale.
*/
function getDataIndexForOrdinalScaleValue(scale, value) {
const dataIndex = scale.bandwidth() === 0 ? Math.floor((value - Math.min(...scale.range()) + scale.step() / 2) / scale.step()) : Math.floor((value - Math.min(...scale.range())) / scale.step());
return dataIndex;
}