import * as React from "react" import * as RechartsPrimitive from "recharts" import type { LegendPayload } from "recharts/types/component/DefaultLegendContent" import { NameType, Payload, ValueType, } from "recharts/types/component/DefaultTooltipContent" import type { Props as LegendProps } from "recharts/types/component/Legend" import { TooltipContentProps } from "recharts/types/component/Tooltip" 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 } export type CustomTooltipProps = TooltipContentProps & { className?: string hideLabel?: boolean hideIndicator?: boolean indicator?: "line" | "dot" | "dashed" nameKey?: string labelKey?: string labelFormatter?: ( label: TooltipContentProps["label"], payload: TooltipContentProps["payload"] ) => React.ReactNode formatter?: ( value: number | string, name: string, item: Payload, index: number, payload: ReadonlyArray> ) => React.ReactNode labelClassName?: string color?: string } export type ChartLegendContentProps = { className?: string hideIcon?: boolean verticalAlign?: LegendProps["verticalAlign"] payload?: LegendPayload[] nameKey?: string } 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 } function ChartContainer({ id, className, children, config, ...props }: React.ComponentProps<"div"> & { config: ChartConfig children: React.ComponentProps< typeof RechartsPrimitive.ResponsiveContainer >["children"] }) { const uniqueId = React.useId() const chartId = `chart-${id || uniqueId.replace(/:/g, "")}` return (
{children}
) } 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 (