import { useMemo, useState, useRef, useCallback } from 'react'; /* ── Palette ────────────────────────────────────────────────── */ const PALETTE = [ '#e8547a', '#9333ea', '#0d9488', '#f59e0b', '#3b82f6', '#10b981', '#f97316', '#06b6d4', '#a855f7', '#84cc16', '#ec4899', '#14b8a6', ]; /* ── Algorithme treemap (binary split équilibré) ─────────────── */ function buildTreemap(items, x, y, w, h) { if (!items.length) return []; if (items.length === 1) return [{ ...items[0], x, y, w, h }]; const total = items.reduce((s, i) => s + i.value, 0); let best = 1, bestDiff = Infinity, acc = 0; for (let i = 0; i < items.length - 1; i++) { acc += items[i].value; const diff = Math.abs(acc - (total - acc)); if (diff < bestDiff) { bestDiff = diff; best = i + 1; } } const g1 = items.slice(0, best); const g2 = items.slice(best); const r1 = g1.reduce((s, i) => s + i.value, 0) / total; if (w >= h) { const w1 = w * r1; return [...buildTreemap(g1, x, y, w1, h), ...buildTreemap(g2, x + w1, y, w - w1, h)]; } else { const h1 = h * r1; return [...buildTreemap(g1, x, y, w, h1), ...buildTreemap(g2, x, y + h1, w, h - h1)]; } } function fmtAmount(v) { return Math.round(v).toLocaleString('fr-FR') + ' €'; } function wrapText(text, maxWidth, fontSize) { const charW = fontSize * 0.58; const maxChars = Math.max(1, Math.floor(maxWidth / charW)); const words = text.split(' '); const lines = []; let current = ''; for (const word of words) { const candidate = current ? current + ' ' + word : word; if (candidate.length <= maxChars) { current = candidate; } else { if (current) lines.push(current); current = word.length > maxChars ? word.slice(0, maxChars - 1) + '…' : word; } } if (current) lines.push(current); return lines.slice(0, 3); } /* ── Composant ──────────────────────────────────────────────── */ export default function InteretsDistributionChart({ rows, netMode }) { const [hoveredIdx, setHoveredIdx] = useState(null); const [tooltip, setTooltip] = useState(null); const svgRef = useRef(null); const wrapRef = useRef(null); /* Intérêts cumulés par plateforme */ const data = useMemo(() => { if (!rows?.length) return []; const byPlat = {}; for (const r of rows) { const key = r.plateforme_nom || `#${r.plateforme_id}`; const val = netMode ? (r.interets_nets || 0) : (r.interets_bruts || 0); byPlat[key] = (byPlat[key] || 0) + val; } return Object.entries(byPlat) .filter(([, v]) => v > 0) .sort(([, a], [, b]) => b - a) .map(([name, value], i) => ({ name, value, color: PALETTE[i % PALETTE.length] })); }, [rows, netMode]); const total = data.reduce((s, i) => s + i.value, 0); const W = 440, H = 290, GAP = 3; const cells = useMemo(() => buildTreemap(data, 0, 0, W, H), [data]); const handleMouseMove = useCallback((e, cell, idx) => { if (!wrapRef.current) return; const wrapRect = wrapRef.current.getBoundingClientRect(); setHoveredIdx(idx); setTooltip({ x: e.clientX - wrapRect.left, y: e.clientY - wrapRect.top, cell }); }, []); const handleMouseLeave = useCallback(() => { setHoveredIdx(null); setTooltip(null); }, []); if (!cells.length) return null; const TIP_W = 160, TIP_H = 66; const tipStyle = tooltip ? (() => { const ww = wrapRef.current?.offsetWidth || 440; const wh = wrapRef.current?.offsetHeight || 360; let tx = tooltip.x + 14; let ty = tooltip.y - TIP_H / 2; if (tx + TIP_W > ww - 4) tx = tooltip.x - TIP_W - 14; if (ty < 4) ty = 4; if (ty + TIP_H > wh - 4) ty = wh - TIP_H - 4; return { left: tx, top: ty }; })() : null; const modeLabel = netMode ? 'Nets' : 'Bruts'; return (