From 6e827308ce332a842c87f19d7adedf827544317e Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Thu, 5 Sep 2024 22:44:37 +0100 Subject: Adds GUI with basic histogram --- gui/src/components/histogram/Histogram.tsx | 70 ++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 gui/src/components/histogram/Histogram.tsx (limited to 'gui/src/components/histogram/Histogram.tsx') diff --git a/gui/src/components/histogram/Histogram.tsx b/gui/src/components/histogram/Histogram.tsx new file mode 100644 index 0000000..ee4b7b0 --- /dev/null +++ b/gui/src/components/histogram/Histogram.tsx @@ -0,0 +1,70 @@ +import Category from "../../types/category"; +import './Histogram.scss'; + +function Histogram({ categories }: Props) { + const maxAbsBalanceToBudgetRatio = Math.max( + ...categories.map((c) => Math.abs(c.balanceToBudgetRatio)) + ); + + return ( + + + + + + + + + + + { + categories + .sort((c1, c2) => c1.balanceToBudgetRatio - c2.balanceToBudgetRatio) + .map((category) => Row({ category, maxAbsBalanceToBudgetRatio })) + } + +
CategoryBalance-to-Budget RatioBalance
+ ); +} + +function Row({ category, maxAbsBalanceToBudgetRatio }: RowProps) { + const normalisedRatio = category.balanceToBudgetRatio / maxAbsBalanceToBudgetRatio; + const variant = getRowVariant(new Number(normalisedRatio).valueOf()); + + return ( + + {category.name} + + + {category.balanceToBudgetRatio.toFixed(2)} + + + + {category.currentBalance.toString()} + + ); +} + +function getRowVariant(balanceToBudgetRatio: number) { + if (balanceToBudgetRatio < 0) { + return 'arrears'; + } + if (balanceToBudgetRatio >= 1) { + return 'spend'; + } + return 'save'; +} + +type Props = { + categories: Category[], +}; + +type RowProps = { + category: Category, + maxAbsBalanceToBudgetRatio: number, +}; + +export default Histogram; -- cgit v1.2.3