victory-core 35.11.3 → 36.2.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.
Files changed (31) hide show
  1. package/dist/victory-core.js +10581 -8123
  2. package/dist/victory-core.min.js +1 -1
  3. package/es/index.js +9 -2
  4. package/es/victory-label/victory-label.js +3 -3
  5. package/es/victory-primitives/point.js +1 -1
  6. package/es/victory-util/hooks/index.js +2 -0
  7. package/es/victory-util/hooks/use-animation-state.js +130 -0
  8. package/es/victory-util/hooks/use-previous-props.js +8 -0
  9. package/es/victory-util/line-helpers.js +45 -0
  10. package/es/{victory-primitives/path-helpers.js → victory-util/point-path-helpers.js} +0 -0
  11. package/es/victory-util/wrapper.js +372 -457
  12. package/lib/index.js +20 -8
  13. package/lib/victory-label/victory-label.js +3 -3
  14. package/lib/victory-primitives/point.js +10 -10
  15. package/lib/victory-util/hooks/index.js +21 -0
  16. package/lib/victory-util/hooks/use-animation-state.js +151 -0
  17. package/lib/victory-util/hooks/use-previous-props.js +22 -0
  18. package/lib/victory-util/line-helpers.js +56 -0
  19. package/lib/{victory-primitives/path-helpers.js → victory-util/point-path-helpers.js} +0 -0
  20. package/lib/victory-util/wrapper.js +389 -446
  21. package/package.json +6 -3
  22. package/src/index.js +5 -1
  23. package/src/victory-label/victory-label.js +3 -3
  24. package/src/victory-primitives/point.js +1 -1
  25. package/src/victory-util/hooks/index.js +2 -0
  26. package/src/victory-util/hooks/use-animation-state.js +123 -0
  27. package/src/victory-util/hooks/use-previous-props.js +9 -0
  28. package/src/victory-util/line-helpers.js +54 -0
  29. package/src/{victory-primitives/path-helpers.js → victory-util/point-path-helpers.js} +0 -0
  30. package/src/victory-util/transitions.js +0 -1
  31. package/src/victory-util/wrapper.js +422 -518
@@ -1,10 +1,10 @@
1
+ /* eslint-disable func-style */
1
2
  import {
2
3
  assign,
3
4
  defaults,
4
5
  flatten,
5
6
  isFunction,
6
7
  uniq,
7
- some,
8
8
  groupBy,
9
9
  uniqBy,
10
10
  values,
@@ -13,7 +13,6 @@ import {
13
13
  import React from "react";
14
14
  import * as Axis from "./axis";
15
15
  import * as Style from "./style";
16
- import * as Transitions from "./transitions";
17
16
  import * as Data from "./data";
18
17
  import * as Domain from "./domain";
19
18
  import * as Events from "./events";
@@ -22,549 +21,454 @@ import * as Helpers from "./helpers";
22
21
  import * as Scale from "./scale";
23
22
  import * as Log from "./log";
24
23
 
25
- export default {
26
- getData(props, childComponents) {
27
- if (props.data) {
28
- return Data.getData(props);
29
- }
30
- childComponents = childComponents || React.Children.toArray(props.children);
31
- return this.getDataFromChildren(childComponents);
32
- },
33
-
34
- getDefaultDomainPadding(props, axis, childComponents) {
35
- if (props.polar || axis !== "x") {
36
- return undefined;
37
- }
38
- const groupComponent = childComponents.filter((child) => {
39
- return child.type && child.type.role && child.type.role === "group";
24
+ export function addBinsToParentPropsIfHistogram({
25
+ children,
26
+ props,
27
+ childComponents,
28
+ parentProps
29
+ }) {
30
+ const someChildrenAreHistograms = children.some((child) => {
31
+ return child.type && child.type.role === "histogram";
32
+ });
33
+
34
+ const allChildrenAreHistograms =
35
+ someChildrenAreHistograms &&
36
+ children.length &&
37
+ children.every((child) => {
38
+ return child.type && child.type.role === "histogram";
40
39
  });
41
40
 
42
- if (groupComponent.length < 1) {
43
- return undefined;
44
- }
45
- const { offset, children } = groupComponent[0].props;
46
- if (!offset) {
47
- return undefined;
48
- }
49
-
50
- const firstChild = Array.isArray(children) && children[0];
51
- if (!firstChild) {
52
- return undefined;
53
- }
54
- let barWidth = firstChild.props.barWidth;
55
- let dataLength =
56
- (firstChild.props.data && firstChild.props.data.length) || 1;
57
- if (firstChild && firstChild.type.role === "stack") {
58
- const nestedChild =
59
- firstChild.props.children && firstChild.props.children[0];
60
- if (!nestedChild) {
61
- return undefined;
62
- }
63
- barWidth = nestedChild.props.barWidth;
64
- dataLength = firstChild.props.children.length;
65
- }
66
- const width = barWidth || this.getWidth(props, children.length, dataLength);
67
- return {
68
- x:
69
- (width * children.length) / 2 +
70
- (offset - width * ((children.length - 1) / 2))
71
- };
72
- },
73
-
74
- getDomain(props, axis, childComponents) {
75
- childComponents = childComponents || React.Children.toArray(props.children);
76
-
77
- const propsDomain = Domain.getDomainFromProps(props, axis);
78
- const domainPadding = this.getDefaultDomainPadding(
79
- props,
80
- axis,
81
- childComponents
41
+ if (someChildrenAreHistograms && !allChildrenAreHistograms) {
42
+ Log.warn(
43
+ "VictoryHistogram only supports being stacked with other VictoryHistogram components. Check to make sure that you are only passing VictoryHistogram components to VictoryStack"
82
44
  );
83
- let domain;
84
-
85
- if (propsDomain) {
86
- domain = propsDomain;
87
- } else {
88
- const minDomain = Domain.getMinFromProps(props, axis);
89
- const maxDomain = Domain.getMaxFromProps(props, axis);
90
- const dataset = (props.data || props.y) && Data.getData(props);
91
- const dataDomain = dataset
92
- ? Domain.getDomainFromData(props, axis, dataset)
93
- : [];
94
- const childDomain = this.getDomainFromChildren(
95
- props,
96
- axis,
97
- childComponents
98
- );
99
- const min =
100
- minDomain || Collection.getMinValue([...dataDomain, ...childDomain]);
101
- const max =
102
- maxDomain || Collection.getMaxValue([...dataDomain, ...childDomain]);
103
- domain = Domain.getDomainFromMinMax(min, max);
104
- }
105
- return Domain.formatDomain(domain, assign({ domainPadding }, props), axis);
106
- },
107
-
108
- getScale(props, axis, childComponents) {
109
- if (props.data) {
110
- return Scale.getBaseScale(props, axis);
111
- }
112
- const children = childComponents
113
- ? childComponents.slice(0)
114
- : React.Children.toArray(props.children);
115
- const iteratee = (child) => {
116
- const sharedProps = assign({}, child.props, {
117
- horizontal: props.horizontal
118
- });
119
- return Scale.getScaleType(sharedProps, axis);
120
- };
121
- const childScale = uniq(Helpers.reduceChildren(children, iteratee, props));
122
- // default to linear scale if more than one uniq scale type is given by children
123
-
124
- return childScale.length > 1
125
- ? Scale.getScaleFromName("linear")
126
- : Scale.getScaleFromName(childScale[0]);
127
- },
45
+ }
128
46
 
129
- setAnimationState(props, nextProps) {
130
- if (!props.animate) {
131
- return;
132
- }
133
- if (props.animate.parentState) {
134
- const nodesWillExit = props.animate.parentState.nodesWillExit;
135
- const oldProps = nodesWillExit ? props : null;
136
- this.setState(
137
- defaults({ oldProps, nextProps }, props.animate.parentState)
138
- );
139
- } else {
140
- const oldChildren = React.Children.toArray(props.children);
141
- const nextChildren = React.Children.toArray(nextProps.children);
142
- const isContinuous = (child) => {
143
- const check = (c) => c.type && c.type.continuous;
144
- return Array.isArray(child) ? some(child, check) : check(child);
145
- };
146
-
147
- const continuous =
148
- !props.polar &&
149
- some(oldChildren, (child) => {
150
- return (
151
- isContinuous(child) ||
152
- (child.props.children && isContinuous(child.props.children))
153
- );
154
- });
155
- const {
156
- nodesWillExit,
157
- nodesWillEnter,
158
- childrenTransitions,
159
- nodesShouldEnter
160
- } = Transitions.getInitialTransitionState(oldChildren, nextChildren);
161
-
162
- this.setState({
163
- nodesWillExit,
164
- nodesWillEnter,
165
- nodesShouldEnter,
166
- childrenTransitions: Collection.isArrayOfArrays(childrenTransitions)
167
- ? childrenTransitions[0]
168
- : childrenTransitions,
169
- oldProps: nodesWillExit ? props : null,
170
- nextProps,
171
- continuous
172
- });
173
- }
174
- },
175
-
176
- getAllEvents(props) {
177
- const components = [
178
- "groupComponent",
179
- "containerComponent",
180
- "labelComponent"
181
- ];
182
- this.componentEvents = Events.getComponentEvents(props, components);
183
- let events = props.events;
184
- if (Array.isArray(this.componentEvents)) {
185
- events = Array.isArray(props.events)
186
- ? this.componentEvents.concat(...props.events)
187
- : this.componentEvents;
188
- }
189
- return events || [];
190
- },
47
+ // if we are stacking histograms, we need to generate explicit bins
48
+ // or else each histogram may end up having different bins
49
+ if (!allChildrenAreHistograms) {
50
+ return parentProps;
51
+ }
191
52
 
192
- getAnimationProps(props, child, index) {
193
- if (!props.animate) {
194
- return child.props.animate;
195
- }
196
- const getFilteredState = () => {
197
- let childrenTransitions = this.state && this.state.childrenTransitions;
198
- childrenTransitions = Collection.isArrayOfArrays(childrenTransitions)
199
- ? childrenTransitions[index]
200
- : childrenTransitions;
201
- return defaults({ childrenTransitions }, this.state);
202
- };
53
+ let childBins = props.bins || childComponents[0].props.bins;
203
54
 
204
- let getTransitions = props.animate && props.animate.getTransitions;
205
- const state = getFilteredState();
206
- const parentState = (props.animate && props.animate.parentState) || state;
207
- if (!getTransitions) {
208
- const getTransitionProps = Transitions.getTransitionPropsFactory(
209
- props,
210
- state,
211
- (newState) => this.setState(newState)
55
+ // if we have explicit bins then we don't need to calculate them
56
+ if (!Array.isArray(childBins)) {
57
+ const combinedData = children.reduce((memo, child) => {
58
+ const xAccessor = Helpers.createAccessor(child.props.x || "x");
59
+ return memo.concat(
60
+ child.props.data.map((datum) => ({ x: xAccessor(datum) }))
212
61
  );
213
- getTransitions = (childComponent) =>
214
- getTransitionProps(childComponent, index);
215
- }
216
- return defaults(
217
- { getTransitions, parentState },
218
- props.animate,
219
- child.props.animate
62
+ }, []);
63
+
64
+ // use the same function to generate bins as VictoryHistogram but with
65
+ // the combined data from above, then get explicit bins from that
66
+ const getFormattedHistogramData = children[0].type.getFormattedData;
67
+ childBins = getFormattedHistogramData({
68
+ data: combinedData,
69
+ bins: childBins
70
+ }).reduce(
71
+ (memo, { x0, x1 }, index) =>
72
+ index === 0 ? memo.concat([x0, x1]) : memo.concat(x1),
73
+ []
220
74
  );
221
- },
222
-
223
- getDomainFromChildren(props, axis, childComponents) {
224
- // eslint-disable-line max-statements, complexity, max-len
225
- const children = childComponents
226
- ? childComponents.slice(0)
227
- : React.Children.toArray(props.children);
228
- const parentData = props.data ? Data.getData(props, axis) : undefined;
229
- const {
230
- polar,
231
- startAngle,
232
- endAngle,
233
- categories,
234
- minDomain,
235
- maxDomain,
236
- horizontal
237
- } = props;
238
- const baseParentProps = {
239
- horizontal,
240
- polar,
241
- startAngle,
242
- endAngle,
243
- minDomain,
244
- maxDomain,
245
- categories
246
- };
247
- const parentProps = parentData
248
- ? assign(baseParentProps, { data: parentData })
249
- : baseParentProps;
250
-
251
- const iteratee = (child) => {
252
- const sharedProps = assign({}, child.props, parentProps);
253
- if (!Domain.isDomainComponent(child)) {
254
- return null;
255
- } else if (child.type && isFunction(child.type.getDomain)) {
256
- return child.props && child.type.getDomain(sharedProps, axis);
257
- } else {
258
- return Domain.getDomain(sharedProps, axis);
259
- }
260
- };
261
-
262
- const childDomains = Helpers.reduceChildren(children, iteratee, props);
263
- const min =
264
- childDomains.length === 0 ? 0 : Collection.getMinValue(childDomains);
265
- const max =
266
- childDomains.length === 0 ? 1 : Collection.getMaxValue(childDomains);
267
- return [min, max];
268
- },
75
+ }
269
76
 
270
- addBinsToParentPropsIfHistogram({
77
+ return { ...parentProps, bins: childBins };
78
+ }
79
+
80
+ export function getDataFromChildren(props, childComponents) {
81
+ const { polar, startAngle, endAngle, categories, minDomain, maxDomain } =
82
+ props;
83
+ let parentProps = {
84
+ polar,
85
+ startAngle,
86
+ endAngle,
87
+ categories,
88
+ minDomain,
89
+ maxDomain
90
+ };
91
+ let stack = 0;
92
+ const children = childComponents
93
+ ? childComponents.slice(0)
94
+ : React.Children.toArray(props.children);
95
+
96
+ parentProps = addBinsToParentPropsIfHistogram({
271
97
  children,
272
98
  props,
273
99
  childComponents,
274
100
  parentProps
275
- }) {
276
- const someChildrenAreHistograms = children.some((child) => {
277
- return child.type && child.type.role === "histogram";
278
- });
101
+ });
102
+
103
+ const iteratee = (child, childName, parent) => {
104
+ const childProps = assign({}, child.props, parentProps);
105
+ let childData;
106
+ if (!Data.isDataComponent(child)) {
107
+ return null;
108
+ } else if (child.type && isFunction(child.type.getData)) {
109
+ child = parent ? React.cloneElement(child, parent.props) : child;
110
+ childData = child.type.getData(childProps);
111
+ } else {
112
+ childData = Data.getData(childProps);
113
+ }
114
+ stack += 1;
115
+ return childData.map((datum, index) =>
116
+ assign({ _stack: stack, _group: index }, datum)
117
+ );
118
+ };
279
119
 
280
- const allChildrenAreHistograms =
281
- someChildrenAreHistograms &&
282
- children.length &&
283
- children.every((child) => {
284
- return child.type && child.type.role === "histogram";
285
- });
120
+ const stacked = children.filter(
121
+ (c) => c.type && c.type.role === "stack"
122
+ ).length;
123
+ const combine = (memo, val) => memo.concat(uniqBy(val, "_group"));
124
+ const datasets = Helpers.reduceChildren(
125
+ children,
126
+ iteratee,
127
+ props,
128
+ [],
129
+ combine
130
+ );
131
+ const group = stacked ? "_group" : "_stack";
132
+ return values(groupBy(datasets, group));
133
+ }
134
+
135
+ export function getData(props, childComponents) {
136
+ if (props.data) {
137
+ return Data.getData(props);
138
+ }
139
+ childComponents = childComponents || React.Children.toArray(props.children);
140
+ return getDataFromChildren(childComponents);
141
+ }
142
+
143
+ export function getWidth(props, groupLength, seriesLength) {
144
+ const { datasets, horizontal } = props;
145
+ const range = horizontal
146
+ ? Helpers.getRange(props, "y")
147
+ : Helpers.getRange(props, "x");
148
+ const extent = Math.abs(range[1] - range[0]);
149
+ seriesLength =
150
+ seriesLength !== undefined
151
+ ? seriesLength
152
+ : (Array.isArray(datasets[0]) && datasets[0].length) || 1;
153
+ groupLength = groupLength || datasets.length;
154
+ const bars = groupLength * seriesLength;
155
+ const barRatio = 0.5;
156
+ return Math.round((barRatio * extent) / bars);
157
+ }
158
+
159
+ export function getDefaultDomainPadding(props, axis, childComponents) {
160
+ if (props.polar || axis !== "x") {
161
+ return undefined;
162
+ }
163
+ const groupComponent = childComponents.filter((child) => {
164
+ return child.type && child.type.role && child.type.role === "group";
165
+ });
286
166
 
287
- if (someChildrenAreHistograms && !allChildrenAreHistograms) {
288
- Log.warn(
289
- "VictoryHistogram only supports being stacked with other VictoryHistogram components. Check to make sure that you are only passing VictoryHistogram components to VictoryStack"
290
- );
291
- }
167
+ if (groupComponent.length < 1) {
168
+ return undefined;
169
+ }
170
+ const { offset, children } = groupComponent[0].props;
171
+ if (!offset) {
172
+ return undefined;
173
+ }
292
174
 
293
- // if we are stacking histograms, we need to generate explicit bins
294
- // or else each histogram may end up having different bins
295
- if (!allChildrenAreHistograms) {
296
- return parentProps;
175
+ const firstChild = Array.isArray(children) && children[0];
176
+ if (!firstChild) {
177
+ return undefined;
178
+ }
179
+ let barWidth = firstChild.props.barWidth;
180
+ let dataLength = (firstChild.props.data && firstChild.props.data.length) || 1;
181
+ if (firstChild && firstChild.type.role === "stack") {
182
+ const nestedChild =
183
+ firstChild.props.children && firstChild.props.children[0];
184
+ if (!nestedChild) {
185
+ return undefined;
297
186
  }
298
-
299
- let childBins = props.bins || childComponents[0].props.bins;
300
-
301
- // if we have explicit bins then we don't need to calculate them
302
- if (!Array.isArray(childBins)) {
303
- const combinedData = children.reduce((memo, child) => {
304
- const xAccessor = Helpers.createAccessor(child.props.x || "x");
305
- return memo.concat(
306
- child.props.data.map((datum) => ({ x: xAccessor(datum) }))
307
- );
308
- }, []);
309
-
310
- // use the same function to generate bins as VictoryHistogram but with
311
- // the combined data from above, then get explicit bins from that
312
- const getFormattedHistogramData = children[0].type.getFormattedData;
313
- childBins = getFormattedHistogramData({
314
- data: combinedData,
315
- bins: childBins
316
- }).reduce(
317
- (memo, { x0, x1 }, index) =>
318
- index === 0 ? memo.concat([x0, x1]) : memo.concat(x1),
319
- []
320
- );
187
+ barWidth = nestedChild.props.barWidth;
188
+ dataLength = firstChild.props.children.length;
189
+ }
190
+ const width = barWidth || getWidth(props, children.length, dataLength);
191
+ return {
192
+ x:
193
+ (width * children.length) / 2 +
194
+ (offset - width * ((children.length - 1) / 2))
195
+ };
196
+ }
197
+
198
+ export function getDomainFromChildren(props, axis, childComponents) {
199
+ // eslint-disable-line max-statements, complexity, max-len
200
+ const children = childComponents
201
+ ? childComponents.slice(0)
202
+ : React.Children.toArray(props.children);
203
+ const parentData = props.data ? Data.getData(props, axis) : undefined;
204
+ const {
205
+ polar,
206
+ startAngle,
207
+ endAngle,
208
+ categories,
209
+ minDomain,
210
+ maxDomain,
211
+ horizontal
212
+ } = props;
213
+ const baseParentProps = {
214
+ horizontal,
215
+ polar,
216
+ startAngle,
217
+ endAngle,
218
+ minDomain,
219
+ maxDomain,
220
+ categories
221
+ };
222
+ const parentProps = parentData
223
+ ? assign(baseParentProps, { data: parentData })
224
+ : baseParentProps;
225
+
226
+ const iteratee = (child) => {
227
+ const sharedProps = assign({}, child.props, parentProps);
228
+ if (!Domain.isDomainComponent(child)) {
229
+ return null;
230
+ } else if (child.type && isFunction(child.type.getDomain)) {
231
+ return child.props && child.type.getDomain(sharedProps, axis);
232
+ } else {
233
+ return Domain.getDomain(sharedProps, axis);
321
234
  }
235
+ };
236
+
237
+ const childDomains = Helpers.reduceChildren(children, iteratee, props);
238
+ const min =
239
+ childDomains.length === 0 ? 0 : Collection.getMinValue(childDomains);
240
+ const max =
241
+ childDomains.length === 0 ? 1 : Collection.getMaxValue(childDomains);
242
+ return [min, max];
243
+ }
244
+
245
+ export function getDomain(props, axis, childComponents) {
246
+ childComponents = childComponents || React.Children.toArray(props.children);
247
+
248
+ const propsDomain = Domain.getDomainFromProps(props, axis);
249
+ const domainPadding = getDefaultDomainPadding(props, axis, childComponents);
250
+ let domain;
251
+
252
+ if (propsDomain) {
253
+ domain = propsDomain;
254
+ } else {
255
+ const minDomain = Domain.getMinFromProps(props, axis);
256
+ const maxDomain = Domain.getMaxFromProps(props, axis);
257
+ const dataset = (props.data || props.y) && Data.getData(props);
258
+ const dataDomain = dataset
259
+ ? Domain.getDomainFromData(props, axis, dataset)
260
+ : [];
261
+ const childDomain = getDomainFromChildren(props, axis, childComponents);
262
+ const min =
263
+ minDomain || Collection.getMinValue([...dataDomain, ...childDomain]);
264
+ const max =
265
+ maxDomain || Collection.getMaxValue([...dataDomain, ...childDomain]);
266
+ domain = Domain.getDomainFromMinMax(min, max);
267
+ }
268
+ return Domain.formatDomain(domain, assign({ domainPadding }, props), axis);
269
+ }
322
270
 
323
- return { ...parentProps, bins: childBins };
324
- },
325
-
326
- getDataFromChildren(props, childComponents) {
327
- const { polar, startAngle, endAngle, categories, minDomain, maxDomain } =
328
- props;
329
- let parentProps = {
330
- polar,
331
- startAngle,
332
- endAngle,
333
- categories,
334
- minDomain,
335
- maxDomain
336
- };
337
- let stack = 0;
338
- const children = childComponents
339
- ? childComponents.slice(0)
340
- : React.Children.toArray(props.children);
341
-
342
- parentProps = this.addBinsToParentPropsIfHistogram({
343
- children,
344
- props,
345
- childComponents,
346
- parentProps
271
+ export function getScale(props, axis, childComponents) {
272
+ if (props.data) {
273
+ return Scale.getBaseScale(props, axis);
274
+ }
275
+ const children = childComponents
276
+ ? childComponents.slice(0)
277
+ : React.Children.toArray(props.children);
278
+ const iteratee = (child) => {
279
+ const sharedProps = assign({}, child.props, {
280
+ horizontal: props.horizontal
347
281
  });
348
-
349
- const iteratee = (child, childName, parent) => {
350
- const childProps = assign({}, child.props, parentProps);
351
- let childData;
352
- if (!Data.isDataComponent(child)) {
353
- return null;
354
- } else if (child.type && isFunction(child.type.getData)) {
355
- child = parent ? React.cloneElement(child, parent.props) : child;
356
- childData = child.type.getData(childProps);
357
- } else {
358
- childData = Data.getData(childProps);
359
- }
360
- stack += 1;
361
- return childData.map((datum, index) =>
362
- assign({ _stack: stack, _group: index }, datum)
363
- );
364
- };
365
-
366
- const stacked = children.filter(
367
- (c) => c.type && c.type.role === "stack"
368
- ).length;
369
- const combine = (memo, val) => memo.concat(uniqBy(val, "_group"));
370
- const datasets = Helpers.reduceChildren(
371
- children,
372
- iteratee,
373
- props,
374
- [],
375
- combine
376
- );
377
- const group = stacked ? "_group" : "_stack";
378
- return values(groupBy(datasets, group));
379
- },
380
-
381
- getColor(calculatedProps, child, index) {
382
- // check for styles first
383
- const { style } = calculatedProps;
384
- let { colorScale, color } = calculatedProps;
385
- if (style && style.data && style.data.fill) {
386
- return style.data.fill;
387
- }
388
- colorScale =
389
- child.props && child.props.colorScale
390
- ? child.props.colorScale
391
- : colorScale;
392
- color = child.props && child.props.color ? child.props.color : color;
393
- if (!colorScale && !color) {
394
- return undefined;
282
+ return Scale.getScaleType(sharedProps, axis);
283
+ };
284
+ const childScale = uniq(Helpers.reduceChildren(children, iteratee, props));
285
+ // default to linear scale if more than one uniq scale type is given by children
286
+
287
+ return childScale.length > 1
288
+ ? Scale.getScaleFromName("linear")
289
+ : Scale.getScaleFromName(childScale[0]);
290
+ }
291
+
292
+ export function getAllEvents(props) {
293
+ const components = ["groupComponent", "containerComponent", "labelComponent"];
294
+ const componentEvents = Events.getComponentEvents(props, components);
295
+ let events = props.events;
296
+ if (Array.isArray(componentEvents)) {
297
+ events = Array.isArray(props.events)
298
+ ? componentEvents.concat(...props.events)
299
+ : componentEvents;
300
+ }
301
+ return events || [];
302
+ }
303
+
304
+ export function getColor(calculatedProps, child, index) {
305
+ // check for styles first
306
+ const { style } = calculatedProps;
307
+ let { colorScale, color } = calculatedProps;
308
+ if (style && style.data && style.data.fill) {
309
+ return style.data.fill;
310
+ }
311
+ colorScale =
312
+ child.props && child.props.colorScale ? child.props.colorScale : colorScale;
313
+ color = child.props && child.props.color ? child.props.color : color;
314
+ if (!colorScale && !color) {
315
+ return undefined;
316
+ }
317
+ const colors = Array.isArray(colorScale)
318
+ ? colorScale
319
+ : Style.getColorScale(colorScale);
320
+ return color || colors[index % colors.length];
321
+ }
322
+
323
+ export function getStyle(theme, style, role) {
324
+ const defaultStyle =
325
+ theme && theme[role] && theme[role].style ? theme[role].style : {};
326
+ return Helpers.getStyles(style, defaultStyle);
327
+ }
328
+
329
+ export function getChildStyle(child, index, calculatedProps) {
330
+ const { style, role } = calculatedProps;
331
+ const childStyle = child.props.style || {};
332
+ if (Array.isArray(childStyle)) {
333
+ return childStyle;
334
+ }
335
+ const childRole = child.type && child.type.role;
336
+ const defaultFill =
337
+ childRole === "stack" ? undefined : getColor(calculatedProps, child, index);
338
+ const defaultColor =
339
+ childRole === "line"
340
+ ? { fill: "none", stroke: defaultFill }
341
+ : { fill: defaultFill };
342
+ const dataWidth =
343
+ role === "stack" ? {} : { width: getWidth(calculatedProps) };
344
+ const dataStyle = defaults(
345
+ {},
346
+ childStyle.data,
347
+ assign({}, dataWidth, style.data, defaultColor)
348
+ );
349
+ const labelsStyle = defaults({}, childStyle.labels, style.labels);
350
+ return {
351
+ parent: style.parent,
352
+ data: dataStyle,
353
+ labels: labelsStyle
354
+ };
355
+ }
356
+
357
+ export function getStringsFromCategories(childComponents, axis) {
358
+ const iteratee = (child) => {
359
+ const childProps = child.props || {};
360
+ if (!Domain.isDomainComponent(child) || !childProps.categories) {
361
+ return null;
362
+ } else {
363
+ const categories =
364
+ childProps.categories && !Array.isArray(childProps.categories)
365
+ ? childProps.categories[axis]
366
+ : childProps.props.categories;
367
+ const categoryStrings =
368
+ categories && categories.filter((val) => typeof val === "string");
369
+ return categoryStrings ? Collection.removeUndefined(categoryStrings) : [];
395
370
  }
396
- const colors = Array.isArray(colorScale)
397
- ? colorScale
398
- : Style.getColorScale(colorScale);
399
- return color || colors[index % colors.length];
400
- },
401
-
402
- getWidth(props, groupLength, seriesLength) {
403
- const { datasets, horizontal } = props;
404
- const range = horizontal
405
- ? Helpers.getRange(props, "y")
406
- : Helpers.getRange(props, "x");
407
- const extent = Math.abs(range[1] - range[0]);
408
- seriesLength =
409
- seriesLength !== undefined
410
- ? seriesLength
411
- : (Array.isArray(datasets[0]) && datasets[0].length) || 1;
412
- groupLength = groupLength || datasets.length;
413
- const bars = groupLength * seriesLength;
414
- const barRatio = 0.5;
415
- return Math.round((barRatio * extent) / bars);
416
- },
417
-
418
- getStyle(theme, style, role) {
419
- const defaultStyle =
420
- theme && theme[role] && theme[role].style ? theme[role].style : {};
421
- return Helpers.getStyles(style, defaultStyle);
422
- },
423
-
424
- getChildStyle(child, index, calculatedProps) {
425
- const { style, role } = calculatedProps;
426
- const childStyle = child.props.style || {};
427
- if (Array.isArray(childStyle)) {
428
- return childStyle;
371
+ };
372
+ return Helpers.reduceChildren(childComponents.slice(0), iteratee);
373
+ }
374
+
375
+ export function getStringsFromData(childComponents) {
376
+ const iteratee = (child) => {
377
+ const childProps = child.props || {};
378
+ let data;
379
+ if (!Data.isDataComponent(child)) {
380
+ return null;
381
+ } else if (child.type && isFunction(child.type.getData)) {
382
+ data = child.type.getData(childProps);
383
+ } else {
384
+ data = Data.getData(childProps);
429
385
  }
430
- const childRole = child.type && child.type.role;
431
- const defaultFill =
432
- childRole === "stack"
433
- ? undefined
434
- : this.getColor(calculatedProps, child, index);
435
- const defaultColor =
436
- childRole === "line"
437
- ? { fill: "none", stroke: defaultFill }
438
- : { fill: defaultFill };
439
- const dataWidth =
440
- role === "stack" ? {} : { width: this.getWidth(calculatedProps) };
441
- const dataStyle = defaults(
442
- {},
443
- childStyle.data,
444
- assign({}, dataWidth, style.data, defaultColor)
445
- );
446
- const labelsStyle = defaults({}, childStyle.labels, style.labels);
386
+ return data.map((d) => ({ x: d.xName, y: d.yName }));
387
+ };
388
+
389
+ const initialMemo = { x: [], y: [] };
390
+ const combine = (memo, datum) => {
391
+ const x = Array.isArray(datum)
392
+ ? datum.map((d) => d.x).filter(Boolean)
393
+ : datum.x;
394
+ const y = Array.isArray(datum)
395
+ ? datum.map((d) => d.y).filter(Boolean)
396
+ : datum.y;
447
397
  return {
448
- parent: style.parent,
449
- data: dataStyle,
450
- labels: labelsStyle
451
- };
452
- },
453
-
454
- getStringsFromCategories(childComponents, axis) {
455
- const iteratee = (child) => {
456
- const childProps = child.props || {};
457
- if (!Domain.isDomainComponent(child) || !childProps.categories) {
458
- return null;
459
- } else {
460
- const categories =
461
- childProps.categories && !Array.isArray(childProps.categories)
462
- ? childProps.categories[axis]
463
- : childProps.props.categories;
464
- const categoryStrings =
465
- categories && categories.filter((val) => typeof val === "string");
466
- return categoryStrings
467
- ? Collection.removeUndefined(categoryStrings)
468
- : [];
469
- }
470
- };
471
- return Helpers.reduceChildren(childComponents.slice(0), iteratee);
472
- },
473
-
474
- getStringsFromData(childComponents) {
475
- const iteratee = (child) => {
476
- const childProps = child.props || {};
477
- let data;
478
- if (!Data.isDataComponent(child)) {
479
- return null;
480
- } else if (child.type && isFunction(child.type.getData)) {
481
- data = child.type.getData(childProps);
482
- } else {
483
- data = Data.getData(childProps);
484
- }
485
- return data.map((d) => ({ x: d.xName, y: d.yName }));
398
+ x: x !== undefined ? memo.x.concat(x) : memo.x,
399
+ y: y !== undefined ? memo.y.concat(y) : memo.y
486
400
  };
487
-
488
- const initialMemo = { x: [], y: [] };
489
- const combine = (memo, datum) => {
490
- const x = Array.isArray(datum)
491
- ? datum.map((d) => d.x).filter(Boolean)
492
- : datum.x;
493
- const y = Array.isArray(datum)
494
- ? datum.map((d) => d.y).filter(Boolean)
495
- : datum.y;
496
- return {
497
- x: x !== undefined ? memo.x.concat(x) : memo.x,
498
- y: y !== undefined ? memo.y.concat(y) : memo.y
499
- };
500
- };
501
- return Helpers.reduceChildren(
502
- childComponents.slice(0),
503
- iteratee,
504
- {},
505
- initialMemo,
506
- combine
507
- );
508
- },
509
-
510
- getCategoryAndAxisStringsFromChildren(props, axis, childComponents) {
511
- const categories = isPlainObject(props.categories)
512
- ? props.categories[axis]
401
+ };
402
+ return Helpers.reduceChildren(
403
+ childComponents.slice(0),
404
+ iteratee,
405
+ {},
406
+ initialMemo,
407
+ combine
408
+ );
409
+ }
410
+
411
+ export function getCategoryAndAxisStringsFromChildren(
412
+ props,
413
+ axis,
414
+ childComponents
415
+ ) {
416
+ const categories = isPlainObject(props.categories)
417
+ ? props.categories[axis]
418
+ : props.categories;
419
+ const axisComponent = Axis.getAxisComponent(childComponents, axis);
420
+ const axisStrings = axisComponent
421
+ ? Data.getStringsFromAxes(axisComponent.props, axis)
422
+ : [];
423
+ const categoryStrings =
424
+ categories || getStringsFromCategories(childComponents, axis);
425
+ return uniq(flatten([...categoryStrings, ...axisStrings]));
426
+ }
427
+
428
+ export function getStringsFromChildren(props, childComponents) {
429
+ childComponents = childComponents || React.Children.toArray(props.children);
430
+
431
+ const xStrings = getCategoryAndAxisStringsFromChildren(
432
+ props,
433
+ "x",
434
+ childComponents
435
+ );
436
+ const yStrings = getCategoryAndAxisStringsFromChildren(
437
+ props,
438
+ "y",
439
+ childComponents
440
+ );
441
+
442
+ const dataStrings = getStringsFromData(childComponents);
443
+
444
+ return {
445
+ x: uniq(flatten([...xStrings, ...dataStrings.x])),
446
+ y: uniq(flatten([...yStrings, ...dataStrings.y]))
447
+ };
448
+ }
449
+
450
+ export function getCategories(props, childComponents, allStrings) {
451
+ const xPropCategories =
452
+ props.categories && !Array.isArray(props.categories)
453
+ ? props.categories.x
513
454
  : props.categories;
514
- const axisComponent = Axis.getAxisComponent(childComponents, axis);
515
- const axisStrings = axisComponent
516
- ? Data.getStringsFromAxes(axisComponent.props, axis)
517
- : [];
518
- const categoryStrings =
519
- categories || this.getStringsFromCategories(childComponents, axis);
520
- return uniq(flatten([...categoryStrings, ...axisStrings]));
521
- },
522
-
523
- getStringsFromChildren(props, childComponents) {
524
- childComponents = childComponents || React.Children.toArray(props.children);
525
-
526
- const xStrings = this.getCategoryAndAxisStringsFromChildren(
527
- props,
528
- "x",
529
- childComponents
530
- );
531
- const yStrings = this.getCategoryAndAxisStringsFromChildren(
532
- props,
533
- "y",
534
- childComponents
535
- );
536
-
537
- const dataStrings = this.getStringsFromData(childComponents);
538
-
539
- return {
540
- x: uniq(flatten([...xStrings, ...dataStrings.x])),
541
- y: uniq(flatten([...yStrings, ...dataStrings.y]))
542
- };
543
- },
544
-
545
- getCategories(props, childComponents, allStrings) {
546
- const xPropCategories =
547
- props.categories && !Array.isArray(props.categories)
548
- ? props.categories.x
549
- : props.categories;
550
455
 
551
- const yPropCategories =
552
- props.categories && !Array.isArray(props.categories)
553
- ? props.categories.y
554
- : props.categories;
456
+ const yPropCategories =
457
+ props.categories && !Array.isArray(props.categories)
458
+ ? props.categories.y
459
+ : props.categories;
555
460
 
556
- const fallbackRequired = !xPropCategories || !yPropCategories;
461
+ const fallbackRequired = !xPropCategories || !yPropCategories;
557
462
 
558
- const fallbackProps = fallbackRequired
559
- ? allStrings || this.getStringsFromChildren(props, childComponents)
560
- : {};
463
+ const fallbackProps = fallbackRequired
464
+ ? allStrings || getStringsFromChildren(props, childComponents)
465
+ : {};
561
466
 
562
- const xCategories = xPropCategories || fallbackProps.x;
563
- const yCategories = yPropCategories || fallbackProps.y;
467
+ const xCategories = xPropCategories || fallbackProps.x;
468
+ const yCategories = yPropCategories || fallbackProps.y;
564
469
 
565
- return {
566
- x: xCategories.length > 0 ? xCategories : undefined,
567
- y: yCategories.length > 0 ? yCategories : undefined
568
- };
569
- }
570
- };
470
+ return {
471
+ x: xCategories.length > 0 ? xCategories : undefined,
472
+ y: yCategories.length > 0 ? yCategories : undefined
473
+ };
474
+ }