import React from "react";
import { formatNumber } from "~/lib/utils";
interface FunnelData {
name: string;
value: number;
label: string;
percentage?: string;
}
interface FunnelChartProps {
data: FunnelData[];
title?: string;
className?: string;
}
export function FunnelChart({ data, title, className = "" }: FunnelChartProps) {
const maxValue = Math.max(...data.map(d => d.value));
const toPercent = (value: number) => {
if (!maxValue || maxValue <= 0) return 0;
return Math.round((value / maxValue) * 100);
};
return (
{title && (
{title}
)}
{/* Start Process Line */}
{/* Funnel Bars */}
{data.map((item, index) => {
const widthPercentage = toPercent(item.value);
const barWidth = Math.max(20, widthPercentage); // Minimum 20% width
return (
{/* Right side label (RTL) */}
{item.label}
{/* Centered bar with equal side spacers so that numbers align on a vertical axis */}
{/* Left spacer */}
{/* Bar */}
{item.value.toLocaleString('fa-IR')}
{/* Right spacer */}
);
})}
{/* End Process Line */}
انتها فرآیند
{(() => {
const lastValue = data[data.length - 1]?.value ?? 0;
const percent = toPercent(lastValue);
return (
);
})()}
);
}