victory-bar
Version:
Bar Component for Victory
152 lines (150 loc) • 4.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getBaseProps = exports.getBarPosition = void 0;
var _victoryCore = require("victory-core");
const getBarPosition = (props, datum) => {
const getDefaultMin = axis => {
const defaultZero = _victoryCore.Scale.getType(props.scale[axis]) === "log" ? 1 / Number.MAX_SAFE_INTEGER : 0;
let defaultMin = defaultZero;
const minY = _victoryCore.Collection.getMinValue(props.domain[axis]);
const maxY = _victoryCore.Collection.getMaxValue(props.domain[axis]);
if (minY < 0 && maxY <= 0) {
defaultMin = maxY;
} else if (minY >= 0 && maxY > 0) {
defaultMin = minY;
}
return datum[`_${axis}`] instanceof Date ? new Date(defaultMin) : defaultMin;
};
const _y0 = datum._y0 !== undefined ? datum._y0 : getDefaultMin("y");
const _x0 = datum._x0 !== undefined ? datum._x0 : getDefaultMin("x");
return _victoryCore.Helpers.scalePoint(props, Object.assign({}, datum, {
_y0,
_x0
}));
};
exports.getBarPosition = getBarPosition;
const getCalculatedValues = props => {
const {
polar
} = props;
const defaultStyles = _victoryCore.Helpers.getDefaultStyles(props, "bar");
const style = !props.disableInlineStyles ? _victoryCore.Helpers.getStyles(props.style, defaultStyles) : {};
const range = props.range || {
x: _victoryCore.Helpers.getRange(props, "x"),
y: _victoryCore.Helpers.getRange(props, "y")
};
const domain = {
x: _victoryCore.Domain.getDomainWithZero(props, "x"),
y: _victoryCore.Domain.getDomainWithZero(props, "y")
};
const scale = {
x: _victoryCore.Scale.getBaseScale(props, "x").domain(domain.x).range(props.horizontal ? range.y : range.x),
y: _victoryCore.Scale.getBaseScale(props, "y").domain(domain.y).range(props.horizontal ? range.x : range.y)
};
const origin = polar ? props.origin || _victoryCore.Helpers.getPolarOrigin(props) : undefined;
let data = _victoryCore.Data.getData(props);
data = _victoryCore.Data.formatDataFromDomain(data, domain, 0);
// when inside a zoom container, reset the _x and _y properties of each datum to the original
// x and y property values so they will not be clipped. See https://github.com/FormidableLabs/victory/pull/2970
if (props.groupComponent?.type === _victoryCore.VictoryClipContainer) {
data = data.map(datum => ({
...datum,
_x: datum.x,
_y: datum.y
}));
}
return {
style,
data,
scale,
domain,
origin
};
};
const getBaseProps = (initialProps, fallbackProps) => {
const modifiedProps = _victoryCore.Helpers.modifyProps(initialProps, fallbackProps, "bar");
const props = Object.assign({}, modifiedProps, getCalculatedValues(modifiedProps));
const {
alignment,
barRatio,
cornerRadius,
data,
disableInlineStyles,
domain,
events,
height,
horizontal,
origin,
padding,
polar,
scale,
sharedEvents,
standalone,
style,
theme,
width,
labels,
name,
barWidth,
getPath
} = props;
const initialChildProps = {
parent: {
horizontal,
domain,
scale,
width,
height,
data,
standalone,
name,
theme,
polar,
origin,
padding,
style: style.parent
}
};
return data.reduce((childProps, datum, index) => {
const eventKey = !_victoryCore.Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
const {
x,
y,
y0,
x0
} = getBarPosition(props, datum);
const dataProps = {
alignment,
barRatio,
barWidth,
cornerRadius,
data,
datum,
disableInlineStyles,
getPath,
horizontal,
index,
polar,
origin,
scale,
style: style.data,
width,
height,
x,
y,
y0,
x0
};
childProps[eventKey] = {
data: dataProps
};
const text = _victoryCore.LabelHelpers.getText(props, datum, index);
if (text !== undefined && text !== null || labels && (events || sharedEvents)) {
childProps[eventKey].labels = _victoryCore.LabelHelpers.getProps(props, index);
}
return childProps;
}, initialChildProps);
};
exports.getBaseProps = getBaseProps;