159 lines
5.7 KiB
JavaScript
159 lines
5.7 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 [];
|
|
}
|
|
}
|
|
|
|
async function obtenerDatosEdades(filtros = {}) {
|
|
const edad1 = await recuperarCantidadEdad("Menor de 18 años", filtros);
|
|
const edad2 = await recuperarCantidadEdad("18 a 24 años", filtros);
|
|
const edad3 = await recuperarCantidadEdad("25 a 34 años", filtros);
|
|
const edad4 = await recuperarCantidadEdad("35 a 44 años", filtros);
|
|
const edad5 = await recuperarCantidadEdad("45 a 54 años", filtros);
|
|
const edad6 = await recuperarCantidadEdad("55 a 64 años", filtros);
|
|
const edad7 = await recuperarCantidadEdad("65 años o más", filtros);
|
|
return [edad1, edad2, edad3, edad4, edad5, edad6, edad7];
|
|
}
|
|
|
|
async function obtenerDatosGeneros(filtros = {}) {
|
|
const femenino = await recuperarCantidadGenero("Femenino", filtros);
|
|
const masculino = await recuperarCantidadGenero("Masculino", filtros);
|
|
const noDefinido = await recuperarCantidadGenero("Prefiero no decirlo", filtros);
|
|
return [femenino, masculino, noDefinido];
|
|
}
|
|
|
|
async function obtenerDatosEstados(filtros = {}) {
|
|
try {
|
|
const estados = await recuperarCantidadEstado("Estados", filtros);
|
|
if (!Array.isArray(estados)) {
|
|
console.error("Error: 'estados' no es un array. Verifica la respuesta del backend");
|
|
return [];
|
|
}
|
|
const estadosValidos = estados.filter(estado => estado.estado && estado.cantidad !== undefined);
|
|
return estadosValidos;
|
|
} catch (error) {
|
|
console.error("Error al obtener datos de estados:", error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
async function obtenerDatosExamenes(filtros = {}) {
|
|
try {
|
|
const examenes = await recuperarCantidadExamen("Examenes", filtros);
|
|
if (!Array.isArray(examenes)) {
|
|
console.error("Error: 'examenes' no es un array. Verifica la respuesta del backend");
|
|
return [];
|
|
}
|
|
const examenesValidos = examenes.filter(examen => examen.examen && examen.cantidad !== undefined);
|
|
return examenesValidos;
|
|
} catch (error) {
|
|
console.error("Error al obtener datos de examenes:", error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
async function obtenerDatosFechas(filtros = {}) {
|
|
try {
|
|
const fechas = await recuperarCantidadFecha("Fechas", filtros);
|
|
if (!Array.isArray(fechas) || !fechas.length) {
|
|
return [];
|
|
}
|
|
// Solo un objeto con la cantidad total
|
|
return fechas;
|
|
} catch (error) {
|
|
console.error("Error al obtener datos de fechas:", error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function getFiltrosSeleccionados() {
|
|
return {
|
|
id_rango_edad: document.getElementById('id_rango_edad')?.value || "NULL",
|
|
id_genero: document.getElementById('id_genero')?.value || "NULL",
|
|
id_examen: document.getElementById('id_examen')?.value || "NULL",
|
|
fechaInicio: document.getElementById('date1')?.value || "",
|
|
fechaFin: document.getElementById('date2')?.value || ""
|
|
};
|
|
}
|
|
|
|
export {
|
|
obtenerDatosGeneros,
|
|
obtenerDatosEdades,
|
|
obtenerDatosEstados,
|
|
obtenerDatosExamenes,
|
|
obtenerDatosFechas,
|
|
getFiltrosSeleccionados
|
|
};
|