@@ -36,40 +36,41 @@ export interface ChartProps {
36
36
}
37
37
38
38
export default function Chart ( props : ChartProps ) {
39
- //console.log(props.timeSlots);
40
- const [ updated , setUpdated ] = useState ( 0 ) ;
39
+ //console.log(props.timeSlots);
41
40
const [ gsValues , setGsValues ] = useState ( [ ] as GsPoint [ ] ) ;
42
41
const [ fuelTypeState , setfuelTypeState ] = useRecoilState ( GlobalState . fuelTypeState ) ;
43
42
const [ lineColor , setLineColor ] = useState ( '#8884d8' ) ;
44
43
const [ avgValue , setAvgValue ] = useState ( 0.0 ) ;
44
+ const [ timeSlots , setTimeSlots ] = useState ( [ ] as TimeSlot [ ] ) ;
45
45
46
46
// eslint-disable-next-line
47
47
useEffect ( ( ) => {
48
- if ( updated < 3 ) {
49
- setUpdated ( updated + 1 ) ;
50
- updateChart ( ) ;
48
+ if ( props . timeSlots . length > 0 && timeSlots !== props . timeSlots ) {
49
+ updateChart ( ) ;
50
+ setTimeSlots ( props . timeSlots ) ;
51
51
}
52
52
} ) ;
53
53
54
54
function updateChart ( ) {
55
+ const avg = props . timeSlots . reduce ( ( acc , value ) => value [ fuelTypeState ] + acc , 0 ) / ( props . timeSlots . length || 1 ) ;
55
56
if ( fuelTypeState === FuelType . E5 ) {
56
- setLineColor ( '#8884d8' )
57
- setAvgValue ( props . timeSlots . reduce ( ( acc , value ) => value . e5 + acc , 0 ) / ( props . timeSlots . length || 1 ) ) ;
58
- setGsValues ( props . timeSlots . map ( myValue => ( { timestamp : myValue . x , price : myValue . e5 - avgValue } as GsPoint ) ) )
57
+ setLineColor ( '#8884d8' ) ;
58
+ setAvgValue ( avg ) ;
59
+ setGsValues ( props . timeSlots . map ( myValue => ( { timestamp : myValue . x , price : myValue . e5 - avg } as GsPoint ) ) )
59
60
} else if ( fuelTypeState === FuelType . E10 ) {
60
- setLineColor ( '#82ca9d' )
61
- setAvgValue ( props . timeSlots . reduce ( ( acc , value ) => value . e10 + acc , 0 ) / ( props . timeSlots . length || 1 ) ) ;
62
- setGsValues ( props . timeSlots . map ( myValue => ( { timestamp : myValue . x , price : myValue . e10 - avgValue } as GsPoint ) ) )
61
+ setLineColor ( '#82ca9d' ) ;
62
+ setAvgValue ( avg ) ;
63
+ setGsValues ( props . timeSlots . map ( myValue => ( { timestamp : myValue . x , price : myValue . e10 - avg } as GsPoint ) ) )
63
64
} else {
64
- setLineColor ( '#82caff' )
65
- setAvgValue ( props . timeSlots . reduce ( ( acc , value ) => value . diesel + acc , 0 ) / ( props . timeSlots . length || 1 ) ) ;
66
- setGsValues ( props . timeSlots . map ( myValue => ( { timestamp : myValue . x , price : myValue . diesel - avgValue } as GsPoint ) ) )
65
+ setLineColor ( '#82caff' ) ;
66
+ setAvgValue ( avg ) ;
67
+ setGsValues ( props . timeSlots . map ( myValue => ( { timestamp : myValue . x , price : myValue . diesel - avg } as GsPoint ) ) )
67
68
}
68
69
}
69
70
70
71
const handleChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
71
72
setfuelTypeState ( ( ( event . target as HTMLInputElement ) . value ) as FuelType ) ;
72
- setUpdated ( 0 ) ;
73
+ setTimeSlots ( [ ] ) ;
73
74
updateChart ( ) ;
74
75
} ;
75
76
return ( < div >
0 commit comments