@@ -19,14 +19,19 @@ import {
1919 ComposedChart ,
2020 Bar ,
2121} from "recharts" ;
22- import { ChartConfig , ChartContainer , ChartLegend , ChartLegendContent } from "@/components/ui/chart" ;
22+ import {
23+ ChartConfig ,
24+ ChartContainer ,
25+ ChartLegend ,
26+ ChartLegendContent ,
27+ } from "@/components/ui/chart" ;
2328
2429export function Covid19LineChart ( {
25- years,
30+ years,
2631 data,
2732} : {
2833 years : string [ ] ;
29- data : Covid19Record [ ] ;
34+ data : Covid19Record [ ] ;
3035} ) {
3136 const [ selectedYears , setSelectedYears ] = useState < string [ ] > ( [
3237 "2020" ,
@@ -35,10 +40,7 @@ export function Covid19LineChart({
3540 "2023" ,
3641 "2024" ,
3742 ] ) ;
38- const type : string [ ] = [
39- "Covid-19" ,
40- "Vaccine"
41- ] ;
43+ const type : string [ ] = [ "Covid-19" , "Vaccine" ] ;
4244 const [ selectedType , setSelectedType ] = useState < string [ ] > ( [
4345 "Covid-19" ,
4446 "Vaccine" ,
@@ -56,30 +58,32 @@ export function Covid19LineChart({
5658
5759 const filteredData = useMemo ( ( ) => {
5860 if ( selectedYears . length === 0 || selectedType . length === 0 ) {
59- return [ ] ;
61+ return [ ] ;
6062 }
61-
63+
6264 return data
63- . filter ( ( item ) => selectedYears . includes ( item . ProveAar ) )
64- . map ( ( item ) => {
65- const result : Covid19Record = {
66- ProveAar : item . ProveAar ,
67- Covid19 : undefined ,
68- Vaccine : undefined
69- } ;
70-
71- if ( selectedType . includes ( "Covid-19" ) ) {
72- result . Covid19 = item . Covid19 ;
73- }
74- if ( selectedType . includes ( "Vaccine" ) ) {
75- result . Vaccine = item . Vaccine ;
76- }
77-
78- return result ;
79- } ) ;
80- } , [ selectedYears , selectedType , data ] ) ;
81-
82- const sortedData = filteredData . sort ( ( a , b ) => parseInt ( a . ProveAar ) - parseInt ( b . ProveAar ) ) ;
65+ . filter ( ( item ) => selectedYears . includes ( item . ProveAar ) )
66+ . map ( ( item ) => {
67+ const result : Covid19Record = {
68+ ProveAar : item . ProveAar ,
69+ Covid19 : undefined ,
70+ Vaccine : undefined ,
71+ } ;
72+
73+ if ( selectedType . includes ( "Covid-19" ) ) {
74+ result . Covid19 = item . Covid19 ;
75+ }
76+ if ( selectedType . includes ( "Vaccine" ) ) {
77+ result . Vaccine = item . Vaccine ;
78+ }
79+
80+ return result ;
81+ } ) ;
82+ } , [ selectedYears , selectedType , data ] ) ;
83+
84+ const sortedData = filteredData . sort (
85+ ( a , b ) => parseInt ( a . ProveAar ) - parseInt ( b . ProveAar )
86+ ) ;
8387
8488 const resetSelections = useCallback ( ( ) => {
8589 setSelectedYears ( [ ] ) ;
@@ -93,14 +97,14 @@ export function Covid19LineChart({
9397
9498 const chartConfig = {
9599 Vaccine : {
96- label : "Vaccine" ,
97- color : generateHslColor ( 30 , 100 ) ,
100+ label : "Vaccine" ,
101+ color : generateHslColor ( 30 , 100 ) ,
98102 } ,
99103 Covid19 : {
100- label : "Covid-19" ,
101- color : generateHslColor ( 30 , 50 ) ,
104+ label : "Covid-19" ,
105+ color : generateHslColor ( 30 , 50 ) ,
102106 } ,
103- } satisfies ChartConfig
107+ } satisfies ChartConfig ;
104108
105109 const renderDropdown = (
106110 title : string ,
@@ -127,71 +131,81 @@ export function Covid19LineChart({
127131 </ DropdownMenu >
128132 ) ;
129133
130- return (
131- < div className = "flex w-full flex-col space-y-4" >
132- < div className = "flex flex-row space-x-2" >
133- < h2 className = "text-xl font-bold" > Filters</ h2 >
134- { renderDropdown (
135- "Select Years" ,
136- years ,
137- selectedYears ,
138- handleSelectionChange ( setSelectedYears )
139- ) }
140- { renderDropdown (
141- "Select Data" ,
142- type ,
143- selectedType ,
144- handleSelectionChange ( setSelectedType )
145- ) }
146-
147- < Button variant = "outline" onClick = { resetSelections } >
148- Reset All
149- </ Button >
150- </ div >
151- < ChartContainer config = { chartConfig } className = "h-[300px] w-full" >
152- < div >
153- < div className = "h-[400px] w-full" >
154- < ResponsiveContainer width = "100%" height = "80%" >
155- < ComposedChart data = { sortedData } >
156- < CartesianGrid strokeDasharray = "5 5" />
157- < XAxis
158- dataKey = "ProveAar"
159- label = { { value : "" , position : "insideBottomRight" , offset : 0 } }
160-
161- />
162- < YAxis
163- label = { { value : "Occurrences" , angle : - 90 , position : "outsideLeft" , dx : - 30 } }
164- yAxisId = "left" orientation = "left"
165- stroke = { chartConfig . Covid19 . color }
166- domain = { [ 'auto' , 'auto' ] }
167- />
168- < YAxis
169- label = { { value : "" , angle : - 90 , position : "outsideLeft" } }
170- yAxisId = "right" orientation = "right"
171- stroke = { chartConfig . Vaccine . color }
172- />
173- < Tooltip />
174- < ChartLegend />
175- < Line
176- key = "Vaccine"
177- type = "monotone"
178- dataKey = "Vaccine"
179- stroke = { chartConfig . Vaccine . color }
180- dot = { false }
181- yAxisId = "right"
182- />
183-
184- < Bar
185- dataKey = "Covid19"
186- barSize = { 30 }
187- fill = { chartConfig . Covid19 . color }
188- yAxisId = "left"
189- />
190- </ ComposedChart >
191- </ ResponsiveContainer >
192- </ div >
134+ return (
135+ < div className = "flex w-full flex-col space-y-4" >
136+ < div className = "flex flex-row space-x-2" >
137+ < h2 className = "text-xl font-bold" > Filters</ h2 >
138+ { renderDropdown (
139+ "Select Years" ,
140+ years ,
141+ selectedYears ,
142+ handleSelectionChange ( setSelectedYears )
143+ ) }
144+ { renderDropdown (
145+ "Select Data" ,
146+ type ,
147+ selectedType ,
148+ handleSelectionChange ( setSelectedType )
149+ ) }
150+
151+ < Button variant = "outline" onClick = { resetSelections } >
152+ Reset All
153+ </ Button >
154+ </ div >
155+ < ChartContainer config = { chartConfig } className = "h-[300px] w-full" >
156+ < div >
157+ < div className = "h-[400px] w-full" >
158+ < ResponsiveContainer width = "100%" height = "80%" >
159+ < ComposedChart data = { sortedData } >
160+ < CartesianGrid strokeDasharray = "5 5" />
161+ < XAxis
162+ dataKey = "ProveAar"
163+ label = { {
164+ value : "" ,
165+ position : "insideBottomRight" ,
166+ offset : 0 ,
167+ } }
168+ />
169+ < YAxis
170+ label = { {
171+ value : "Occurrences" ,
172+ angle : - 90 ,
173+ position : "outsideLeft" ,
174+ dx : - 30 ,
175+ } }
176+ yAxisId = "left"
177+ orientation = "left"
178+ stroke = { chartConfig . Covid19 . color }
179+ domain = { [ "auto" , "auto" ] }
180+ />
181+ < YAxis
182+ label = { { value : "" , angle : - 90 , position : "outsideLeft" } }
183+ yAxisId = "right"
184+ orientation = "right"
185+ stroke = { chartConfig . Vaccine . color }
186+ />
187+ < Tooltip />
188+ < ChartLegend />
189+ < Line
190+ key = "Vaccine"
191+ type = "monotone"
192+ dataKey = "Vaccine"
193+ stroke = { chartConfig . Vaccine . color }
194+ dot = { false }
195+ yAxisId = "right"
196+ />
197+
198+ < Bar
199+ dataKey = "Covid19"
200+ barSize = { 30 }
201+ fill = { chartConfig . Covid19 . color }
202+ yAxisId = "left"
203+ />
204+ </ ComposedChart >
205+ </ ResponsiveContainer >
206+ </ div >
207+ </ div >
208+ </ ChartContainer >
193209 </ div >
194- </ ChartContainer >
195- </ div >
196- ) ;
210+ ) ;
197211}
0 commit comments