143 lines
4.2 KiB
JavaScript
143 lines
4.2 KiB
JavaScript
async function recuperarCantidadGenero(tipoConsulta) {
|
|
try {
|
|
const response = await fetch("./php/graficos.php", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ tipoConsulta }),
|
|
});
|
|
|
|
const data = await response.json();
|
|
return data.cantidad || 0;
|
|
} catch (error) {
|
|
console.error("Error al recuperar datos:", error);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
async function recuperarCantidadEdad(tipoConsulta) {
|
|
try {
|
|
const response = await fetch("./php/graficos.php", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ tipoConsulta }),
|
|
});
|
|
|
|
const data = await response.json();
|
|
return data.cantidad || 0;
|
|
} catch (error) {
|
|
console.error("Error al recuperar datos:", error);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
async function recuperarCantidadEstado(tipoConsulta) {
|
|
try {
|
|
const response = await fetch("./php/graficos.php", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ tipoConsulta }),
|
|
});
|
|
|
|
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) {
|
|
try {
|
|
const response = await fetch("./php/graficos.php", {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ tipoConsulta }),
|
|
});
|
|
|
|
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 obtenerDatosEdades() {
|
|
const edad1 = await recuperarCantidadEdad("Menor de 18 años");
|
|
const edad2 = await recuperarCantidadEdad("18 a 24 años");
|
|
const edad3 = await recuperarCantidadEdad("25 a 34 años");
|
|
const edad4 = await recuperarCantidadEdad("35 a 44 años");
|
|
const edad5 = await recuperarCantidadEdad("45 a 54 años");
|
|
const edad6 = await recuperarCantidadEdad("55 a 64 años");
|
|
const edad7 = await recuperarCantidadEdad("65 años o más");
|
|
|
|
return [edad1, edad2, edad3, edad4, edad5, edad6];
|
|
}
|
|
|
|
|
|
async function obtenerDatosGeneros() {
|
|
const femenino = await recuperarCantidadGenero("Femenino");
|
|
const masculino = await recuperarCantidadGenero("Masculino");
|
|
const noDefinido = await recuperarCantidadGenero("Prefiero no decirlo");
|
|
|
|
return [femenino, masculino, noDefinido];
|
|
}
|
|
|
|
async function obtenerDatosEstados() {
|
|
try {
|
|
const estados = await recuperarCantidadEstado("Estados");
|
|
|
|
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() {
|
|
try {
|
|
const examenes = await recuperarCantidadExamen("Examenes");
|
|
|
|
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 [];
|
|
}
|
|
}
|
|
|
|
|
|
|
|
export { obtenerDatosGeneros, obtenerDatosEdades, obtenerDatosEstados, obtenerDatosExamenes };
|