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.
@@ -1,171 +1,187 @@
1
- Object.defineProperty(exports,"__esModule",{value:true});var _omit2=require("lodash/omit");var _omit3=_interopRequireDefault(_omit2);var _isFunction2=require("lodash/isFunction");var _isFunction3=_interopRequireDefault(_isFunction2);var _defaults2=require("lodash/defaults");var _defaults3=_interopRequireDefault(_defaults2);var _assign2=require("lodash/assign");var _assign3=_interopRequireDefault(_assign2);
2
-
3
- var _d3Shape=require("d3-shape");var d3Shape=_interopRequireWildcard(_d3Shape);
4
-
5
- var _victoryCore=require("victory-core");function _interopRequireWildcard(obj){if(obj&&obj.__esModule){return obj;}else{var newObj={};if(obj!=null){for(var key in obj){if(Object.prototype.hasOwnProperty.call(obj,key))newObj[key]=obj[key];}}newObj.default=obj;return newObj;}}function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}exports.default=
6
-
7
- {
8
- degreesToRadians:function degreesToRadians(degrees){
9
- return degrees*(Math.PI/180);
10
- },
11
-
12
- checkForValidText:function checkForValidText(text){
13
- if(text===undefined||text===null){
14
- return text;
15
- }else{
16
- return""+text;
17
- }
18
- },
19
-
20
- getSliceStyle:function getSliceStyle(datum,index,calculatedValues){var
21
- style=calculatedValues.style,colors=calculatedValues.colors;
22
- var fill=this.getColor(style,colors,index);
23
- var dataStyles=(0,_omit3.default)(datum,["_x","_y","x","y","label"]);
24
- return(0,_defaults3.default)({},dataStyles,{fill:fill},style.data);
25
- },
26
-
27
- getBaseProps:function getBaseProps(props,fallbackProps){
28
- props=_victoryCore.Helpers.modifyProps(props,fallbackProps,"pie");
29
- var calculatedValues=this.getCalculatedValues(props);var
30
- slices=calculatedValues.slices,style=calculatedValues.style,pathFunction=calculatedValues.pathFunction,data=calculatedValues.data,origin=calculatedValues.origin;
31
- var childProps={
32
- parent:{
33
- standalone:props.standalone,slices:slices,pathFunction:pathFunction,
34
- width:props.width,height:props.height,style:style.parent}};
35
-
36
-
37
-
38
- for(var index=0,len=slices.length;index<len;index++){
39
- var slice=slices[index];
40
- var datum=data[index];
41
- var eventKey=datum.eventKey||index;
42
- var dataProps={
43
- index:index,slice:slice,pathFunction:pathFunction,datum:datum,data:data,origin:origin,
44
- style:this.getSliceStyle(datum,index,calculatedValues)};
45
-
46
-
47
- childProps[eventKey]={
48
- data:dataProps,
49
- labels:this.getLabelProps(props,dataProps,calculatedValues)};
50
-
51
- }
52
- return childProps;
53
- },
54
-
55
- getLabelProps:function getLabelProps(props,dataProps,calculatedValues){var
56
- index=dataProps.index,datum=dataProps.datum,data=dataProps.data,slice=dataProps.slice;var
57
- style=calculatedValues.style,radius=calculatedValues.radius,origin=calculatedValues.origin;
58
- var labelStyle=(0,_assign3.default)({padding:0},style.labels);
59
- var labelRadius=_victoryCore.Helpers.evaluateProp(props.labelRadius,datum);
60
- var labelPosition=this.getLabelPosition(radius,labelRadius,labelStyle);
61
- var position=labelPosition.centroid(slice);
62
- var orientation=this.getLabelOrientation(slice);
63
- return{
64
- index:index,datum:datum,data:data,slice:slice,orientation:orientation,
65
- style:labelStyle,
66
- x:Math.round(position[0])+origin.x,
67
- y:Math.round(position[1])+origin.y,
68
- text:this.getLabelText(props,datum,index),
69
- textAnchor:labelStyle.textAnchor||this.getTextAnchor(orientation),
70
- verticalAnchor:labelStyle.verticalAnchor||this.getVerticalAnchor(orientation),
71
- angle:labelStyle.angle};
72
-
73
- },
74
-
75
- getCalculatedValues:function getCalculatedValues(props){var
76
- theme=props.theme,colorScale=props.colorScale,width=props.width,height=props.height;
77
- var styleObject=theme&&theme.pie&&theme.pie.style?theme.pie.style:{};
78
- var style=_victoryCore.Helpers.getStyles(props.style,styleObject,"auto","100%");
79
- var colors=Array.isArray(colorScale)?colorScale:_victoryCore.Style.getColorScale(colorScale);
80
- var padding=_victoryCore.Helpers.getPadding(props);
81
- var radius=this.getRadius(props,padding);
82
- var offsetWidth=width/2+padding.left-padding.right;
83
- var offsetHeight=height/2+padding.top-padding.bottom;
84
- var origin={
85
- x:offsetWidth+radius>width?radius+padding.left-padding.right:offsetWidth,
86
- y:offsetHeight+radius>height?radius+padding.top-padding.bottom:offsetHeight};
87
-
88
- var data=_victoryCore.Data.getData(props);
89
- var slices=this.getSlices(props,data);
90
- var pathFunction=d3Shape.arc().
91
- cornerRadius(props.cornerRadius).
92
- outerRadius(radius).
93
- innerRadius(props.innerRadius);
94
- return{style:style,colors:colors,padding:padding,radius:radius,data:data,slices:slices,pathFunction:pathFunction,origin:origin};
95
- },
96
-
97
- getColor:function getColor(style,colors,index){
98
- if(style&&style.data&&style.data.fill){
99
- return style.data.fill;
100
- }
101
- return colors&&colors[index%colors.length];
102
- },
103
-
104
- getRadius:function getRadius(props,padding){
105
- return Math.min(
106
- props.width-padding.left-padding.right,
107
- props.height-padding.top-padding.bottom)/
108
- 2;
109
- },
110
-
111
- getLabelPosition:function getLabelPosition(radius,labelRadius,style){
112
- var padding=style&&style.padding||0;
113
- var arcRadius=labelRadius||radius+padding;
114
- return d3Shape.arc().
115
- outerRadius(arcRadius).
116
- innerRadius(arcRadius);
117
- },
118
-
119
- getLabelOrientation:function getLabelOrientation(slice){
120
- var radiansToDegrees=function radiansToDegrees(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
-
137
- getTextAnchor:function getTextAnchor(orientation){
138
- if(orientation==="top"||orientation==="bottom"){
139
- return"middle";
140
- }
141
- return orientation==="right"?"start":"end";
142
- },
143
-
144
- getVerticalAnchor:function getVerticalAnchor(orientation){
145
- if(orientation==="left"||orientation==="right"){
146
- return"middle";
147
- }
148
- return orientation==="bottom"?"start":"end";
149
- },
150
-
151
- getLabelText:function getLabelText(props,datum,index){
152
- var text=void 0;
153
- if(datum.label){
154
- text=datum.label;
155
- }else if(Array.isArray(props.labels)){
156
- text=props.labels[index];
157
- }else{
158
- text=(0,_isFunction3.default)(props.labels)?props.labels(datum):datum.xName||datum._x;
159
- }
160
- return this.checkForValidText(text);
161
- },
162
-
163
- getSlices:function getSlices(props,data){
164
- var layoutFunction=d3Shape.pie().
165
- sort(null).
166
- startAngle(this.degreesToRadians(props.startAngle)).
167
- endAngle(this.degreesToRadians(props.endAngle)).
168
- padAngle(this.degreesToRadians(props.padAngle)).
169
- value(function(datum){return datum._y;});
170
- return layoutFunction(data);
171
- }};
1
+ Object.defineProperty(exports, "__esModule", {
2
+ value: true
3
+ });
4
+
5
+ var _omit2 = require("lodash/omit");
6
+
7
+ var _omit3 = _interopRequireDefault(_omit2);
8
+
9
+ var _isFunction2 = require("lodash/isFunction");
10
+
11
+ var _isFunction3 = _interopRequireDefault(_isFunction2);
12
+
13
+ var _defaults2 = require("lodash/defaults");
14
+
15
+ var _defaults3 = _interopRequireDefault(_defaults2);
16
+
17
+ var _assign2 = require("lodash/assign");
18
+
19
+ var _assign3 = _interopRequireDefault(_assign2);
20
+
21
+ var _d3Shape = require("d3-shape");
22
+
23
+ var d3Shape = _interopRequireWildcard(_d3Shape);
24
+
25
+ var _victoryCore = require("victory-core");
26
+
27
+ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
28
+
29
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
+
31
+ /*eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2, 45, 135, 180, 225, 315] }]*/
32
+ exports.default = {
33
+ degreesToRadians: function (degrees) {
34
+ return degrees * (Math.PI / 180);
35
+ },
36
+ checkForValidText: function (text) {
37
+ if (text === undefined || text === null) {
38
+ return text;
39
+ } else {
40
+ return "" + text;
41
+ }
42
+ },
43
+ getSliceStyle: function (datum, index, calculatedValues) {
44
+ var style = calculatedValues.style,
45
+ colors = calculatedValues.colors;
46
+
47
+ var fill = this.getColor(style, colors, index);
48
+ var dataStyles = (0, _omit3.default)(datum, ["_x", "_y", "x", "y", "label"]);
49
+ return (0, _defaults3.default)({}, dataStyles, { fill: fill }, style.data);
50
+ },
51
+ getBaseProps: function (props, fallbackProps) {
52
+ props = _victoryCore.Helpers.modifyProps(props, fallbackProps, "pie");
53
+ var calculatedValues = this.getCalculatedValues(props);
54
+ var slices = calculatedValues.slices,
55
+ style = calculatedValues.style,
56
+ pathFunction = calculatedValues.pathFunction,
57
+ data = calculatedValues.data,
58
+ origin = calculatedValues.origin;
59
+
60
+ var childProps = {
61
+ parent: {
62
+ standalone: props.standalone, slices: slices, pathFunction: pathFunction,
63
+ width: props.width, height: props.height, style: style.parent
64
+ }
65
+ };
66
+
67
+ for (var index = 0, len = slices.length; index < len; index++) {
68
+ var slice = slices[index];
69
+ var datum = data[index];
70
+ var eventKey = datum.eventKey || index;
71
+ var dataProps = {
72
+ index: index, slice: slice, pathFunction: pathFunction, datum: datum, data: data, origin: origin,
73
+ style: this.getSliceStyle(datum, index, calculatedValues)
74
+ };
75
+
76
+ childProps[eventKey] = {
77
+ data: dataProps,
78
+ labels: this.getLabelProps(props, dataProps, calculatedValues)
79
+ };
80
+ }
81
+ return childProps;
82
+ },
83
+ getLabelProps: function (props, dataProps, calculatedValues) {
84
+ var index = dataProps.index,
85
+ datum = dataProps.datum,
86
+ data = dataProps.data,
87
+ slice = dataProps.slice;
88
+ var style = calculatedValues.style,
89
+ radius = calculatedValues.radius,
90
+ origin = calculatedValues.origin;
91
+
92
+ var labelStyle = _victoryCore.Helpers.evaluateStyle((0, _assign3.default)({ padding: 0 }, style.labels), datum, props.active);
93
+ var labelRadius = _victoryCore.Helpers.evaluateProp(props.labelRadius, datum);
94
+ var labelPosition = this.getLabelPosition(radius, labelRadius, labelStyle);
95
+ var position = labelPosition.centroid(slice);
96
+ var orientation = this.getLabelOrientation(slice);
97
+ return {
98
+ index: index, datum: datum, data: data, slice: slice, orientation: orientation,
99
+ style: labelStyle,
100
+ x: Math.round(position[0]) + origin.x,
101
+ y: Math.round(position[1]) + origin.y,
102
+ text: this.getLabelText(props, datum, index),
103
+ textAnchor: labelStyle.textAnchor || this.getTextAnchor(orientation),
104
+ verticalAnchor: labelStyle.verticalAnchor || this.getVerticalAnchor(orientation),
105
+ angle: labelStyle.angle
106
+ };
107
+ },
108
+ getCalculatedValues: function (props) {
109
+ var theme = props.theme,
110
+ colorScale = props.colorScale,
111
+ width = props.width,
112
+ height = props.height;
113
+
114
+ var styleObject = theme && theme.pie && theme.pie.style ? theme.pie.style : {};
115
+ var style = _victoryCore.Helpers.getStyles(props.style, styleObject, "auto", "100%");
116
+ var colors = Array.isArray(colorScale) ? colorScale : _victoryCore.Style.getColorScale(colorScale);
117
+ var padding = _victoryCore.Helpers.getPadding(props);
118
+ var radius = this.getRadius(props, padding);
119
+ var offsetWidth = (radius + padding.left + (width - radius - padding.right)) / 2;
120
+ var offsetHeight = (radius + padding.top + (height - radius - padding.bottom)) / 2;
121
+ var origin = { x: offsetWidth, y: offsetHeight };
122
+ var data = _victoryCore.Data.getData(props);
123
+ var slices = this.getSlices(props, data);
124
+ var pathFunction = d3Shape.arc().cornerRadius(props.cornerRadius).outerRadius(radius).innerRadius(props.innerRadius);
125
+ return { style: style, colors: colors, padding: padding, radius: radius, data: data, slices: slices, pathFunction: pathFunction, origin: origin };
126
+ },
127
+ getColor: function (style, colors, index) {
128
+ if (style && style.data && style.data.fill) {
129
+ return style.data.fill;
130
+ }
131
+ return colors && colors[index % colors.length];
132
+ },
133
+ getRadius: function (props, padding) {
134
+ return Math.min(props.width - padding.left - padding.right, props.height - padding.top - padding.bottom) / 2;
135
+ },
136
+ getLabelPosition: function (radius, labelRadius, style) {
137
+ var padding = style && style.padding || 0;
138
+ var arcRadius = labelRadius || radius + padding;
139
+ return d3Shape.arc().outerRadius(arcRadius).innerRadius(arcRadius);
140
+ },
141
+ getLabelOrientation: function (slice) {
142
+ var radiansToDegrees = function (radians) {
143
+ return radians * (180 / Math.PI);
144
+ };
145
+ var start = radiansToDegrees(slice.startAngle);
146
+ var end = radiansToDegrees(slice.endAngle);
147
+ var degree = start + (end - start) / 2;
148
+ if (degree < 45 || degree > 315) {
149
+ return "top";
150
+ } else if (degree >= 45 && degree < 135) {
151
+ return "right";
152
+ } else if (degree >= 135 && degree < 225) {
153
+ return "bottom";
154
+ } else {
155
+ return "left";
156
+ }
157
+ },
158
+ getTextAnchor: function (orientation) {
159
+ if (orientation === "top" || orientation === "bottom") {
160
+ return "middle";
161
+ }
162
+ return orientation === "right" ? "start" : "end";
163
+ },
164
+ getVerticalAnchor: function (orientation) {
165
+ if (orientation === "left" || orientation === "right") {
166
+ return "middle";
167
+ }
168
+ return orientation === "bottom" ? "start" : "end";
169
+ },
170
+ getLabelText: function (props, datum, index) {
171
+ var text = void 0;
172
+ if (datum.label) {
173
+ text = datum.label;
174
+ } else if (Array.isArray(props.labels)) {
175
+ text = props.labels[index];
176
+ } else {
177
+ text = (0, _isFunction3.default)(props.labels) ? props.labels(datum) : datum.xName || datum._x;
178
+ }
179
+ return this.checkForValidText(text);
180
+ },
181
+ getSlices: function (props, data) {
182
+ var layoutFunction = d3Shape.pie().sort(null).startAngle(this.degreesToRadians(props.startAngle)).endAngle(this.degreesToRadians(props.endAngle)).padAngle(this.degreesToRadians(props.padAngle)).value(function (datum) {
183
+ return datum._y;
184
+ });
185
+ return layoutFunction(data);
186
+ }
187
+ };