expreciones_algebraicas/app/funciones.js

65 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-09-13 14:55:25 -06:00
import React, { useEffect } from 'react';
import { Chart, registerables } from 'chart.js';
// Registra los módulos necesarios de Chart.js
Chart.register(...registerables);
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'
}
},
y: {
title: {
display: true,
2024-09-13 15:47:35 -06:00
text: 'f(x)'
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
return <canvas id="myChart" width="400" height="200"></canvas>;
};
2024-09-13 15:47:35 -06:00
export default Tablero;