victory-pie 11.2.0 → 11.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/dist/victory-pie.js +25069 -20280
- package/dist/victory-pie.min.js +1 -9
- package/es/components/helper-methods.js +165 -0
- package/es/components/victory-pie.js +140 -0
- package/es/index.js +2 -0
- package/lib/components/helper-methods.js +187 -171
- package/lib/components/victory-pie.js +150 -161
- package/lib/index.js +12 -2
- package/package.json +6 -4
- package/src/components/helper-methods.js +6 -7
- package/src/components/victory-pie.js +1 -1
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import _omit from "lodash/omit";
|
|
2
|
+
import _isFunction from "lodash/isFunction";
|
|
3
|
+
import _defaults from "lodash/defaults";
|
|
4
|
+
import _assign from "lodash/assign"; /*eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2, 45, 135, 180, 225, 315] }]*/
|
|
5
|
+
|
|
6
|
+
import * as d3Shape from "d3-shape";
|
|
7
|
+
|
|
8
|
+
import { Helpers, Data, Style } from "victory-core";
|
|
9
|
+
|
|
10
|
+
export default {
|
|
11
|
+
degreesToRadians: function (degrees) {
|
|
12
|
+
return degrees * (Math.PI / 180);
|
|
13
|
+
},
|
|
14
|
+
checkForValidText: function (text) {
|
|
15
|
+
if (text === undefined || text === null) {
|
|
16
|
+
return text;
|
|
17
|
+
} else {
|
|
18
|
+
return "" + text;
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
getSliceStyle: function (datum, index, calculatedValues) {
|
|
22
|
+
var style = calculatedValues.style,
|
|
23
|
+
colors = calculatedValues.colors;
|
|
24
|
+
|
|
25
|
+
var fill = this.getColor(style, colors, index);
|
|
26
|
+
var dataStyles = _omit(datum, ["_x", "_y", "x", "y", "label"]);
|
|
27
|
+
return _defaults({}, dataStyles, { fill: fill }, style.data);
|
|
28
|
+
},
|
|
29
|
+
getBaseProps: function (props, fallbackProps) {
|
|
30
|
+
props = Helpers.modifyProps(props, fallbackProps, "pie");
|
|
31
|
+
var calculatedValues = this.getCalculatedValues(props);
|
|
32
|
+
var slices = calculatedValues.slices,
|
|
33
|
+
style = calculatedValues.style,
|
|
34
|
+
pathFunction = calculatedValues.pathFunction,
|
|
35
|
+
data = calculatedValues.data,
|
|
36
|
+
origin = calculatedValues.origin;
|
|
37
|
+
|
|
38
|
+
var childProps = {
|
|
39
|
+
parent: {
|
|
40
|
+
standalone: props.standalone, slices: slices, pathFunction: pathFunction,
|
|
41
|
+
width: props.width, height: props.height, style: style.parent
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
for (var index = 0, len = slices.length; index < len; index++) {
|
|
46
|
+
var slice = slices[index];
|
|
47
|
+
var datum = data[index];
|
|
48
|
+
var eventKey = datum.eventKey || index;
|
|
49
|
+
var dataProps = {
|
|
50
|
+
index: index, slice: slice, pathFunction: pathFunction, datum: datum, data: data, origin: origin,
|
|
51
|
+
style: this.getSliceStyle(datum, index, calculatedValues)
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
childProps[eventKey] = {
|
|
55
|
+
data: dataProps,
|
|
56
|
+
labels: this.getLabelProps(props, dataProps, calculatedValues)
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return childProps;
|
|
60
|
+
},
|
|
61
|
+
getLabelProps: function (props, dataProps, calculatedValues) {
|
|
62
|
+
var index = dataProps.index,
|
|
63
|
+
datum = dataProps.datum,
|
|
64
|
+
data = dataProps.data,
|
|
65
|
+
slice = dataProps.slice;
|
|
66
|
+
var style = calculatedValues.style,
|
|
67
|
+
radius = calculatedValues.radius,
|
|
68
|
+
origin = calculatedValues.origin;
|
|
69
|
+
|
|
70
|
+
var labelStyle = Helpers.evaluateStyle(_assign({ padding: 0 }, style.labels), datum, props.active);
|
|
71
|
+
var labelRadius = Helpers.evaluateProp(props.labelRadius, datum);
|
|
72
|
+
var labelPosition = this.getLabelPosition(radius, labelRadius, labelStyle);
|
|
73
|
+
var position = labelPosition.centroid(slice);
|
|
74
|
+
var orientation = this.getLabelOrientation(slice);
|
|
75
|
+
return {
|
|
76
|
+
index: index, datum: datum, data: data, slice: slice, orientation: orientation,
|
|
77
|
+
style: labelStyle,
|
|
78
|
+
x: Math.round(position[0]) + origin.x,
|
|
79
|
+
y: Math.round(position[1]) + origin.y,
|
|
80
|
+
text: this.getLabelText(props, datum, index),
|
|
81
|
+
textAnchor: labelStyle.textAnchor || this.getTextAnchor(orientation),
|
|
82
|
+
verticalAnchor: labelStyle.verticalAnchor || this.getVerticalAnchor(orientation),
|
|
83
|
+
angle: labelStyle.angle
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
getCalculatedValues: function (props) {
|
|
87
|
+
var theme = props.theme,
|
|
88
|
+
colorScale = props.colorScale,
|
|
89
|
+
width = props.width,
|
|
90
|
+
height = props.height;
|
|
91
|
+
|
|
92
|
+
var styleObject = theme && theme.pie && theme.pie.style ? theme.pie.style : {};
|
|
93
|
+
var style = Helpers.getStyles(props.style, styleObject, "auto", "100%");
|
|
94
|
+
var colors = Array.isArray(colorScale) ? colorScale : Style.getColorScale(colorScale);
|
|
95
|
+
var padding = Helpers.getPadding(props);
|
|
96
|
+
var radius = this.getRadius(props, padding);
|
|
97
|
+
var offsetWidth = (radius + padding.left + (width - radius - padding.right)) / 2;
|
|
98
|
+
var offsetHeight = (radius + padding.top + (height - radius - padding.bottom)) / 2;
|
|
99
|
+
var origin = { x: offsetWidth, y: offsetHeight };
|
|
100
|
+
var data = Data.getData(props);
|
|
101
|
+
var slices = this.getSlices(props, data);
|
|
102
|
+
var pathFunction = d3Shape.arc().cornerRadius(props.cornerRadius).outerRadius(radius).innerRadius(props.innerRadius);
|
|
103
|
+
return { style: style, colors: colors, padding: padding, radius: radius, data: data, slices: slices, pathFunction: pathFunction, origin: origin };
|
|
104
|
+
},
|
|
105
|
+
getColor: function (style, colors, index) {
|
|
106
|
+
if (style && style.data && style.data.fill) {
|
|
107
|
+
return style.data.fill;
|
|
108
|
+
}
|
|
109
|
+
return colors && colors[index % colors.length];
|
|
110
|
+
},
|
|
111
|
+
getRadius: function (props, padding) {
|
|
112
|
+
return Math.min(props.width - padding.left - padding.right, props.height - padding.top - padding.bottom) / 2;
|
|
113
|
+
},
|
|
114
|
+
getLabelPosition: function (radius, labelRadius, style) {
|
|
115
|
+
var padding = style && style.padding || 0;
|
|
116
|
+
var arcRadius = labelRadius || radius + padding;
|
|
117
|
+
return d3Shape.arc().outerRadius(arcRadius).innerRadius(arcRadius);
|
|
118
|
+
},
|
|
119
|
+
getLabelOrientation: function (slice) {
|
|
120
|
+
var radiansToDegrees = function (radians) {
|
|
121
|
+
return radians * (180 / Math.PI);
|
|
122
|
+
};
|
|
123
|
+
var start = radiansToDegrees(slice.startAngle);
|
|
124
|
+
var end = radiansToDegrees(slice.endAngle);
|
|
125
|
+
var degree = start + (end - start) / 2;
|
|
126
|
+
if (degree < 45 || degree > 315) {
|
|
127
|
+
return "top";
|
|
128
|
+
} else if (degree >= 45 && degree < 135) {
|
|
129
|
+
return "right";
|
|
130
|
+
} else if (degree >= 135 && degree < 225) {
|
|
131
|
+
return "bottom";
|
|
132
|
+
} else {
|
|
133
|
+
return "left";
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
getTextAnchor: function (orientation) {
|
|
137
|
+
if (orientation === "top" || orientation === "bottom") {
|
|
138
|
+
return "middle";
|
|
139
|
+
}
|
|
140
|
+
return orientation === "right" ? "start" : "end";
|
|
141
|
+
},
|
|
142
|
+
getVerticalAnchor: function (orientation) {
|
|
143
|
+
if (orientation === "left" || orientation === "right") {
|
|
144
|
+
return "middle";
|
|
145
|
+
}
|
|
146
|
+
return orientation === "bottom" ? "start" : "end";
|
|
147
|
+
},
|
|
148
|
+
getLabelText: function (props, datum, index) {
|
|
149
|
+
var text = void 0;
|
|
150
|
+
if (datum.label) {
|
|
151
|
+
text = datum.label;
|
|
152
|
+
} else if (Array.isArray(props.labels)) {
|
|
153
|
+
text = props.labels[index];
|
|
154
|
+
} else {
|
|
155
|
+
text = _isFunction(props.labels) ? props.labels(datum) : datum.xName || datum._x;
|
|
156
|
+
}
|
|
157
|
+
return this.checkForValidText(text);
|
|
158
|
+
},
|
|
159
|
+
getSlices: function (props, data) {
|
|
160
|
+
var layoutFunction = d3Shape.pie().sort(null).startAngle(this.degreesToRadians(props.startAngle)).endAngle(this.degreesToRadians(props.endAngle)).padAngle(this.degreesToRadians(props.padAngle)).value(function (datum) {
|
|
161
|
+
return datum._y;
|
|
162
|
+
});
|
|
163
|
+
return layoutFunction(data);
|
|
164
|
+
}
|
|
165
|
+
};
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import _partialRight from "lodash/partialRight";
|
|
2
|
+
|
|
3
|
+
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
|
4
|
+
|
|
5
|
+
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
6
|
+
|
|
7
|
+
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
|
8
|
+
|
|
9
|
+
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
|
10
|
+
|
|
11
|
+
/*eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2] }]*/
|
|
12
|
+
import React from "react";
|
|
13
|
+
import PropTypes from "prop-types";
|
|
14
|
+
|
|
15
|
+
import { addEvents, Helpers, Data, PropTypes as CustomPropTypes, Slice, VictoryContainer, VictoryLabel, VictoryTheme } from "victory-core";
|
|
16
|
+
import PieHelpers from "./helper-methods";
|
|
17
|
+
|
|
18
|
+
var fallbackProps = {
|
|
19
|
+
endAngle: 360,
|
|
20
|
+
height: 400,
|
|
21
|
+
innerRadius: 0,
|
|
22
|
+
cornerRadius: 0,
|
|
23
|
+
padAngle: 0,
|
|
24
|
+
padding: 30,
|
|
25
|
+
width: 400,
|
|
26
|
+
startAngle: 0,
|
|
27
|
+
colorScale: ["#ffffff", "#f0f0f0", "#d9d9d9", "#bdbdbd", "#969696", "#737373", "#525252", "#252525", "#000000"]
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
var animationWhitelist = ["data", "endAngle", "height", "innerRadius", "cornerRadius", "padAngle", "padding", "colorScale", "startAngle", "style", "width"];
|
|
31
|
+
|
|
32
|
+
var VictoryPie = function (_React$Component) {
|
|
33
|
+
_inherits(VictoryPie, _React$Component);
|
|
34
|
+
|
|
35
|
+
function VictoryPie() {
|
|
36
|
+
_classCallCheck(this, VictoryPie);
|
|
37
|
+
|
|
38
|
+
return _possibleConstructorReturn(this, (VictoryPie.__proto__ || Object.getPrototypeOf(VictoryPie)).apply(this, arguments));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
_createClass(VictoryPie, [{
|
|
42
|
+
key: "shouldAnimate",
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
// Overridden in victory-native
|
|
46
|
+
value: function shouldAnimate() {
|
|
47
|
+
return Boolean(this.props.animate);
|
|
48
|
+
}
|
|
49
|
+
}, {
|
|
50
|
+
key: "render",
|
|
51
|
+
value: function render() {
|
|
52
|
+
var role = this.constructor.role;
|
|
53
|
+
|
|
54
|
+
var props = Helpers.modifyProps(this.props, fallbackProps, role);
|
|
55
|
+
if (this.shouldAnimate()) {
|
|
56
|
+
return this.animateComponent(props, animationWhitelist);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
var children = this.renderData(props);
|
|
60
|
+
return props.standalone ? this.renderContainer(props.containerComponent, children) : children;
|
|
61
|
+
}
|
|
62
|
+
}]);
|
|
63
|
+
|
|
64
|
+
return VictoryPie;
|
|
65
|
+
}(React.Component);
|
|
66
|
+
|
|
67
|
+
VictoryPie.displayName = "VictoryPie";
|
|
68
|
+
VictoryPie.defaultTransitions = {
|
|
69
|
+
onExit: {
|
|
70
|
+
duration: 500,
|
|
71
|
+
before: function () {
|
|
72
|
+
return { _y: 0, label: " " };
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
onEnter: {
|
|
76
|
+
duration: 500,
|
|
77
|
+
before: function () {
|
|
78
|
+
return { _y: 0, label: " " };
|
|
79
|
+
},
|
|
80
|
+
after: function (datum) {
|
|
81
|
+
return { y_: datum._y, label: datum.label };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
VictoryPie.propTypes = {
|
|
86
|
+
animate: PropTypes.oneOfType([PropTypes.bool, PropTypes.object]),
|
|
87
|
+
colorScale: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string), PropTypes.oneOf(["grayscale", "qualitative", "heatmap", "warm", "cool", "red", "green", "blue"])]),
|
|
88
|
+
containerComponent: PropTypes.element,
|
|
89
|
+
cornerRadius: CustomPropTypes.nonNegative,
|
|
90
|
+
data: PropTypes.array,
|
|
91
|
+
dataComponent: PropTypes.element,
|
|
92
|
+
endAngle: PropTypes.number,
|
|
93
|
+
eventKey: PropTypes.oneOfType([PropTypes.func, CustomPropTypes.allOfType([CustomPropTypes.integer, CustomPropTypes.nonNegative]), PropTypes.string]),
|
|
94
|
+
events: PropTypes.arrayOf(PropTypes.shape({
|
|
95
|
+
target: PropTypes.oneOf(["data", "labels", "parent"]),
|
|
96
|
+
eventKey: PropTypes.oneOfType([PropTypes.func, CustomPropTypes.allOfType([CustomPropTypes.integer, CustomPropTypes.nonNegative]), PropTypes.string]),
|
|
97
|
+
eventHandlers: PropTypes.object
|
|
98
|
+
})),
|
|
99
|
+
groupComponent: PropTypes.element,
|
|
100
|
+
height: CustomPropTypes.nonNegative,
|
|
101
|
+
innerRadius: CustomPropTypes.nonNegative,
|
|
102
|
+
labelComponent: PropTypes.element,
|
|
103
|
+
labelRadius: PropTypes.oneOfType([CustomPropTypes.nonNegative, PropTypes.func]),
|
|
104
|
+
labels: PropTypes.oneOfType([PropTypes.func, PropTypes.array]),
|
|
105
|
+
name: PropTypes.string,
|
|
106
|
+
padAngle: CustomPropTypes.nonNegative,
|
|
107
|
+
padding: PropTypes.oneOfType([PropTypes.number, PropTypes.shape({
|
|
108
|
+
top: PropTypes.number, bottom: PropTypes.number,
|
|
109
|
+
left: PropTypes.number, right: PropTypes.number
|
|
110
|
+
})]),
|
|
111
|
+
sharedEvents: PropTypes.shape({
|
|
112
|
+
events: PropTypes.array,
|
|
113
|
+
getEventState: PropTypes.func
|
|
114
|
+
}),
|
|
115
|
+
sortKey: PropTypes.oneOfType([PropTypes.func, CustomPropTypes.allOfType([CustomPropTypes.integer, CustomPropTypes.nonNegative]), PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
|
|
116
|
+
standalone: PropTypes.bool,
|
|
117
|
+
startAngle: PropTypes.number,
|
|
118
|
+
style: PropTypes.shape({
|
|
119
|
+
parent: PropTypes.object, data: PropTypes.object, labels: PropTypes.object
|
|
120
|
+
}),
|
|
121
|
+
theme: PropTypes.object,
|
|
122
|
+
width: CustomPropTypes.nonNegative,
|
|
123
|
+
x: PropTypes.oneOfType([PropTypes.func, CustomPropTypes.allOfType([CustomPropTypes.integer, CustomPropTypes.nonNegative]), PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
|
|
124
|
+
y: PropTypes.oneOfType([PropTypes.func, CustomPropTypes.allOfType([CustomPropTypes.integer, CustomPropTypes.nonNegative]), PropTypes.string, PropTypes.arrayOf(PropTypes.string)])
|
|
125
|
+
};
|
|
126
|
+
VictoryPie.defaultProps = {
|
|
127
|
+
data: [{ x: "A", y: 1 }, { x: "B", y: 2 }, { x: "C", y: 3 }, { x: "D", y: 1 }, { x: "E", y: 2 }],
|
|
128
|
+
standalone: true,
|
|
129
|
+
dataComponent: React.createElement(Slice, null),
|
|
130
|
+
labelComponent: React.createElement(VictoryLabel, null),
|
|
131
|
+
containerComponent: React.createElement(VictoryContainer, null),
|
|
132
|
+
groupComponent: React.createElement("g", null),
|
|
133
|
+
theme: VictoryTheme.grayscale
|
|
134
|
+
};
|
|
135
|
+
VictoryPie.getBaseProps = _partialRight(PieHelpers.getBaseProps.bind(PieHelpers), fallbackProps);
|
|
136
|
+
VictoryPie.getData = Data.getData.bind(Data);
|
|
137
|
+
VictoryPie.expectedComponents = ["dataComponent", "labelComponent", "groupComponent", "containerComponent"];
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
export default addEvents(VictoryPie);
|
package/es/index.js
ADDED