forked from uber/react-vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseries-utils.js
279 lines (254 loc) · 8.16 KB
/
series-utils.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
// Copyright (c) 2016 - 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import React from 'react';
import AbstractSeries from 'plot/series/abstract-series';
import {DISCRETE_COLOR_RANGE, DEFAULT_OPACITY} from 'theme';
/**
* Check if the component is series or not.
* @param {React.Component} child Component.
* @returns {boolean} True if the child is series, false otherwise.
*/
export function isSeriesChild(child) {
const {prototype} = child.type;
return prototype instanceof AbstractSeries;
}
/**
* Get all series from the 'children' object of the component.
* @param {Object} children Children.
* @returns {Array} Array of children.
*/
export function getSeriesChildren(children) {
return React.Children.toArray(children).filter(
child => child && isSeriesChild(child)
);
}
/**
* Collect the map of repetitions of the series type for all children.
* @param {Array} children Array of children.
* @returns {{}} Map of repetitions where sameTypeTotal is the total amount and
* sameTypeIndex is always 0.
*/
function collectSeriesTypesInfo(children) {
const result = {};
children.filter(isSeriesChild).forEach(child => {
const {displayName} = child.type;
const {cluster} = child.props;
if (!result[displayName]) {
result[displayName] = {
sameTypeTotal: 0,
sameTypeIndex: 0,
clusters: new Set()
};
}
result[displayName].clusters.add(cluster);
result[displayName].sameTypeTotal++;
});
return result;
}
/**
* Check series to see if it has angular data that needs to be converted
* @param {Array} data - an array of objects to check
* @returns {Boolean} whether or not this series contains polar configuration
*/
function seriesHasAngleRadius(data = []) {
if (!data) {
return false;
}
return data.some(row => row.radius && row.angle);
}
/**
* Possibly convert polar coordinates to x/y for computing domain
* @param {Array} data - an array of objects to check
* @param {String} attr - the property being checked
* @returns {Boolean} whether or not this series contains polar configuration
*/
function prepareData(data) {
if (!seriesHasAngleRadius(data)) {
return data;
}
return data.map(row => ({
...row,
x: row.radius * Math.cos(row.angle),
y: row.radius * Math.sin(row.angle)
}));
}
/**
* Collect the stacked data for all children in use. If the children don't have
* the data (e.g. the child is invalid series or something else), then the child
* is skipped.
* Each next value of attr is equal to the previous value plus the difference
* between attr0 and attr.
* @param {Array} children Array of children.
* @param {string} attr Attribute to stack by.
* @returns {Array} New array of children for the series.
*/
export function getStackedData(children, attr) {
const areSomeSeriesStacked = children.some(
series => series && series.props.stack
);
// It stores the last segment position added to each bar, separated by cluster.
const latestAttrPositions = {};
return children.reduce((accumulator, series, seriesIndex) => {
// Skip the children that are not series (e.g. don't have any data).
if (!series) {
accumulator.push(null);
return accumulator;
}
const seriesType = series.type.displayName;
const {data, cluster = 'default', stack} = series.props;
const preppedData = prepareData(data, attr);
if (
!attr ||
!preppedData ||
!preppedData.length ||
(areSomeSeriesStacked && !stack)
) {
accumulator.push(preppedData);
return accumulator;
}
const attr0 = `${attr}0`;
const baseAttr = attr === 'y' ? 'x' : 'y';
accumulator.push(
preppedData.map((d, dIndex) => {
if (!latestAttrPositions[cluster]) {
latestAttrPositions[cluster] = {};
}
if (!latestAttrPositions[cluster][seriesType]){
latestAttrPositions[cluster][seriesType] = {};
}
const prevD = latestAttrPositions[cluster][seriesType][d[baseAttr]];
// It is the first segment of a bar.
if (!prevD) {
latestAttrPositions[cluster][seriesType][d[baseAttr]] = {
[attr0]: d[attr0],
[attr]: d[attr]
};
return {...d};
}
// Calculate the position of the next segment in a bar.
const nextD = {
...d,
[attr0]: prevD[attr],
[attr]: prevD[attr] + d[attr] - (d[attr0] || 0)
};
latestAttrPositions[cluster][seriesType][d[baseAttr]] = {
[attr0]: nextD[attr0],
[attr]: nextD[attr]
};
return nextD;
})
);
return accumulator;
}, []);
}
/**
* Get the list of series props for a child.
* @param {Array} children Array of all children.
* @returns {Array} Array of series props for each child. If a child is not a
* series, than it's undefined.
*/
export function getSeriesPropsFromChildren(children) {
const result = [];
const seriesTypesInfo = collectSeriesTypesInfo(children);
let seriesIndex = 0;
const _opacityValue = DEFAULT_OPACITY;
children.forEach(child => {
let props;
if (isSeriesChild(child)) {
const seriesTypeInfo = seriesTypesInfo[child.type.displayName];
const _colorValue =
DISCRETE_COLOR_RANGE[seriesIndex % DISCRETE_COLOR_RANGE.length];
props = {
...seriesTypeInfo,
seriesIndex,
_colorValue,
_opacityValue
};
seriesTypeInfo.sameTypeIndex++;
seriesIndex++;
if (child.props.cluster) {
props.cluster = child.props.cluster;
// Using Array.from() so we can use .indexOf
props.clusters = Array.from(seriesTypeInfo.clusters);
props.sameTypeTotal = props.clusters.length;
props.sameTypeIndex = props.clusters.indexOf(child.props.cluster);
}
}
result.push(props);
});
return result;
}
/**
* Find the max radius value from the nodes to be rendered after they have been
* transformed into an array
* @param {Array} data - the tree data after it has been broken into a iterable
* it is an array of objects!
* @returns {number} the maximum value in coordinates for the radial variable
*/
export function getRadialDomain(data) {
return data.reduce((res, row) => Math.max(row.radius, res), 0);
}
export const ANIMATED_SERIES_PROPS = [
'xRange',
'xDomain',
'x',
'yRange',
'yDomain',
'y',
'colorRange',
'colorDomain',
'color',
'opacityRange',
'opacityDomain',
'opacity',
'strokeRange',
'strokeDomain',
'stroke',
'fillRange',
'fillDomain',
'fill',
'width',
'height',
'marginLeft',
'marginTop',
'marginRight',
'marginBottom',
'data',
'angleDomain',
'angleRange',
'angle',
'radiusDomain',
'radiusRange',
'radius',
'innerRadiusDomain',
'innerRadiusRange',
'innerRadius'
];
export function getStackParams(props) {
const {_stackBy, valuePosAttr, cluster} = props;
let {sameTypeTotal = 1, sameTypeIndex = 0} = props;
// If bars are stacked, but not clustering, override `sameTypeTotal` and
// `sameTypeIndex` such that bars are stacked and not staggered.
if (_stackBy === valuePosAttr && !cluster) {
sameTypeTotal = 1;
sameTypeIndex = 0;
}
return {sameTypeTotal, sameTypeIndex};
}