@@ -28,67 +28,61 @@ See the [Animations Guide][] for more detail on animations and transitions
28
28
_ example:_ ` animate={{ duration: 2000 }} `
29
29
30
30
``` playground_norender
31
- class App extends React.Component {
32
-
33
- render() {
34
- return (
35
- <VictoryChart
36
- domain={{ y: [0, 1] }}
37
- animate={{ duration: 2000 }}
38
- >
39
- <VictoryScatter
40
- size={this.state.size}
41
- data={this.state.data}
42
- style={{ data: { opacity: ({ datum }) => datum.opacity || 1 } }}
43
- animate={{
44
- animationWhitelist: ["style", "data", "size"], // Try removing "size"
45
- onExit: {
46
- duration: 500,
47
- before: () => ({ opacity: 0.3, _y: 0 })
48
- },
49
- onEnter: {
50
- duration: 500,
51
- before: () => ({ opacity: 0.3, _y: 0 }),
52
- after: (datum) => ({ opacity: 1, _y: datum._y })
53
- }
54
- }}
55
- />
56
- </VictoryChart>
57
- );
58
- }
59
-
60
- constructor(props) {
61
- super(props);
62
- this.state = {
63
- data: this.getData(),
64
- size: this.getSize()
65
- };
66
- }
67
-
68
- componentDidMount() {
69
- this.setStateInterval = window.setInterval(() => {
70
- this.setState({
71
- data: this.getData(),
72
- size: this.getSize()
31
+ function App(props) {
32
+ const [state, setState] = React.useState({
33
+ data: getData(),
34
+ size: getSize()
35
+ });
36
+
37
+ React.useEffect(() => {
38
+ const setStateInterval = window.setInterval(() => {
39
+ setState({
40
+ data: getData(),
41
+ size: getSize()
73
42
});
74
43
}, 3000);
75
- }
76
44
77
- componentWillUnmount() {
78
- window.clearInterval(this.setStateInterval);
79
- }
45
+ return () => {
46
+ window.clearInterval(setStateInterval);
47
+ };
48
+ }, []);
80
49
81
- getData() {
82
- const num = Math.floor(10 * Math.random() + 5);
83
- const points = new Array(num).fill(1);
84
- return points.map((point, index) => {
85
- return { x: index + 1, y: Math.random() };
86
- });
87
- }
50
+ return (
51
+ <VictoryChart
52
+ domain={{ y: [0, 1] }}
53
+ animate={{ duration: 2000 }}
54
+ >
55
+ <VictoryScatter
56
+ size={state.size}
57
+ data={state.data}
58
+ style={{ data: { opacity: ({ datum }) => datum.opacity || 1 } }}
59
+ animate={{
60
+ animationWhitelist: ["style", "data", "size"], // Try removing "size"
61
+ onExit: {
62
+ duration: 500,
63
+ before: () => ({ opacity: 0.3, _y: 0 })
64
+ },
65
+ onEnter: {
66
+ duration: 500,
67
+ before: () => ({ opacity: 0.3, _y: 0 }),
68
+ after: (datum) => ({ opacity: 1, _y: datum._y })
69
+ }
70
+ }}
71
+ />
72
+ </VictoryChart>
73
+ );
74
+ }
88
75
89
- getSize() {
90
- return Math.random() * 10
91
- }
76
+ function getData() {
77
+ const num = Math.floor(10 * Math.random() + 5);
78
+ const points = new Array(num).fill(1);
79
+ return points.map((point, index) => {
80
+ return { x: index + 1, y: Math.random() };
81
+ });
82
+ }
83
+
84
+ function getSize() {
85
+ return Math.random() * 10
92
86
}
93
87
94
88
render(<App/>);
@@ -201,31 +195,29 @@ See the [Custom Components Guide][] for more detail on creating your own `dataCo
201
195
_ examples:_ ` dataComponent={<Area/>} `
202
196
203
197
``` playground_norender
204
- class CatPoint extends React.Component {
205
- render() {
206
- const {x, y, datum} = this.props; // VictoryScatter supplies x, y and datum
207
- const cat = datum._y >= 0 ? "😻" : "😹";
208
- return (
209
- <text x={x} y={y} fontSize={30}>
210
- {cat}
211
- </text>
212
- );
213
- }
198
+ function CatPoint(props) {
199
+ const {x, y, datum} = props; // VictoryScatter supplies x, y and datum
200
+ const cat = datum._y >= 0 ? "😻" : "😹";
201
+
202
+ return (
203
+ <text x={x} y={y} fontSize={30}>
204
+ {cat}
205
+ </text>
206
+ );
214
207
}
215
208
216
- class App extends React.Component {
217
- render() {
218
- return (
219
- <VictoryChart>
220
- <VictoryScatter
221
- dataComponent={<CatPoint/>}
222
- y={(d) => Math.sin(2 * Math.PI * d.x)}
223
- samples={15}
224
- />
225
- </VictoryChart>
226
- );
227
- }
209
+ function App() {
210
+ return (
211
+ <VictoryChart>
212
+ <VictoryScatter
213
+ dataComponent={<CatPoint/>}
214
+ y={(d) => Math.sin(2 * Math.PI * d.x)}
215
+ samples={15}
216
+ />
217
+ </VictoryChart>
218
+ );
228
219
}
220
+
229
221
render(<App/>);
230
222
```
231
223
@@ -418,77 +410,74 @@ externalEventMutations: PropTypes.arrayOf(
418
410
The ` target ` , ` eventKey ` , and ` childName ` (when applicable) must always be specified. The ` mutation ` function will be called with the current props of the element specified by the ` target ` , ` eventKey ` and ` childName ` provided. The mutation function should return a mutation object for that element. The ` callback ` prop should be used to clear the ` externalEventMutations ` prop once the mutation has been applied. Clearing ` externalEventMutations ` is crucial for charts that animate.
419
411
420
412
``` playground_norender
421
- class App extends React.Component {
422
- constructor() {
423
- super();
424
- this.state = {
425
- externalMutations: undefined
426
- };
427
- }
428
- removeMutation() {
429
- this.setState({
413
+ function App() {
414
+ const [state, setState] = React.useState({
415
+ externalMutations: undefined
416
+ });
417
+
418
+ function removeMutation() {
419
+ setState({
430
420
externalMutations: undefined
431
421
});
432
422
}
433
423
434
- clearClicks() {
435
- this. setState({
424
+ function clearClicks() {
425
+ setState({
436
426
externalMutations: [
437
427
{
438
428
childName: "Bar-1",
439
429
target: ["data"],
440
430
eventKey: "all",
441
431
mutation: () => ({ style: undefined }),
442
- callback: this. removeMutation.bind(this)
432
+ callback: removeMutation
443
433
}
444
434
]
445
435
});
446
436
}
447
437
448
- render() {
449
- const buttonStyle = {
450
- backgroundColor: "black",
451
- color: "white",
452
- padding: "10px",
453
- marginTop: "10px"
454
- };
455
- return (
456
- <div>
457
- <button
458
- onClick={this.clearClicks.bind(this)}
459
- style={buttonStyle}
460
- >
461
- Reset
462
- </button>
463
- <VictoryChart domain={{ x: [0, 5 ] }}
464
- externalEventMutations={this.state.externalMutations}
465
- events={[
466
- {
467
- target: "data",
468
- childName: "Bar-1",
469
- eventHandlers: {
470
- onClick: () => ({
471
- target: "data",
472
- mutation: () => ({ style: { fill: "orange" } })
473
- })
474
- }
438
+ const buttonStyle = {
439
+ backgroundColor: "black",
440
+ color: "white",
441
+ padding: "10px",
442
+ marginTop: "10px"
443
+ };
444
+
445
+ return (
446
+ <div>
447
+ <button
448
+ onClick={clearClicks}
449
+ style={buttonStyle}
450
+ >
451
+ Reset
452
+ </button>
453
+ <VictoryChart domain={{ x: [0, 5 ] }}
454
+ externalEventMutations={state.externalMutations}
455
+ events={[
456
+ {
457
+ target: "data",
458
+ childName: "Bar-1",
459
+ eventHandlers: {
460
+ onClick: () => ({
461
+ target: "data",
462
+ mutation: () => ({ style: { fill: "orange" } })
463
+ })
475
464
}
465
+ }
466
+ ]}
467
+ >
468
+ <VictoryBar name="Bar-1"
469
+ style={{ data: { fill: "grey"} }}
470
+ labels={() => "click me!"}
471
+ data={[
472
+ { x: 1, y: 2 },
473
+ { x: 2, y: 4 },
474
+ { x: 3, y: 1 },
475
+ { x: 4, y: 5 }
476
476
]}
477
- >
478
- <VictoryBar name="Bar-1"
479
- style={{ data: { fill: "grey"} }}
480
- labels={() => "click me!"}
481
- data={[
482
- { x: 1, y: 2 },
483
- { x: 2, y: 4 },
484
- { x: 3, y: 1 },
485
- { x: 4, y: 5 }
486
- ]}
487
- />
488
- </VictoryChart>
489
- </div>
490
- )
491
- }
477
+ />
478
+ </VictoryChart>
479
+ </div>
480
+ )
492
481
}
493
482
494
483
render(<App/>);
0 commit comments