From 3bcfea96dd7485f7b4f548b7802d524e2d3c4e1f Mon Sep 17 00:00:00 2001 From: "stefan.meyer" Date: Thu, 5 Dec 2024 06:44:18 +0000 Subject: [PATCH] Added recharts for diagramms --- package.json | 3 +- src/components/ui/chart.tsx | 365 ++++++++++++++++++++++++++++ src/server/services/node.service.ts | 3 +- 3 files changed, 369 insertions(+), 2 deletions(-) create mode 100644 src/components/ui/chart.tsx diff --git a/package.json b/package.json index 62d42b3..066672e 100644 --- a/package.json +++ b/package.json @@ -47,7 +47,7 @@ "cross-env": "^7.0.3", "date-fns": "^4.1.0", "date-fns-tz": "^3.2.0", - "lucide-react": "^0.462.0", + "lucide-react": "^0.465.0", "moment": "^2.30.1", "next": "14.2.15", "next-auth": "^4.24.8", @@ -60,6 +60,7 @@ "react-day-picker": "8.10.1", "react-dom": "^18.3.1", "react-hook-form": "^7.53.1", + "recharts": "^2.14.1", "reflect-metadata": "^0.2.2", "simple-git": "^3.27.0", "socket.io": "^4.8.1", diff --git a/src/components/ui/chart.tsx b/src/components/ui/chart.tsx new file mode 100644 index 0000000..8620baa --- /dev/null +++ b/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 ( +