From 90c715c02adfb9289ce5c4dd410a85bb565b5f43 Mon Sep 17 00:00:00 2001 From: Bharat Kathi Date: Sun, 1 Sep 2024 14:13:12 -0700 Subject: [PATCH] add recharts --- web/package-lock.json | 1 + web/src/components/ui/chart.tsx | 365 ++++++++++++++++++++++++++++++++ web/src/index.css | 10 + 3 files changed, 376 insertions(+) create mode 100644 web/src/components/ui/chart.tsx diff --git a/web/package-lock.json b/web/package-lock.json index 5f601e9..9ea7e96 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -7335,6 +7335,7 @@ "version": "2.12.7", "resolved": "https://registry.npmjs.org/recharts/-/recharts-2.12.7.tgz", "integrity": "sha512-hlLJMhPQfv4/3NBSAyq3gzGg4h2v69RJh6KU7b3pXYNNAELs9kEoXOjbkxdXpALqKBoVmVptGfLpxdaVYqjmXQ==", + "license": "MIT", "dependencies": { "clsx": "^2.0.0", "eventemitter3": "^4.0.1", diff --git a/web/src/components/ui/chart.tsx b/web/src/components/ui/chart.tsx new file mode 100644 index 0000000..74551d7 --- /dev/null +++ b/web/src/components/ui/chart.tsx @@ -0,0 +1,365 @@ +"use client"; + +import * as React from "react"; +import * as RechartsPrimitive from "recharts"; + +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 ( +