Skip to content

Commit

Permalink
feat: bar chart & fix react warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
bohdangarchu committed Dec 15, 2024
1 parent f3673df commit 8f52fe2
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 82 deletions.
4 changes: 1 addition & 3 deletions src/app/comparison-portal/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ export default async function ComparisonPortal() {
return (
<div>
<h1>Comparison Portal</h1>
<div>
<CountryComparison countryMapData={countryMapData} globalFcsData={globalFcsData} />
</div>
<CountryComparison countryMapData={countryMapData} globalFcsData={globalFcsData} />
</div>
);
}
2 changes: 1 addition & 1 deletion src/components/Charts/CategoricalChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Skeleton } from '@nextui-org/skeleton';
import React from 'react';
import { v4 as uuid } from 'uuid';

export default function ComparisonAccordionSkeleton() {
const N_ITEMS = 5;
return (
<div className="overflow-x-auto rounded-lg shadow-none">
<div className="flex flex-col gap-2 mb-4">
{[...Array(N_ITEMS)].map(() => (
<div className="rounded-medium last:border-b-0 bg-content1 white:bg-white overflow-hidden shadow-md">
<div
key={uuid()}
className="rounded-medium last:border-b-0 bg-content1 white:bg-white overflow-hidden shadow-md"
>
<Skeleton className="rounded-lg bg-content1 dark:bg-content1">
<div className="h-[69px]" />
</Skeleton>
Expand Down
24 changes: 9 additions & 15 deletions src/components/ComparisonPortal/CountryComparison.tsx
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -16,21 +14,17 @@ interface CountryComparisonProps {
}

export default function CountryComparison({ countryMapData, globalFcsData }: CountryComparisonProps) {
const [selectedCountries, setSelectedCountries] = useState<CountryMapData[] | undefined>(undefined);
const [selectedCountries, setSelectedCountries] = useState<CountryMapData[]>([]);

return (
<div>
<Suspense fallback={<CountrySelectionSkeleton />}>
<CountrySelection
countryMapData={countryMapData}
globalFcsData={globalFcsData}
selectedCountries={selectedCountries}
setSelectedCountries={setSelectedCountries}
/>
</Suspense>
<Suspense fallback={<ComparisonAccordionSkeleton />}>
<CountryComparisonAccordion selectedCountries={selectedCountries} />
</Suspense>
<CountrySelection
countryMapData={countryMapData}
globalFcsData={globalFcsData}
selectedCountries={selectedCountries}
setSelectedCountries={setSelectedCountries}
/>
<CountryComparisonAccordion selectedCountries={selectedCountries} />
</div>
);
}
9 changes: 6 additions & 3 deletions src/components/ComparisonPortal/NoDataHint.tsx
Original file line number Diff line number Diff line change
@@ -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<string | null>(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)
);
Expand All @@ -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 ? (
<Alert
Expand Down
4 changes: 4 additions & 0 deletions src/domain/entities/charts/LineChartData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ export interface LineChartData {
verticalLines?: ChartVerticalLine[];
verticalBands?: ChartVerticalBand[];
}

export function isLineChartData(data: unknown): data is LineChartData {
return (data as LineChartData).type === LineChartDataType.LINE_CHART_DATA;
}
6 changes: 4 additions & 2 deletions src/domain/entities/comparison/CountryComparisonChartdata.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { CategoricalChartData } from '../charts/CategoricalChartData';
import { LineChartData } from '../charts/LineChartData';

export interface CountryComparisonChartData {
fcsChartData?: LineChartData;
rcsiChartData?: LineChartData;
foodSecurityBarChartData?: LineChartData;
importDependencyBarChartData?: LineChartData;
foodSecurityBarChartData?: CategoricalChartData;
populationBarChartData?: CategoricalChartData;
importDependencyBarChartData?: CategoricalChartData;
balanceOfTradeData?: LineChartData;
headlineInflationData?: LineChartData;
foodInflationData?: LineChartData;
Expand Down
4 changes: 3 additions & 1 deletion src/domain/props/NoDataHintProps.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { LineChartData } from '@/domain/entities/charts/LineChartData.ts';

import { CategoricalChartData } from '../entities/charts/CategoricalChartData';

export interface NoDataHintProps {
lineChartData: LineChartData;
chartData: LineChartData | CategoricalChartData;
selectedCountryNames: string[];
isLoading: boolean;
}
107 changes: 51 additions & 56 deletions src/operations/comparison-portal/CountryComparisonOperations.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { Spacer } from '@nextui-org/react';
import { UseQueryResult } from '@tanstack/react-query';

import { CategoricalChart } from '@/components/Charts/CategoricalChart';
import { LineChart } from '@/components/Charts/LineChart';
import NoDataHint from '@/components/ComparisonPortal/NoDataHint';
import CustomInfoCircle from '@/components/CustomInfoCircle/CustomInfoCircle';
import { AccordionItemProps } from '@/domain/entities/accordions/Accordions';
import { CategoricalChartData } from '@/domain/entities/charts/CategoricalChartData';
import { LineChartData } from '@/domain/entities/charts/LineChartData';
import { ChartData } from '@/domain/entities/common/ChartData.ts';
import { CountryComparisonChartData } from '@/domain/entities/comparison/CountryComparisonChartdata';
Expand Down Expand Up @@ -58,53 +60,51 @@ export class CountryComparisonOperations {
});
}

static getPopulationBarChartData(
countryDataList: CountryDataRecord[],
countryMapData: CountryMapData[]
): CategoricalChartData {
return {
yAxisLabel: 'Mill',
categories: countryDataList.map((countryData) => ({
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,
},
})),
};
}

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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -215,6 +217,7 @@ export class CountryComparisonOperations {
fcsChartData,
rcsiChartData,
foodSecurityBarChartData,
populationBarChartData,
importDependencyBarChartData,
balanceOfTradeData,
headlineInflationData,
Expand All @@ -227,23 +230,15 @@ export class CountryComparisonOperations {
{
title: 'Food Security',
content: (
<div>
<div className="flex">
{foodSecurityBarChartData && (
<>
<LineChart
data={foodSecurityBarChartData}
small
noPadding
transparentBackground
// TODO: f-165 bar chart
/>
<NoDataHint
lineChartData={foodSecurityBarChartData}
selectedCountryNames={selectedCountryNames}
isLoading={isLoading}
/>
</>
<CategoricalChart
title="Number of people with insufficient food consumption"
data={foodSecurityBarChartData}
/>
)}
<Spacer x={6} />
{populationBarChartData && <CategoricalChart title="Population" data={populationBarChartData} />}
</div>
),
},
Expand All @@ -261,7 +256,7 @@ export class CountryComparisonOperations {
transparentBackground
/>
<NoDataHint
lineChartData={fcsChartData}
chartData={fcsChartData}
selectedCountryNames={selectedCountryNames}
isLoading={isLoading}
/>
Expand All @@ -278,7 +273,7 @@ export class CountryComparisonOperations {
transparentBackground
/>
<NoDataHint
lineChartData={rcsiChartData}
chartData={rcsiChartData}
selectedCountryNames={selectedCountryNames}
isLoading={isLoading}
/>
Expand All @@ -288,16 +283,16 @@ export class CountryComparisonOperations {
),
},
{
title: 'Import Dependency',
title: 'Macro-economic',
infoIcon: <CustomInfoCircle />,
popoverInfo: FcsAccordionOperations.getMacroEconomicPopoverInfo(),
content: (
<div>
{importDependencyBarChartData && (
<>
<LineChart data={importDependencyBarChartData} small noPadding transparentBackground />
<CategoricalChart title="Import Dependency" data={importDependencyBarChartData} />
<NoDataHint
lineChartData={importDependencyBarChartData}
chartData={importDependencyBarChartData}
selectedCountryNames={selectedCountryNames}
isLoading={isLoading}
/>
Expand All @@ -316,7 +311,7 @@ export class CountryComparisonOperations {
<>
<LineChart data={balanceOfTradeData} small noPadding transparentBackground />
<NoDataHint
lineChartData={balanceOfTradeData}
chartData={balanceOfTradeData}
selectedCountryNames={selectedCountryNames}
isLoading={isLoading}
/>
Expand All @@ -341,7 +336,7 @@ export class CountryComparisonOperations {
transparentBackground
/>
<NoDataHint
lineChartData={headlineInflationData}
chartData={headlineInflationData}
selectedCountryNames={selectedCountryNames}
isLoading={isLoading}
/>
Expand All @@ -351,7 +346,7 @@ export class CountryComparisonOperations {
<>
<LineChart title="Food Inflation" data={foodInflationData} small noPadding transparentBackground />
<NoDataHint
lineChartData={foodInflationData}
chartData={foodInflationData}
selectedCountryNames={selectedCountryNames}
isLoading={isLoading}
/>
Expand Down

0 comments on commit 8f52fe2

Please sign in to comment.