'use client'; import { Bar, BarChart, CartesianGrid, Legend, XAxis, YAxis } from 'recharts'; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'; import { type ChartConfig, ChartContainer, ChartTooltip, ChartTooltipContent } from '@/components/ui/chart'; import type { ReactNode } from 'react'; export interface BarChartData { [key: string]: string | number; } interface XAxisConfig { key: string; tickFormatter?: (value: string) => string; tickMargin?: number; angle?: number; } interface BarChartProps { data: BarChartData[]; config: ChartConfig; title: string; description?: string; xAxis: XAxisConfig; className?: string; footer?: ReactNode; showYAxis?: boolean; } const defaultTickFormatter = (value: string) => { if (!value) { return ''; } // Try comma split first const commaSplit = value.split(',')[0]; if (commaSplit !== value) { return commaSplit; } // If no comma, truncate to first 3 chars return value.slice(0, 3); }; const defaultTickMargin = 8; export function BarChartComponent({ data, config, title, description, xAxis, footer, showYAxis }: BarChartProps) { const dataKeys = Object.keys(config); const { key, tickFormatter = defaultTickFormatter, tickMargin = defaultTickMargin } = xAxis; return ( {title} {description && {description}} {dataKeys.length > 1 && ( config[value].label} /> )} {showYAxis && } } /> {dataKeys.map((key) => ( ))} {dataKeys.map((key) => ( ))} {footer && {footer}} ); }