Spaces:
Sleeping
Sleeping
File size: 1,646 Bytes
197ac2a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import { useEffect, useRef } from "react";
import { Chart } from "chart.js/auto";
export default function TrendChart({ dias, metrica, rotulo }) {
const canvasRef = useRef(null);
const chartRef = useRef(null);
useEffect(() => {
if (!dias.length) {
chartRef.current?.destroy();
chartRef.current = null;
return;
}
const [grupo, campo] = metrica.split(".");
const labels = dias.map((d) => (d.date || "").slice(5));
const valores = dias.map((d) => {
const v = (d.metrics?.[grupo] || {})[campo];
return typeof v === "number" ? v : v == null ? null : parseFloat(v);
});
const tinta = (getComputedStyle(document.documentElement).getPropertyValue("--muted") || "#6b7280").trim();
chartRef.current?.destroy();
chartRef.current = new Chart(canvasRef.current, {
type: "line",
data: {
labels,
datasets: [
{
label: rotulo,
data: valores,
borderColor: "#7c3aed",
backgroundColor: "rgba(124,58,237,.16)",
fill: true,
tension: 0.35,
spanGaps: true,
pointRadius: 3,
pointBackgroundColor: "#7c3aed",
},
],
},
options: {
plugins: { legend: { labels: { color: tinta } } },
scales: {
x: { ticks: { color: tinta }, grid: { display: false } },
y: { ticks: { color: tinta }, grid: { color: "rgba(148,163,184,.15)" } },
},
},
});
return () => chartRef.current?.destroy();
}, [dias, metrica, rotulo]);
return <canvas ref={canvasRef} height="110"></canvas>;
}
|