victory-bar
Version:
Bar Component for Victory
85 lines (84 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getStyle = exports.getCornerRadius = exports.getBarWidth = void 0;
var _isPlainObject = _interopRequireDefault(require("lodash/isPlainObject"));
var _victoryCore = require("victory-core");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const DEFAULT_BAR_WIDTH = 8;
const getBarWidth = (barWidth, props) => {
const {
scale,
data,
style
} = props;
if (barWidth) {
return _victoryCore.Helpers.evaluateProp(barWidth, props);
} else if (style.width) {
return style.width;
}
const range = scale.x.range();
const extent = Math.abs(range[1] - range[0]);
const bars = data.length + 2;
const barRatio = props.barRatio || 0.5;
const defaultWidth = barRatio * (data.length < 2 ? DEFAULT_BAR_WIDTH : extent / bars);
return Math.max(1, defaultWidth);
};
exports.getBarWidth = getBarWidth;
const getCornerRadiusFromObject = (cornerRadius, props) => {
const realCornerRadius = {
topLeft: 0,
topRight: 0,
bottomLeft: 0,
bottomRight: 0
};
const updateCornerRadius = (corner, fallback) => {
if (!_victoryCore.Helpers.isNil(cornerRadius[corner])) {
realCornerRadius[corner] = _victoryCore.Helpers.evaluateProp(cornerRadius[corner], props);
} else if (!_victoryCore.Helpers.isNil(cornerRadius[fallback])) {
realCornerRadius[corner] = _victoryCore.Helpers.evaluateProp(cornerRadius[fallback], props);
}
};
updateCornerRadius("topLeft", "top");
updateCornerRadius("topRight", "top");
updateCornerRadius("bottomLeft", "bottom");
updateCornerRadius("bottomRight", "bottom");
return realCornerRadius;
};
function isCornerRadiusObject(cornerRadius) {
return (0, _isPlainObject.default)(cornerRadius);
}
const getCornerRadius = (cornerRadius, props) => {
const realCornerRadius = {
topLeft: 0,
topRight: 0,
bottomLeft: 0,
bottomRight: 0
};
if (!cornerRadius) {
return realCornerRadius;
}
if (isCornerRadiusObject(cornerRadius)) {
return getCornerRadiusFromObject(cornerRadius, props);
}
realCornerRadius.topLeft = _victoryCore.Helpers.evaluateProp(cornerRadius, props);
realCornerRadius.topRight = _victoryCore.Helpers.evaluateProp(cornerRadius, props);
return realCornerRadius;
};
exports.getCornerRadius = getCornerRadius;
const getStyle = function (style, props) {
if (style === void 0) {
style = {};
}
if (props.disableInlineStyles) {
return {};
}
const stroke = style.fill || "black";
const baseStyle = {
fill: "black",
stroke
};
return _victoryCore.Helpers.evaluateStyle(Object.assign(baseStyle, style), props);
};
exports.getStyle = getStyle;