-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ru Chern Chong
committed
Nov 3, 2023
1 parent
3545b57
commit db609fd
Showing
3 changed files
with
79 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
"use client"; | ||
|
||
import { useState } from "react"; | ||
import { LineChart } from "@/components/LineChart"; | ||
import { ChartData, ChartOptions } from "chart.js"; | ||
import { generateUniqueRandomHexColours } from "@/lib/generateUniqueRandomHexColours"; | ||
import { transformDataToDatasets } from "@/lib/transformDataToDatasets"; | ||
import type { Car, ChartDataset, Dataset } from "@/types"; | ||
|
||
type InfographicProps = { | ||
electricVehicles: Car[]; | ||
}; | ||
|
||
export const Infographic = ({ electricVehicles }: InfographicProps) => { | ||
const datasetColour: string[] = | ||
generateUniqueRandomHexColours(electricVehicles); | ||
|
||
const initialDatasets: ChartDataset[] = transformDataToDatasets( | ||
electricVehicles, | ||
).map((car: Dataset, i: number) => { | ||
const isPopular = car.data.some((item) => item >= 10); | ||
|
||
return { | ||
...car, | ||
borderColor: datasetColour[i], | ||
checked: isPopular, | ||
}; | ||
}); | ||
|
||
const [datasets, setDatasets] = useState(initialDatasets); | ||
|
||
const data: ChartData<"line"> = { | ||
labels: [...new Set(electricVehicles.map(({ month }) => month))], | ||
datasets: datasets.filter(({ checked }) => checked), | ||
}; | ||
|
||
const options: ChartOptions = { | ||
maintainAspectRatio: false, | ||
}; | ||
|
||
const handleMakeChange = (index: number) => () => { | ||
const newDatasets = [...datasets]; | ||
newDatasets[index].checked = !datasets[index].checked; | ||
setDatasets(newDatasets); | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className="aspect-video w-full"> | ||
<LineChart data={data} options={options} /> | ||
</div> | ||
<fieldset className="prose dark:prose-invert prose-neutral flex flex-wrap gap-2"> | ||
<legend>Make</legend> | ||
{datasets.map(({ label, checked }, index) => { | ||
const key = `make-${label}`; | ||
|
||
return ( | ||
<> | ||
<input | ||
key={key} | ||
type="checkbox" | ||
id={key} | ||
value={label} | ||
defaultChecked={checked} | ||
onChange={handleMakeChange(index)} | ||
/> | ||
<label htmlFor={key}>{label}</label> | ||
</> | ||
); | ||
})} | ||
</fieldset> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters