From 87a83a5604b20550430ef4eefbcd573d309694e2 Mon Sep 17 00:00:00 2001 From: Maze Winther Date: Sun, 22 Jun 2025 10:02:50 +0200 Subject: Initial commit --- apps/web/src/components/ui/chart.tsx | 370 +++++++++++++++++++++++++++++++++++ 1 file changed, 370 insertions(+) create mode 100644 apps/web/src/components/ui/chart.tsx (limited to 'apps/web/src/components/ui/chart.tsx') diff --git a/apps/web/src/components/ui/chart.tsx b/apps/web/src/components/ui/chart.tsx new file mode 100644 index 00000000..a2c19116 --- /dev/null +++ b/apps/web/src/components/ui/chart.tsx @@ -0,0 +1,370 @@ +"use client"; + +import * as React from "react"; +import * as RechartsPrimitive from "recharts"; +import { + NameType, + Payload, + ValueType, +} from "recharts/types/component/DefaultTooltipContent"; + +import { cn } from "../../lib/utils"; + +// Format: { THEME_NAME: CSS_SELECTOR } +const THEMES = { light: "", dark: ".dark" } as const; + +export type ChartConfig = { + [k in string]: { + label?: React.ReactNode; + icon?: React.ComponentType; + } & ( + | { color?: string; theme?: never } + | { color?: never; theme: Record } + ); +}; + +type ChartContextProps = { + config: ChartConfig; +}; + +const ChartContext = React.createContext(null); + +function useChart() { + const context = React.useContext(ChartContext); + + if (!context) { + throw new Error("useChart must be used within a "); + } + + return context; +} + +const ChartContainer = React.forwardRef< + HTMLDivElement, + React.ComponentProps<"div"> & { + config: ChartConfig; + children: React.ComponentProps< + typeof RechartsPrimitive.ResponsiveContainer + >["children"]; + } +>(({ id, className, children, config, ...props }, ref) => { + const uniqueId = React.useId(); + const chartId = `chart-${id || uniqueId.replace(/:/g, "")}`; + + return ( + +
+ + + {children} + +
+
+ ); +}); +ChartContainer.displayName = "Chart"; + +const ChartStyle = ({ id, config }: { id: string; config: ChartConfig }) => { + const colorConfig = Object.entries(config).filter( + ([_, config]) => config.theme || config.color + ); + + if (!colorConfig.length) { + return null; + } + + return ( +