informe-comercial-alfa/app/components/pipeline/DashDonut.js

47 lines
2.1 KiB
JavaScript
Raw Normal View History

import { money } from "../../lib/constants";
import { agg } from "../../lib/derive";
import FilterableRow from "../FilterableRow";
// Donut para Segmento — equivalente de dashDonut() (pages/resumen.js), con cross-filter de clic
// en la leyenda vía FilterableRow (mismo patrón que Panel.js/DashBar.js).
export default function DashDonut({ title, arr, dimKey }) {
const rows = agg(arr, dimKey, (o) => o.val ?? o.actual ?? 0).sort((a, b) => b.v - a.v);
const total = rows.reduce((s, d) => s + d.v, 0) || 1;
const R = 48, r = 30, C = 2 * Math.PI * R, cx = 64, cy = 64;
let acc = 0;
const color = (k) => (k === "Cartera Activa" ? "var(--alerta)" : "var(--marca)");
const arcs = rows.map((d) => {
const frac = d.v / total, len = frac * C, rot = (acc / C) * 360 - 90;
acc += len;
return (
<circle
key={d.k}
cx={cx} cy={cy} r={R} fill="none" stroke={color(d.k)} strokeWidth={r}
strokeDasharray={`${len.toFixed(2)} ${(C - len).toFixed(2)}`}
transform={`rotate(${rot.toFixed(2)} ${cx} ${cy})`}
// Tooltip vía atributo `title` (no `<title>` hijo): un `<title>` anidado dentro de <svg>
// dispara un mismatch de hidratación real en este árbol (mismo fix que DonutChart.js).
title={`${d.k}: ${d.n} oport. · ${money(d.v)}`}
/>
);
});
return (
<div className="panel dash-panel">
<h3>{title}</h3>
<div style={{ display: "flex", alignItems: "center", gap: 16, flexWrap: "wrap" }}>
<svg viewBox="0 0 128 128" width="128" height="128">{arcs}</svg>
<div style={{ flex: 1, minWidth: 150 }}>
{rows.length ? rows.map((d) => (
<FilterableRow as="div" key={d.k} pairs={[[dimKey, d.k]]} className="donut-leg-row">
<span className="donut-dot" style={{ background: color(d.k) }} />
<span className="nm">{d.k}</span>
<span className="v">{d.n}</span>
<span className="c">{money(d.v)}</span>
</FilterableRow>
)) : <p className="hint">Sin datos.</p>}
</div>
</div>
</div>
);
}