/* Primitivas de gráfico SVG — mismo lenguaje visual que el dashboard existente */
const OC = {
mint: '#8FDDB4', yellow: '#F2C96B', orange: '#EE8B57', red: '#EC6A5E',
blue: '#A4C0F3', pale: '#DCE6F7', grey: '#94A3AA', line: 'rgba(255,255,255,.14)',
ink: '#FFFFFF', ink2: '#C9D2D6', ink3: '#9EADB4', cell: '#3F4D53', card: '#4C5A61',
};
function useW(initial = 400) {
const ref = React.useRef(null);
const [w, setW] = React.useState(initial);
React.useLayoutEffect(() => {
if (!ref.current) return;
const ro = new ResizeObserver(([e]) => setW(Math.max(80, Math.floor(e.contentRect.width))));
ro.observe(ref.current);
return () => ro.disconnect();
}, []);
return [ref, w];
}
function occColor(p) {
if (p >= 0.8) return OC.orange;
if (p >= 0.7) return OC.yellow;
return OC.blue;
}
/* Escala continua para mapas de calor: celda → azul → amarillo → naranja */
function heatColor(p) {
const stops = [[0, [70, 86, 94]], [0.3, [94, 124, 160]], [0.55, [164, 192, 243]], [0.7, [242, 201, 107]], [0.82, [238, 139, 87]], [1, [236, 106, 94]]];
p = Math.max(0, Math.min(1, p));
for (let i = 1; i < stops.length; i++) {
if (p <= stops[i][0]) {
const [a, ca] = stops[i - 1], [b, cb] = stops[i]; const t = (p - a) / (b - a);
const c = ca.map((v, k) => Math.round(v + (cb[k] - v) * t));
return `rgb(${c[0]},${c[1]},${c[2]})`;
}
}
return 'rgb(236,106,94)';
}
/* ── Barras verticales */
function Bars({ data, h = 64, color = OC.blue, gap = 2, labels = true, hi = -1, fmt = (v) => OC_nf(v), refY, refLabel }) {
const [ref, w] = useW();
const max = Math.max(...data.map(d => d.v), refY || 0) || 1;
const lh = labels ? 14 : 0;
const bw = (w - gap * (data.length - 1)) / data.length;
return (
);
}
/* ── Serie temporal de 15 min (05:00–23:00) */
function TimeArea({ series, h = 110, yMax, from = 6, to = 22, refs = [], marks = [], now, ticks = [7, 9, 11, 13, 15, 17, 19, 21], yFmt, yTicks, right }) {
const [ref, w] = useW();
const padL = yTicks ? 30 : 0, padR = right ? 34 : 0, padB = 16, padT = 6;
const iw = w - padL - padR, ih = h - padB - padT;
const s0 = Math.round((from - OCC.T0) / 0.25), s1 = Math.round((to - OCC.T0) / 0.25);
const xOf = (s) => padL + (s - s0) / (s1 - s0) * iw;
const xT = (t) => xOf((t - OCC.T0) / 0.25);
const max = yMax || Math.max(...series.flatMap(s => s.values.slice(s0, s1 + 1))) * 1.1;
const yOf = (v, m = max) => padT + ih - Math.min(1.05, v / m) * ih;
const path = (vals, m) => { let pen = false; return vals.slice(s0, s1 + 1).map((v, i) => { if (v == null) { pen = false; return ''; } const c = (pen ? 'L' : 'M') + xOf(s0 + i).toFixed(1) + ',' + yOf(v, m).toFixed(1); pen = true; return c; }).join(''); };
const lastIdx = (vals) => { let k = s1; while (k > s0 && vals[k] == null) k--; return k; };
return (
);
}
/* ── Mapa de calor */
function Heatmap({ rows, cols, values, cell = 14, rowLabelW = 16, colLabel, fmt }) {
const [ref, w] = useW();
const cw = (w - rowLabelW) / cols.length;
const ch = cell;
return (
);
}
/* ── Barra apilada (patrón existente) */
function StackBar({ segs, h = 7 }) {
const tot = segs.reduce((s, x) => s + x.v, 0) || 1;
return (
);
}
function Legend({ items, style }) {
return (
{items.map((it, i) =>
{it.l}
)}
);
}
/* ── Medidor semicircular con umbrales 70/80 */
function Gauge({ value, size = 132, label }) {
const r = size / 2 - 10, cx = size / 2, cy = size / 2 + 2;
const a = (p) => Math.PI * (1 - Math.min(1.0, p));
const pt = (p, rr = r) => [cx + rr * Math.cos(a(p)), cy - rr * Math.sin(a(p))];
const arc = (p0, p1, rr = r) => { const [x0, y0] = pt(p0, rr), [x1, y1] = pt(p1, rr); return `M${x0},${y0} A${rr},${rr} 0 0 1 ${x1},${y1}`; };
return (
);
}
/* ── Lista de barras horizontales con marcas 70/80 */
function HBarList({ items, max = 1, marks = [0.7, 0.8], labelW = 150, fmt = (v) => OCC.pct(v), barH = 6, rowH = 22, valueW = 44 }) {
return (
{items.map((it, i) => (
{it.label}
{it.v2 != null &&
}
{marks.map(m =>
)}
{fmt(it.v, it)}
))}
);
}
/* ── Dispersión */
function Scatter({ pts, h = 150, xDom = [0, 1], yDom, xFmt = (v) => OCC.pct(v), yFmt = (v) => OC_nf(v), xTicks, yTicks, fit = true, vlines = [], xLabel, yLabel, r = 3.2, color = OC.blue }) {
const [ref, w] = useW();
const padL = 34, padB = 28, padT = 8, padR = 8;
const iw = w - padL - padR, ih = h - padB - padT;
const yd = yDom || [Math.min(...pts.map(p => p.y)) * 0.9, Math.max(...pts.map(p => p.y)) * 1.08];
const X = (v) => padL + (v - xDom[0]) / (xDom[1] - xDom[0]) * iw;
const Y = (v) => padT + ih - (v - yd[0]) / (yd[1] - yd[0]) * ih;
let line = null;
if (fit && pts.length > 2) {
const mx = OCC.avg(pts.map(p => p.x)), my = OCC.avg(pts.map(p => p.y));
const b = OCC.sum(pts.map(p => (p.x - mx) * (p.y - my))) / (OCC.sum(pts.map(p => (p.x - mx) ** 2)) || 1);
const a = my - b * mx;
const xs = [Math.min(...pts.map(p => p.x)), Math.max(...pts.map(p => p.x))];
line = xs.map(x => [X(x), Y(a + b * x)]);
}
return (
);
}
/* ── Cronograma clima vs ocupación */
function HvacTimeline({ rows, from = 5, to = 23, labelW = 150, rowH = 18 }) {
const [ref, w] = useW();
const iw = w - labelW;
const X = (t) => labelW + (t - from) / (to - from) * iw;
return (
);
}
function OC_nf(v, d = 0) { return OCC.nf(v, d); }
Object.assign(window, { OC, useW, occColor, heatColor, Bars, TimeArea, Heatmap, StackBar, Legend, Gauge, HBarList, Scatter, HvacTimeline, OC_nf });