|
| 1 | +import Decimal from "decimal.js"; |
| 2 | +import { |
| 3 | + Bar, |
| 4 | + CartesianGrid, |
| 5 | + ComposedChart, |
| 6 | + Legend, |
| 7 | + Line, |
| 8 | + XAxis, |
| 9 | + YAxis, |
| 10 | +} from "recharts"; |
| 11 | +import { |
| 12 | + Card, |
| 13 | + CardContent, |
| 14 | + CardDescription, |
| 15 | + CardHeader, |
| 16 | + CardTitle, |
| 17 | +} from "~/components/ui/card"; |
| 18 | +import { |
| 19 | + ChartConfig, |
| 20 | + ChartContainer, |
| 21 | + ChartTooltip, |
| 22 | + ChartTooltipContent, |
| 23 | +} from "~/components/ui/chart"; |
| 24 | +import { APP_CURRENCY } from "~/constants"; |
| 25 | +import { calculateZeroInclusiveYAxisDomain } from "~/utils/chart"; |
| 26 | +import { formatDate } from "~/utils/date"; |
| 27 | +import { formatNumber } from "~/utils/number"; |
| 28 | + |
| 29 | +type ChartDataItem = { |
| 30 | + id: string; |
| 31 | + timestamp: Date; |
| 32 | + netValue: Decimal; |
| 33 | + totalAssets: Decimal; |
| 34 | + totalDebts: Decimal; |
| 35 | +}; |
| 36 | + |
| 37 | +const netWorthChartConfig: ChartConfig = { |
| 38 | + netWorth: { |
| 39 | + label: "Net worth", |
| 40 | + color: "var(--chart-3)", |
| 41 | + }, |
| 42 | + totalAssets: { |
| 43 | + label: "Assets", |
| 44 | + color: "var(--chart-1)", |
| 45 | + }, |
| 46 | + totalDebts: { |
| 47 | + label: "Debts", |
| 48 | + color: "var(--chart-2)", |
| 49 | + }, |
| 50 | +}; |
| 51 | + |
| 52 | +export function Chart({ data }: { data: ChartDataItem[] }) { |
| 53 | + const chartData = data.toReversed().map((nw) => ({ |
| 54 | + month: formatDate({ |
| 55 | + date: nw.timestamp, |
| 56 | + options: { |
| 57 | + dateStyle: undefined, |
| 58 | + month: "short", |
| 59 | + }, |
| 60 | + }), |
| 61 | + netWorth: nw.netValue.toNumber(), |
| 62 | + totalAssets: nw.totalAssets.toNumber(), |
| 63 | + totalDebts: nw.totalDebts.toNumber(), |
| 64 | + })); |
| 65 | + console.log(data, chartData); |
| 66 | + |
| 67 | + return ( |
| 68 | + <Card> |
| 69 | + <CardHeader> |
| 70 | + <CardTitle>Net worth history</CardTitle> |
| 71 | + <CardDescription>All time</CardDescription> |
| 72 | + </CardHeader> |
| 73 | + <CardContent> |
| 74 | + <ChartContainer |
| 75 | + config={netWorthChartConfig} |
| 76 | + className="h-[15rem] w-full" |
| 77 | + > |
| 78 | + <ComposedChart accessibilityLayer data={chartData}> |
| 79 | + <CartesianGrid vertical={false} /> |
| 80 | + <XAxis |
| 81 | + dataKey="month" |
| 82 | + tickLine={false} |
| 83 | + axisLine={false} |
| 84 | + tickMargin={8} |
| 85 | + /> |
| 86 | + |
| 87 | + <YAxis |
| 88 | + tickLine={false} |
| 89 | + axisLine={false} |
| 90 | + tickMargin={8} |
| 91 | + domain={calculateZeroInclusiveYAxisDomain} |
| 92 | + tickFormatter={(value: number) => |
| 93 | + formatNumber({ |
| 94 | + value, |
| 95 | + options: { |
| 96 | + style: "currency", |
| 97 | + currency: APP_CURRENCY, |
| 98 | + maximumFractionDigits: 0, |
| 99 | + }, |
| 100 | + }) |
| 101 | + } |
| 102 | + /> |
| 103 | + |
| 104 | + <ChartTooltip cursor={false} content={<ChartTooltipContent />} /> |
| 105 | + |
| 106 | + <Bar |
| 107 | + dataKey="totalAssets" |
| 108 | + fill="var(--color-totalAssets)" |
| 109 | + barSize={30} |
| 110 | + radius={4} |
| 111 | + name="Assets" |
| 112 | + /> |
| 113 | + |
| 114 | + <Bar |
| 115 | + dataKey="totalDebts" |
| 116 | + fill="var(--color-totalDebts)" |
| 117 | + barSize={30} |
| 118 | + radius={4} |
| 119 | + name="Debts" |
| 120 | + /> |
| 121 | + |
| 122 | + <Line |
| 123 | + dataKey="netWorth" |
| 124 | + type="monotone" |
| 125 | + stroke="var(--color-netWorth)" |
| 126 | + strokeWidth={3} |
| 127 | + dot={false} |
| 128 | + name="Net worth" |
| 129 | + /> |
| 130 | + |
| 131 | + <Legend /> |
| 132 | + </ComposedChart> |
| 133 | + </ChartContainer> |
| 134 | + </CardContent> |
| 135 | + </Card> |
| 136 | + ); |
| 137 | +} |
0 commit comments