Skip to content

Commit 337d6b5

Browse files
committed
fix: charts
1 parent 10adb09 commit 337d6b5

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

frontend/src/components/Chart.tsx

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,40 +36,41 @@ export interface ChartProps {
3636
}
3737

3838
export default function Chart(props: ChartProps) {
39-
//console.log(props.timeSlots);
40-
const [updated, setUpdated] = useState(0);
39+
//console.log(props.timeSlots);
4140
const [gsValues, setGsValues] = useState([] as GsPoint[]);
4241
const [fuelTypeState, setfuelTypeState] = useRecoilState(GlobalState.fuelTypeState);
4342
const [lineColor, setLineColor] = useState('#8884d8');
4443
const [avgValue, setAvgValue] = useState(0.0);
44+
const [timeSlots, setTimeSlots] = useState([] as TimeSlot[]);
4545

4646
// eslint-disable-next-line
4747
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);
5151
}
5252
});
5353

5454
function updateChart() {
55+
const avg = props.timeSlots.reduce((acc, value) => value[fuelTypeState] + acc, 0) / (props.timeSlots.length || 1);
5556
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)))
5960
} 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)))
6364
} 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)))
6768
}
6869
}
6970

7071
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
7172
setfuelTypeState(((event.target as HTMLInputElement).value) as FuelType);
72-
setUpdated(0);
73+
setTimeSlots([]);
7374
updateChart();
7475
};
7576
return (<div>

0 commit comments

Comments
 (0)