1
- import React , { PureComponent } from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
import {
3
- Label ,
4
3
LineChart ,
5
4
Line ,
6
5
CartesianGrid ,
@@ -58,25 +57,21 @@ const initialState = {
58
57
animation : true ,
59
58
} ;
60
59
61
- export default class Example extends PureComponent {
62
-
63
-
64
- constructor ( props ) {
65
- super ( props ) ;
66
- this . state = initialState ;
67
- }
68
-
69
- zoom ( ) {
70
- let { refAreaLeft, refAreaRight } = this . state ;
71
- const { data } = this . state ;
60
+ const Example = ( ) => {
61
+ const [ state , setState ] = useState ( initialState ) ;
62
+
63
+ const zoom = ( ) => {
64
+ let { refAreaLeft, refAreaRight } = state ;
65
+ const { data } = state ;
72
66
73
67
if ( refAreaLeft === refAreaRight || refAreaRight === '' ) {
74
- this . setState ( ( ) => ( {
68
+ setState ( ( prevState ) => ( {
69
+ ...prevState ,
75
70
refAreaLeft : '' ,
76
71
refAreaRight : '' ,
77
72
} ) ) ;
78
73
return ;
79
- }
74
+ } ;
80
75
81
76
// xAxis domain
82
77
if ( refAreaLeft > refAreaRight ) [ refAreaLeft , refAreaRight ] = [ refAreaRight , refAreaLeft ] ;
@@ -85,7 +80,8 @@ export default class Example extends PureComponent {
85
80
const [ bottom , top ] = getAxisYDomain ( refAreaLeft , refAreaRight , 'cost' , 1 ) ;
86
81
const [ bottom2 , top2 ] = getAxisYDomain ( refAreaLeft , refAreaRight , 'impression' , 50 ) ;
87
82
88
- this . setState ( ( ) => ( {
83
+ setState ( ( prevState ) => ( {
84
+ ...prevState ,
89
85
refAreaLeft : '' ,
90
86
refAreaRight : '' ,
91
87
data : data . slice ( ) ,
@@ -96,11 +92,12 @@ export default class Example extends PureComponent {
96
92
bottom2,
97
93
top2,
98
94
} ) ) ;
99
- }
95
+ } ;
100
96
101
- zoomOut ( ) {
102
- const { data } = this . state ;
103
- this . setState ( ( ) => ( {
97
+ const zoomOut = ( ) => {
98
+ const { data } = state ;
99
+ setState ( ( prevState ) => ( {
100
+ ...prevState ,
104
101
data : data . slice ( ) ,
105
102
refAreaLeft : '' ,
106
103
refAreaRight : '' ,
@@ -111,41 +108,50 @@ export default class Example extends PureComponent {
111
108
top2 : 'dataMax+50' ,
112
109
bottom2 : 'dataMin+50' ,
113
110
} ) ) ;
114
- }
115
-
116
- render ( ) {
117
- const { data, barIndex, left, right, refAreaLeft, refAreaRight, top, bottom, top2, bottom2 } = this . state ;
118
-
119
- return (
120
- < div className = "highlight-bar-charts" style = { { userSelect : 'none' , width : '100%' } } >
121
- < button type = "button" className = "btn update" onClick = { this . zoomOut . bind ( this ) } >
122
- Zoom Out
123
- </ button >
124
-
125
- < ResponsiveContainer width = "100%" height = { 400 } >
126
- < LineChart
127
- width = { 800 }
128
- height = { 400 }
129
- data = { data }
130
- onMouseDown = { ( e ) => this . setState ( { refAreaLeft : e . activeLabel } ) }
131
- onMouseMove = { ( e ) => this . state . refAreaLeft && this . setState ( { refAreaRight : e . activeLabel } ) }
132
- // eslint-disable-next-line react/jsx-no-bind
133
- onMouseUp = { this . zoom . bind ( this ) }
134
- >
135
- < CartesianGrid strokeDasharray = "3 3" />
136
- < XAxis allowDataOverflow dataKey = "name" domain = { [ left , right ] } type = "number" />
137
- < YAxis allowDataOverflow domain = { [ bottom , top ] } type = "number" yAxisId = "1" />
138
- < YAxis orientation = "right" allowDataOverflow domain = { [ bottom2 , top2 ] } type = "number" yAxisId = "2" />
139
- < Tooltip />
140
- < Line yAxisId = "1" type = "natural" dataKey = "cost" stroke = "#8884d8" animationDuration = { 300 } />
141
- < Line yAxisId = "2" type = "natural" dataKey = "impression" stroke = "#82ca9d" animationDuration = { 300 } />
111
+ } ;
112
+
113
+ const {
114
+ data,
115
+ refAreaLeft,
116
+ refAreaRight,
117
+ left,
118
+ right,
119
+ top,
120
+ bottom,
121
+ top2,
122
+ bottom2,
123
+ } = state ;
124
+
125
+ return (
126
+ < div className = "highlight-bar-charts" style = { { userSelect : 'none' , width : '100%' } } >
127
+ < button type = "button" className = "btn update" onClick = { zoomOut } >
128
+ Zoom Out
129
+ </ button >
130
+
131
+ < ResponsiveContainer width = "100%" height = { 400 } >
132
+ < LineChart
133
+ width = { 800 }
134
+ height = { 400 }
135
+ data = { data }
136
+ onMouseDown = { ( e ) => setState ( prevState => ( { ...prevState , refAreaLeft : e . activeLabel } ) ) }
137
+ onMouseMove = { ( e ) => state . refAreaLeft && setState ( prevState => ( { ...prevState , refAreaRight : e . activeLabel } ) ) }
138
+ onMouseUp = { zoom }
139
+ >
140
+ < CartesianGrid strokeDasharray = "3 3" />
141
+ < XAxis allowDataOverflow dataKey = "name" domain = { [ left , right ] } type = "number" />
142
+ < YAxis allowDataOverflow domain = { [ bottom , top ] } type = "number" yAxisId = "1" />
143
+ < YAxis orientation = "right" allowDataOverflow domain = { [ bottom2 , top2 ] } type = "number" yAxisId = "2" />
144
+ < Tooltip />
145
+ < Line yAxisId = "1" type = "natural" dataKey = "cost" stroke = "#8884d8" animationDuration = { 300 } />
146
+ < Line yAxisId = "2" type = "natural" dataKey = "impression" stroke = "#82ca9d" animationDuration = { 300 } />
147
+
148
+ { refAreaLeft && refAreaRight ? (
149
+ < ReferenceArea yAxisId = "1" x1 = { refAreaLeft } x2 = { refAreaRight } strokeOpacity = { 0.3 } />
150
+ ) : null }
151
+ </ LineChart >
152
+ </ ResponsiveContainer >
153
+ </ div >
154
+ ) ;
155
+ } ;
142
156
143
- { refAreaLeft && refAreaRight ? (
144
- < ReferenceArea yAxisId = "1" x1 = { refAreaLeft } x2 = { refAreaRight } strokeOpacity = { 0.3 } />
145
- ) : null }
146
- </ LineChart >
147
- </ ResponsiveContainer >
148
- </ div >
149
- ) ;
150
- }
151
- }
157
+ export default Example ;
0 commit comments