Skip to content

Commit

Permalink
added story 1 graph
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriele Granello committed Nov 22, 2024
1 parent 1d09d7c commit fcdd265
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 0 deletions.
113 changes: 113 additions & 0 deletions app/components/graphs/UpfrontComparisonBarChart.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"use client";

import { Bar, BarChart, CartesianGrid, XAxis } from "recharts";
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "@/components/ui/card";
import {
ChartConfig,
ChartContainer,
ChartLegend,
ChartLegendContent,
ChartTooltip,
ChartTooltipContent,
} from "@/components/ui/chart";

const chartConfig = {
land: {
label: "Land",
color: "hsl(var(--chart-1))",
},
house: {
label: "House",
color: "hsl(var(--chart-2))",
},
} satisfies ChartConfig;

import React from "react";

// Define type for DataInput
type DataInput = {
category: string;
marketPurchase: number;
fairholdLandPurchase: number;
fairholdLandRent: number;
[key: string]: string | number;
};

// Define the props type for StackedBarChart
interface StackedBarChartProps {
data: DataInput[];
}

// Implementation of the Chart.js Stacked Bar Chart
const UpfrontComparisonBarChart: React.FC<StackedBarChartProps> = ({
data,
}) => {
const chartData = [
{
tenure: "market: purchase",
land: data[0].marketPurchase,
house: data[1].marketPurchase,
},
{
tenure: "fairhold: land purchase",
land: data[0].fairholdLandPurchase,
house: data[1].fairholdLandPurchase,
},
{
tenure: "fairhold: land rent",
land: data[0].fairholdLandRent,
house: data[1].fairholdLandRent,
},
];

return (
<Card>
<CardHeader>
<CardTitle>Tenure comparison</CardTitle>
<CardDescription>Monthly cost in £</CardDescription>
</CardHeader>
<CardContent>
<ChartContainer config={chartConfig}>
<BarChart accessibilityLayer data={chartData}>
<CartesianGrid vertical={false} />
<XAxis
dataKey="tenure"
tickLine={false}
tickMargin={10}
axisLine={false}
tickFormatter={(value) => value}
/>
<ChartTooltip content={<ChartTooltipContent hideLabel />} />
<ChartLegend content={<ChartLegendContent />} />
<Bar
dataKey="land"
stackId="a"
fill="var(--color-land)"
radius={[0, 0, 4, 4]}
/>
<Bar
dataKey="house"
stackId="a"
fill="var(--color-house)"
radius={[4, 4, 0, 0]}
/>
</BarChart>
</ChartContainer>
</CardContent>
<CardFooter className="flex-col items-start gap-2 text-sm">
<div className="leading-none text-muted-foreground">
Showing the upfront cost.
</div>
</CardFooter>
</Card>
);
};

export default UpfrontComparisonBarChart;
59 changes: 59 additions & 0 deletions app/components/graphs/UpfrontComparisonWrapper.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use client";
import React from "react";
import ErrorBoundary from "../ErrorBoundary";
import { Household } from "@/app/models/Household";
import UpfrontComparisonBarChart from "./UpfrontComparisonBarChart";

interface UpfrontComparisonWrapperProps {
household: Household;
mortgageLand?: number;
averageRentLand?: number;
socialRentMonthlyLand?: number;
mortgageFairholdLandPurchase?: number;
fairholdLandRent?: number;
mortgageHouse?: number;
mortgageDepreciatedHouse?: number;
averageRentHouse?: number;
socialRentMonthlyHouse?: number;
}

const UpfrontComparisonWrapper: React.FC<UpfrontComparisonWrapperProps> = ({
household,
}) => {
if (!household) {
return <div>No household data available</div>;
}

const formatData = (household: Household) => {
return [
{
category: "Land price",
marketPurchase: household.property.landPrice || 0,
fairholdLandPurchase:
household.tenure.fairholdLandPurchase?.discountedLandPrice || 0,
fairholdLandRent: 0,
},
{
category: "House price",
marketPurchase:
household.property.averageMarketPrice -
household.property.landPrice || 0,
fairholdLandPurchase: household.property.depreciatedBuildPrice || 0,
fairholdLandRent: household.property.depreciatedBuildPrice || 0,
},
];
};

const formattedData = formatData(household);

return (
<ErrorBoundary>
<div>
<h2>Tenure Comparison Chart</h2>
<UpfrontComparisonBarChart data={formattedData} />
</div>
</ErrorBoundary>
);
};

export default UpfrontComparisonWrapper;
2 changes: 2 additions & 0 deletions app/components/ui/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import TenureComparisonWrapper from "../graphs/TenureComparisonWrapper";
import UpfrontComparisonWrapper from "../graphs/UpfrontComparisonWrapper";
import TotalPaymentWrapper from "../graphs/TotalPaymentWrapper";
import LifetimeMarketPurchaseWrapper from "../graphs/LifetimeMarketPurchaseWrapper";
import LifetimeMarketRentWrapper from "../graphs/LifetimeMarketRentWrapper";
Expand All @@ -16,6 +17,7 @@ const Dashboard: React.FC<DashboardProps> = ({ data }) => {
<div>
<h1>Dashboard</h1>
{/* Render multiple graph components here */}
<UpfrontComparisonWrapper household={data} />
<TenureComparisonWrapper household={data} />
<TotalPaymentWrapper household={data} />
<LifetimeMarketPurchaseWrapper household={data} />
Expand Down

0 comments on commit fcdd265

Please sign in to comment.