Skip to content

Commit dba242b

Browse files
committed
feat: line chart
1 parent b11d705 commit dba242b

File tree

1 file changed

+75
-30
lines changed

1 file changed

+75
-30
lines changed

frontend/src/components/Chart.tsx

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,86 @@
1010
See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
13-
import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts';
13+
import { LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer } from 'recharts';
14+
import { useEffect, useState } from 'react';
15+
import Radio from '@mui/material/Radio';
16+
import RadioGroup from '@mui/material/RadioGroup';
17+
import FormControlLabel from '@mui/material/FormControlLabel';
18+
import FormControl from '@mui/material/FormControl';
19+
20+
enum FuelType {
21+
E5 = 'e5',
22+
E10 = 'e10',
23+
Diesel = 'diesel'
24+
}
25+
26+
export interface GsPoint {
27+
timestamp: string;
28+
price: number;
29+
}
1430

1531
export interface TimeSlot {
16-
x: string;
17-
e5: number;
18-
e10: number;
19-
diesel: number;
32+
x: string;
33+
e5: number;
34+
e10: number;
35+
diesel: number;
2036
}
2137

2238
export interface ChartProps {
23-
timeSlots: TimeSlot[];
39+
timeSlots: TimeSlot[];
2440
}
2541

26-
export default function Chart(props: ChartProps) {
27-
//console.log(props.timeSlots);
28-
return (
29-
<ResponsiveContainer width="100%" height={300}>
30-
<BarChart
31-
margin={{
32-
top: 20,
33-
right: 20,
34-
bottom: 20,
35-
left: 20,
36-
}}
37-
data={props.timeSlots}
38-
>
39-
<CartesianGrid strokeDasharray="3 3" />
40-
<XAxis dataKey="x" />
41-
<YAxis />
42-
<Tooltip />
43-
<Legend />
44-
<Bar dataKey="e5" fill="#8884d8" />
45-
<Bar dataKey="e10" fill="#82ca9d" />
46-
<Bar dataKey="diesel" fill="#82caff" />
47-
</BarChart>
48-
</ResponsiveContainer>
49-
);
42+
export default function Chart(props: ChartProps) {
43+
//console.log(props.timeSlots);
44+
const [gsValues, setGsValues] = useState([] as GsPoint[]);
45+
const [fuelType, setFuelType] = useState(FuelType.E5);
46+
const [lineColor, setLineColor] = useState('#8884d8')
47+
48+
// eslint-disable-next-line
49+
useEffect(() => {
50+
if (fuelType === FuelType.E5) {
51+
setLineColor('#8884d8')
52+
const avgValue = props.timeSlots.reduce((acc, value) => value.e5 + acc, 0) / (props.timeSlots.length || 1);
53+
setGsValues(props.timeSlots.map(myValue => ({ timestamp: myValue.x, price: myValue.e5 - avgValue } as GsPoint)))
54+
} else if (fuelType === FuelType.E10) {
55+
setLineColor('#82ca9d')
56+
const avgValue = props.timeSlots.reduce((acc, value) => value.e10 + acc, 0) / (props.timeSlots.length || 1);
57+
setGsValues(props.timeSlots.map(myValue => ({ timestamp: myValue.x, price: myValue.e10 - avgValue } as GsPoint)))
58+
} else {
59+
setLineColor('#82caff')
60+
const avgValue = props.timeSlots.reduce((acc, value) => value.diesel + acc, 0) / (props.timeSlots.length || 1);
61+
setGsValues(props.timeSlots.map(myValue => ({ timestamp: myValue.x, price: myValue.diesel - avgValue } as GsPoint)))
62+
}
63+
});
64+
65+
const handleChange = (event: React.ChangeEvent<HTMLInputElement>) => {
66+
setFuelType(((event.target as HTMLInputElement).value) as FuelType);
67+
};
68+
return (<div>
69+
<ResponsiveContainer width="100%" height={300}>
70+
<LineChart data={gsValues}
71+
margin={{ top: 20, right: 20, left: 20, bottom: 20 }}>
72+
<CartesianGrid strokeDasharray="3 3" />
73+
<XAxis dataKey="timestamp" />
74+
<YAxis />
75+
<Tooltip />
76+
<Legend />
77+
<Line type="monotone" dataKey="price" stroke={lineColor} />
78+
</LineChart>
79+
</ResponsiveContainer>
80+
<FormControl>
81+
<RadioGroup
82+
row
83+
aria-labelledby="demo-row-radio-buttons-group-label"
84+
name="row-radio-buttons-group"
85+
value={fuelType}
86+
onChange={handleChange}
87+
>
88+
<FormControlLabel value={FuelType.E5} control={<Radio />} label="E5" />
89+
<FormControlLabel value={FuelType.E10} control={<Radio />} label="E10" />
90+
<FormControlLabel value={FuelType.Diesel} control={<Radio />} label="Diesel" />
91+
</RadioGroup>
92+
</FormControl>
93+
</div>
94+
);
5095
}

0 commit comments

Comments
 (0)