victory-pie
Advanced tools
Comparing version 14.0.1 to 14.0.2
# VictoryPie Changelog | ||
## 14.0.2 (2018-03-27) | ||
-[168](https://github.com/FormidableLabs/victory-pie/pull/168) Refactor helper method exports | ||
## 14.0.1 (2018-02-05) | ||
@@ -4,0 +8,0 @@ |
@@ -10,157 +10,168 @@ import _omit from "lodash/omit"; | ||
export default { | ||
degreesToRadians: function (degrees) { | ||
return degrees * (Math.PI / 180); | ||
}, | ||
checkForValidText: function (text) { | ||
if (text === undefined || text === null) { | ||
return text; | ||
} else { | ||
return "" + text; | ||
} | ||
}, | ||
getSliceStyle: function (datum, index, calculatedValues) { | ||
var style = calculatedValues.style, | ||
colors = calculatedValues.colors; | ||
var degreesToRadians = function (degrees) { | ||
return degrees * (Math.PI / 180); | ||
}; | ||
var fill = this.getColor(style, colors, index); | ||
var dataStyles = _omit(datum, ["_x", "_y", "x", "y", "label"]); | ||
return _defaults({}, dataStyles, { fill: fill }, style.data); | ||
}, | ||
getBaseProps: function (props, fallbackProps) { | ||
props = Helpers.modifyProps(props, fallbackProps, "pie"); | ||
var calculatedValues = this.getCalculatedValues(props); | ||
var slices = calculatedValues.slices, | ||
style = calculatedValues.style, | ||
pathFunction = calculatedValues.pathFunction, | ||
data = calculatedValues.data, | ||
origin = calculatedValues.origin; | ||
var checkForValidText = function (text) { | ||
if (text === undefined || text === null) { | ||
return text; | ||
} else { | ||
return "" + text; | ||
} | ||
}; | ||
var childProps = { | ||
parent: { | ||
standalone: props.standalone, slices: slices, pathFunction: pathFunction, | ||
width: props.width, height: props.height, style: style.parent | ||
} | ||
}; | ||
var getColor = function (style, colors, index) { | ||
if (style && style.data && style.data.fill) { | ||
return style.data.fill; | ||
} | ||
return colors && colors[index % colors.length]; | ||
}; | ||
for (var index = 0, len = slices.length; index < len; index++) { | ||
var slice = slices[index]; | ||
var datum = data[index]; | ||
var eventKey = datum.eventKey || index; | ||
var dataProps = { | ||
index: index, slice: slice, pathFunction: pathFunction, datum: datum, data: data, origin: origin, | ||
style: this.getSliceStyle(datum, index, calculatedValues) | ||
}; | ||
var getRadius = function (props, padding) { | ||
return Math.min(props.width - padding.left - padding.right, props.height - padding.top - padding.bottom) / 2; | ||
}; | ||
childProps[eventKey] = { | ||
data: dataProps, | ||
labels: this.getLabelProps(props, dataProps, calculatedValues) | ||
}; | ||
var getSlices = function (props, data) { | ||
var layoutFunction = d3Shape.pie().sort(null).startAngle(degreesToRadians(props.startAngle)).endAngle(degreesToRadians(props.endAngle)).padAngle(degreesToRadians(props.padAngle)).value(function (datum) { | ||
return datum._y; | ||
}); | ||
return layoutFunction(data); | ||
}; | ||
var getCalculatedValues = function (props) { | ||
var theme = props.theme, | ||
colorScale = props.colorScale, | ||
width = props.width, | ||
height = props.height; | ||
var styleObject = theme && theme.pie && theme.pie.style ? theme.pie.style : {}; | ||
var style = Helpers.getStyles(props.style, styleObject, "auto", "100%"); | ||
var colors = Array.isArray(colorScale) ? colorScale : Style.getColorScale(colorScale); | ||
var padding = Helpers.getPadding(props); | ||
var radius = getRadius(props, padding); | ||
var offsetWidth = (radius + padding.left + (width - radius - padding.right)) / 2; | ||
var offsetHeight = (radius + padding.top + (height - radius - padding.bottom)) / 2; | ||
var origin = { x: offsetWidth, y: offsetHeight }; | ||
var data = Data.getData(props); | ||
var slices = getSlices(props, data); | ||
var pathFunction = d3Shape.arc().cornerRadius(props.cornerRadius).outerRadius(radius).innerRadius(props.innerRadius); | ||
return { style: style, colors: colors, padding: padding, radius: radius, data: data, slices: slices, pathFunction: pathFunction, origin: origin }; | ||
}; | ||
var getSliceStyle = function (datum, index, calculatedValues) { | ||
var style = calculatedValues.style, | ||
colors = calculatedValues.colors; | ||
var fill = getColor(style, colors, index); | ||
var dataStyles = _omit(datum, ["_x", "_y", "x", "y", "label"]); | ||
return _defaults({}, dataStyles, { fill: fill }, style.data); | ||
}; | ||
var getLabelText = function (props, datum, index) { | ||
var text = void 0; | ||
if (datum.label) { | ||
text = datum.label; | ||
} else if (Array.isArray(props.labels)) { | ||
text = props.labels[index]; | ||
} else { | ||
text = _isFunction(props.labels) ? props.labels(datum) : datum.xName || datum._x; | ||
} | ||
return checkForValidText(text); | ||
}; | ||
var getLabelPosition = function (radius, labelRadius, style) { | ||
var padding = style && style.padding || 0; | ||
var arcRadius = labelRadius || radius + padding; | ||
return d3Shape.arc().outerRadius(arcRadius).innerRadius(arcRadius); | ||
}; | ||
var getLabelOrientation = function (slice) { | ||
var radiansToDegrees = function (radians) { | ||
return radians * (180 / Math.PI); | ||
}; | ||
var start = radiansToDegrees(slice.startAngle); | ||
var end = radiansToDegrees(slice.endAngle); | ||
var degree = start + (end - start) / 2; | ||
if (degree < 45 || degree > 315) { | ||
return "top"; | ||
} else if (degree >= 45 && degree < 135) { | ||
return "right"; | ||
} else if (degree >= 135 && degree < 225) { | ||
return "bottom"; | ||
} else { | ||
return "left"; | ||
} | ||
}; | ||
var getTextAnchor = function (orientation) { | ||
if (orientation === "top" || orientation === "bottom") { | ||
return "middle"; | ||
} | ||
return orientation === "right" ? "start" : "end"; | ||
}; | ||
var getVerticalAnchor = function (orientation) { | ||
if (orientation === "left" || orientation === "right") { | ||
return "middle"; | ||
} | ||
return orientation === "bottom" ? "start" : "end"; | ||
}; | ||
var getLabelProps = function (props, dataProps, calculatedValues) { | ||
var index = dataProps.index, | ||
datum = dataProps.datum, | ||
data = dataProps.data, | ||
slice = dataProps.slice; | ||
var style = calculatedValues.style, | ||
radius = calculatedValues.radius, | ||
origin = calculatedValues.origin; | ||
var labelStyle = Helpers.evaluateStyle(_assign({ padding: 0 }, style.labels), datum, props.active); | ||
var labelRadius = Helpers.evaluateProp(props.labelRadius, datum); | ||
var labelPosition = getLabelPosition(radius, labelRadius, labelStyle); | ||
var position = labelPosition.centroid(slice); | ||
var orientation = getLabelOrientation(slice); | ||
return { | ||
index: index, datum: datum, data: data, slice: slice, orientation: orientation, | ||
style: labelStyle, | ||
x: Math.round(position[0]) + origin.x, | ||
y: Math.round(position[1]) + origin.y, | ||
text: getLabelText(props, datum, index), | ||
textAnchor: labelStyle.textAnchor || getTextAnchor(orientation), | ||
verticalAnchor: labelStyle.verticalAnchor || getVerticalAnchor(orientation), | ||
angle: labelStyle.angle | ||
}; | ||
}; | ||
export var getBaseProps = function (props, fallbackProps) { | ||
props = Helpers.modifyProps(props, fallbackProps, "pie"); | ||
var calculatedValues = getCalculatedValues(props); | ||
var slices = calculatedValues.slices, | ||
style = calculatedValues.style, | ||
pathFunction = calculatedValues.pathFunction, | ||
data = calculatedValues.data, | ||
origin = calculatedValues.origin; | ||
var childProps = { | ||
parent: { | ||
standalone: props.standalone, slices: slices, pathFunction: pathFunction, | ||
width: props.width, height: props.height, style: style.parent | ||
} | ||
return childProps; | ||
}, | ||
getLabelProps: function (props, dataProps, calculatedValues) { | ||
var index = dataProps.index, | ||
datum = dataProps.datum, | ||
data = dataProps.data, | ||
slice = dataProps.slice; | ||
var style = calculatedValues.style, | ||
radius = calculatedValues.radius, | ||
origin = calculatedValues.origin; | ||
}; | ||
var labelStyle = Helpers.evaluateStyle(_assign({ padding: 0 }, style.labels), datum, props.active); | ||
var labelRadius = Helpers.evaluateProp(props.labelRadius, datum); | ||
var labelPosition = this.getLabelPosition(radius, labelRadius, labelStyle); | ||
var position = labelPosition.centroid(slice); | ||
var orientation = this.getLabelOrientation(slice); | ||
return { | ||
index: index, datum: datum, data: data, slice: slice, orientation: orientation, | ||
style: labelStyle, | ||
x: Math.round(position[0]) + origin.x, | ||
y: Math.round(position[1]) + origin.y, | ||
text: this.getLabelText(props, datum, index), | ||
textAnchor: labelStyle.textAnchor || this.getTextAnchor(orientation), | ||
verticalAnchor: labelStyle.verticalAnchor || this.getVerticalAnchor(orientation), | ||
angle: labelStyle.angle | ||
for (var index = 0, len = slices.length; index < len; index++) { | ||
var slice = slices[index]; | ||
var datum = data[index]; | ||
var eventKey = datum.eventKey || index; | ||
var dataProps = { | ||
index: index, slice: slice, pathFunction: pathFunction, datum: datum, data: data, origin: origin, | ||
style: getSliceStyle(datum, index, calculatedValues) | ||
}; | ||
}, | ||
getCalculatedValues: function (props) { | ||
var theme = props.theme, | ||
colorScale = props.colorScale, | ||
width = props.width, | ||
height = props.height; | ||
var styleObject = theme && theme.pie && theme.pie.style ? theme.pie.style : {}; | ||
var style = Helpers.getStyles(props.style, styleObject, "auto", "100%"); | ||
var colors = Array.isArray(colorScale) ? colorScale : Style.getColorScale(colorScale); | ||
var padding = Helpers.getPadding(props); | ||
var radius = this.getRadius(props, padding); | ||
var offsetWidth = (radius + padding.left + (width - radius - padding.right)) / 2; | ||
var offsetHeight = (radius + padding.top + (height - radius - padding.bottom)) / 2; | ||
var origin = { x: offsetWidth, y: offsetHeight }; | ||
var data = Data.getData(props); | ||
var slices = this.getSlices(props, data); | ||
var pathFunction = d3Shape.arc().cornerRadius(props.cornerRadius).outerRadius(radius).innerRadius(props.innerRadius); | ||
return { style: style, colors: colors, padding: padding, radius: radius, data: data, slices: slices, pathFunction: pathFunction, origin: origin }; | ||
}, | ||
getColor: function (style, colors, index) { | ||
if (style && style.data && style.data.fill) { | ||
return style.data.fill; | ||
} | ||
return colors && colors[index % colors.length]; | ||
}, | ||
getRadius: function (props, padding) { | ||
return Math.min(props.width - padding.left - padding.right, props.height - padding.top - padding.bottom) / 2; | ||
}, | ||
getLabelPosition: function (radius, labelRadius, style) { | ||
var padding = style && style.padding || 0; | ||
var arcRadius = labelRadius || radius + padding; | ||
return d3Shape.arc().outerRadius(arcRadius).innerRadius(arcRadius); | ||
}, | ||
getLabelOrientation: function (slice) { | ||
var radiansToDegrees = function (radians) { | ||
return radians * (180 / Math.PI); | ||
childProps[eventKey] = { | ||
data: dataProps, | ||
labels: getLabelProps(props, dataProps, calculatedValues) | ||
}; | ||
var start = radiansToDegrees(slice.startAngle); | ||
var end = radiansToDegrees(slice.endAngle); | ||
var degree = start + (end - start) / 2; | ||
if (degree < 45 || degree > 315) { | ||
return "top"; | ||
} else if (degree >= 45 && degree < 135) { | ||
return "right"; | ||
} else if (degree >= 135 && degree < 225) { | ||
return "bottom"; | ||
} else { | ||
return "left"; | ||
} | ||
}, | ||
getTextAnchor: function (orientation) { | ||
if (orientation === "top" || orientation === "bottom") { | ||
return "middle"; | ||
} | ||
return orientation === "right" ? "start" : "end"; | ||
}, | ||
getVerticalAnchor: function (orientation) { | ||
if (orientation === "left" || orientation === "right") { | ||
return "middle"; | ||
} | ||
return orientation === "bottom" ? "start" : "end"; | ||
}, | ||
getLabelText: function (props, datum, index) { | ||
var text = void 0; | ||
if (datum.label) { | ||
text = datum.label; | ||
} else if (Array.isArray(props.labels)) { | ||
text = props.labels[index]; | ||
} else { | ||
text = _isFunction(props.labels) ? props.labels(datum) : datum.xName || datum._x; | ||
} | ||
return this.checkForValidText(text); | ||
}, | ||
getSlices: function (props, data) { | ||
var layoutFunction = d3Shape.pie().sort(null).startAngle(this.degreesToRadians(props.startAngle)).endAngle(this.degreesToRadians(props.endAngle)).padAngle(this.degreesToRadians(props.padAngle)).value(function (datum) { | ||
return datum._y; | ||
}); | ||
return layoutFunction(data); | ||
} | ||
return childProps; | ||
}; |
@@ -16,3 +16,3 @@ import _partialRight from "lodash/partialRight"; | ||
import { addEvents, Helpers, Data, PropTypes as CustomPropTypes, Slice, VictoryContainer, VictoryLabel, VictoryTheme } from "victory-core"; | ||
import PieHelpers from "./helper-methods"; | ||
import { getBaseProps } from "./helper-methods"; | ||
@@ -146,3 +146,3 @@ var fallbackProps = { | ||
}; | ||
VictoryPie.getBaseProps = _partialRight(PieHelpers.getBaseProps.bind(PieHelpers), fallbackProps); | ||
VictoryPie.getBaseProps = _partialRight(getBaseProps, fallbackProps); | ||
VictoryPie.getData = Data.getData.bind(Data); | ||
@@ -149,0 +149,0 @@ VictoryPie.expectedComponents = ["dataComponent", "labelComponent", "groupComponent", "containerComponent"]; |
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.getBaseProps = undefined; | ||
@@ -32,157 +33,168 @@ var _omit2 = require("lodash/omit"); | ||
/*eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2, 45, 135, 180, 225, 315] }]*/ | ||
exports.default = { | ||
degreesToRadians: function (degrees) { | ||
return degrees * (Math.PI / 180); | ||
}, | ||
checkForValidText: function (text) { | ||
if (text === undefined || text === null) { | ||
return text; | ||
} else { | ||
return "" + text; | ||
} | ||
}, | ||
getSliceStyle: function (datum, index, calculatedValues) { | ||
var style = calculatedValues.style, | ||
colors = calculatedValues.colors; | ||
var degreesToRadians = function (degrees) { | ||
return degrees * (Math.PI / 180); | ||
}; | ||
var fill = this.getColor(style, colors, index); | ||
var dataStyles = (0, _omit3.default)(datum, ["_x", "_y", "x", "y", "label"]); | ||
return (0, _defaults3.default)({}, dataStyles, { fill: fill }, style.data); | ||
}, | ||
getBaseProps: function (props, fallbackProps) { | ||
props = _victoryCore.Helpers.modifyProps(props, fallbackProps, "pie"); | ||
var calculatedValues = this.getCalculatedValues(props); | ||
var slices = calculatedValues.slices, | ||
style = calculatedValues.style, | ||
pathFunction = calculatedValues.pathFunction, | ||
data = calculatedValues.data, | ||
origin = calculatedValues.origin; | ||
var checkForValidText = function (text) { | ||
if (text === undefined || text === null) { | ||
return text; | ||
} else { | ||
return "" + text; | ||
} | ||
}; | ||
var childProps = { | ||
parent: { | ||
standalone: props.standalone, slices: slices, pathFunction: pathFunction, | ||
width: props.width, height: props.height, style: style.parent | ||
} | ||
}; | ||
var getColor = function (style, colors, index) { | ||
if (style && style.data && style.data.fill) { | ||
return style.data.fill; | ||
} | ||
return colors && colors[index % colors.length]; | ||
}; | ||
for (var index = 0, len = slices.length; index < len; index++) { | ||
var slice = slices[index]; | ||
var datum = data[index]; | ||
var eventKey = datum.eventKey || index; | ||
var dataProps = { | ||
index: index, slice: slice, pathFunction: pathFunction, datum: datum, data: data, origin: origin, | ||
style: this.getSliceStyle(datum, index, calculatedValues) | ||
}; | ||
var getRadius = function (props, padding) { | ||
return Math.min(props.width - padding.left - padding.right, props.height - padding.top - padding.bottom) / 2; | ||
}; | ||
childProps[eventKey] = { | ||
data: dataProps, | ||
labels: this.getLabelProps(props, dataProps, calculatedValues) | ||
}; | ||
var getSlices = function (props, data) { | ||
var layoutFunction = d3Shape.pie().sort(null).startAngle(degreesToRadians(props.startAngle)).endAngle(degreesToRadians(props.endAngle)).padAngle(degreesToRadians(props.padAngle)).value(function (datum) { | ||
return datum._y; | ||
}); | ||
return layoutFunction(data); | ||
}; | ||
var getCalculatedValues = function (props) { | ||
var theme = props.theme, | ||
colorScale = props.colorScale, | ||
width = props.width, | ||
height = props.height; | ||
var styleObject = theme && theme.pie && theme.pie.style ? theme.pie.style : {}; | ||
var style = _victoryCore.Helpers.getStyles(props.style, styleObject, "auto", "100%"); | ||
var colors = Array.isArray(colorScale) ? colorScale : _victoryCore.Style.getColorScale(colorScale); | ||
var padding = _victoryCore.Helpers.getPadding(props); | ||
var radius = getRadius(props, padding); | ||
var offsetWidth = (radius + padding.left + (width - radius - padding.right)) / 2; | ||
var offsetHeight = (radius + padding.top + (height - radius - padding.bottom)) / 2; | ||
var origin = { x: offsetWidth, y: offsetHeight }; | ||
var data = _victoryCore.Data.getData(props); | ||
var slices = getSlices(props, data); | ||
var pathFunction = d3Shape.arc().cornerRadius(props.cornerRadius).outerRadius(radius).innerRadius(props.innerRadius); | ||
return { style: style, colors: colors, padding: padding, radius: radius, data: data, slices: slices, pathFunction: pathFunction, origin: origin }; | ||
}; | ||
var getSliceStyle = function (datum, index, calculatedValues) { | ||
var style = calculatedValues.style, | ||
colors = calculatedValues.colors; | ||
var fill = getColor(style, colors, index); | ||
var dataStyles = (0, _omit3.default)(datum, ["_x", "_y", "x", "y", "label"]); | ||
return (0, _defaults3.default)({}, dataStyles, { fill: fill }, style.data); | ||
}; | ||
var getLabelText = function (props, datum, index) { | ||
var text = void 0; | ||
if (datum.label) { | ||
text = datum.label; | ||
} else if (Array.isArray(props.labels)) { | ||
text = props.labels[index]; | ||
} else { | ||
text = (0, _isFunction3.default)(props.labels) ? props.labels(datum) : datum.xName || datum._x; | ||
} | ||
return checkForValidText(text); | ||
}; | ||
var getLabelPosition = function (radius, labelRadius, style) { | ||
var padding = style && style.padding || 0; | ||
var arcRadius = labelRadius || radius + padding; | ||
return d3Shape.arc().outerRadius(arcRadius).innerRadius(arcRadius); | ||
}; | ||
var getLabelOrientation = function (slice) { | ||
var radiansToDegrees = function (radians) { | ||
return radians * (180 / Math.PI); | ||
}; | ||
var start = radiansToDegrees(slice.startAngle); | ||
var end = radiansToDegrees(slice.endAngle); | ||
var degree = start + (end - start) / 2; | ||
if (degree < 45 || degree > 315) { | ||
return "top"; | ||
} else if (degree >= 45 && degree < 135) { | ||
return "right"; | ||
} else if (degree >= 135 && degree < 225) { | ||
return "bottom"; | ||
} else { | ||
return "left"; | ||
} | ||
}; | ||
var getTextAnchor = function (orientation) { | ||
if (orientation === "top" || orientation === "bottom") { | ||
return "middle"; | ||
} | ||
return orientation === "right" ? "start" : "end"; | ||
}; | ||
var getVerticalAnchor = function (orientation) { | ||
if (orientation === "left" || orientation === "right") { | ||
return "middle"; | ||
} | ||
return orientation === "bottom" ? "start" : "end"; | ||
}; | ||
var getLabelProps = function (props, dataProps, calculatedValues) { | ||
var index = dataProps.index, | ||
datum = dataProps.datum, | ||
data = dataProps.data, | ||
slice = dataProps.slice; | ||
var style = calculatedValues.style, | ||
radius = calculatedValues.radius, | ||
origin = calculatedValues.origin; | ||
var labelStyle = _victoryCore.Helpers.evaluateStyle((0, _assign3.default)({ padding: 0 }, style.labels), datum, props.active); | ||
var labelRadius = _victoryCore.Helpers.evaluateProp(props.labelRadius, datum); | ||
var labelPosition = getLabelPosition(radius, labelRadius, labelStyle); | ||
var position = labelPosition.centroid(slice); | ||
var orientation = getLabelOrientation(slice); | ||
return { | ||
index: index, datum: datum, data: data, slice: slice, orientation: orientation, | ||
style: labelStyle, | ||
x: Math.round(position[0]) + origin.x, | ||
y: Math.round(position[1]) + origin.y, | ||
text: getLabelText(props, datum, index), | ||
textAnchor: labelStyle.textAnchor || getTextAnchor(orientation), | ||
verticalAnchor: labelStyle.verticalAnchor || getVerticalAnchor(orientation), | ||
angle: labelStyle.angle | ||
}; | ||
}; | ||
var getBaseProps = exports.getBaseProps = function (props, fallbackProps) { | ||
props = _victoryCore.Helpers.modifyProps(props, fallbackProps, "pie"); | ||
var calculatedValues = getCalculatedValues(props); | ||
var slices = calculatedValues.slices, | ||
style = calculatedValues.style, | ||
pathFunction = calculatedValues.pathFunction, | ||
data = calculatedValues.data, | ||
origin = calculatedValues.origin; | ||
var childProps = { | ||
parent: { | ||
standalone: props.standalone, slices: slices, pathFunction: pathFunction, | ||
width: props.width, height: props.height, style: style.parent | ||
} | ||
return childProps; | ||
}, | ||
getLabelProps: function (props, dataProps, calculatedValues) { | ||
var index = dataProps.index, | ||
datum = dataProps.datum, | ||
data = dataProps.data, | ||
slice = dataProps.slice; | ||
var style = calculatedValues.style, | ||
radius = calculatedValues.radius, | ||
origin = calculatedValues.origin; | ||
}; | ||
var labelStyle = _victoryCore.Helpers.evaluateStyle((0, _assign3.default)({ padding: 0 }, style.labels), datum, props.active); | ||
var labelRadius = _victoryCore.Helpers.evaluateProp(props.labelRadius, datum); | ||
var labelPosition = this.getLabelPosition(radius, labelRadius, labelStyle); | ||
var position = labelPosition.centroid(slice); | ||
var orientation = this.getLabelOrientation(slice); | ||
return { | ||
index: index, datum: datum, data: data, slice: slice, orientation: orientation, | ||
style: labelStyle, | ||
x: Math.round(position[0]) + origin.x, | ||
y: Math.round(position[1]) + origin.y, | ||
text: this.getLabelText(props, datum, index), | ||
textAnchor: labelStyle.textAnchor || this.getTextAnchor(orientation), | ||
verticalAnchor: labelStyle.verticalAnchor || this.getVerticalAnchor(orientation), | ||
angle: labelStyle.angle | ||
for (var index = 0, len = slices.length; index < len; index++) { | ||
var slice = slices[index]; | ||
var datum = data[index]; | ||
var eventKey = datum.eventKey || index; | ||
var dataProps = { | ||
index: index, slice: slice, pathFunction: pathFunction, datum: datum, data: data, origin: origin, | ||
style: getSliceStyle(datum, index, calculatedValues) | ||
}; | ||
}, | ||
getCalculatedValues: function (props) { | ||
var theme = props.theme, | ||
colorScale = props.colorScale, | ||
width = props.width, | ||
height = props.height; | ||
var styleObject = theme && theme.pie && theme.pie.style ? theme.pie.style : {}; | ||
var style = _victoryCore.Helpers.getStyles(props.style, styleObject, "auto", "100%"); | ||
var colors = Array.isArray(colorScale) ? colorScale : _victoryCore.Style.getColorScale(colorScale); | ||
var padding = _victoryCore.Helpers.getPadding(props); | ||
var radius = this.getRadius(props, padding); | ||
var offsetWidth = (radius + padding.left + (width - radius - padding.right)) / 2; | ||
var offsetHeight = (radius + padding.top + (height - radius - padding.bottom)) / 2; | ||
var origin = { x: offsetWidth, y: offsetHeight }; | ||
var data = _victoryCore.Data.getData(props); | ||
var slices = this.getSlices(props, data); | ||
var pathFunction = d3Shape.arc().cornerRadius(props.cornerRadius).outerRadius(radius).innerRadius(props.innerRadius); | ||
return { style: style, colors: colors, padding: padding, radius: radius, data: data, slices: slices, pathFunction: pathFunction, origin: origin }; | ||
}, | ||
getColor: function (style, colors, index) { | ||
if (style && style.data && style.data.fill) { | ||
return style.data.fill; | ||
} | ||
return colors && colors[index % colors.length]; | ||
}, | ||
getRadius: function (props, padding) { | ||
return Math.min(props.width - padding.left - padding.right, props.height - padding.top - padding.bottom) / 2; | ||
}, | ||
getLabelPosition: function (radius, labelRadius, style) { | ||
var padding = style && style.padding || 0; | ||
var arcRadius = labelRadius || radius + padding; | ||
return d3Shape.arc().outerRadius(arcRadius).innerRadius(arcRadius); | ||
}, | ||
getLabelOrientation: function (slice) { | ||
var radiansToDegrees = function (radians) { | ||
return radians * (180 / Math.PI); | ||
childProps[eventKey] = { | ||
data: dataProps, | ||
labels: getLabelProps(props, dataProps, calculatedValues) | ||
}; | ||
var start = radiansToDegrees(slice.startAngle); | ||
var end = radiansToDegrees(slice.endAngle); | ||
var degree = start + (end - start) / 2; | ||
if (degree < 45 || degree > 315) { | ||
return "top"; | ||
} else if (degree >= 45 && degree < 135) { | ||
return "right"; | ||
} else if (degree >= 135 && degree < 225) { | ||
return "bottom"; | ||
} else { | ||
return "left"; | ||
} | ||
}, | ||
getTextAnchor: function (orientation) { | ||
if (orientation === "top" || orientation === "bottom") { | ||
return "middle"; | ||
} | ||
return orientation === "right" ? "start" : "end"; | ||
}, | ||
getVerticalAnchor: function (orientation) { | ||
if (orientation === "left" || orientation === "right") { | ||
return "middle"; | ||
} | ||
return orientation === "bottom" ? "start" : "end"; | ||
}, | ||
getLabelText: function (props, datum, index) { | ||
var text = void 0; | ||
if (datum.label) { | ||
text = datum.label; | ||
} else if (Array.isArray(props.labels)) { | ||
text = props.labels[index]; | ||
} else { | ||
text = (0, _isFunction3.default)(props.labels) ? props.labels(datum) : datum.xName || datum._x; | ||
} | ||
return this.checkForValidText(text); | ||
}, | ||
getSlices: function (props, data) { | ||
var layoutFunction = d3Shape.pie().sort(null).startAngle(this.degreesToRadians(props.startAngle)).endAngle(this.degreesToRadians(props.endAngle)).padAngle(this.degreesToRadians(props.padAngle)).value(function (datum) { | ||
return datum._y; | ||
}); | ||
return layoutFunction(data); | ||
} | ||
return childProps; | ||
}; |
@@ -23,4 +23,2 @@ Object.defineProperty(exports, "__esModule", { | ||
var _helperMethods2 = _interopRequireDefault(_helperMethods); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -162,5 +160,5 @@ | ||
}; | ||
VictoryPie.getBaseProps = (0, _partialRight3.default)(_helperMethods2.default.getBaseProps.bind(_helperMethods2.default), fallbackProps); | ||
VictoryPie.getBaseProps = (0, _partialRight3.default)(_helperMethods.getBaseProps, fallbackProps); | ||
VictoryPie.getData = _victoryCore.Data.getData.bind(_victoryCore.Data); | ||
VictoryPie.expectedComponents = ["dataComponent", "labelComponent", "groupComponent", "containerComponent"]; | ||
exports.default = (0, _victoryCore.addEvents)(VictoryPie); |
{ | ||
"name": "victory-pie", | ||
"version": "14.0.1", | ||
"version": "14.0.2", | ||
"description": "D3 pie & donut chart component for React", | ||
@@ -34,4 +34,4 @@ "main": "lib/index.js", | ||
"d3-shape": "^1.0.0", | ||
"lodash": "^4.17.4", | ||
"victory-core": "^21.0.2" | ||
"lodash": "^4.17.5", | ||
"victory-core": "^21.1.0" | ||
}, | ||
@@ -38,0 +38,0 @@ "devDependencies": { |
@@ -7,166 +7,164 @@ /*eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2, 45, 135, 180, 225, 315] }]*/ | ||
export default { | ||
degreesToRadians(degrees) { | ||
return degrees * (Math.PI / 180); | ||
}, | ||
const degreesToRadians = (degrees) => { | ||
return degrees * (Math.PI / 180); | ||
}; | ||
checkForValidText(text) { | ||
if (text === undefined || text === null) { | ||
return text; | ||
} else { | ||
return `${text}`; | ||
} | ||
}, | ||
const checkForValidText = (text) => { | ||
if (text === undefined || text === null) { | ||
return text; | ||
} else { | ||
return `${text}`; | ||
} | ||
}; | ||
getSliceStyle(datum, index, calculatedValues) { | ||
const { style, colors } = calculatedValues; | ||
const fill = this.getColor(style, colors, index); | ||
const dataStyles = omit(datum, ["_x", "_y", "x", "y", "label"]); | ||
return defaults({}, dataStyles, { fill }, style.data); | ||
}, | ||
const getColor = (style, colors, index) => { | ||
if (style && style.data && style.data.fill) { | ||
return style.data.fill; | ||
} | ||
return colors && colors[index % colors.length]; | ||
}; | ||
getBaseProps(props, fallbackProps) { | ||
props = Helpers.modifyProps(props, fallbackProps, "pie"); | ||
const calculatedValues = this.getCalculatedValues(props); | ||
const { slices, style, pathFunction, data, origin } = calculatedValues; | ||
const childProps = { | ||
parent: { | ||
standalone: props.standalone, slices, pathFunction, | ||
width: props.width, height: props.height, style: style.parent | ||
} | ||
}; | ||
const getRadius = (props, padding) => { | ||
return Math.min( | ||
props.width - padding.left - padding.right, | ||
props.height - padding.top - padding.bottom | ||
) / 2; | ||
}; | ||
for (let index = 0, len = slices.length; index < len; index++) { | ||
const slice = slices[index]; | ||
const datum = data[index]; | ||
const eventKey = datum.eventKey || index; | ||
const dataProps = { | ||
index, slice, pathFunction, datum, data, origin, | ||
style: this.getSliceStyle(datum, index, calculatedValues) | ||
}; | ||
const getSlices = (props, data) => { | ||
const layoutFunction = d3Shape.pie() | ||
.sort(null) | ||
.startAngle(degreesToRadians(props.startAngle)) | ||
.endAngle(degreesToRadians(props.endAngle)) | ||
.padAngle(degreesToRadians(props.padAngle)) | ||
.value((datum) => { return datum._y; }); | ||
return layoutFunction(data); | ||
}; | ||
childProps[eventKey] = { | ||
data: dataProps, | ||
labels: this.getLabelProps(props, dataProps, calculatedValues) | ||
}; | ||
} | ||
return childProps; | ||
}, | ||
const getCalculatedValues = (props) => { | ||
const { theme, colorScale, width, height } = props; | ||
const styleObject = theme && theme.pie && theme.pie.style ? theme.pie.style : {}; | ||
const style = Helpers.getStyles(props.style, styleObject, "auto", "100%"); | ||
const colors = Array.isArray(colorScale) ? colorScale : Style.getColorScale(colorScale); | ||
const padding = Helpers.getPadding(props); | ||
const radius = getRadius(props, padding); | ||
const offsetWidth = ((radius + padding.left) + (width - radius - padding.right)) / 2; | ||
const offsetHeight = ((radius + padding.top) + (height - radius - padding.bottom)) / 2; | ||
const origin = { x: offsetWidth, y: offsetHeight }; | ||
const data = Data.getData(props); | ||
const slices = getSlices(props, data); | ||
const pathFunction = d3Shape.arc() | ||
.cornerRadius(props.cornerRadius) | ||
.outerRadius(radius) | ||
.innerRadius(props.innerRadius); | ||
return { style, colors, padding, radius, data, slices, pathFunction, origin }; | ||
}; | ||
getLabelProps(props, dataProps, calculatedValues) { | ||
const { index, datum, data, slice } = dataProps; | ||
const { style, radius, origin } = calculatedValues; | ||
const labelStyle = Helpers.evaluateStyle( | ||
assign({ padding: 0 }, style.labels), datum, props.active | ||
); | ||
const labelRadius = Helpers.evaluateProp(props.labelRadius, datum); | ||
const labelPosition = this.getLabelPosition(radius, labelRadius, labelStyle); | ||
const position = labelPosition.centroid(slice); | ||
const orientation = this.getLabelOrientation(slice); | ||
return { | ||
index, datum, data, slice, orientation, | ||
style: labelStyle, | ||
x: Math.round(position[0]) + origin.x, | ||
y: Math.round(position[1]) + origin.y, | ||
text: this.getLabelText(props, datum, index), | ||
textAnchor: labelStyle.textAnchor || this.getTextAnchor(orientation), | ||
verticalAnchor: labelStyle.verticalAnchor || this.getVerticalAnchor(orientation), | ||
angle: labelStyle.angle | ||
}; | ||
}, | ||
const getSliceStyle = (datum, index, calculatedValues) => { | ||
const { style, colors } = calculatedValues; | ||
const fill = getColor(style, colors, index); | ||
const dataStyles = omit(datum, ["_x", "_y", "x", "y", "label"]); | ||
return defaults({}, dataStyles, { fill }, style.data); | ||
}; | ||
getCalculatedValues(props) { | ||
const { theme, colorScale, width, height } = props; | ||
const styleObject = theme && theme.pie && theme.pie.style ? theme.pie.style : {}; | ||
const style = Helpers.getStyles(props.style, styleObject, "auto", "100%"); | ||
const colors = Array.isArray(colorScale) ? colorScale : Style.getColorScale(colorScale); | ||
const padding = Helpers.getPadding(props); | ||
const radius = this.getRadius(props, padding); | ||
const offsetWidth = ((radius + padding.left) + (width - radius - padding.right)) / 2; | ||
const offsetHeight = ((radius + padding.top) + (height - radius - padding.bottom)) / 2; | ||
const origin = { x: offsetWidth, y: offsetHeight }; | ||
const data = Data.getData(props); | ||
const slices = this.getSlices(props, data); | ||
const pathFunction = d3Shape.arc() | ||
.cornerRadius(props.cornerRadius) | ||
.outerRadius(radius) | ||
.innerRadius(props.innerRadius); | ||
return { style, colors, padding, radius, data, slices, pathFunction, origin }; | ||
}, | ||
const getLabelText = (props, datum, index) => { | ||
let text; | ||
if (datum.label) { | ||
text = datum.label; | ||
} else if (Array.isArray(props.labels)) { | ||
text = props.labels[index]; | ||
} else { | ||
text = isFunction(props.labels) ? props.labels(datum) : datum.xName || datum._x; | ||
} | ||
return checkForValidText(text); | ||
}; | ||
getColor(style, colors, index) { | ||
if (style && style.data && style.data.fill) { | ||
return style.data.fill; | ||
} | ||
return colors && colors[index % colors.length]; | ||
}, | ||
const getLabelPosition = (radius, labelRadius, style) => { | ||
const padding = style && style.padding || 0; | ||
const arcRadius = labelRadius || radius + padding; | ||
return d3Shape.arc() | ||
.outerRadius(arcRadius) | ||
.innerRadius(arcRadius); | ||
}; | ||
getRadius(props, padding) { | ||
return Math.min( | ||
props.width - padding.left - padding.right, | ||
props.height - padding.top - padding.bottom | ||
) / 2; | ||
}, | ||
const getLabelOrientation = (slice) => { | ||
const radiansToDegrees = (radians) => { | ||
return radians * (180 / Math.PI); | ||
}; | ||
const start = radiansToDegrees(slice.startAngle); | ||
const end = radiansToDegrees(slice.endAngle); | ||
const degree = start + (end - start) / 2; | ||
if (degree < 45 || degree > 315) { | ||
return "top"; | ||
} else if (degree >= 45 && degree < 135) { | ||
return "right"; | ||
} else if (degree >= 135 && degree < 225) { | ||
return "bottom"; | ||
} else { | ||
return "left"; | ||
} | ||
}; | ||
getLabelPosition(radius, labelRadius, style) { | ||
const padding = style && style.padding || 0; | ||
const arcRadius = labelRadius || radius + padding; | ||
return d3Shape.arc() | ||
.outerRadius(arcRadius) | ||
.innerRadius(arcRadius); | ||
}, | ||
const getTextAnchor = (orientation) => { | ||
if (orientation === "top" || orientation === "bottom") { | ||
return "middle"; | ||
} | ||
return orientation === "right" ? "start" : "end"; | ||
}; | ||
getLabelOrientation(slice) { | ||
const radiansToDegrees = (radians) => { | ||
return radians * (180 / Math.PI); | ||
}; | ||
const start = radiansToDegrees(slice.startAngle); | ||
const end = radiansToDegrees(slice.endAngle); | ||
const degree = start + (end - start) / 2; | ||
if (degree < 45 || degree > 315) { | ||
return "top"; | ||
} else if (degree >= 45 && degree < 135) { | ||
return "right"; | ||
} else if (degree >= 135 && degree < 225) { | ||
return "bottom"; | ||
} else { | ||
return "left"; | ||
} | ||
}, | ||
const getVerticalAnchor = (orientation) => { | ||
if (orientation === "left" || orientation === "right") { | ||
return "middle"; | ||
} | ||
return orientation === "bottom" ? "start" : "end"; | ||
}; | ||
getTextAnchor(orientation) { | ||
if (orientation === "top" || orientation === "bottom") { | ||
return "middle"; | ||
} | ||
return orientation === "right" ? "start" : "end"; | ||
}, | ||
const getLabelProps = (props, dataProps, calculatedValues) => { | ||
const { index, datum, data, slice } = dataProps; | ||
const { style, radius, origin } = calculatedValues; | ||
const labelStyle = Helpers.evaluateStyle( | ||
assign({ padding: 0 }, style.labels), datum, props.active | ||
); | ||
const labelRadius = Helpers.evaluateProp(props.labelRadius, datum); | ||
const labelPosition = getLabelPosition(radius, labelRadius, labelStyle); | ||
const position = labelPosition.centroid(slice); | ||
const orientation = getLabelOrientation(slice); | ||
return { | ||
index, datum, data, slice, orientation, | ||
style: labelStyle, | ||
x: Math.round(position[0]) + origin.x, | ||
y: Math.round(position[1]) + origin.y, | ||
text: getLabelText(props, datum, index), | ||
textAnchor: labelStyle.textAnchor || getTextAnchor(orientation), | ||
verticalAnchor: labelStyle.verticalAnchor || getVerticalAnchor(orientation), | ||
angle: labelStyle.angle | ||
}; | ||
}; | ||
getVerticalAnchor(orientation) { | ||
if (orientation === "left" || orientation === "right") { | ||
return "middle"; | ||
export const getBaseProps = (props, fallbackProps) => { | ||
props = Helpers.modifyProps(props, fallbackProps, "pie"); | ||
const calculatedValues = getCalculatedValues(props); | ||
const { slices, style, pathFunction, data, origin } = calculatedValues; | ||
const childProps = { | ||
parent: { | ||
standalone: props.standalone, slices, pathFunction, | ||
width: props.width, height: props.height, style: style.parent | ||
} | ||
return orientation === "bottom" ? "start" : "end"; | ||
}, | ||
}; | ||
getLabelText(props, datum, index) { | ||
let text; | ||
if (datum.label) { | ||
text = datum.label; | ||
} else if (Array.isArray(props.labels)) { | ||
text = props.labels[index]; | ||
} else { | ||
text = isFunction(props.labels) ? props.labels(datum) : datum.xName || datum._x; | ||
} | ||
return this.checkForValidText(text); | ||
}, | ||
for (let index = 0, len = slices.length; index < len; index++) { | ||
const slice = slices[index]; | ||
const datum = data[index]; | ||
const eventKey = datum.eventKey || index; | ||
const dataProps = { | ||
index, slice, pathFunction, datum, data, origin, | ||
style: getSliceStyle(datum, index, calculatedValues) | ||
}; | ||
getSlices(props, data) { | ||
const layoutFunction = d3Shape.pie() | ||
.sort(null) | ||
.startAngle(this.degreesToRadians(props.startAngle)) | ||
.endAngle(this.degreesToRadians(props.endAngle)) | ||
.padAngle(this.degreesToRadians(props.padAngle)) | ||
.value((datum) => { return datum._y; }); | ||
return layoutFunction(data); | ||
childProps[eventKey] = { | ||
data: dataProps, | ||
labels: getLabelProps(props, dataProps, calculatedValues) | ||
}; | ||
} | ||
return childProps; | ||
}; |
@@ -9,3 +9,3 @@ /*eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2] }]*/ | ||
} from "victory-core"; | ||
import PieHelpers from "./helper-methods"; | ||
import { getBaseProps } from "./helper-methods"; | ||
@@ -164,3 +164,3 @@ const fallbackProps = { | ||
static getBaseProps = partialRight(PieHelpers.getBaseProps.bind(PieHelpers), fallbackProps); | ||
static getBaseProps = partialRight(getBaseProps, fallbackProps); | ||
static getData = Data.getData.bind(Data); | ||
@@ -167,0 +167,0 @@ static expectedComponents = [ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1418587
24927
Updatedlodash@^4.17.5
Updatedvictory-core@^21.1.0