Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Ru Chern Chong committed Nov 1, 2023
1 parent 65d46cf commit b92f3f6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
20 changes: 9 additions & 11 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import Image from "next/image";
import type { ChartData } from "chart.js";
import { LineChart } from "@/components/LineChart";
import { getCarRegistrationByMake } from "@/lib/getCarRegistrationByMake";
import { transformDataToDatasets } from "@/lib/transformDataToDatasets";
import { generateUniqueRandomHexColours } from "@/lib/generateUniqueRandomHexColours";
import { Car } from "@/types";
import type { ChartDataset, Dataset } from "@/types";

const Home = async () => {
const electricVehicles = await getCarRegistrationByMake(
`http://localhost:3000/data/M03-Car_Regn_by_make.csv`,
);

const datasetColour = generateUniqueRandomHexColours(electricVehicles);
const datasets = transformDataToDatasets(electricVehicles).map(
(ev: Car, i: number) => {
return {
...ev,
borderColor: datasetColour[i],
};
},
);
const datasets: ChartDataset[] = transformDataToDatasets(
electricVehicles,
).map((car: Dataset, i: number) => ({
...car,
borderColor: datasetColour[i],
}));

const data = {
const data: ChartData<"line"> = {
labels: [...new Set(electricVehicles.map(({ month }) => month))],
datasets,
};
Expand Down
8 changes: 5 additions & 3 deletions lib/getCarRegistrationByMake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import * as d3 from "d3";
import { FUEL_TYPE } from "@/config";
import type { Car } from "@/types";

export const getCarRegistrationByMake = async (filePath: string) => {
const csvContent = await fetch(filePath).then((res) => res.text());
export const getCarRegistrationByMake = async (
filePath: string,
): Promise<Car[]> => {
const csvContent: string = await fetch(filePath).then((res) => res.text());
const carRegistrationByMake: Car[] = d3.csvParse(csvContent, (car: Car) => ({
...car,
number: +car.number,
}));

const electricCars = carRegistrationByMake
const electricCars: Car[] = carRegistrationByMake
.filter(
({ fuel_type, number }) =>
fuel_type === FUEL_TYPE.ELECTRIC && number !== 0,
Expand Down
37 changes: 24 additions & 13 deletions lib/transformDataToDatasets.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
export const transformDataToDatasets = (inputData) => {
const transformedData = inputData.map((item) => ({
label: item.make,
data: [item.number],
import type { Car, Dataset } from "@/types";

export const transformDataToDatasets = (inputData: Car[]): Dataset[] => {
const transformedData: Dataset[] = inputData.map(({ make, number }: Car) => ({
label: make,
data: [number],
}));

const datasets = transformedData.reduce((acc, item) => {
const existingDataset = acc.find((dataset) => dataset.label === item.label);
if (existingDataset) {
existingDataset.data.push(item.data[0]);
} else {
acc.push(item);
}
return acc;
}, []);
const datasets: Dataset[] = transformedData.reduce(
(acc: Dataset[], item: Dataset) => {
const existingDataset = acc.find(
(dataset) => dataset.label === item.label,
);

if (existingDataset) {
existingDataset.data.push(item.data[0]);
} else {
acc.push(item);
}

return acc;
},
[],
);

console.log({ datasets });

return datasets;
};
9 changes: 9 additions & 0 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,12 @@ export interface Car {
fuel_type: string;
number: any;
}

export type Dataset = {
label: string;
data: number[];
};

export interface ChartDataset extends Dataset {
borderColor: string;
}

0 comments on commit b92f3f6

Please sign in to comment.