UNPKG

victory-chart

Version:
209 lines (208 loc) 7.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getBackgroundWithProps = getBackgroundWithProps; exports.getCalculatedProps = getCalculatedProps; exports.getChildComponents = void 0; exports.getChildren = getChildren; var _react = _interopRequireDefault(require("react")); var _victoryCore = require("victory-core"); var _defaults = _interopRequireDefault(require("lodash/defaults")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const fallbackProps = { width: 450, height: 300, padding: 50 }; function getAxisProps(child, props, calculatedProps) { const { domain, scale, stringMap, categories, horizontal } = calculatedProps; return { stringMap, horizontal, categories, startAngle: props.startAngle, endAngle: props.endAngle, innerRadius: props.innerRadius, domain, scale }; } function getBackgroundWithProps(props, calculatedProps) { const backgroundElement = props.backgroundComponent; const height = props.polar ? calculatedProps.range.y[1] : calculatedProps.range.y[0] - calculatedProps.range.y[1]; const width = calculatedProps.range.x[1] - calculatedProps.range.x[0]; const xScale = props.horizontal ? calculatedProps.scale.y.range()[0] : calculatedProps.scale.x.range()[0]; const yScale = props.horizontal ? calculatedProps.scale.x.range()[1] : calculatedProps.scale.y.range()[1]; const xCoordinate = props.polar ? calculatedProps.origin.x : xScale; const yCoordinate = props.polar ? calculatedProps.origin.y : yScale; const parentName = props.name || "chart"; const backgroundProps = { height, polar: props.polar, scale: calculatedProps.scale, style: props.style.background, x: xCoordinate, y: yCoordinate, key: `${parentName}-background`, width }; return /*#__PURE__*/_react.default.cloneElement(backgroundElement, (0, _defaults.default)({}, backgroundElement.props, backgroundProps)); } function getChildProps(child, props, calculatedProps) { const axisChild = _victoryCore.Axis.findAxisComponents([child]); if (axisChild.length > 0) { return getAxisProps(axisChild[0], props, calculatedProps); } const { categories, domain, range, scale, stringMap, horizontal } = calculatedProps; return { categories, domain, range, scale, stringMap, horizontal }; } function getStyles(props) { const styleProps = props.style && props.style.parent; return { parent: (0, _defaults.default)({}, styleProps, { height: "100%", width: "100%", userSelect: "none" }) }; } function getCalculatedProps(initialProps, childComponents) { const style = getStyles(initialProps); const props = _victoryCore.Helpers.modifyProps(initialProps, fallbackProps, "chart"); const { horizontal, polar } = props; const allStrings = _victoryCore.Wrapper.getStringsFromChildren(props, childComponents); const categories = _victoryCore.Wrapper.getCategories(props, childComponents, allStrings); const stringMap = createStringMap(props, childComponents, allStrings); const domain = { x: getDomain(Object.assign({}, props, { categories }), "x", childComponents), y: getDomain(Object.assign({}, props, { categories }), "y", childComponents) }; const range = { x: _victoryCore.Helpers.getRange(props, "x"), y: _victoryCore.Helpers.getRange(props, "y") }; const baseScale = { x: _victoryCore.Scale.getScaleFromProps(props, "x") || _victoryCore.Wrapper.getScale(props, "x"), y: _victoryCore.Scale.getScaleFromProps(props, "y") || _victoryCore.Wrapper.getScale(props, "y") }; const scale = { x: baseScale.x.domain(domain.x).range(horizontal ? range.y : range.x), y: baseScale.y.domain(domain.y).range(horizontal ? range.x : range.y) }; const origin = polar ? _victoryCore.Helpers.getPolarOrigin(props) : _victoryCore.Axis.getOrigin(domain); const padding = _victoryCore.Helpers.getPadding(props.padding); return { categories, domain, range, horizontal, scale, stringMap, style, origin, padding }; } function getChildren(props, childComponents, calculatedProps) { const children = childComponents || getChildComponents(props); const newCalculatedProps = calculatedProps || getCalculatedProps(props, children); const baseStyle = newCalculatedProps.style.parent; const { height, polar, theme, width } = props; const { origin, horizontal } = newCalculatedProps; const parentName = props.name || "chart"; return children.filter(_react.default.isValidElement).map((child, index) => { const role = child.type && child.type.role; const style = Array.isArray(child.props.style) ? child.props.style : (0, _defaults.default)({}, child.props.style, { parent: baseStyle }); const childProps = getChildProps(child, props, newCalculatedProps); const name = child.props.name || `${parentName}-${role}-${index}`; const newProps = (0, _defaults.default)({ horizontal, height, polar, theme, width, style, name, origin: polar ? origin : undefined, padding: newCalculatedProps.padding, key: `${name}-key-${index}`, standalone: false }, childProps); return /*#__PURE__*/_react.default.cloneElement(child, newProps); }); } const getChildComponents = (props, defaultAxes) => { let childComponents = _react.default.Children.toArray(props.children); if (childComponents.length === 0) { childComponents.push(defaultAxes.independent, defaultAxes.dependent); } else { const axisComponents = { dependent: _victoryCore.Axis.getAxisComponentsWithParent(childComponents, "dependent"), independent: _victoryCore.Axis.getAxisComponentsWithParent(childComponents, "independent") }; if (axisComponents.dependent.length === 0 && axisComponents.independent.length === 0) { childComponents = props.prependDefaultAxes ? [defaultAxes.independent, defaultAxes.dependent].concat(childComponents) : childComponents.concat([defaultAxes.independent, defaultAxes.dependent]); } } return childComponents; }; exports.getChildComponents = getChildComponents; const getDomain = (props, axis, childComponents) => { const children = childComponents || _react.default.Children.toArray(props.children); const domain = _victoryCore.Wrapper.getDomain(props, axis, children); const axisComponent = _victoryCore.Axis.getAxisComponent(children, axis); const invertDomain = axisComponent && axisComponent.props && axisComponent.props.invertAxis; return invertDomain ? domain.concat().reverse() : domain; }; const createStringMap = (props, childComponents, allStrings) => { const x = !allStrings.x || allStrings.x.length === 0 ? null : allStrings.x.reduce((memo, string, index) => { memo[string] = index + 1; return memo; }, {}); const y = !allStrings.y || allStrings.y.length === 0 ? null : allStrings.y.reduce((memo, string, index) => { memo[string] = index + 1; return memo; }, {}); return { x, y }; };