Spaces:
Sleeping
Sleeping
File size: 12,922 Bytes
f16cb34 3d701d8 73ce5b8 3d701d8 73ce5b8 3d701d8 73ce5b8 3c0668b 7270531 3c0668b 73ce5b8 f16cb34 3d701d8 5bc74c0 3d701d8 f16cb34 73ce5b8 3d701d8 be1c0ed 3d701d8 717d87a 3d701d8 be1c0ed 3d701d8 be1c0ed 3d701d8 be1c0ed e259887 3d701d8 be1c0ed 717d87a f16cb34 3d701d8 3f06d5a 3d701d8 3f06d5a 3d701d8 7a7f358 3d701d8 717d87a 3d701d8 3f06d5a f16cb34 73ce5b8 5bc74c0 f16cb34 3d701d8 717d87a 3d701d8 f16cb34 3d701d8 717d87a 3d701d8 7270531 3f06d5a f16cb34 3d701d8 3f06d5a 3d701d8 3f06d5a 3d701d8 3f06d5a 3d701d8 f16cb34 3d701d8 f16cb34 3d701d8 f16cb34 3d701d8 3c0668b 00a3408 73ce5b8 3c0668b 73ce5b8 7270531 73ce5b8 3c0668b 73ce5b8 7270531 73ce5b8 3c0668b 73ce5b8 7270531 73ce5b8 3c0668b 73ce5b8 7270531 73ce5b8 3c0668b 73ce5b8 7270531 73ce5b8 3c0668b 73ce5b8 7270531 73ce5b8 3c0668b 73ce5b8 7270531 73ce5b8 3c0668b 73ce5b8 7270531 73ce5b8 3d701d8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
import { useState, useEffect, useRef } from "react";
import {
Box,
Grid,
GridItem,
Text,
Flex,
Spinner,
SimpleGrid,
RangeSlider,
RangeSliderTrack,
RangeSliderFilledTrack,
RangeSliderThumb,
} from "@chakra-ui/react";
import Select from "react-select";
import {
Chart as ChartJS,
BarElement,
CategoryScale,
LinearScale,
Title,
Tooltip,
Legend,
ArcElement,
LineElement,
PointElement,
RadialLinearScale,
type ChartData,
type ChartOptions
} from "chart.js";
import { Bar, Pie, Doughnut, Line } from "react-chartjs-2";
import type { FilterState, AvailableFilters } from "../hooks/types";
// register chart components
ChartJS.register(
BarElement,
CategoryScale,
LinearScale,
Title,
Tooltip,
Legend,
ArcElement,
LineElement,
PointElement,
RadialLinearScale
);
type LegendPosition = "top" | "bottom" | "left" | "right" | "chartArea";
interface ChartDataShape { labels: string[]; values: number[]; }
interface Stats { [key: string]: ChartDataShape; }
// define the six charts and their component types
const chartOrder: { key:string; name: string; type: ChartType }[] = [
{ key: "ppy", name: "Projects per Year", type: "line" },
{ key: "psd",name: "Project-Size Distribution", type: "bar" },
{ key: "frs",name: "Top 10 Funding Schemes", type: "bar" },
{ key: "top10",name: "Top 10 Topics (€ M)", type: "bar" },
{ key: "frb",name: "Funding Range Breakdown", type: "pie" },
{ key: "ppc",name: "Projects per Country", type: "doughnut" },
];
const FILTER_LABELS: Record<keyof FilterState, string> = {
status: "Status",
organization: "Organization",
country: "Country",
legalBasis: "Legal Basis",
topic: "EuroSciVoc",
fundingScheme: "Funding Scheme",
};
type ChartType = "bar" | "pie" | "doughnut" | "line";
interface DashboardProps {
stats: Stats;
filters: FilterState;
setFilters: React.Dispatch<React.SetStateAction<FilterState>>;
availableFilters: AvailableFilters;
}
const Dashboard: React.FC<DashboardProps> = ({
stats: initialStats,
filters,
setFilters,
availableFilters,
}) => {
const [orgInput, setOrgInput] = useState("");
const [statsData, setStatsData] = useState<Stats>(initialStats);
const [loadingStats, setLoadingStats] = useState(false);
const fetchTimer = useRef<number | null>(null);
// Debounced stats & filters fetch
useEffect(() => {
if (fetchTimer.current) clearTimeout(fetchTimer.current);
fetchTimer.current = window.setTimeout(() => {
const qs = new URLSearchParams();
Object.entries(filters).forEach(([k, v]) => v && qs.set(k, v));
setLoadingStats(true);
fetch(`/api/stats?${qs.toString()}`)
.then(r => r.json())
.then((data: Stats) => setStatsData(data))
.catch(console.error)
.finally(() => setLoadingStats(false));
}, 300);
return () => { if (fetchTimer.current) clearTimeout(fetchTimer.current); };
}, [filters]);
const updateFilter = (key: keyof FilterState) =>
(opt: { value: string } | null) =>
setFilters(prev => ({ ...prev, [key]: opt?.value || "" }));
const updateSlider = (
k1: 'minYear' | 'minFunding' | 'minEndYear',
k2: 'maxYear' | 'maxFunding' | 'maxEndYear'
) => ([min, max]: number[]) =>
setFilters(prev => ({
...prev,
[k1]: String(min),
[k2]: String(max),
}));
const filterKeys: Array<keyof FilterState> = [
'status', 'organization', 'country', 'legalBasis','topic','fundingScheme',
];
if (loadingStats && !Object.keys(statsData).length) {
return <Flex justify="center" mt={10}><Spinner size="xl" /></Flex>;
}
return (
<Box>
{/* Filters */}
<Box borderWidth="1px" borderRadius="lg" p={4} mb={6} bg="gray.50">
<Grid templateColumns={{ base: '1fr', sm: 'repeat(2,1fr)', md: 'repeat(4,1fr)', lg: 'repeat(6,1fr)' }} gap={6}>
{filterKeys.map(key => {
const opts = availableFilters[
key === 'status' ? 'statuses'
: key === 'organization' ? 'organizations'
: key === 'country' ? 'countries'
: key === "legalBasis" ? "legalBases"
: key === "topic" ? "topics"
: 'fundingSchemes'
] || [];
const isOrg = key === 'organization';
return (
<GridItem key={key} colSpan={1}>
<Text fontSize="sm" mb={1} fontWeight="medium">{FILTER_LABELS[key]}</Text>
<Select
options={opts.map(v => ({ label: v, value: v }))}
placeholder={FILTER_LABELS[key]}
onChange={updateFilter(key)}
isClearable
isSearchable
{...(isOrg && { menuIsOpen: orgInput.length>0, onInputChange: setOrgInput })}
/>
</GridItem>
);
})}
{/* Start Year */}
<GridItem colSpan={{ base: 1, md: 2 }}>
<Box mb={6}>
<Flex justify="space-between" mb={1}>
<Text fontSize="sm" fontWeight="medium">Start Year</Text>
<Text fontSize="xs" color="gray.600">
{filters.minYear} – {filters.maxYear}
</Text>
</Flex>
<RangeSlider
aria-label={["Min Start Year","Max Start Year"]}
min={2000}
max={2025}
step={1}
defaultValue={[+filters.minYear, +filters.maxYear]}
onChange={updateSlider("minYear","maxYear")}
size="md"
>
<RangeSliderTrack><RangeSliderFilledTrack/></RangeSliderTrack>
<RangeSliderThumb index={0} boxSize={4}/>
<RangeSliderThumb index={1} boxSize={4}/>
</RangeSlider>
</Box>
</GridItem>
{/* End Year */}
<GridItem colSpan={{ base: 1, md: 2 }}>
<Box mb={6}>
<Flex justify="space-between" mb={1}>
<Text fontSize="sm" fontWeight="medium">End Year</Text>
<Text fontSize="xs" color="gray.600">
{filters.minEndYear} – {filters.maxEndYear}
</Text>
</Flex>
<RangeSlider
aria-label={["Min End Year","Max End Year"]}
min={2000}
max={2025}
step={1}
defaultValue={[+filters.minEndYear, +filters.maxEndYear]}
onChange={updateSlider("minEndYear","maxEndYear")}
size="md"
>
<RangeSliderTrack><RangeSliderFilledTrack/></RangeSliderTrack>
<RangeSliderThumb index={0} boxSize={4}/>
<RangeSliderThumb index={1} boxSize={4}/>
</RangeSlider>
</Box>
</GridItem>
{/* Funding Range */}
<GridItem colSpan={{ base: 1, md: 2 }}>
<Box>
<Flex justify="space-between" mb={1}>
<Text fontSize="sm" fontWeight="medium">Funding (€)</Text>
<Text fontSize="xs" color="gray.600">
€{Number(filters.minFunding).toLocaleString()} – €{Number(filters.maxFunding).toLocaleString()}
</Text>
</Flex>
<RangeSlider
aria-label={["Min Funding","Max Funding"]}
min={0}
max={1e7}
step={1e5}
defaultValue={[+filters.minFunding, +filters.maxFunding]}
onChange={updateSlider("minFunding","maxFunding")}
size="md"
>
<RangeSliderTrack>
<RangeSliderFilledTrack />
</RangeSliderTrack>
<RangeSliderThumb index={0} boxSize={4}/>
<RangeSliderThumb index={1} boxSize={4}/>
</RangeSlider>
</Box>
</GridItem>
</Grid>
</Box>
{/* Charts */}
{loadingStats && (
<Flex justify="center" mb={6}>
<Spinner />
</Flex>
)}
<SimpleGrid columns={{ base:1, md:2, lg:3 }} spacing={6}>
{chartOrder.map(({key,name, type }) => {
const raw = statsData[key];
if (!raw || !Array.isArray(raw.labels) || !Array.isArray(raw.values)) {
console.warn(`Skipping chart ${key}, no data:`, raw);
return null;
}
// ---- properly typed Chart.js data & options ----
if (type === "bar") {
const data: ChartData<"bar", number[], string> = {
labels: raw.labels,
datasets: [
{
label: name,
data: raw.values,
backgroundColor: "#003399",
borderColor: "#FFCC00",
borderWidth: 1,
},
],
};
const options: ChartOptions<"bar"> = {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: "top" as LegendPosition,
},
title: {
display: true,
text: name,
},
},
};
return (
<Box key={name} bg="white" borderRadius="md" p={4} height="300px" display="flex" alignItems="center">
<Bar data={data} options={options} height={250}/>
</Box>
);
}
if (type === "line") {
const data: ChartData<"line", number[], string> = {
labels: raw.labels,
datasets: [
{
label: name,
data: raw.values,
backgroundColor: "#003399",
borderColor: "#FFCC00",
borderWidth: 1,
},
],
};
const options: ChartOptions<"line"> = {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: "top" as LegendPosition,
},
title: {
display: true,
text: name,
},
},
};
return (
<Box key={name} bg="white" borderRadius="md" p={4} height="300px" display="flex" alignItems="center">
<Line data={data} options={options} height={250}/>
</Box>
);
}
if (type === "pie") {
const data: ChartData<"pie", number[], string> = {
labels: raw.labels,
datasets: [
{
label: name,
data: raw.values,
backgroundColor: "#003399",
borderColor: "#FFCC00",
borderWidth: 1,
},
],
};
const options: ChartOptions<"pie"> = {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: "top" as LegendPosition,
},
title: {
display: true,
text: name,
},
},
};
return (
<Box key={name} bg="white" borderRadius="md" p={4} height="300px" display="flex" alignItems="center">
<Pie data={data} options={options} height={250}/>
</Box>
);
}
if (type === "doughnut") {
const data: ChartData<"doughnut", number[], string> = {
labels: raw.labels,
datasets: [
{
label: name,
data: raw.values,
backgroundColor: "#003399",
borderColor: "#FFCC00",
borderWidth: 1,
},
],
};
const options: ChartOptions<"doughnut"> = {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: "top" as LegendPosition,
},
title: {
display: true,
text: name,
},
},
};
return (
<Box key={name} bg="white" borderRadius="md" p={4} height="300px" display="flex" alignItems="center">
<Doughnut data={data} options={options} height={250}/>
</Box>
);
}
return null;
})}
</SimpleGrid>
</Box>
);
};
export default Dashboard;
|