From 8f52fe2a4bc63654a1a256093828a09efee27f99 Mon Sep 17 00:00:00 2001 From: Bohdan Garchu Date: Sun, 15 Dec 2024 22:33:24 +0100 Subject: [PATCH] feat: bar chart & fix react warnings --- src/app/comparison-portal/page.tsx | 4 +- src/components/Charts/CategoricalChart.tsx | 2 +- .../ComparisonAccordionSkeleton.tsx | 6 +- .../ComparisonPortal/CountryComparison.tsx | 24 ++-- .../ComparisonPortal/NoDataHint.tsx | 9 +- src/domain/entities/charts/LineChartData.ts | 4 + .../comparison/CountryComparisonChartdata.ts | 6 +- src/domain/props/NoDataHintProps.ts | 4 +- .../CountryComparisonOperations.tsx | 107 +++++++++--------- 9 files changed, 84 insertions(+), 82 deletions(-) diff --git a/src/app/comparison-portal/page.tsx b/src/app/comparison-portal/page.tsx index a0bf7f01..d358ca6c 100644 --- a/src/app/comparison-portal/page.tsx +++ b/src/app/comparison-portal/page.tsx @@ -9,9 +9,7 @@ export default async function ComparisonPortal() { return (

Comparison Portal

-
- -
+
); } diff --git a/src/components/Charts/CategoricalChart.tsx b/src/components/Charts/CategoricalChart.tsx index 24fe166a..aa76e876 100644 --- a/src/components/Charts/CategoricalChart.tsx +++ b/src/components/Charts/CategoricalChart.tsx @@ -49,7 +49,7 @@ export function CategoricalChart({ // handling the bar and pie chart switch and the theme switch; useEffect(() => { setChartOptions(CategoricalChartOperations.getHighChartOptions(data, showPieChart)); - }, [showPieChart, theme]); + }, [showPieChart, theme, data]); const alternativeSwitchButtonProps = disablePieChartSwitch ? undefined diff --git a/src/components/ComparisonPortal/ComparisonAccordionSkeleton.tsx b/src/components/ComparisonPortal/ComparisonAccordionSkeleton.tsx index edf6c3cf..0e370ade 100644 --- a/src/components/ComparisonPortal/ComparisonAccordionSkeleton.tsx +++ b/src/components/ComparisonPortal/ComparisonAccordionSkeleton.tsx @@ -1,5 +1,6 @@ import { Skeleton } from '@nextui-org/skeleton'; import React from 'react'; +import { v4 as uuid } from 'uuid'; export default function ComparisonAccordionSkeleton() { const N_ITEMS = 5; @@ -7,7 +8,10 @@ export default function ComparisonAccordionSkeleton() {
{[...Array(N_ITEMS)].map(() => ( -
+
diff --git a/src/components/ComparisonPortal/CountryComparison.tsx b/src/components/ComparisonPortal/CountryComparison.tsx index 061c4f8e..d7497b85 100644 --- a/src/components/ComparisonPortal/CountryComparison.tsx +++ b/src/components/ComparisonPortal/CountryComparison.tsx @@ -1,9 +1,7 @@ 'use client'; -import { Suspense, useState } from 'react'; +import { useState } from 'react'; -import ComparisonAccordionSkeleton from '@/components/ComparisonPortal/ComparisonAccordionSkeleton'; -import CountrySelectionSkeleton from '@/components/ComparisonPortal/CountrySelectSkeleton'; import { GlobalFcsData } from '@/domain/entities/country/CountryFcsData'; import { CountryMapData, CountryMapDataWrapper } from '@/domain/entities/country/CountryMapData'; @@ -16,21 +14,17 @@ interface CountryComparisonProps { } export default function CountryComparison({ countryMapData, globalFcsData }: CountryComparisonProps) { - const [selectedCountries, setSelectedCountries] = useState(undefined); + const [selectedCountries, setSelectedCountries] = useState([]); return (
- }> - - - }> - - + +
); } diff --git a/src/components/ComparisonPortal/NoDataHint.tsx b/src/components/ComparisonPortal/NoDataHint.tsx index 6548ff6e..23e22676 100644 --- a/src/components/ComparisonPortal/NoDataHint.tsx +++ b/src/components/ComparisonPortal/NoDataHint.tsx @@ -1,15 +1,18 @@ import { Alert } from '@nextui-org/alert'; import { useEffect, useState } from 'react'; +import { isLineChartData } from '@/domain/entities/charts/LineChartData'; import { NoDataHintProps } from '@/domain/props/NoDataHintProps.ts'; -export default function NoDataHint({ lineChartData, selectedCountryNames, isLoading }: NoDataHintProps) { +export default function NoDataHint({ chartData, selectedCountryNames, isLoading }: NoDataHintProps) { const [formattedMissingCountryNames, setFormattedMissingCountryNames] = useState(null); useEffect(() => { if (isLoading) return; - const countryNamesInChart = lineChartData.lines.map((i) => i.name); + const countryNamesInChart = isLineChartData(chartData) + ? chartData.lines.map((line) => line.name) + : chartData.categories.map((category) => category.name); const missingCountryNames = selectedCountryNames.filter( (countryName) => !countryNamesInChart.includes(countryName) ); @@ -25,7 +28,7 @@ export default function NoDataHint({ lineChartData, selectedCountryNames, isLoad `${missingCountryNames.slice(0, -1).join(', ')} and ${missingCountryNames.slice(-1)}` ); } - }, [isLoading, lineChartData, selectedCountryNames]); + }, [isLoading, chartData, selectedCountryNames]); return formattedMissingCountryNames ? ( ({ + name: this.getCountryNameById(countryData.id, countryMapData), + dataPoint: { + y: countryData.population, + }, + })), + }; + } + static getFoodSecurityBarChartData( countryDataList: CountryDataRecord[], countryMapData: CountryMapData[] - ): LineChartData { + ): CategoricalChartData { return { - type: LineChartDataType.LINE_CHART_DATA, - xAxisType: 'category', yAxisLabel: 'Mill', - lines: countryDataList.map((countryData) => ({ + categories: countryDataList.map((countryData) => ({ name: this.getCountryNameById(countryData.id, countryMapData), - showRange: false, - dataPoints: [ - { - x: 0, // TODO: f-165 this should be 'population' - y: countryData.population, - }, - { - x: 1, // TODO: f-165 this should be 'people with insufficient food consumption' - y: countryData.fcs, - }, - ], + dataPoint: { + y: countryData.fcs, + }, })), }; } @@ -86,25 +93,18 @@ export class CountryComparisonOperations { static getImportDependencyBarChartData( countryDataList: CountryDataRecord[], selectedCountries: CountryMapData[] - ): LineChartData { - return this.chartWithoutEmptyLines({ - type: LineChartDataType.LINE_CHART_DATA, - xAxisType: 'category', + ): CategoricalChartData { + return { yAxisLabel: '% of Cereals', - lines: countryDataList.map((countryData) => ({ - name: this.getCountryNameById(countryData.id, selectedCountries), - showRange: false, - dataPoints: - countryData.importDependency !== null - ? [ - { - x: 0, // TODO: f-165: should be the individual value of the country variable - y: countryData.importDependency, - }, - ] - : [], - })), - }); + categories: countryDataList + .filter((countryData) => countryData.importDependency !== null) + .map((countryData) => ({ + name: this.getCountryNameById(countryData.id, selectedCountries), + dataPoint: { + y: countryData.importDependency!, + }, + })), + }; } static getBalanceOfTradeData( @@ -169,6 +169,8 @@ export class CountryComparisonOperations { rcsiChartData: countryDataList.length > 1 ? this.getRcsiChartData(countryDataList, selectedCountries) : undefined, foodSecurityBarChartData: countryDataList.length > 1 ? this.getFoodSecurityBarChartData(countryDataList, selectedCountries) : undefined, + populationBarChartData: + countryDataList.length > 1 ? this.getPopulationBarChartData(countryDataList, selectedCountries) : undefined, importDependencyBarChartData: countryDataList.length > 1 ? this.getImportDependencyBarChartData(countryDataList, selectedCountries) @@ -215,6 +217,7 @@ export class CountryComparisonOperations { fcsChartData, rcsiChartData, foodSecurityBarChartData, + populationBarChartData, importDependencyBarChartData, balanceOfTradeData, headlineInflationData, @@ -227,23 +230,15 @@ export class CountryComparisonOperations { { title: 'Food Security', content: ( -
+
{foodSecurityBarChartData && ( - <> - - - + )} + + {populationBarChartData && }
), }, @@ -261,7 +256,7 @@ export class CountryComparisonOperations { transparentBackground /> @@ -278,7 +273,7 @@ export class CountryComparisonOperations { transparentBackground /> @@ -288,16 +283,16 @@ export class CountryComparisonOperations { ), }, { - title: 'Import Dependency', + title: 'Macro-economic', infoIcon: , popoverInfo: FcsAccordionOperations.getMacroEconomicPopoverInfo(), content: (
{importDependencyBarChartData && ( <> - + @@ -316,7 +311,7 @@ export class CountryComparisonOperations { <> @@ -341,7 +336,7 @@ export class CountryComparisonOperations { transparentBackground /> @@ -351,7 +346,7 @@ export class CountryComparisonOperations { <>