|
| 1 | +/* |
| 2 | + - Copyright 2022 Sven Loesekann |
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + Unless required by applicable law or agreed to in writing, software |
| 8 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + See the License for the specific language governing permissions and |
| 11 | + limitations under the License. |
| 12 | +*/ |
| 13 | +import * as React from 'react'; |
| 14 | +import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts'; |
| 15 | +import { useEffect, useState } from 'react'; |
| 16 | +import Radio from '@mui/material/Radio'; |
| 17 | +import RadioGroup from '@mui/material/RadioGroup'; |
| 18 | +import FormControlLabel from '@mui/material/FormControlLabel'; |
| 19 | +import FormControl from '@mui/material/FormControl'; |
| 20 | +import GlobalState, { FuelType } from '../GlobalState'; |
| 21 | +import { type TimeSlot } from '../model/time-slot-response'; |
| 22 | +import { type GsPoint } from '../model/gs-point'; |
| 23 | +import { useAtom } from "jotai"; |
| 24 | + |
| 25 | +export interface ChartProps { |
| 26 | + timeSlots: TimeSlot[]; |
| 27 | +} |
| 28 | + |
| 29 | +export default function Chart(props: ChartProps) { |
| 30 | + //console.log(props.timeSlots); |
| 31 | + const [gsValues, setGsValues] = useState([] as GsPoint[]); |
| 32 | + const [fuelTypeState, setfuelTypeState] = useAtom(GlobalState.fuelTypeState); |
| 33 | + const [lineColor, setLineColor] = useState('#8884d8'); |
| 34 | + const [avgValue, setAvgValue] = useState(0.0); |
| 35 | + const [timeSlots, setTimeSlots] = useState([] as TimeSlot[]); |
| 36 | + |
| 37 | + // eslint-disable-next-line |
| 38 | + useEffect(() => { |
| 39 | + if(props.timeSlots.length > 0 && timeSlots !== props.timeSlots) { |
| 40 | + updateChart(); |
| 41 | + setTimeSlots(props.timeSlots); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + function updateChart() { |
| 46 | + const avg = props.timeSlots.reduce((acc, value) => value[fuelTypeState] + acc, 0) / (props.timeSlots.length || 1); |
| 47 | + if (fuelTypeState === FuelType.E5) { |
| 48 | + setLineColor('#8884d8'); |
| 49 | + setAvgValue(avg); |
| 50 | + setGsValues(props.timeSlots.map(myValue => ({ timestamp: myValue.x, price: myValue.e5 - avg } as GsPoint))) |
| 51 | + } else if (fuelTypeState === FuelType.E10) { |
| 52 | + setLineColor('#82ca9d'); |
| 53 | + setAvgValue(avg); |
| 54 | + setGsValues(props.timeSlots.map(myValue => ({ timestamp: myValue.x, price: myValue.e10 - avg } as GsPoint))) |
| 55 | + } else { |
| 56 | + setLineColor('#82caff'); |
| 57 | + setAvgValue(avg); |
| 58 | + setGsValues(props.timeSlots.map(myValue => ({ timestamp: myValue.x, price: myValue.diesel - avg } as GsPoint))) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => { |
| 63 | + setfuelTypeState(((event.target as HTMLInputElement).value) as FuelType); |
| 64 | + setTimeSlots([]); |
| 65 | + updateChart(); |
| 66 | + }; |
| 67 | + return (<div> |
| 68 | + <ResponsiveContainer width="100%" height={300}> |
| 69 | + <LineChart data={gsValues} |
| 70 | + margin={{ top: 20, right: 20, left: 20, bottom: 20 }}> |
| 71 | + <CartesianGrid strokeDasharray="3 3" /> |
| 72 | + <XAxis dataKey="timestamp" /> |
| 73 | + <YAxis /> |
| 74 | + <Tooltip /> |
| 75 | + <Legend /> |
| 76 | + <Line name={'AvgPrice: '+(Math.round(avgValue*1000)/1000)} type="monotone" dataKey="price" stroke={lineColor} /> |
| 77 | + </LineChart> |
| 78 | + </ResponsiveContainer> |
| 79 | + <FormControl> |
| 80 | + <RadioGroup |
| 81 | + row |
| 82 | + aria-labelledby="demo-row-radio-buttons-group-label" |
| 83 | + name="row-radio-buttons-group" |
| 84 | + value={fuelTypeState} |
| 85 | + onChange={handleChange} |
| 86 | + > |
| 87 | + <FormControlLabel value={FuelType.E5} control={<Radio />} label="E5" /> |
| 88 | + <FormControlLabel value={FuelType.E10} control={<Radio />} label="E10" /> |
| 89 | + <FormControlLabel value={FuelType.Diesel} control={<Radio />} label="Diesel" /> |
| 90 | + </RadioGroup> |
| 91 | + </FormControl> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +} |
0 commit comments