expreciones_algebraicas/app/funciones.js

98 lines
2.9 KiB
JavaScript
Raw Normal View History

2024-09-17 11:16:03 -06:00
import React, { useRef, useEffect } from 'react';
2024-09-13 14:55:25 -06:00
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-17 11:16:03 -06:00
const canvasRef = useRef(null);
2024-09-13 14:55:25 -06:00
useEffect(() => {
2024-09-17 11:16:03 -06:00
// Asegúrate de que el código solo se ejecute en el cliente
const canvas = canvasRef.current;
2024-09-13 15:47:35 -06:00
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");
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,
2024-09-17 11:16:03 -06:00
mode: 'x',
2024-09-16 17:13:11 -06:00
},
pan: {
enabled: true,
2024-09-17 11:16:03 -06:00
mode: 'x',
2024-09-16 17:13:11 -06:00
},
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,
2024-09-17 11:16:03 -06:00
mode: 'y',
2024-09-16 17:13:11 -06:00
},
pan: {
enabled: true,
2024-09-17 11:16:03 -06:00
mode: 'y',
2024-09-16 17:13:11 -06:00
},
}
},
plugins: {
zoom: {
pan: {
enabled: true,
2024-09-17 11:16:03 -06:00
mode: 'xy',
2024-09-16 17:13:11 -06:00
},
zoom: {
enabled: true,
2024-09-17 11:16:03 -06:00
mode: 'xy',
speed: 0.1,
sensitivity: 3,
threshold: 2,
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-17 11:16:03 -06:00
return <canvas ref={canvasRef} width="400" height="175"></canvas>;
2024-09-13 14:55:25 -06:00
};
2024-09-13 15:47:35 -06:00
export default Tablero;