Skip to content
This repository has been archived by the owner on Feb 19, 2022. It is now read-only.

Export helper methods as named exports #574

Merged
merged 2 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 59 additions & 60 deletions src/components/victory-area/helper-methods.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,68 @@
import { assign } from "lodash";
import { Helpers, LabelHelpers, Data, Domain, Scale } from "victory-core";

export default {
const getDataWithBaseline = (props, scale) => {
let data = Data.getData(props);

getBaseProps(props, fallbackProps) {
const modifiedProps = Helpers.modifyProps(props, fallbackProps, "area");
props = assign({}, modifiedProps, this.getCalculatedValues(modifiedProps));
const {
data, domain, events, groupComponent, height, interpolation, origin, padding, polar,
scale, sharedEvents, standalone, style, theme, width
} = props;
const initialChildProps = {
parent: {
style: style.parent, width, height, scale, data, domain,
standalone, theme, polar, origin, padding
},
all: {
data: { polar, origin, scale, data, interpolation, groupComponent, style: style.data }
}
};
return data.reduce((childProps, datum, index) => {
const text = LabelHelpers.getText(props, datum, index);
if (text !== undefined && text !== null || events || sharedEvents) {
const eventKey = datum.eventKey || index;
childProps[eventKey] = { labels: LabelHelpers.getProps(props, index) };
}
return childProps;
}, initialChildProps);
},
if (data.length < 2) {
data = [];
}
const defaultMin = Scale.getType(scale.y) === "log" ? 1 / Number.MAX_SAFE_INTEGER : 0;
const domainY = scale.y.domain();
const minY = Math.min(...domainY) > 0 ? Math.min(...domainY) : defaultMin;

getCalculatedValues(props) {
const { theme, polar } = props;
const defaultStyles = theme && theme.area && theme.area.style ? theme.area.style : {};
const style = Helpers.getStyles(props.style, defaultStyles);
const range = {
x: Helpers.getRange(props, "x"),
y: Helpers.getRange(props, "y")
};
const domain = {
x: Domain.getDomainWithZero(props, "x"),
y: Domain.getDomainWithZero(props, "y")
};
const scale = {
x: Scale.getBaseScale(props, "x").domain(domain.x).range(range.x),
y: Scale.getBaseScale(props, "y").domain(domain.y).range(range.y)
};
const origin = polar ? props.origin || Helpers.getPolarOrigin(props) : undefined;
const data = this.getDataWithBaseline(props, scale);
return { style, data, scale, domain, origin };
},
return data.map((datum) => {
const _y1 = datum._y1 !== undefined ? datum._y1 : datum._y;
const _y0 = datum._y0 !== undefined ? datum._y0 : minY;
return assign({}, datum, { _y0, _y1 });
});
};

getDataWithBaseline(props, scale) {
let data = Data.getData(props);
const getCalculatedValues = (props) => {
const { theme, polar } = props;
const defaultStyles = theme && theme.area && theme.area.style ? theme.area.style : {};
const style = Helpers.getStyles(props.style, defaultStyles);
const range = {
x: Helpers.getRange(props, "x"),
y: Helpers.getRange(props, "y")
};
const domain = {
x: Domain.getDomainWithZero(props, "x"),
y: Domain.getDomainWithZero(props, "y")
};
const scale = {
x: Scale.getBaseScale(props, "x").domain(domain.x).range(range.x),
y: Scale.getBaseScale(props, "y").domain(domain.y).range(range.y)
};
const origin = polar ? props.origin || Helpers.getPolarOrigin(props) : undefined;
const data = getDataWithBaseline(props, scale);
return { style, data, scale, domain, origin };
};

if (data.length < 2) {
data = [];
const getBaseProps = (props, fallbackProps) => {
const modifiedProps = Helpers.modifyProps(props, fallbackProps, "area");
props = assign({}, modifiedProps, getCalculatedValues(modifiedProps));
const {
data, domain, events, groupComponent, height, interpolation, origin, padding, polar,
scale, sharedEvents, standalone, style, theme, width
} = props;
const initialChildProps = {
parent: {
style: style.parent, width, height, scale, data, domain,
standalone, theme, polar, origin, padding
},
all: {
data: { polar, origin, scale, data, interpolation, groupComponent, style: style.data }
}
const defaultMin = Scale.getType(scale.y) === "log" ? 1 / Number.MAX_SAFE_INTEGER : 0;
const domainY = scale.y.domain();
const minY = Math.min(...domainY) > 0 ? Math.min(...domainY) : defaultMin;

return data.map((datum) => {
const _y1 = datum._y1 !== undefined ? datum._y1 : datum._y;
const _y0 = datum._y0 !== undefined ? datum._y0 : minY;
return assign({}, datum, { _y0, _y1 });
});
}
};
return data.reduce((childProps, datum, index) => {
const text = LabelHelpers.getText(props, datum, index);
if (text !== undefined && text !== null || events || sharedEvents) {
const eventKey = datum.eventKey || index;
childProps[eventKey] = { labels: LabelHelpers.getProps(props, index) };
}
return childProps;
}, initialChildProps);
};

export { getBaseProps, getDataWithBaseline };
4 changes: 2 additions & 2 deletions src/components/victory-area/victory-area.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { partialRight } from "lodash";
import PropTypes from "prop-types";
import React from "react";
import AreaHelpers from "./helper-methods";
import { getBaseProps } from "./helper-methods";
import {
PropTypes as CustomPropTypes, Helpers, VictoryLabel, VictoryContainer,
DefaultTransitions, Area, VictoryClipContainer, addEvents, VictoryTheme, Data, Domain
Expand Down Expand Up @@ -58,7 +58,7 @@ class VictoryArea extends React.Component {
static defaultPolarTransitions = DefaultTransitions.continuousPolarTransitions();
static getDomain = Domain.getDomainWithZero.bind(Domain);
static getData = Data.getData.bind(Data);
static getBaseProps = partialRight(AreaHelpers.getBaseProps.bind(AreaHelpers), fallbackProps);
static getBaseProps = partialRight(getBaseProps, fallbackProps);
static expectedComponents = [
"dataComponent", "labelComponent", "groupComponent", "containerComponent"
];
Expand Down
141 changes: 70 additions & 71 deletions src/components/victory-bar/helper-methods.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,80 @@
import { assign, defaults, keys, omit, isNaN } from "lodash";
import { Helpers, LabelHelpers, Data, Domain, Scale } from "victory-core";

export default {

getBarPosition(props, datum) {
const getDefaultMin = (axis) => {
const defaultMin = Scale.getType(props.scale[axis]) === "log" ?
1 / Number.MAX_SAFE_INTEGER : 0;
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 Helpers.scalePoint(props, assign({}, datum, { _y0, _x0 }));
},
const getBarPosition = (props, datum) => {
const getDefaultMin = (axis) => {
const defaultMin = Scale.getType(props.scale[axis]) === "log" ?
1 / Number.MAX_SAFE_INTEGER : 0;
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 Helpers.scalePoint(props, assign({}, datum, { _y0, _x0 }));
};

getBarStyle(datum, baseStyle) {
const numKeys = keys(datum).filter((k) => isNaN(k));
const omitKeys = [
"x", "y", "y0", "_x", "_y", "_y0", "name", "label", "eventKey"
];
const styleData = omit(datum, [...omitKeys, ...numKeys]);
return defaults({}, styleData, baseStyle);
},
const getBarStyle = (datum, baseStyle) => {
const numKeys = keys(datum).filter((k) => isNaN(k));
const omitKeys = [
"x", "y", "y0", "_x", "_y", "_y0", "name", "label", "eventKey"
];
const styleData = omit(datum, [...omitKeys, ...numKeys]);
return defaults({}, styleData, baseStyle);
};

getCalculatedValues(props) {
const { theme, horizontal, polar } = props;
const defaultStyles = theme && theme.bar && theme.bar.style ? theme.bar.style : {};
const style = Helpers.getStyles(props.style, defaultStyles);
const data = Data.getData(props);
const range = {
x: Helpers.getRange(props, "x"),
y: Helpers.getRange(props, "y")
};
const domain = {
x: Domain.getDomainWithZero(props, "x"),
y: Domain.getDomainWithZero(props, "y")
};
const xScale = Scale.getBaseScale(props, "x").domain(domain.x).range(range.x);
const yScale = Scale.getBaseScale(props, "y").domain(domain.y).range(range.y);
const scale = {
x: horizontal ? yScale : xScale,
y: horizontal ? xScale : yScale
};
const origin = polar ? props.origin || Helpers.getPolarOrigin(props) : undefined;
return { style, data, scale, domain, origin };
},
const getCalculatedValues = (props) => {
const { theme, horizontal, polar } = props;
const defaultStyles = theme && theme.bar && theme.bar.style ? theme.bar.style : {};
const style = Helpers.getStyles(props.style, defaultStyles);
const data = Data.getData(props);
const range = {
x: Helpers.getRange(props, "x"),
y: Helpers.getRange(props, "y")
};
const domain = {
x: Domain.getDomainWithZero(props, "x"),
y: Domain.getDomainWithZero(props, "y")
};
const xScale = Scale.getBaseScale(props, "x").domain(domain.x).range(range.x);
const yScale = Scale.getBaseScale(props, "y").domain(domain.y).range(range.y);
const scale = {
x: horizontal ? yScale : xScale,
y: horizontal ? xScale : yScale
};
const origin = polar ? props.origin || Helpers.getPolarOrigin(props) : undefined;
return { style, data, scale, domain, origin };
};

getBaseProps(props, fallbackProps) {
const modifiedProps = Helpers.modifyProps(props, fallbackProps, "bar");
props = assign({}, modifiedProps, this.getCalculatedValues(modifiedProps));
const {
alignment, barRatio, cornerRadius, data, domain, events, height, horizontal, origin, padding,
polar, scale, sharedEvents, standalone, style, theme, width
} = props;
const initialChildProps = { parent: {
domain, scale, width, height, data, standalone,
theme, polar, origin, padding, style: style.parent
} };
const getBaseProps = (props, fallbackProps) => {
const modifiedProps = Helpers.modifyProps(props, fallbackProps, "bar");
props = assign({}, modifiedProps, getCalculatedValues(modifiedProps));
const {
alignment, barRatio, cornerRadius, data, domain, events, height, horizontal, origin, padding,
polar, scale, sharedEvents, standalone, style, theme, width
} = props;
const initialChildProps = { parent: {
domain, scale, width, height, data, standalone,
theme, polar, origin, padding, style: style.parent
} };

return data.reduce((childProps, datum, index) => {
const eventKey = datum.eventKey || index;
const { x, y, y0, x0 } = this.getBarPosition(props, datum);
const barStyle = this.getBarStyle(datum, style.data);
const dataProps = {
alignment, barRatio, cornerRadius, data, datum, horizontal, index, padding, polar, origin,
scale, style: barStyle, width, height, x, y, y0, x0
};
return data.reduce((childProps, datum, index) => {
const eventKey = datum.eventKey || index;
const { x, y, y0, x0 } = getBarPosition(props, datum);
const barStyle = getBarStyle(datum, style.data);
const dataProps = {
alignment, barRatio, cornerRadius, data, datum, horizontal, index, padding, polar, origin,
scale, style: barStyle, width, height, x, y, y0, x0
};

childProps[eventKey] = {
data: dataProps
};
childProps[eventKey] = {
data: dataProps
};

const text = LabelHelpers.getText(props, datum, index);
if (text !== undefined && text !== null || events || sharedEvents) {
childProps[eventKey].labels = LabelHelpers.getProps(props, index);
}
return childProps;
}, initialChildProps);
}
const text = LabelHelpers.getText(props, datum, index);
if (text !== undefined && text !== null || events || sharedEvents) {
childProps[eventKey].labels = LabelHelpers.getProps(props, index);
}
return childProps;
}, initialChildProps);
};

export { getBaseProps };
4 changes: 2 additions & 2 deletions src/components/victory-bar/victory-bar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PropTypes from "prop-types";
import React from "react";
import BarHelpers from "./helper-methods";
import { getBaseProps } from "./helper-methods";
import { partialRight } from "lodash";
import {
Helpers, VictoryLabel, VictoryContainer, VictoryTheme, Bar, addEvents, Data, Domain
Expand Down Expand Up @@ -68,7 +68,7 @@ class VictoryBar extends React.Component {

static getDomain = Domain.getDomainWithZero.bind(Domain);
static getData = Data.getData.bind(Data);
static getBaseProps = partialRight(BarHelpers.getBaseProps.bind(BarHelpers), fallbackProps);
static getBaseProps = partialRight(getBaseProps, fallbackProps);
static expectedComponents = [
"dataComponent", "labelComponent", "groupComponent", "containerComponent"
];
Expand Down
Loading