victory-pie 36.9.2 → 37.0.0

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,83 +1,78 @@
1
1
  import _isPlainObject from "lodash/isPlainObject";
2
2
  import _defaults from "lodash/defaults";
3
+ /* eslint no-magic-numbers: ["error", { "ignore": [-1, 0, 1, 2, 45, 90, 135, 180, 225, 270, 315, 360] }]*/
3
4
  import * as d3Shape from "victory-vendor/d3-shape";
4
5
  import { Helpers, Data, Style } from "victory-core";
5
-
6
- var checkForValidText = function (text) {
6
+ const checkForValidText = text => {
7
7
  if (text === undefined || text === null || Helpers.isFunction(text)) {
8
8
  return text;
9
9
  }
10
-
11
- return "".concat(text);
10
+ return `${text}`;
12
11
  };
13
-
14
- var getColor = function (style, colors, index) {
12
+ const getColor = (style, colors, index) => {
15
13
  if (style && style.data && style.data.fill) {
16
14
  return style.data.fill;
17
15
  }
18
-
19
16
  return colors && colors[index % colors.length];
20
17
  };
21
-
22
- var getRadius = function (props, padding) {
18
+ const getRadius = (props, padding) => {
23
19
  if (typeof props.radius === "number") {
24
20
  return props.radius;
25
21
  }
26
-
27
22
  return Math.min(props.width - padding.left - padding.right, props.height - padding.top - padding.bottom) / 2;
28
23
  };
29
-
30
- var getOrigin = function (props, padding) {
31
- var width = props.width,
32
- height = props.height;
33
- var origin = _isPlainObject(props.origin) ? props.origin : {};
24
+ const getOrigin = (props, padding) => {
25
+ const {
26
+ width,
27
+ height
28
+ } = props;
29
+ const origin = _isPlainObject(props.origin) ? props.origin : {};
34
30
  return {
35
31
  x: origin.x !== undefined ? origin.x : (padding.left - padding.right + width) / 2,
36
32
  y: origin.y !== undefined ? origin.y : (padding.top - padding.bottom + height) / 2
37
33
  };
38
34
  };
39
-
40
- var getSlices = function (props, data) {
41
- var padAngle = Helpers.isFunction(props.padAngle) ? 0 : props.padAngle;
42
- var layoutFunction = d3Shape.pie().sort(null).startAngle(Helpers.degreesToRadians(props.startAngle)).endAngle(Helpers.degreesToRadians(props.endAngle)).padAngle(Helpers.degreesToRadians(padAngle)).value(function (datum) {
35
+ const getSlices = (props, data) => {
36
+ const padAngle = Helpers.isFunction(props.padAngle) ? 0 : props.padAngle;
37
+ const layoutFunction = d3Shape.pie().sort(null).startAngle(Helpers.degreesToRadians(props.startAngle)).endAngle(Helpers.degreesToRadians(props.endAngle)).padAngle(Helpers.degreesToRadians(padAngle)).value(datum => {
43
38
  return datum._y;
44
39
  });
45
40
  return layoutFunction(data);
46
41
  };
47
-
48
- var getCalculatedValues = function (props) {
49
- var colorScale = props.colorScale;
50
- var styleObject = Helpers.getDefaultStyles(props, "pie");
51
- var style = Helpers.getStyles(props.style, styleObject);
52
- var colors = Array.isArray(colorScale) ? colorScale : Style.getColorScale(colorScale);
53
- var padding = Helpers.getPadding(props);
54
- var defaultRadius = getRadius(props, padding);
55
- var origin = getOrigin(props, padding);
56
- var data = Data.getData(props);
57
- var slices = getSlices(props, data);
42
+ const getCalculatedValues = props => {
43
+ const {
44
+ colorScale
45
+ } = props;
46
+ const styleObject = Helpers.getDefaultStyles(props, "pie");
47
+ const style = Helpers.getStyles(props.style, styleObject);
48
+ const colors = Array.isArray(colorScale) ? colorScale : Style.getColorScale(colorScale);
49
+ const padding = Helpers.getPadding(props);
50
+ const defaultRadius = getRadius(props, padding);
51
+ const origin = getOrigin(props, padding);
52
+ const data = Data.getData(props);
53
+ const slices = getSlices(props, data);
58
54
  return Object.assign({}, props, {
59
- style: style,
60
- colors: colors,
61
- padding: padding,
62
- defaultRadius: defaultRadius,
63
- data: data,
64
- slices: slices,
65
- origin: origin
55
+ style,
56
+ colors,
57
+ padding,
58
+ defaultRadius,
59
+ data,
60
+ slices,
61
+ origin
66
62
  });
67
63
  };
68
-
69
- var getSliceStyle = function (index, calculatedValues) {
70
- var style = calculatedValues.style,
71
- colors = calculatedValues.colors;
72
- var fill = getColor(style, colors, index);
64
+ const getSliceStyle = (index, calculatedValues) => {
65
+ const {
66
+ style,
67
+ colors
68
+ } = calculatedValues;
69
+ const fill = getColor(style, colors, index);
73
70
  return Object.assign({
74
- fill: fill
71
+ fill
75
72
  }, style.data);
76
73
  };
77
-
78
- var getLabelText = function (props, datum, index) {
79
- var text;
80
-
74
+ const getLabelText = (props, datum, index) => {
75
+ let text;
81
76
  if (datum.label) {
82
77
  text = datum.label;
83
78
  } else if (Array.isArray(props.labels)) {
@@ -85,35 +80,29 @@ var getLabelText = function (props, datum, index) {
85
80
  } else {
86
81
  text = Helpers.isFunction(props.labels) ? props.labels : datum.xName || datum._x;
87
82
  }
88
-
89
83
  return checkForValidText(text);
90
84
  };
91
-
92
- var getLabelArc = function (labelRadius) {
85
+ const getLabelArc = labelRadius => {
93
86
  return d3Shape.arc().outerRadius(labelRadius).innerRadius(labelRadius);
94
87
  };
95
-
96
- var getCalculatedLabelRadius = function (radius, labelRadius, style) {
97
- var padding = style && style.padding || 0;
88
+ const getCalculatedLabelRadius = (radius, labelRadius, style) => {
89
+ const padding = style && style.padding || 0;
98
90
  return labelRadius || radius + padding;
99
91
  };
100
-
101
- var getLabelPosition = function (arc, slice, position) {
102
- var construct = {
92
+ const getLabelPosition = (arc, slice, position) => {
93
+ const construct = {
103
94
  startAngle: position === "startAngle" ? slice.startAngle : slice.endAngle,
104
95
  endAngle: position === "endAngle" ? slice.endAngle : slice.startAngle
105
96
  };
106
- var clonedArc = Object.assign({}, slice, construct);
97
+ const clonedArc = Object.assign({}, slice, construct);
107
98
  return arc.centroid(clonedArc);
108
99
  };
109
-
110
- var getLabelOrientation = function (degree, labelPlacement) {
100
+ const getLabelOrientation = (degree, labelPlacement) => {
111
101
  if (labelPlacement === "perpendicular") {
112
102
  return degree > 90 && degree < 270 ? "bottom" : "top";
113
103
  } else if (labelPlacement === "parallel") {
114
104
  return degree >= 0 && degree <= 180 ? "right" : "left";
115
105
  }
116
-
117
106
  if (degree < 45 || degree > 315) {
118
107
  return "top";
119
108
  } else if (degree >= 45 && degree < 135) {
@@ -121,29 +110,22 @@ var getLabelOrientation = function (degree, labelPlacement) {
121
110
  } else if (degree >= 135 && degree < 225) {
122
111
  return "bottom";
123
112
  }
124
-
125
113
  return "left";
126
114
  };
127
-
128
- var getTextAnchor = function (orientation) {
115
+ const getTextAnchor = orientation => {
129
116
  if (orientation === "top" || orientation === "bottom") {
130
117
  return "middle";
131
118
  }
132
-
133
119
  return orientation === "right" ? "start" : "end";
134
120
  };
135
-
136
- var getVerticalAnchor = function (orientation) {
121
+ const getVerticalAnchor = orientation => {
137
122
  if (orientation === "left" || orientation === "right") {
138
123
  return "middle";
139
124
  }
140
-
141
125
  return orientation === "bottom" ? "start" : "end";
142
126
  };
143
-
144
- var getBaseLabelAngle = function (slice, labelPosition, labelStyle) {
145
- var baseAngle = 0;
146
-
127
+ const getBaseLabelAngle = (slice, labelPosition, labelStyle) => {
128
+ let baseAngle = 0;
147
129
  if (labelPosition.angle !== undefined) {
148
130
  baseAngle = labelStyle.angle;
149
131
  } else if (labelPosition === "centroid") {
@@ -151,204 +133,192 @@ var getBaseLabelAngle = function (slice, labelPosition, labelStyle) {
151
133
  } else {
152
134
  baseAngle = labelPosition === "startAngle" ? Helpers.radiansToDegrees(slice.startAngle) : Helpers.radiansToDegrees(slice.endAngle);
153
135
  }
154
-
155
- var positiveAngle = baseAngle < 0 ? 360 - baseAngle : baseAngle;
136
+ const positiveAngle = baseAngle < 0 ? 360 - baseAngle : baseAngle;
156
137
  return positiveAngle % 360;
157
138
  };
158
-
159
- var getLabelAngle = function (baseAngle, labelPlacement) {
139
+ const getLabelAngle = (baseAngle, labelPlacement) => {
160
140
  if (labelPlacement === "vertical") {
161
141
  return 0;
162
142
  }
163
-
164
143
  if (labelPlacement === "parallel") {
165
144
  return baseAngle > 180 && baseAngle < 360 ? baseAngle + 90 : baseAngle - 90;
166
145
  }
167
-
168
146
  return baseAngle > 90 && baseAngle < 270 ? baseAngle - 180 : baseAngle;
169
147
  };
170
-
171
- var getLabelProps = function (text, dataProps, calculatedValues) {
172
- var index = dataProps.index,
173
- datum = dataProps.datum,
174
- data = dataProps.data,
175
- slice = dataProps.slice,
176
- labelComponent = dataProps.labelComponent,
177
- theme = dataProps.theme;
178
- var style = calculatedValues.style,
179
- defaultRadius = calculatedValues.defaultRadius,
180
- origin = calculatedValues.origin,
181
- width = calculatedValues.width,
182
- height = calculatedValues.height;
183
- var labelRadius = Helpers.evaluateProp(calculatedValues.labelRadius, Object.assign({
184
- text: text
148
+ const getLabelProps = (text, dataProps, calculatedValues) => {
149
+ const {
150
+ index,
151
+ datum,
152
+ data,
153
+ slice,
154
+ labelComponent,
155
+ theme
156
+ } = dataProps;
157
+ const {
158
+ style,
159
+ defaultRadius,
160
+ origin,
161
+ width,
162
+ height
163
+ } = calculatedValues;
164
+ const labelRadius = Helpers.evaluateProp(calculatedValues.labelRadius, Object.assign({
165
+ text
185
166
  }, dataProps));
186
- var labelPosition = Helpers.evaluateProp(calculatedValues.labelPosition, Object.assign({
187
- text: text
167
+ const labelPosition = Helpers.evaluateProp(calculatedValues.labelPosition, Object.assign({
168
+ text
188
169
  }, dataProps)) || "centroid";
189
- var labelPlacement = Helpers.evaluateProp(calculatedValues.labelPlacement, Object.assign({
190
- text: text
170
+ const labelPlacement = Helpers.evaluateProp(calculatedValues.labelPlacement, Object.assign({
171
+ text
191
172
  }, dataProps)) || "vertical";
192
- var labelStyle = Object.assign({
173
+ const labelStyle = Object.assign({
193
174
  padding: 0
194
175
  }, style.labels);
195
- var evaluatedStyle = Helpers.evaluateStyle(labelStyle, Object.assign({
196
- labelRadius: labelRadius,
197
- text: text
176
+ const evaluatedStyle = Helpers.evaluateStyle(labelStyle, Object.assign({
177
+ labelRadius,
178
+ text
198
179
  }, dataProps));
199
- var calculatedLabelRadius = getCalculatedLabelRadius(defaultRadius, labelRadius, evaluatedStyle);
200
- var labelArc = getLabelArc(calculatedLabelRadius);
201
- var position = getLabelPosition(labelArc, slice, labelPosition);
202
- var baseAngle = getBaseLabelAngle(slice, labelPosition, labelStyle);
203
- var labelAngle = getLabelAngle(baseAngle, labelPlacement);
204
- var orientation = getLabelOrientation(baseAngle, labelPlacement);
205
- var textAnchor = labelStyle.textAnchor || getTextAnchor(orientation);
206
- var verticalAnchor = labelStyle.verticalAnchor || getVerticalAnchor(orientation);
207
- var labelProps = {
208
- width: width,
209
- height: height,
210
- index: index,
211
- datum: datum,
212
- data: data,
213
- slice: slice,
214
- orientation: orientation,
215
- text: text,
180
+ const calculatedLabelRadius = getCalculatedLabelRadius(defaultRadius, labelRadius, evaluatedStyle);
181
+ const labelArc = getLabelArc(calculatedLabelRadius);
182
+ const position = getLabelPosition(labelArc, slice, labelPosition);
183
+ const baseAngle = getBaseLabelAngle(slice, labelPosition, labelStyle);
184
+ const labelAngle = getLabelAngle(baseAngle, labelPlacement);
185
+ const orientation = getLabelOrientation(baseAngle, labelPlacement);
186
+ const textAnchor = labelStyle.textAnchor || getTextAnchor(orientation);
187
+ const verticalAnchor = labelStyle.verticalAnchor || getVerticalAnchor(orientation);
188
+ const labelProps = {
189
+ width,
190
+ height,
191
+ index,
192
+ datum,
193
+ data,
194
+ slice,
195
+ orientation,
196
+ text,
216
197
  style: labelStyle,
217
198
  x: Math.round(position[0]) + origin.x,
218
199
  y: Math.round(position[1]) + origin.y,
219
- textAnchor: textAnchor,
220
- verticalAnchor: verticalAnchor,
200
+ textAnchor,
201
+ verticalAnchor,
221
202
  angle: labelAngle,
222
- calculatedLabelRadius: calculatedLabelRadius
203
+ calculatedLabelRadius
223
204
  };
224
-
225
205
  if (!Helpers.isTooltip(labelComponent)) {
226
206
  return labelProps;
227
207
  }
228
-
229
- var tooltipTheme = theme && theme.tooltip || {};
208
+ const tooltipTheme = theme && theme.tooltip || {};
230
209
  return _defaults({}, labelProps, Helpers.omit(tooltipTheme, ["style"]));
231
210
  };
232
-
233
- export var getXOffsetMultiplayerByAngle = function (angle) {
234
- return Math.cos(angle - Helpers.degreesToRadians(90));
235
- };
236
- export var getYOffsetMultiplayerByAngle = function (angle) {
237
- return Math.sin(angle - Helpers.degreesToRadians(90));
238
- };
239
- export var getXOffset = function (offset, angle) {
240
- return offset * getXOffsetMultiplayerByAngle(angle);
241
- };
242
- export var getYOffset = function (offset, angle) {
243
- return offset * getYOffsetMultiplayerByAngle(angle);
244
- };
245
- export var getAverage = function (array) {
246
- return array.reduce(function (acc, cur) {
247
- return acc + cur;
248
- }, 0) / array.length;
249
- };
250
- export var getLabelIndicatorPropsForLineSegment = function (props, calculatedValues, labelProps) {
251
- var innerRadius = props.innerRadius,
252
- radius = props.radius,
253
- _props$slice = props.slice,
254
- startAngle = _props$slice.startAngle,
255
- endAngle = _props$slice.endAngle,
256
- labelIndicatorInnerOffset = props.labelIndicatorInnerOffset,
257
- labelIndicatorOuterOffset = props.labelIndicatorOuterOffset,
258
- index = props.index;
259
- var height = calculatedValues.height,
260
- width = calculatedValues.width;
261
- var calculatedLabelRadius = labelProps.calculatedLabelRadius; // calculation
262
-
263
- var middleRadius = getAverage([innerRadius, radius]);
264
- var midAngle = getAverage([endAngle, startAngle]);
265
- var centerX = width / 2;
266
- var centerY = height / 2;
267
- var innerOffset = middleRadius + labelIndicatorInnerOffset;
268
- var outerOffset = calculatedLabelRadius - labelIndicatorOuterOffset;
269
- var x1 = centerX + getXOffset(innerOffset, midAngle);
270
- var y1 = centerY + getYOffset(innerOffset, midAngle);
271
- var x2 = centerX + getXOffset(outerOffset, midAngle);
272
- var y2 = centerY + getYOffset(outerOffset, midAngle);
273
- var labelIndicatorProps = {
274
- x1: x1,
275
- y1: y1,
276
- x2: x2,
277
- y2: y2,
278
- index: index
211
+ export const getXOffsetMultiplayerByAngle = angle => Math.cos(angle - Helpers.degreesToRadians(90));
212
+ export const getYOffsetMultiplayerByAngle = angle => Math.sin(angle - Helpers.degreesToRadians(90));
213
+ export const getXOffset = (offset, angle) => offset * getXOffsetMultiplayerByAngle(angle);
214
+ export const getYOffset = (offset, angle) => offset * getYOffsetMultiplayerByAngle(angle);
215
+ export const getAverage = array => array.reduce((acc, cur) => acc + cur, 0) / array.length;
216
+ export const getLabelIndicatorPropsForLineSegment = (props, calculatedValues, labelProps) => {
217
+ const {
218
+ innerRadius,
219
+ radius,
220
+ slice: {
221
+ startAngle,
222
+ endAngle
223
+ },
224
+ labelIndicatorInnerOffset,
225
+ labelIndicatorOuterOffset,
226
+ index
227
+ } = props;
228
+ const {
229
+ height,
230
+ width
231
+ } = calculatedValues;
232
+ const {
233
+ calculatedLabelRadius
234
+ } = labelProps;
235
+ // calculation
236
+ const middleRadius = getAverage([innerRadius, radius]);
237
+ const midAngle = getAverage([endAngle, startAngle]);
238
+ const centerX = width / 2;
239
+ const centerY = height / 2;
240
+ const innerOffset = middleRadius + labelIndicatorInnerOffset;
241
+ const outerOffset = calculatedLabelRadius - labelIndicatorOuterOffset;
242
+ const x1 = centerX + getXOffset(innerOffset, midAngle);
243
+ const y1 = centerY + getYOffset(innerOffset, midAngle);
244
+ const x2 = centerX + getXOffset(outerOffset, midAngle);
245
+ const y2 = centerY + getYOffset(outerOffset, midAngle);
246
+ const labelIndicatorProps = {
247
+ x1,
248
+ y1,
249
+ x2,
250
+ y2,
251
+ index
279
252
  };
280
253
  return _defaults({}, labelIndicatorProps);
281
254
  };
282
- export var getBaseProps = function (initialProps, fallbackProps) {
283
- var props = Helpers.modifyProps(initialProps, fallbackProps, "pie");
284
- var calculatedValues = getCalculatedValues(props);
285
- var slices = calculatedValues.slices,
286
- style = calculatedValues.style,
287
- data = calculatedValues.data,
288
- origin = calculatedValues.origin,
289
- defaultRadius = calculatedValues.defaultRadius,
290
- labels = calculatedValues.labels,
291
- events = calculatedValues.events,
292
- sharedEvents = calculatedValues.sharedEvents,
293
- height = calculatedValues.height,
294
- width = calculatedValues.width,
295
- standalone = calculatedValues.standalone,
296
- name = calculatedValues.name,
297
- innerRadius = calculatedValues.innerRadius,
298
- cornerRadius = calculatedValues.cornerRadius,
299
- padAngle = calculatedValues.padAngle,
300
- disableInlineStyles = calculatedValues.disableInlineStyles,
301
- labelIndicator = calculatedValues.labelIndicator;
302
- var radius = props.radius || defaultRadius;
303
- var initialChildProps = {
255
+ export const getBaseProps = (initialProps, fallbackProps) => {
256
+ const props = Helpers.modifyProps(initialProps, fallbackProps, "pie");
257
+ const calculatedValues = getCalculatedValues(props);
258
+ const {
259
+ slices,
260
+ style,
261
+ data,
262
+ origin,
263
+ defaultRadius,
264
+ labels,
265
+ events,
266
+ sharedEvents,
267
+ height,
268
+ width,
269
+ standalone,
270
+ name,
271
+ innerRadius,
272
+ cornerRadius,
273
+ padAngle,
274
+ disableInlineStyles,
275
+ labelIndicator
276
+ } = calculatedValues;
277
+ const radius = props.radius || defaultRadius;
278
+ const initialChildProps = {
304
279
  parent: {
305
- standalone: standalone,
306
- height: height,
307
- width: width,
308
- slices: slices,
309
- name: name,
280
+ standalone,
281
+ height,
282
+ width,
283
+ slices,
284
+ name,
310
285
  style: style.parent
311
286
  }
312
287
  };
313
- return slices.reduce(function (childProps, slice, index) {
314
- var datum = _defaults({}, data[index], {
288
+ return slices.reduce((childProps, slice, index) => {
289
+ const datum = _defaults({}, data[index], {
315
290
  startAngle: Helpers.radiansToDegrees(slice.startAngle),
316
291
  endAngle: Helpers.radiansToDegrees(slice.endAngle),
317
292
  padAngle: Helpers.radiansToDegrees(slice.padAngle)
318
293
  });
319
-
320
- var eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
321
- var dataProps = {
322
- index: index,
323
- slice: slice,
324
- datum: datum,
325
- data: data,
326
- origin: origin,
327
- innerRadius: innerRadius,
328
- radius: radius,
329
- cornerRadius: cornerRadius,
330
- padAngle: padAngle,
294
+ const eventKey = !Helpers.isNil(datum.eventKey) ? datum.eventKey : index;
295
+ const dataProps = {
296
+ index,
297
+ slice,
298
+ datum,
299
+ data,
300
+ origin,
301
+ innerRadius,
302
+ radius,
303
+ cornerRadius,
304
+ padAngle,
331
305
  style: disableInlineStyles ? {} : getSliceStyle(index, calculatedValues),
332
- disableInlineStyles: disableInlineStyles
306
+ disableInlineStyles
333
307
  };
334
308
  childProps[eventKey] = {
335
309
  data: dataProps
336
310
  };
337
- var text = getLabelText(props, datum, index);
338
-
311
+ const text = getLabelText(props, datum, index);
339
312
  if (text !== undefined && text !== null || labels && (events || sharedEvents)) {
340
- var evaluatedText = Helpers.evaluateProp(text, dataProps);
313
+ const evaluatedText = Helpers.evaluateProp(text, dataProps);
341
314
  childProps[eventKey].labels = getLabelProps(evaluatedText, Object.assign({}, props, dataProps), calculatedValues);
342
-
343
315
  if (labelIndicator) {
344
- var labelProps = childProps[eventKey].labels;
345
-
316
+ const labelProps = childProps[eventKey].labels;
346
317
  if (labelProps.calculatedLabelRadius > radius) {
347
318
  childProps[eventKey].labelIndicators = getLabelIndicatorPropsForLineSegment(Object.assign({}, props, dataProps), calculatedValues, labelProps);
348
319
  }
349
320
  }
350
321
  }
351
-
352
322
  return childProps;
353
323
  }, initialChildProps);
354
324
  };