victory-chart 35.11.2 → 36.1.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "victory-chart",
3
- "version": "35.11.2",
3
+ "version": "36.1.0",
4
4
  "description": "Chart Component for Victory",
5
5
  "keywords": [
6
6
  "data visualization",
@@ -22,16 +22,17 @@
22
22
  "lodash": "^4.17.19",
23
23
  "prop-types": "^15.5.8",
24
24
  "react-fast-compare": "^2.0.0",
25
- "victory-axis": "^35.11.2",
26
- "victory-core": "^35.11.2",
27
- "victory-polar-axis": "^35.11.2",
28
- "victory-shared-events": "^35.11.2"
25
+ "victory-axis": "^36.1.0",
26
+ "victory-core": "^36.1.0",
27
+ "victory-polar-axis": "^36.1.0",
28
+ "victory-shared-events": "^36.1.0"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "react": "^16.6.0 || ^17.0.0"
32
32
  },
33
33
  "scripts": {
34
- "version": "nps build-libs && nps build-dists"
34
+ "version": "yarn run nps build-libs && nps build-dists"
35
35
  },
36
- "sideEffects": false
36
+ "sideEffects": false,
37
+ "gitHead": "038cd9b70d9ce54ac6b861014165583760734c99"
37
38
  }
@@ -8,7 +8,9 @@ import {
8
8
  VictoryTheme,
9
9
  CommonProps,
10
10
  PropTypes as CustomPropTypes,
11
- Wrapper
11
+ Wrapper,
12
+ usePreviousProps,
13
+ useAnimationState
12
14
  } from "victory-core";
13
15
  import { VictorySharedEvents } from "victory-shared-events";
14
16
  import { VictoryAxis } from "victory-axis";
@@ -27,78 +29,47 @@ const fallbackProps = {
27
29
  padding: 50
28
30
  };
29
31
 
30
- export default class VictoryChart extends React.Component {
31
- static displayName = "VictoryChart";
32
-
33
- static propTypes = {
34
- ...CommonProps.baseProps,
35
- backgroundComponent: PropTypes.element,
36
- children: PropTypes.oneOfType([
37
- PropTypes.arrayOf(PropTypes.node),
38
- PropTypes.node
39
- ]),
40
- defaultAxes: PropTypes.shape({
41
- independent: PropTypes.element,
42
- dependent: PropTypes.element
43
- }),
44
- defaultPolarAxes: PropTypes.shape({
45
- independent: PropTypes.element,
46
- dependent: PropTypes.element
47
- }),
48
- endAngle: PropTypes.number,
49
- innerRadius: CustomPropTypes.nonNegative,
50
- prependDefaultAxes: PropTypes.bool,
51
- startAngle: PropTypes.number
52
- };
53
-
54
- static defaultProps = {
55
- backgroundComponent: <Background />,
56
- containerComponent: <VictoryContainer />,
57
- defaultAxes: {
58
- independent: <VictoryAxis />,
59
- dependent: <VictoryAxis dependentAxis />
60
- },
61
- defaultPolarAxes: {
62
- independent: <VictoryPolarAxis />,
63
- dependent: <VictoryPolarAxis dependentAxis />
64
- },
65
- groupComponent: <g />,
66
- standalone: true,
67
- theme: VictoryTheme.grayscale
68
- };
69
-
70
- static expectedComponents = ["groupComponent", "containerComponent"];
71
-
72
- constructor(props) {
73
- super(props);
74
- this.state = {};
75
- if (props.animate) {
76
- this.state = {
77
- nodesShouldLoad: false,
78
- nodesDoneLoad: false,
79
- animating: true
80
- };
81
- this.setAnimationState = Wrapper.setAnimationState.bind(this);
82
- }
83
- }
84
-
85
- shouldComponentUpdate(nextProps) {
86
- if (this.props.animate) {
87
- if (!isEqual(this.props, nextProps)) {
88
- this.setAnimationState(this.props, nextProps);
89
- return false;
90
- }
91
- }
92
- return true;
93
- }
94
-
95
- getNewChildren(props, childComponents, calculatedProps) {
32
+ const VictoryChart = (initialProps) => {
33
+ const role = "chart";
34
+ const { getAnimationProps, setAnimationState, getProps } =
35
+ useAnimationState();
36
+ const props = getProps(initialProps);
37
+
38
+ const modifiedProps = Helpers.modifyProps(props, fallbackProps, role);
39
+ const {
40
+ eventKey,
41
+ containerComponent,
42
+ standalone,
43
+ groupComponent,
44
+ externalEventMutations,
45
+ width,
46
+ height,
47
+ theme,
48
+ polar,
49
+ name
50
+ } = modifiedProps;
51
+
52
+ const axes = props.polar
53
+ ? modifiedProps.defaultPolarAxes
54
+ : modifiedProps.defaultAxes;
55
+
56
+ const childComponents = React.useMemo(
57
+ () => getChildComponents(modifiedProps, axes),
58
+ [modifiedProps, axes]
59
+ );
60
+
61
+ const calculatedProps = React.useMemo(
62
+ () => getCalculatedProps(modifiedProps, childComponents),
63
+ [modifiedProps, childComponents]
64
+ );
65
+ const { domain, scale, style, origin, radius, horizontal } = calculatedProps;
66
+
67
+ const newChildren = React.useMemo(() => {
96
68
  const children = getChildren(props, childComponents, calculatedProps);
97
- const getAnimationProps = Wrapper.getAnimationProps.bind(this);
98
69
 
99
- const newChildren = children.map((child, index) => {
70
+ const mappedChildren = children.map((child, index) => {
100
71
  const childProps = assign(
101
- { animate: getAnimationProps(props, child, index) },
72
+ { animate: getAnimationProps(props, child, index, "victory chart") },
102
73
  child.props
103
74
  );
104
75
  return React.cloneElement(child, childProps);
@@ -110,80 +81,127 @@ export default class VictoryChart extends React.Component {
110
81
  calculatedProps
111
82
  );
112
83
 
113
- newChildren.unshift(backgroundComponent);
84
+ mappedChildren.unshift(backgroundComponent);
114
85
  }
115
86
 
116
- return newChildren;
117
- }
87
+ return mappedChildren;
88
+ }, [getAnimationProps, childComponents, props, calculatedProps]);
89
+
90
+ const containerProps = React.useMemo(() => {
91
+ if (standalone) {
92
+ return {
93
+ domain,
94
+ scale,
95
+ width,
96
+ height,
97
+ standalone,
98
+ theme,
99
+ style: style.parent,
100
+ horizontal,
101
+ name,
102
+ polar,
103
+ radius,
104
+ origin: polar ? origin : undefined
105
+ };
106
+ }
107
+ return {};
108
+ }, [
109
+ domain,
110
+ scale,
111
+ width,
112
+ height,
113
+ standalone,
114
+ theme,
115
+ style,
116
+ horizontal,
117
+ name,
118
+ polar,
119
+ radius,
120
+ origin
121
+ ]);
122
+
123
+ const container = React.useMemo(() => {
124
+ if (standalone) {
125
+ const defaultContainerProps = defaults(
126
+ {},
127
+ containerComponent.props,
128
+ containerProps
129
+ );
130
+ return React.cloneElement(containerComponent, defaultContainerProps);
131
+ }
132
+ return groupComponent;
133
+ }, [groupComponent, standalone, containerComponent, containerProps]);
118
134
 
119
- renderContainer(containerComponent, props) {
120
- const containerProps = defaults({}, containerComponent.props, props);
121
- return React.cloneElement(containerComponent, containerProps);
122
- }
135
+ const events = React.useMemo(() => {
136
+ return Wrapper.getAllEvents(props);
137
+ }, [props]);
123
138
 
124
- getContainerProps(props, calculatedProps) {
125
- const { width, height, standalone, theme, polar, name } = props;
126
- const { domain, scale, style, origin, radius, horizontal } =
127
- calculatedProps;
128
- return {
129
- domain,
130
- scale,
131
- width,
132
- height,
133
- standalone,
134
- theme,
135
- style: style.parent,
136
- horizontal,
137
- name,
138
- polar,
139
- radius,
140
- origin: polar ? origin : undefined
141
- };
142
- }
139
+ const previousProps = usePreviousProps(initialProps);
143
140
 
144
- render() {
145
- const props =
146
- this.state && this.state.nodesWillExit
147
- ? this.state.oldProps || this.props
148
- : this.props;
149
- const modifiedProps = Helpers.modifyProps(props, fallbackProps, "chart");
150
- const {
151
- eventKey,
152
- containerComponent,
153
- groupComponent,
154
- standalone,
155
- externalEventMutations
156
- } = modifiedProps;
157
- const axes = props.polar
158
- ? modifiedProps.defaultPolarAxes
159
- : modifiedProps.defaultAxes;
160
- const childComponents = getChildComponents(modifiedProps, axes);
161
- const calculatedProps = getCalculatedProps(modifiedProps, childComponents);
162
- const newChildren = this.getNewChildren(
163
- modifiedProps,
164
- childComponents,
165
- calculatedProps
141
+ React.useEffect(() => {
142
+ // This is called before dismount to keep state in sync
143
+ return () => {
144
+ if (initialProps.animate) {
145
+ setAnimationState(previousProps, initialProps);
146
+ }
147
+ };
148
+ }, [setAnimationState, previousProps, initialProps]);
149
+
150
+ if (!isEmpty(events)) {
151
+ return (
152
+ <VictorySharedEvents
153
+ container={container}
154
+ eventKey={eventKey}
155
+ events={events}
156
+ externalEventMutations={externalEventMutations}
157
+ >
158
+ {newChildren}
159
+ </VictorySharedEvents>
166
160
  );
167
- const containerProps = standalone
168
- ? this.getContainerProps(modifiedProps, calculatedProps)
169
- : {};
170
- const container = standalone
171
- ? this.renderContainer(containerComponent, containerProps)
172
- : groupComponent;
173
- const events = Wrapper.getAllEvents(props);
174
-
175
- if (!isEmpty(events)) {
176
- return (
177
- <VictorySharedEvents
178
- container={container}
179
- eventKey={eventKey}
180
- events={events}
181
- externalEventMutations={externalEventMutations}
182
- >
183
- {newChildren}
184
- </VictorySharedEvents>
185
- );
186
- }
187
- return React.cloneElement(container, container.props, newChildren);
188
161
  }
189
- }
162
+ return React.cloneElement(container, container.props, newChildren);
163
+ };
164
+
165
+ VictoryChart.propTypes = {
166
+ ...CommonProps.baseProps,
167
+ backgroundComponent: PropTypes.element,
168
+ children: PropTypes.oneOfType([
169
+ PropTypes.arrayOf(PropTypes.node),
170
+ PropTypes.node
171
+ ]),
172
+ defaultAxes: PropTypes.shape({
173
+ independent: PropTypes.element,
174
+ dependent: PropTypes.element
175
+ }),
176
+ defaultPolarAxes: PropTypes.shape({
177
+ independent: PropTypes.element,
178
+ dependent: PropTypes.element
179
+ }),
180
+ endAngle: PropTypes.number,
181
+ innerRadius: CustomPropTypes.nonNegative,
182
+ prependDefaultAxes: PropTypes.bool,
183
+ startAngle: PropTypes.number
184
+ };
185
+
186
+ VictoryChart.defaultProps = {
187
+ backgroundComponent: <Background />,
188
+ containerComponent: <VictoryContainer />,
189
+ defaultAxes: {
190
+ independent: <VictoryAxis />,
191
+ dependent: <VictoryAxis dependentAxis />
192
+ },
193
+ defaultPolarAxes: {
194
+ independent: <VictoryPolarAxis />,
195
+ dependent: <VictoryPolarAxis dependentAxis />
196
+ },
197
+ groupComponent: <g />,
198
+ standalone: true,
199
+ theme: VictoryTheme.grayscale
200
+ };
201
+
202
+ const VictoryChartMemo = React.memo(VictoryChart, isEqual);
203
+
204
+ VictoryChartMemo.displayName = "VictoryChart";
205
+ VictoryChartMemo.expectedComponents = ["groupComponent", "containerComponent"];
206
+
207
+ export default VictoryChartMemo;