-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(saas): Reusable Line Chart Component using recharts
- Loading branch information
1 parent
a9e99ec
commit 36a478c
Showing
5 changed files
with
189 additions
and
147 deletions.
There are no files selected for viewing
144 changes: 0 additions & 144 deletions
144
starterkits/saas/src/app/(app)/admin/dashboard/_components/chart.tsx
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
starterkits/saas/src/app/(app)/admin/dashboard/_components/users-chart.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"use client"; | ||
|
||
import { LineChart } from "@/components/charts"; | ||
import { formatDate, thousandToK } from "@/lib/utils"; | ||
|
||
type UsersChartProps = { | ||
data: unknown[]; | ||
}; | ||
|
||
export function UsersChart({ data }: UsersChartProps) { | ||
return ( | ||
<LineChart | ||
data={data} | ||
lineDataKeys={["Users", "Active Users"]} | ||
xAxisDataKey="Date" | ||
yAxisDataKey="Users" | ||
yAxisProps={{ | ||
tickFormatter: (value) => { | ||
if (value >= 10000) { | ||
return `${thousandToK(Number(value)).toFixed(1)}k`; | ||
} else { | ||
return value as string; | ||
} | ||
}, | ||
}} | ||
xAxisProps={{ | ||
tickFormatter: formatDate, | ||
}} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
import { | ||
ResponsiveContainer, | ||
LineChart as LineReCharts, | ||
Line, | ||
CartesianGrid, | ||
YAxis, | ||
XAxis, | ||
Tooltip, | ||
type CartesianGridProps, | ||
type YAxisProps, | ||
type XAxisProps, | ||
} from "recharts"; | ||
|
||
type LineChartProps = { | ||
data: unknown[]; | ||
lineDataKeys: string[]; | ||
xAxisDataKey: string; | ||
yAxisDataKey: string; | ||
lineProps?: React.ComponentPropsWithoutRef<typeof Line>; | ||
CartesionGridProps?: CartesianGridProps; | ||
yAxisProps?: YAxisProps; | ||
xAxisProps?: XAxisProps; | ||
}; | ||
|
||
export const LineChart = ({ | ||
data, | ||
lineDataKeys, | ||
xAxisDataKey, | ||
yAxisDataKey, | ||
lineProps, | ||
CartesionGridProps, | ||
yAxisProps, | ||
xAxisProps, | ||
}: LineChartProps) => { | ||
return ( | ||
<ResponsiveContainer width="100%" minHeight={250}> | ||
<LineReCharts data={data}> | ||
{lineDataKeys.map((lineDataKey) => ( | ||
<Line | ||
key={lineDataKey} | ||
type="monotone" | ||
dataKey={lineDataKey} | ||
stroke="hsl(var(--primary))" | ||
dot={false} | ||
{...lineProps} | ||
/> | ||
))} | ||
|
||
<CartesianGrid | ||
stroke="hsl(var(--border))" | ||
strokeDasharray="3 3" | ||
{...CartesionGridProps} | ||
/> | ||
|
||
<YAxis | ||
stroke="hsl(var(--muted-foreground))" | ||
strokeOpacity={0.2} | ||
fontSize={"0.75rem"} | ||
fontWeight={500} | ||
tickCount={6} | ||
tickMargin={18} | ||
tickLine={false} | ||
axisLine={false} | ||
dataKey={yAxisDataKey} | ||
{...yAxisProps} | ||
/> | ||
|
||
<XAxis | ||
dataKey={xAxisDataKey} | ||
tickCount={5} | ||
stroke="hsl(var(--muted-foreground))" | ||
strokeOpacity={0.2} | ||
fontSize={"0.75rem"} | ||
fontWeight={500} | ||
tickLine={false} | ||
axisLine={false} | ||
tickMargin={16} | ||
{...xAxisProps} | ||
/> | ||
|
||
<Tooltip | ||
cursorStyle={{ | ||
stroke: "hsl(var(--border))", | ||
}} | ||
content={({ active, payload }) => { | ||
if (active) { | ||
const payloadItem = payload?.[0]?.payload as Record< | ||
string, | ||
string | ||
>; | ||
|
||
const payloadItemArray = Object.entries( | ||
payloadItem, | ||
).map(([key, value]) => ({ key, value })); | ||
|
||
console.log(payloadItemArray); | ||
|
||
return <CustomTooltip payload={payloadItemArray} />; | ||
} | ||
|
||
return null; | ||
}} | ||
/> | ||
</LineReCharts> | ||
</ResponsiveContainer> | ||
); | ||
}; | ||
|
||
type CustomTooltipProps = { | ||
payload: { key: string; value: string }[]; | ||
}; | ||
|
||
export const CustomTooltip = ({ payload }: CustomTooltipProps) => { | ||
return ( | ||
<div className="grid divide-y rounded-sm border border-border bg-background shadow-md"> | ||
{payload.map(({ key, value }) => ( | ||
<p key={key} className="flex flex-row gap-2 p-2 text-xs"> | ||
<span className="font-medium text-muted-foreground"> | ||
{key}: | ||
</span> | ||
<span className="font-medium">{value}</span> | ||
</p> | ||
))} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters