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