Public
Edited
Feb 22
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
csvURL = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSIpanNx_aFG0xJNYpht71XkhOBxPRixhIyOxadGg26fRofcVNc4V2-9OxRjmKoTWW1veFuJFgX5k63/pub?gid=40627714&single=true&output=csv"
Insert cell
dataRaw = d3.csv(csvURL)
Insert cell
data = dataRaw.map(item => {
return {
...item,
año: parseInt(item.año),
hombre: parseInt(item.hombre),
mujer: parseInt(item.mujer),
total: parseInt(item.total),
}
})
Insert cell
dataRechazos = data.filter(d => d.tipo == "rechazos")
Insert cell
dataIngresos = data.filter(d => d.tipo == "ingresos")
Insert cell
yearsRange = Array.from(new Set(data.map(d => d.año))).sort((a, b) => a - b)
Insert cell
totalAerolineas = Array.from(
d3.rollup(
dataRechazos,
v => d3.sum(v, d => d.total), // Summing the 'total' for each group
d => d.aerolínea // Grouping by 'aerolínea'
),
([aerolínea, total]) => ({ aerolínea, total }) // Mapping the result to an array of objects
).sort((a, b) => b.total - a.total); // Sorting in descending order based on total
Insert cell
topAerolineas = totalAerolineas.slice(0, 6).map(item => item.aerolínea)
Insert cell
aerolineasAcumuladasRechazos = dataRechazos.map(d => ({
...d,
aerolínea: topAerolineas.includes(d.aerolínea) ? d.aerolínea : "Otra"
}))
Insert cell
groupedAerolineasAcumuladas = Array.from(
d3.rollup(
aerolineasAcumuladasRechazos,
v => d3.sum(v, d => d.total), // Summing the 'total' for each group
d => d.aerolínea, // First level of grouping by 'aerolínea'
d => d.año // Second level of grouping by 'año'
)
).flatMap(([aerolínea, yearValues]) =>
Array.from(yearValues, ([año, total]) => ({
aerolínea: aerolínea,
año: año,
total: total
}))
)
Insert cell
groupedRechazosIngresos = Array.from(
d3.rollup(
data,
v => {
const total_rechazos = d3.sum(v.filter(d => d.tipo === "rechazos"), d => d.total); // Sum for 'rechazos'
const total_ingresos = d3.sum(v.filter(d => d.tipo === "ingresos"), d => d.total); // Sum for 'ingresos'
return {
total_rechazos,
total_ingresos,
total: total_rechazos + total_ingresos // Calculate total
};
},
d => d.aerolínea, // Group by 'aerolínea'
d => d.año // Then group by 'año'
),
([aerolinea, años]) =>
Array.from(años, ([año, totals]) => ({
aerolínea: aerolinea,
año: año,
total_rechazos: totals.total_rechazos,
total_ingresos: totals.total_ingresos,
total: totals.total,
per_rechazos: (totals.total_rechazos / totals.total),
per_ingresos: (totals.total_ingresos / totals.total)
}))
).flat().sort((a, b) => a.año - b.año)
Insert cell
groupedAerolineasFiltered = groupedRechazosIngresos.filter(d => topAerolineas.includes(d.aerolínea))
Insert cell
aerolineaSelected = dataRechazos.filter(d => aerolineaSelection.includes(d.aerolínea))
Insert cell
aerolineasFiltered = data.filter(d => topAerolineas.includes(d.aerolínea))
Insert cell
aerolineasRechazadosFiltered = dataRechazos.filter(d => topAerolineas.includes(d.aerolínea))
Insert cell
Insert cell
csvGruposEtariosURL = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSIpanNx_aFG0xJNYpht71XkhOBxPRixhIyOxadGg26fRofcVNc4V2-9OxRjmKoTWW1veFuJFgX5k63/pub?gid=979970740&single=true&output=csv"
Insert cell
dataGruposEtariosRaw = d3.csv(csvGruposEtariosURL)
Insert cell
dataGruposEtarios = dataGruposEtariosRaw.map(item => {
return {
...item,
año: parseInt(item.año),
hombre: parseInt(item.hombre),
mujer: parseInt(item.mujer),
total: parseInt(item.total),
}
})
Insert cell
dataGruposEtariosFiltered = dataGruposEtarios.filter(d => d.tipo == "rechazos")
Insert cell
Insert cell
csvAerolineaPaises = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSIpanNx_aFG0xJNYpht71XkhOBxPRixhIyOxadGg26fRofcVNc4V2-9OxRjmKoTWW1veFuJFgX5k63/pub?gid=317877256&single=true&output=csv"
Insert cell
arolineaPaisesRawData = d3.csv(csvAerolineaPaises)
Insert cell
arolineaPaisesData = arolineaPaisesRawData.map(item => {
return {
...item,
año: parseInt(item.año),
total: parseInt(item.total),
}
})
Insert cell
sankeyAerolineaRechazosData = arolineaPaisesData.filter(d => d.tipo == "rechazos").map(({ país_de_vuelo_proveniente, total, aerolínea}) => ({
source: país_de_vuelo_proveniente,
target: aerolínea,
value: total,
}))
Insert cell
sankeyAerolineaData = arolineaPaisesData.map(({ tipo, país_de_vuelo_proveniente, total, aerolínea}) => ({
source: país_de_vuelo_proveniente,
target: tipo,
value: total,
aerolínea: aerolínea
}))
Insert cell
sankeyFiltered = sankeyAerolineaData.filter(d => d.aerolínea == airline).filter(d => countriesAirlines.includes(d.source)).filter(d => statusAirlines.includes(d.target))
Insert cell
Insert cell
csvAeropuertosURL = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSIpanNx_aFG0xJNYpht71XkhOBxPRixhIyOxadGg26fRofcVNc4V2-9OxRjmKoTWW1veFuJFgX5k63/pub?gid=1512133585&single=true&output=csv"
Insert cell
rawDataAeropuertos = d3.csv(csvAeropuertosURL)
Insert cell
dataAeropuertos = rawDataAeropuertos.map(item => {
return {
...item,
año: parseInt(item.año),
hombre: parseInt(item.hombre),
mujer: parseInt(item.mujer),
total: parseInt(item.total),
}
})
Insert cell
dataRechazosAeropuertos = dataAeropuertos.filter(item => item.tipo == "rechazos")
Insert cell
aerolineaAeropuerto = dataRechazosAeropuertos.filter( d => d.aerolínea == aerolinea)
Insert cell
groupedAeropuertos = Array.from(
d3.rollup(
dataRechazosAeropuertos,
v => d3.sum(v, d => d.total), // Summing the 'total' for each group
d => d.aeropuerto, // First level of grouping by 'aeropuerto'
d => d.año // Second level of grouping by 'año'
)
).flatMap(([aeropuerto, yearValues]) =>
Array.from(yearValues, ([año, total]) => ({
aeropuerto: aeropuerto,
año: año,
total: total
}))
).sort((a, b) => a.año - b.año)
Insert cell
import {data as dataMeses} from "@fer-aguirre/extranjeros-rechazados-por-via-aerea-en-mexico"
Insert cell
dataMesesCol = dataMeses.filter(item => item.pais == "Colombia" &&
yearsRange.includes(item.año)
)
Insert cell
Insert cell
csvMesesSolicitudURL = "https://docs.google.com/spreadsheets/d/e/2PACX-1vSIpanNx_aFG0xJNYpht71XkhOBxPRixhIyOxadGg26fRofcVNc4V2-9OxRjmKoTWW1veFuJFgX5k63/pub?gid=321170573&single=true&output=csv"
Insert cell
dataMesesSolicitudRaw = d3.csv(csvMesesSolicitudURL)
Insert cell
dataMesesSolicitud = dataMesesSolicitudRaw.map(item => {
return {
...item,
año: parseInt(item.año),
mes: parseInt(item.mes),
eventos_de_entrada: parseInt(item.eventos_de_entrada),
rechazados: parseInt(item.rechazados),
total_de_eventos: parseInt(item.total_de_eventos),
porcentaje_rechazo: parseFloat(d3.format(".2f")(item.porcentaje_rechazo.replaceAll(",", ".")))
}
})
Insert cell
dataFlujoAnual = Object.values(
[...dataMesesCol.map(d => ({
año: d.año,
mes: d.mes,
total_ingresos: d.eventos_de_entrada,
total_rechazos: d.rechazados,
total_eventos: d.total_de_eventos,
fuente: "UPMRIP"
})),
...dataMesesSolicitud.map(d => ({
año: d.año,
mes: d.mes,
total_ingresos: d.eventos_de_entrada,
total_rechazos: d.rechazados,
total_eventos: d.total_de_eventos,
fuente: "solicitud al INM"
}))]
.reduce((acc, curr) => {
const key = `${curr.año}-${curr.fuente}`;
if (!acc[key]) {
acc[key] = {
año: curr.año,
fuente: curr.fuente,
total_ingresos: 0,
total_rechazos: 0,
total_eventos: 0
};
}
acc[key].total_ingresos += curr.total_ingresos;
acc[key].total_rechazos += curr.total_rechazos;
acc[key].total_eventos += curr.total_eventos;
return acc;
}, {})
).map(item => ({
...item,
porcentaje_rechazo: item.total_eventos > 0
? item.total_rechazos / item.total_eventos
: 0 // Avoid division by zero
})).sort((a, b) => b.año - a.año);
Insert cell
Insert cell
renderTermsMarkdown = (array) =>
array.map(item => ` - ${item}`).join('\n')
Insert cell
Insert cell
import {searchCheckbox} from "@john-guerra/search-checkbox"
Insert cell
marked = require('marked')
Insert cell
import {SankeyChart} from "@d3/sankey-component"
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more