import { ChartConfig, ChartContainer } from '@/components/ui/chart'; import { formatPercentValue, PercentDataPoint } from '@/lib/chart-calculations'; import { cn } from '@/lib/utils'; import { __ } from '@/utils/i18n'; import { useEffect, useRef } from 'react'; import { Bar, BarChart, Cell, ReferenceLine, Tooltip, XAxis, YAxis, } from 'recharts'; interface MoMPercentChartProps { data: PercentDataPoint[]; xAxisFormatter?: (value: string) => string; className?: string; minBarWidth?: number; } const chartConfig: ChartConfig = { percent: { label: 'Change %', color: 'var(--color-chart-1)', }, }; interface TooltipPayloadItem { payload?: { month?: string; value?: number; percent?: number | null; }; } interface CustomTooltipProps { active?: boolean; payload?: TooltipPayloadItem[]; } function CustomTooltip({ active, payload }: CustomTooltipProps) { if (!active || !payload?.length) return null; const data = payload[0].payload; if (!data) return null; const percent = data.percent; return (
{data.month}
{__('Change')} 0 && 'text-emerald-600', percent !== null && percent < 0 && 'text-red-600', )} > {formatPercentValue(percent ?? null)}
); } export function MoMPercentChart({ data, xAxisFormatter, className, minBarWidth = 50, }: MoMPercentChartProps) { const scrollContainerRef = useRef(null); const minChartWidth = data.length * minBarWidth; useEffect(() => { if (scrollContainerRef.current) { scrollContainerRef.current.scrollLeft = scrollContainerRef.current.scrollWidth; } }, [data]); const chartData = data.map((point) => ({ ...point, displayValue: point.percent, })); return (
`${value.toFixed(0)}%` } width={50} /> } cursor={{ fill: 'var(--color-muted)', opacity: 0.3 }} /> {chartData.map((entry, index) => { const value = entry.displayValue; let fill = 'var(--color-muted)'; if (value !== null) { fill = value >= 0 ? 'var(--color-chart-2)' : 'var(--color-chart-5)'; } return ; })}
); }