LANIA_Proyecto/js/funcionesGraficos.js

310 lines
10 KiB
JavaScript

async function recuperarCantidadGenero(tipoConsulta, filtros = {}) {
try {
const response = await fetch("../controllers/graficos.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ tipoConsulta, ...filtros }),
});
const data = await response.json();
return data.cantidad || 0;
} catch (error) {
console.error("Error al recuperar datos:", error);
return 0;
}
}
async function recuperarCantidadEdad(tipoConsulta, filtros = {}) {
try {
const response = await fetch("../controllers/graficos.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ tipoConsulta, ...filtros }),
});
const data = await response.json();
return data.cantidad || 0;
} catch (error) {
console.error("Error al recuperar datos:", error);
return 0;
}
}
async function recuperarCantidadEstado(tipoConsulta, filtros = {}) {
try {
const response = await fetch("../controllers/graficos.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ tipoConsulta, ...filtros }),
});
const data = await response.json();
if (!Array.isArray(data)) throw new Error("La respuesta del backend no es un array.");
return data;
} catch (error) {
console.error("Error al recuperar datos de estados:", error);
return [];
}
}
async function recuperarCantidadExamen(tipoConsulta, filtros = {}) {
try {
const response = await fetch("../controllers/graficos.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ tipoConsulta, ...filtros }),
});
const data = await response.json();
if (!Array.isArray(data)) throw new Error("La respuesta del backend no es un array");
return data;
} catch (error) {
console.error("Error al recuperar datos de examenes:", error);
return [];
}
}
async function recuperarCantidadFecha(tipoConsulta, filtros = {}) {
try {
const response = await fetch("../controllers/graficos.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ tipoConsulta, ...filtros }),
});
const data = await response.json();
if (!Array.isArray(data)) throw new Error("La respuesta del backend no es un array.");
return data;
} catch (error) {
console.error("Error al recuperar datos de fechas:", error);
return [];
}
}
let filtroActivo = null; // Puede ser 'id_rango_edad', 'id_genero', 'id_examen', 'fechas' o null
function getFiltrosSeleccionados() {
const id_rango_edad = document.getElementById('id_rango_edad')?.value || "NULL";
const id_genero = document.getElementById('id_genero')?.value || "NULL";
const id_examen = document.getElementById('id_examen')?.value || "NULL";
const fechaInicio = document.getElementById('date1')?.value || "";
const fechaFin = document.getElementById('date2')?.value || "";
const filtros = {};
if (id_rango_edad !== "NULL") {
filtros.id_rango_edad = id_rango_edad;
}
if (id_genero !== "NULL") {
filtros.id_genero = id_genero;
}
if (id_examen !== "NULL") {
filtros.id_examen = id_examen;
}
if (fechaInicio && fechaFin) {
filtros.fechaInicio = fechaInicio;
filtros.fechaFin = fechaFin;
}
return filtros;
}
function setFiltroActivo(tipo) {
filtroActivo = tipo;
// Limpiar otros filtros cuando se activa uno nuevo
if (tipo === 'id_rango_edad') {
document.getElementById('id_genero').value = "NULL";
document.getElementById('id_examen').value = "NULL";
document.getElementById('date1').value = "";
document.getElementById('date2').value = "";
} else if (tipo === 'id_genero') {
document.getElementById('id_rango_edad').value = "NULL";
document.getElementById('id_examen').value = "NULL";
document.getElementById('date1').value = "";
document.getElementById('date2').value = "";
} else if (tipo === 'id_examen') {
document.getElementById('id_rango_edad').value = "NULL";
document.getElementById('id_genero').value = "NULL";
document.getElementById('date1').value = "";
document.getElementById('date2').value = "";
} else if (tipo === 'fechas') {
document.getElementById('id_rango_edad').value = "NULL";
document.getElementById('id_genero').value = "NULL";
document.getElementById('id_examen').value = "NULL";
}
}
function getFiltroActivo() {
return filtroActivo;
}
async function obtenerDatosGeneros(filtros = {}) {
try {
const response = await fetch("../controllers/graficos.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
tipoConsulta: "Generos",
...filtros
}),
});
const data = await response.json();
if (!Array.isArray(data)) {
console.error("La respuesta del backend no es un array:", data);
return [];
}
return data;
} catch (error) {
console.error("Error al obtener datos de géneros:", error);
return [];
}
}
async function obtenerDatosEdades(filtros = {}) {
try {
const response = await fetch("../controllers/graficos.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
tipoConsulta: "Edades",
...filtros
}),
});
const data = await response.json();
if (!Array.isArray(data)) {
console.error("La respuesta del backend no es un array:", data);
return [];
}
return data;
} catch (error) {
console.error("Error al obtener datos de edades:", error);
return [];
}
}
async function obtenerDatosEstados(filtros = {}) {
try {
const response = await fetch("../controllers/graficos.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
tipoConsulta: "Estados",
...filtros
}),
});
const data = await response.json();
if (!Array.isArray(data)) {
console.error("La respuesta del backend no es un array:", data);
return [];
}
return data;
} catch (error) {
console.error("Error al obtener datos de estados:", error);
return [];
}
}
async function obtenerDatosExamenes(filtros = {}) {
try {
const response = await fetch("../controllers/graficos.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
tipoConsulta: "Examenes",
...filtros
}),
});
const data = await response.json();
if (!Array.isArray(data)) {
console.error("La respuesta del backend no es un array:", data);
return [];
}
return data;
} catch (error) {
console.error("Error al obtener datos de exámenes:", error);
return [];
}
}
async function obtenerDatosFechas(filtros = {}) {
const response = await fetch("../controllers/graficos.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ tipoConsulta: "Fechas", ...filtros }),
});
return await response.json();
}
async function obtenerResumenGenero(filtros = {}) {
const response = await fetch("../controllers/graficos.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ tipoConsulta: "ResumenGenero", ...filtros }),
});
return await response.json();
}
// Función para actualizar todos los gráficos
async function actualizarTodosLosGraficos() {
const filtros = getFiltrosSeleccionados();
// Actualizar gráfico de géneros
const datosGeneros = await obtenerDatosGeneros(filtros);
if (chartGenero) {
chartGenero.updateOptions({
xaxis: {
categories: datosGeneros.map(d => d.genero)
}
});
chartGenero.updateSeries([{
name: 'Cantidad',
data: datosGeneros.map(d => parseInt(d.cantidad))
}]);
}
// Actualizar gráfico de estados
const datosEstados = await obtenerDatosEstados(filtros);
if (chartEstados) {
chartEstados.updateOptions({
xaxis: {
categories: datosEstados.map(d => d.estado)
}
});
chartEstados.updateSeries([{
name: 'Cantidad',
data: datosEstados.map(d => parseInt(d.cantidad))
}]);
}
// Actualizar otros gráficos...
}
// Agregar event listeners para los filtros
document.addEventListener('DOMContentLoaded', function() {
const selectExamen = document.getElementById('id_examen');
const selectGenero = document.getElementById('id_genero');
if (selectExamen) {
selectExamen.addEventListener('change', function() {
if (this.value !== "NULL") {
setFiltroActivo('id_examen');
actualizarTodosLosGraficos();
}
});
}
if (selectGenero) {
selectGenero.addEventListener('change', function() {
if (this.value !== "NULL") {
setFiltroActivo('id_genero');
actualizarTodosLosGraficos();
}
});
}
});
export {
obtenerDatosGeneros,
obtenerDatosEdades,
obtenerDatosEstados,
obtenerDatosExamenes,
obtenerDatosFechas,
getFiltrosSeleccionados,
setFiltroActivo,
getFiltroActivo,
obtenerResumenGenero // <-- Exporta la nueva función
};