@@ -43,21 +43,22 @@ export default function Chart(props: ChartProps) {
43
43
//console.log(props.timeSlots);
44
44
const [ gsValues , setGsValues ] = useState ( [ ] as GsPoint [ ] ) ;
45
45
const [ fuelType , setFuelType ] = useState ( FuelType . E5 ) ;
46
- const [ lineColor , setLineColor ] = useState ( '#8884d8' )
46
+ const [ lineColor , setLineColor ] = useState ( '#8884d8' ) ;
47
+ const [ avgValue , setAvgValue ] = useState ( 0.0 ) ;
47
48
48
49
// eslint-disable-next-line
49
50
useEffect ( ( ) => {
50
51
if ( fuelType === FuelType . E5 ) {
51
52
setLineColor ( '#8884d8' )
52
- const avgValue = props . timeSlots . reduce ( ( acc , value ) => value . e5 + acc , 0 ) / ( props . timeSlots . length || 1 ) ;
53
+ setAvgValue ( props . timeSlots . reduce ( ( acc , value ) => value . e5 + acc , 0 ) / ( props . timeSlots . length || 1 ) ) ;
53
54
setGsValues ( props . timeSlots . map ( myValue => ( { timestamp : myValue . x , price : myValue . e5 - avgValue } as GsPoint ) ) )
54
55
} else if ( fuelType === FuelType . E10 ) {
55
56
setLineColor ( '#82ca9d' )
56
- const avgValue = props . timeSlots . reduce ( ( acc , value ) => value . e10 + acc , 0 ) / ( props . timeSlots . length || 1 ) ;
57
+ setAvgValue ( props . timeSlots . reduce ( ( acc , value ) => value . e10 + acc , 0 ) / ( props . timeSlots . length || 1 ) ) ;
57
58
setGsValues ( props . timeSlots . map ( myValue => ( { timestamp : myValue . x , price : myValue . e10 - avgValue } as GsPoint ) ) )
58
59
} else {
59
60
setLineColor ( '#82caff' )
60
- const avgValue = props . timeSlots . reduce ( ( acc , value ) => value . diesel + acc , 0 ) / ( props . timeSlots . length || 1 ) ;
61
+ setAvgValue ( props . timeSlots . reduce ( ( acc , value ) => value . diesel + acc , 0 ) / ( props . timeSlots . length || 1 ) ) ;
61
62
setGsValues ( props . timeSlots . map ( myValue => ( { timestamp : myValue . x , price : myValue . diesel - avgValue } as GsPoint ) ) )
62
63
}
63
64
} ) ;
@@ -74,7 +75,7 @@ export default function Chart(props: ChartProps) {
74
75
< YAxis />
75
76
< Tooltip />
76
77
< Legend />
77
- < Line type = "monotone" dataKey = "price" stroke = { lineColor } />
78
+ < Line name = { 'AvgPrice: ' + ( Math . round ( avgValue * 1000 ) / 1000 ) } type = "monotone" dataKey = "price" stroke = { lineColor } />
78
79
</ LineChart >
79
80
</ ResponsiveContainer >
80
81
< FormControl >
0 commit comments