Skip to content

Commit 245626a

Browse files
committed
feat: chart
1 parent f995a13 commit 245626a

File tree

2 files changed

+33
-38
lines changed

2 files changed

+33
-38
lines changed

frontend/src/components/Chart.tsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,30 @@ import {
2121
Legend,
2222
ResponsiveContainer,
2323
} from 'recharts';
24-
25-
export enum FuelType {
26-
e5, e10, diesel
27-
}
24+
import { useEffect, useState, SyntheticEvent } from 'react';
2825

2926
export interface TimeSlot {
30-
x: Date;
31-
y: number;
32-
fuelType: FuelType;
27+
x: string;
28+
e5: number;
29+
e10: number;
30+
diesel: number;
3331
}
3432

3533
export interface ChartProps {
36-
e5: TimeSlot[];
37-
e10: TimeSlot[];
38-
diesel: TimeSlot[];
34+
timeSlots: TimeSlot[];
3935
}
4036

37+
const values = [{diesel: 1700, e10: 1750, e5: 1800, x: '19:00'} as TimeSlot, {diesel: 1710, e10: 1760, e5: 1810, x: '19:30'} as TimeSlot]
38+
4139
export default function Chart(props: ChartProps) {
42-
43-
return ( (props?.diesel?.length > 0 || props?.e10?.length > 0 || props?.e5?.length > 0) ?
40+
const [avgTimeSlots, setAvgTimeSlots] = useState([] as TimeSlot[])
41+
if(props.timeSlots.length !== avgTimeSlots.length) {
42+
setAvgTimeSlots(props.timeSlots);
43+
}
44+
//console.log(avgTimeSlots);
45+
//console.log(avgTimeSlots.filter(value => value.e5 > 10).map(value => value.e5));
46+
console.log(props.timeSlots);
47+
return ( props.timeSlots.length > 0 ?
4448
<ResponsiveContainer width="100%" height={300}>
4549
<ScatterChart
4650
margin={{
@@ -51,16 +55,16 @@ export default function Chart(props: ChartProps) {
5155
}}
5256
>
5357
<CartesianGrid />
54-
<XAxis type="number" dataKey="x" name="time" />
55-
<YAxis type="number" dataKey="y" name="price" />
58+
<XAxis type="category" dataKey="x" name="time" />
59+
<YAxis type="number" dataKey="e5" name="price" />
5660
<ZAxis type="number" range={[100]} />
5761
<Tooltip cursor={{ strokeDasharray: '3 3' }} />
5862
<Legend />
59-
<Scatter name="E5" data={props.e5} fill="#8884d8" line shape="cross" />
60-
<Scatter name="E10" data={props.e10} fill="#82ca9d" line shape="diamond" />
61-
<Scatter name="Diesel" data={props.diesel} fill="#ff8042" line shape="triangle" />
63+
<Scatter name="E5" data={avgTimeSlots.filter(value => value.e5 > 10).map(value => value.e5)} fill="#8884d8" line shape="cross" />
64+
<Scatter name="E10" data={avgTimeSlots.filter(value => value.e10 > 10).map(value => value.e10)} fill="#82ca9d" line shape="diamond" />
65+
<Scatter name="Diesel" data={avgTimeSlots.filter(value => value.diesel > 10).map(value => value.diesel)} fill="#ff8042" line shape="triangle" />
6266
</ScatterChart>
6367
</ResponsiveContainer>
6468
: <div></div>
6569
);
66-
}
70+
}

frontend/src/components/Main.tsx

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import GsMap, { GsValue } from './GsMap';
1717
import { useRecoilRefresher_UNSTABLE, useRecoilValue } from 'recoil';
1818
import GlobalState from '../GlobalState';
1919
import styles from './main.module.scss';
20-
import Chart, {FuelType, TimeSlot} from './Chart';
20+
import Chart, {TimeSlot} from './Chart';
2121

2222
interface GasPriceAvgs {
2323
Postcode: string
@@ -204,7 +204,7 @@ export default function Main() {
204204
signal: controller?.signal
205205
}
206206
fetch(`/usernotification/current/${globalUserUuidState}`, requestOptions1).then(myResult => myResult?.json() as Promise<Notification[]>).then(myJson => {
207-
console.log(myJson);
207+
//console.log(myJson);
208208
const result = myJson?.map(value => {
209209
//console.log(JSON.parse(value?.DataJson));
210210
return (JSON.parse(value?.DataJson) as MyDataJson[])?.map(value2 => {
@@ -226,25 +226,16 @@ export default function Main() {
226226
fetch(`/postcode/countytimeslots/${myPostcode}`, requestOptions2).then(myResult1 => myResult1.json() as Promise<TimeSlotResponse[]>).then(myJson1 => {
227227
const timeSlots = [] as TimeSlot[];
228228
timeSlots.push(...myJson1.filter(myValue => myValue.AvgDiesel > 10).map(myValue => {
229-
let dieselTimeSlot = {fuelType: FuelType.diesel, x: new Date(), y: 0} as TimeSlot;
230-
dieselTimeSlot.x = myValue.StartDate;
231-
dieselTimeSlot.y = myValue.AvgDiesel;
229+
const dieselTimeSlot = {x: '', diesel: 0, e10: 0, e5: 0} as TimeSlot;
230+
const myDate = new Date(myValue.StartDate);
231+
dieselTimeSlot.x = ''+myDate.getHours()+':'+myDate.getMinutes();
232+
dieselTimeSlot.diesel = myValue.AvgDiesel;
233+
dieselTimeSlot.e10 = myValue.AvgE10;
234+
dieselTimeSlot.e5 = myValue.AvgE5;
232235
return dieselTimeSlot;
233-
}));
234-
timeSlots.push(...myJson1.filter(myValue => myValue.AvgE10 > 10).map(myValue => {
235-
let e10TimeSlot = {fuelType: FuelType.e10, x: new Date(), y: 0} as TimeSlot;
236-
e10TimeSlot.x = myValue.StartDate;
237-
e10TimeSlot.y = myValue.AvgE10;
238-
return e10TimeSlot;
239-
}));
240-
timeSlots.push(...myJson1.filter(myValue => myValue.AvgE5 > 10).map(myValue => {
241-
let e5TimeSlot = {fuelType: FuelType.e5, x: new Date(), y: 0} as TimeSlot;
242-
e5TimeSlot.x = myValue.StartDate;
243-
e5TimeSlot.y = myValue.AvgE5;
244-
return e5TimeSlot;
245-
}));
236+
}));
246237
setAvgTimeSlots(timeSlots);
247-
console.log(myJson1);
238+
//console.log(myJson1);
248239
});
249240
})
250241
.then(() => setController(null));
@@ -268,7 +259,7 @@ export default function Main() {
268259
<DataTable diesel='Diesel' e10='E10' e5='E5' location='Location' showAverages={true} time='Time' rows={rows}></DataTable>
269260
</TabPanel>
270261
<TabPanel value={value} index={1}>
271-
<Chart e5={avgTimeSlots.filter(value => value.fuelType === FuelType.e5)} e10={avgTimeSlots.filter(value => value.fuelType === FuelType.e10)} diesel={avgTimeSlots.filter(value => value.fuelType === FuelType.diesel)}></Chart>
262+
<Chart timeSlots={avgTimeSlots}></Chart>
272263
<DataTable diesel='Diesel' e10='E10' e5='E5' location='Location' showAverages={true} time='Time' rows={rows}></DataTable>
273264
</TabPanel>
274265
<TabPanel value={value} index={2}>

0 commit comments

Comments
 (0)