import { calculateNiceRange, formatNumber } from "~/lib/utils"; export interface BarChartData { label: string; value: number; maxValue?: number; color?: string; labelColor?: string; valuePrefix?: string; valueSuffix?: string; } interface CustomBarChartProps { data: BarChartData[]; title?: string; height?: string; barHeight?: string; showAxisLabels?: boolean; className?: string; loading?: boolean; hasPercent?: boolean; } export function CustomBarChart({ data, title, height = "auto", barHeight = "h-6", showAxisLabels = true, className = "", loading = false, hasPercent = true, }: CustomBarChartProps) { // استفاده از nice numbers برای محاسبه دامنه مناسب const values = data.map((item) => item.maxValue || item.value); const { niceMax, ticks } = calculateNiceRange(values, 0, 5); const globalMaxValue = niceMax; // Loading skeleton if (loading) { return (
{title && (
)}
{Array.from({ length: 4 }).map((_, index) => (
{/* Label skeleton */}
{/* Bar skeleton */}
{/* Value skeleton */}
))}
); } return (
{title && (

{title}

)}
{data.map((item, index) => { // محاسبه درصد بر اساس nice max value const percentage = globalMaxValue > 0 ? (item.value / globalMaxValue) * 100 : 0; const displayValue: any = item.value; return (
{item.label}
{item.valuePrefix || ""} {formatNumber(parseFloat(displayValue))} {hasPercent ? "%" : ""} {item.valueSuffix || ""}
); })} {/* Axis Labels با استفاده از nice numbers */} {showAxisLabels && globalMaxValue > 0 && (
{ticks.map((tick, index) => ( {formatNumber(tick)}% ))}
)}
); }