This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 88
/
victory-stack.js
247 lines (223 loc) · 8.08 KB
/
victory-stack.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
import { assign, defaults } from "lodash";
import PropTypes from "prop-types";
import React from "react";
import { Helpers, VictorySharedEvents, VictoryContainer, VictoryTheme, Scale } from "victory-core";
import Wrapper from "../../helpers/wrapper";
import { BaseProps } from "../../helpers/common-props";
const fallbackProps = {
width: 450,
height: 300,
padding: 50
};
export default class VictoryStack extends React.Component {
static displayName = "VictoryStack";
static role = "stack";
static propTypes = {
...BaseProps,
categories: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.string),
PropTypes.shape({
x: PropTypes.arrayOf(PropTypes.string), y: PropTypes.arrayOf(PropTypes.string)
})
]),
children: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.node), PropTypes.node
]),
colorScale: PropTypes.oneOfType([
PropTypes.arrayOf(PropTypes.string),
PropTypes.oneOf([
"grayscale", "qualitative", "heatmap", "warm", "cool", "red", "green", "blue"
])
]),
fillInMissingData: PropTypes.bool,
horizontal: PropTypes.bool,
labelComponent: PropTypes.element,
labels: PropTypes.oneOfType([ PropTypes.func, PropTypes.array ]),
style: PropTypes.shape({
parent: PropTypes.object, data: PropTypes.object, labels: PropTypes.object
}),
xOffset: PropTypes.number
};
static defaultProps = {
containerComponent: <VictoryContainer/>,
groupComponent: <g/>,
scale: "linear",
standalone: true,
theme: VictoryTheme. grayscale,
fillInMissingData: true
};
static expectedComponents = [
"groupComponent", "containerComponent", "labelComponent"
];
static getDomain = Wrapper.getStackedDomain.bind(Wrapper);
static getData = Wrapper.getData.bind(Wrapper);
constructor(props) {
super(props);
if (props.animate) {
this.state = {
nodesShouldLoad: false,
nodesDoneLoad: false,
animating: true
};
this.setAnimationState = Wrapper.setAnimationState.bind(this);
this.events = Wrapper.getAllEvents(props);
}
}
componentWillMount() {
this.events = Wrapper.getAllEvents(this.props);
}
componentWillReceiveProps(nextProps) {
if (this.props.animate) {
this.setAnimationState(this.props, nextProps);
}
this.events = Wrapper.getAllEvents(nextProps);
}
getCalculatedProps(props, childComponents) {
const { role } = this.constructor;
const style = this.getStyle(props.theme, props.style, role);
const horizontal = props.horizontal || childComponents.every(
(component) => component.props.horizontal
);
const dataFromChildren = Wrapper.getDataFromChildren(props);
const datasets = Wrapper.fillInMissingData(props, dataFromChildren);
const domain = {
x: Wrapper.getStackedDomain(props, "x", datasets),
y: Wrapper.getStackedDomain(props, "y", datasets)
};
const range = {
x: Helpers.getRange(props, "x"),
y: Helpers.getRange(props, "y")
};
const baseScale = {
x: Scale.getScaleFromProps(props, "x") || Scale.getDefaultScale(),
y: Scale.getScaleFromProps(props, "y") || Scale.getDefaultScale()
};
const xScale = baseScale.x.domain(domain.x).range(range.x);
const yScale = baseScale.y.domain(domain.y).range(range.y);
const scale = {
x: horizontal ? yScale : xScale,
y: horizontal ? xScale : yScale
};
const categories = {
x: Wrapper.getCategories(props, "x"),
y: Wrapper.getCategories(props, "y")
};
const colorScale = props.colorScale;
return { datasets, categories, range, domain, horizontal, scale, style, colorScale, role };
}
/* eslint-disable max-params, no-nested-ternary */
addLayoutData(props, calculatedProps, datasets, index) {
const xOffset = props.xOffset || 0;
return datasets[index].map((datum) => {
const yOffset = Wrapper.getY0(datum, index, calculatedProps) || 0;
return assign({}, datum, {
_y0: !(datum._y instanceof Date) ? yOffset : (
yOffset ? new Date(yOffset) : datum._y
),
_y1: datum._y === null ? null : (
datum._y instanceof Date ? new Date(+datum._y + +yOffset) : datum._y + yOffset
),
_x1: datum._x === null ? null : (
datum._x instanceof Date ? new Date(+datum._x + +xOffset) : datum._x + xOffset
)
});
});
}
/* eslint-enable max-params, no-nested-ternary */
getLabels(props, datasets, index) {
if (!props.labels) {
return undefined;
}
return datasets.length === index + 1 ? props.labels : undefined;
}
getChildProps(props, calculatedProps) {
const { categories, domain, range, scale, horizontal } = calculatedProps;
return {
height: props.height,
width: props.width,
padding: Helpers.getPadding(props),
standalone: false,
theme: props.theme,
categories,
domain,
range,
scale,
horizontal
};
}
getColorScale(props, child) {
const role = child.type && child.type.role;
const colorScaleOptions = child.props.colorScale || props.colorScale;
if (role !== "group" && role !== "stack") {
return undefined;
}
return props.theme ? colorScaleOptions || props.theme.props.colorScale
: colorScaleOptions;
}
// the old ones were bad
getNewChildren(props, childComponents, calculatedProps) {
const { datasets } = calculatedProps;
const childProps = this.getChildProps(props, calculatedProps);
const getAnimationProps = Wrapper.getAnimationProps.bind(this);
return childComponents.map((child, index) => {
const data = this.addLayoutData(props, calculatedProps, datasets, index);
const style = Wrapper.getChildStyle(child, index, calculatedProps);
const labels = props.labels ? this.getLabels(props, datasets, index) : child.props.labels;
return React.cloneElement(child, assign({
animate: getAnimationProps(props, child, index),
key: index,
labels,
domainPadding: child.props.domainPadding || props.domainPadding,
theme: props.theme,
labelComponent: props.labelComponent || child.props.labelComponent,
style,
colorScale: this.getColorScale(props, child),
data,
polar: props.polar
}, childProps));
});
}
renderContainer(containerComponent, props) {
const containerProps = defaults({}, containerComponent.props, props);
return React.cloneElement(containerComponent, containerProps);
}
getContainerProps(props, calculatedProps) {
const { width, height, standalone, theme, polar } = props;
const { domain, scale, style, origin } = calculatedProps;
return {
domain, scale, width, height, standalone, theme, style: style.parent, polar, origin
};
}
getStyle(theme, style, role) {
const defaultStyle = theme && theme[role] && theme[role].style ? theme[role].style : {};
return Helpers.getStyles(style, defaultStyle);
}
render() {
const { role } = this.constructor;
const props = this.state && this.state.nodesWillExit ?
this.state.oldProps || this.props : this.props;
const modifiedProps = Helpers.modifyProps(props, fallbackProps, role);
const {
eventKey, containerComponent, standalone, groupComponent, externalEventMutations
} = modifiedProps;
const childComponents = React.Children.toArray(modifiedProps.children);
const calculatedProps = this.getCalculatedProps(modifiedProps, childComponents);
const newChildren = this.getNewChildren(modifiedProps, childComponents, calculatedProps);
const containerProps = standalone ? this.getContainerProps(modifiedProps, calculatedProps) : {};
const container = standalone ?
this.renderContainer(containerComponent, containerProps) : groupComponent;
if (this.events) {
return (
<VictorySharedEvents
container={container}
eventKey={eventKey}
events={this.events}
externalEventMutations={externalEventMutations}
>
{newChildren}
</VictorySharedEvents>
);
}
return React.cloneElement(container, container.props, newChildren);
}
}