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 (
Total: {totalTons} tons CO₂
| Project Name | Percentage | Tons CO₂ |
|---|---|---|
| {item.name} | {item.percentage.toFixed(1)}% | {item.tons} |
{item.name}
{item.tons} tons ({item.percentage.toFixed(1)}%)