import React from 'react'; import { PieChart, Pie, Cell, ResponsiveContainer, Label } from 'recharts'; import type { OffsetProject } from '../types'; import { getProjectColor } from '../utils/portfolioColors'; interface RechartsPortfolioPieChartProps { projects: OffsetProject[]; totalTons: number; size?: number; } interface ChartDataPoint { name: string; value: number; tons: number; percentage: number; fill: string; } export function RechartsPortfolioPieChart({ projects, totalTons, size = 280, }: RechartsPortfolioPieChartProps) { // Transform data to Recharts format const chartData: ChartDataPoint[] = projects .filter(project => project.percentage && project.percentage > 0) .map((project, index) => { const tons = totalTons * (project.percentage || 0); return { name: project.name, value: tons, tons: parseFloat(tons.toFixed(2)), percentage: (project.percentage || 0) * 100, fill: getProjectColor(index, projects.length), }; }); // Custom label component for multi-line labels const renderCustomLabel = (props: any) => { const { cx, cy, midAngle, outerRadius, name, tons, percentage, fill, } = props; const RADIAN = Math.PI / 180; const radius = outerRadius + 35; const x = cx + radius * Math.cos(-midAngle * RADIAN); const y = cy + radius * Math.sin(-midAngle * RADIAN); const textAnchor = x > cx ? 'start' : 'end'; // Calculate line endpoint for label connector const lineEndX = x > cx ? x + 30 : x - 30; const labelStartX = cx + (outerRadius + 10) * Math.cos(-midAngle * RADIAN); const labelStartY = cy + (outerRadius + 10) * Math.sin(-midAngle * RADIAN); return ( {/* Connector line from pie edge to label */} {/* Single-line label text - project name with percentage */} {name} ({percentage.toFixed(1)}%) ); }; return (
{/* SVG Chart - Responsive container (hidden in print) */}
{chartData.map((entry, index) => ( ))} {/* Center label showing total */}
{/* Print-optimized table (visible only in print) */}

Portfolio Distribution

Total: {totalTons} tons CO₂

{chartData.map((item, index) => ( ))}
Project Name Percentage Tons CO₂
{item.name} {item.percentage.toFixed(1)}% {item.tons}
{/* Legend below chart (for screen clarity, hidden in print) */}
{chartData.map((item, index) => (

{item.name}

{item.tons} tons ({item.percentage.toFixed(1)}%)

))}
); }