2024-09-13 14:55:25 -06:00
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
import { Chart, registerables } from 'chart.js';
|
2024-09-16 17:13:11 -06:00
|
|
|
import zoomPlugin from 'chartjs-plugin-zoom';
|
2024-09-13 14:55:25 -06:00
|
|
|
|
2024-09-16 17:13:11 -06:00
|
|
|
// Registra los módulos necesarios de Chart.js y el plugin de zoom
|
|
|
|
Chart.register(...registerables, zoomPlugin);
|
2024-09-13 14:55:25 -06:00
|
|
|
|
2024-09-13 15:47:35 -06:00
|
|
|
const Tablero = ({ labels, dataPoints }) => {
|
2024-09-13 14:55:25 -06:00
|
|
|
useEffect(() => {
|
2024-09-13 15:47:35 -06:00
|
|
|
// Verifica si el canvas está disponible
|
|
|
|
const canvas = document.getElementById("myChart");
|
|
|
|
if (!canvas) {
|
|
|
|
console.error("Canvas element not found");
|
|
|
|
return;
|
|
|
|
}
|
2024-09-13 14:55:25 -06:00
|
|
|
|
2024-09-13 15:47:35 -06:00
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
|
|
|
|
// Verifica si el contexto es válido
|
|
|
|
if (!ctx) {
|
|
|
|
console.error("Failed to get canvas context");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Datos del gráfico
|
2024-09-13 14:55:25 -06:00
|
|
|
const data = {
|
|
|
|
labels: labels,
|
2024-09-13 15:47:35 -06:00
|
|
|
datasets: [{
|
|
|
|
label: 'f(x)',
|
|
|
|
borderColor: 'rgba(75, 192, 192, 1)',
|
|
|
|
data: dataPoints,
|
|
|
|
fill: false
|
|
|
|
}]
|
2024-09-13 14:55:25 -06:00
|
|
|
};
|
|
|
|
|
2024-09-13 15:47:35 -06:00
|
|
|
// Configuración del gráfico
|
|
|
|
const myChart = new Chart(ctx, {
|
2024-09-13 14:55:25 -06:00
|
|
|
type: 'line',
|
|
|
|
data: data,
|
|
|
|
options: {
|
|
|
|
scales: {
|
|
|
|
x: {
|
|
|
|
title: {
|
|
|
|
display: true,
|
|
|
|
text: 'X'
|
2024-09-16 17:13:11 -06:00
|
|
|
},
|
|
|
|
zoom: {
|
|
|
|
enabled: true,
|
|
|
|
mode: 'x', // Habilita el zoom en el eje X
|
|
|
|
},
|
|
|
|
pan: {
|
|
|
|
enabled: true,
|
|
|
|
mode: 'x', // Habilita el desplazamiento en el eje X
|
|
|
|
},
|
2024-09-13 14:55:25 -06:00
|
|
|
},
|
|
|
|
y: {
|
|
|
|
title: {
|
|
|
|
display: true,
|
2024-09-13 15:47:35 -06:00
|
|
|
text: 'f(x)'
|
2024-09-16 17:13:11 -06:00
|
|
|
},
|
|
|
|
zoom: {
|
|
|
|
enabled: true,
|
|
|
|
mode: 'y', // Habilita el zoom en el eje Y
|
|
|
|
},
|
|
|
|
pan: {
|
|
|
|
enabled: true,
|
|
|
|
mode: 'y', // Habilita el desplazamiento en el eje Y
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
plugins: {
|
|
|
|
zoom: {
|
|
|
|
pan: {
|
|
|
|
enabled: true,
|
|
|
|
mode: 'xy', // Permite el desplazamiento en ambos ejes
|
|
|
|
},
|
|
|
|
zoom: {
|
|
|
|
enabled: true,
|
|
|
|
mode: 'xy', // Permite el zoom en ambos ejes
|
|
|
|
speed: 0.1, // Velocidad de zoom
|
|
|
|
sensitivity: 3, // Sensibilidad del zoom
|
|
|
|
threshold: 2, // Umbral de activación del zoom
|
2024-09-13 14:55:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Limpieza del gráfico cuando el componente se desmonte
|
2024-09-13 15:47:35 -06:00
|
|
|
return () => myChart.destroy();
|
|
|
|
}, [labels, dataPoints]);
|
2024-09-13 14:55:25 -06:00
|
|
|
|
2024-09-16 17:13:11 -06:00
|
|
|
return <canvas id="myChart" width="400" height="175"></canvas>;
|
2024-09-13 14:55:25 -06:00
|
|
|
};
|
|
|
|
|
2024-09-13 15:47:35 -06:00
|
|
|
export default Tablero;
|