import React from 'react'; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { PieChart, Pie, Cell, ResponsiveContainer, Legend, Tooltip } from 'recharts'; import { CategorySummary } from '@/types/financialTypes'; interface CategoryChartProps { categories: CategorySummary[]; isLoading?: boolean; } const CategoryChart: React.FC = ({ categories, isLoading = false }) => { // Filter out any categories with zero value to prevent rendering issues const validCategories = categories.filter(cat => cat.valor > 0); // Calcular o valor total para exibir no centro do gráfico const totalValue = validCategories.reduce((sum, cat) => sum + cat.valor, 0); const renderCustomizedLabel = ({ cx, cy, midAngle, innerRadius, outerRadius, percent, index, name }: any) => { if (percent < 0.05) return null; // Não mostrar texto para fatias muito pequenas const radius = innerRadius + (outerRadius - innerRadius) * 0.7; const x = cx + radius * Math.cos(-midAngle * Math.PI / 180); const y = cy + radius * Math.sin(-midAngle * Math.PI / 180); return ( cx ? 'start' : 'end'} dominantBaseline="central" className="text-xs font-bold" > {`${(percent * 100).toFixed(0)}%`} ); }; const formatCurrency = (value: number) => { return new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL', }).format(value); }; const renderCustomLegend = (props: any) => { const { payload } = props; return (
{payload.map((entry: any, index: number) => (
{entry.value}
))}
); }; const renderCustomTooltip = ({ active, payload }: any) => { if (active && payload && payload.length) { return (

{payload[0].name}

{formatCurrency(payload[0].value)}

{`${(payload[0].payload.percentage * 100).toFixed(1)}%`}

); } return null; }; return ( Gastos por Categoria {isLoading ? (
) : validCategories.length === 0 ? (

Sem dados disponíveis

Verifique se existem transações do tipo 'despesa' cadastradas

) : (
{validCategories.map((entry, index) => ( ))} {/* Valor total no centro do gráfico */}
{formatCurrency(totalValue)}
)} ); }; export default CategoryChart;