This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #574 from eysteinn13/master
Export helper methods as named exports
- Loading branch information
Showing
19 changed files
with
1,104 additions
and
1,092 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.