Public
Edited
Jun 12, 2023
1 fork
Importers
1 star
Insert cell
Insert cell
Insert cell
viewof movement = Inputs.radio(["All", "International", "Domestic"], {label: "Show which type of movement", value: "All"})
Insert cell
chart = slopeChart(processData(order))
Insert cell
finalData = processData(order)
Insert cell
processData(order)
Insert cell
function slopeChart(data) {
return Plot.plot({
width: 500,
height: 1000,
x: {type: "point", axis: "top"},
y: {axis: null, inset: 10},
color: {
type: "ordinal",
domain: continents,
range: colors
},
marks: [
Plot.line(data, {x: "year", y: 'value', z: "name", strokeWidth: 1.5, stroke: "continent"}),
Plot.text(data, Plot.selectFirst({x: "year", y: 'value', z: "name", text: d => getText(d, 'value', data, label), textAnchor: "end", dx: -3, fill: "continent", fontSize: 6})),
Plot.text(data, Plot.selectLast({x: "year", y: 'value', z: "name", text: d => getText(d, 'value', data, label), textAnchor: "start", dx: 3, fill: "continent", fontSize: 6})),
]
})
}
Insert cell
changeProps(chart, 10)
Insert cell
function changeProps (chart, fontsize) {
d3
.select(chart)
.selectAll("[aria-label=text]")
.selectAll("text")
.each(function (d) {
// d is the tick's value (in this case, a number)
d3.select(this)
// .attr("fill", color)
.attr("font-size", fontsize)
.attr("font-family", "IBM Plex Sans")
})

d3.select(chart)
.selectAll("[aria-label=x-axis]")
.selectAll("text")
.each(function (d) {
// d is the tick's value (in this case, a number)
d3.select(this)
// .attr("fill", color)
.attr('font-weight', 400)
.attr('font-size', 28)
.attr("font-family", "IBM Plex Sans");
})
return chart
}
Insert cell
function getText(d, type, data, label) {
// if (type === 'rank' || type === 'value' || type === 'pagerank'){
// return `${d.name} ${Math.abs(d.rank - data.length/2)}`;
// } else if (type === 'pagerank_order'){
// return ` ${Math.abs(d.pagerank_order-data.length/2)} ${d.name}`;
// }
if (d.year === 2019){
//return `${format(Math.round(d.volume/1000)*1000)} ${d.name} ${Math.abs(d['value'] - val)}`;
return `${d.name} ${Math.abs(d['value'] - val)}`;
} else {
//return ` ${Math.abs(d['value'] - val)} ${d.name} ${format(Math.round(d.volume/1000)*1000)}`;
return ` ${Math.abs(d['value'] - val)} ${d.name}`;
}
}
Insert cell
Insert cell
city = slopeChart(cityData)
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
cityData = processDataC()
Insert cell
changeProps(city, 10)
Insert cell
locale = d3.formatLocale({
thousands: " ",
grouping: [3]
});
Insert cell
format = locale.format(",.0f")
Insert cell
format(221000)
Insert cell
Insert cell
val = 100
Insert cell
continents = ["EU", "AS", "SA", "AF", "NA", "OC"]
Insert cell
data = ({All: all, International: intl, Domestic: dom})
Insert cell
colors = ["#ff595e","#ff924c","#ffca3a","#8ac926","#1982c4","#6a4c93"]
Insert cell
Insert cell
order
Insert cell
data[movement]['tf50'].filter(row => row.rank_2050 == 'rank_0').map(row => row.id);
Insert cell
test = data[movement]['tf50'].filter(row => row.rank_2050 == 'rank_0')
Insert cell
usa = test.filter(row=> row.country == 'USA');
Insert cell
eu = test.filter(row=> row.continent == 'EU');
Insert cell
na = test.filter(row=> row.continent == 'NA');
Insert cell
as = test.filter(row=> row.continent == 'AS');
Insert cell
data[movement]['tf19'].filter(row => row.rank_2019 == 'rank_0').map(row => row.id);
Insert cell
function processData(order) {
let df = [];
let copy19, copy50;

let value = label[order];
let base = data[movement];
let pr19 = base['pr19'];
let pr50 = base['pr50'];
let tf19 = base['tf19'];
let tf50 = base['tf50'];
if (order.includes('Pagerank')){
copy19 = JSON.parse(JSON.stringify(pr19));
copy50 = JSON.parse(JSON.stringify(pr50));
} else {
copy19 = JSON.parse(JSON.stringify(tf19));
copy50 = JSON.parse(JSON.stringify(tf50));
}
console.log(copy19);
copy19.map((row, i) => {
let year = {name: row['id'] +', ' + row['country'], value: row[value], year: 2019, continent: row['continent'], volume: row['total_flights_2019']}
df.push(year);
});

copy50.map((row, i) => {
let year = {name: row['id'] +', ' + row['country'], value: row[value], year: 2050, continent: row['continent'], volume: row['total_flights_2050']}
df.push(year);
});
return df;
}
Insert cell
function processDataC() {
let df = [];
let copy19, copy50;
copy19 = JSON.parse(JSON.stringify(city19));
copy50 = JSON.parse(JSON.stringify(city50));

copy19.map((row, i) => {
let year = {name: row['id'] +', ' + row['country'], value: row['level'], year: 2019, continent: row['continent_x'], volume: row['value']}
df.push(year);
});
copy50.map((row, i) => {
let year = {name: row['id'] +', ' + row['country'], value: row['level'], year: 2050, continent: row['continent_x'], volume: row['total_flights_2050']}
df.push(year);
});
return df;
}
Insert cell
processDataC().slice(-1)
Insert cell
pr50a[0]
Insert cell
Insert cell
city19 = FileAttachment("city-reg-19@5.csv").csv({typed: true})
Insert cell
city50 = FileAttachment("city-reg-50@5.csv").csv({typed: true})
Insert cell
intl = ({ pr19: pr19i, pr50: pr50i, tf19: tf19i, tf50: tf50i })
Insert cell
all = ({ pr19: pr19a, pr50: pr50a, tf19: tf19a, tf50: tf50a })
Insert cell
dom = ({ pr19: pr19d, pr50: pr50d, tf19: tf19d, tf50: tf50d })
Insert cell
pr19a = FileAttachment("top-pr-19-all@4.csv").csv({typed: true})
Insert cell
tf19a = FileAttachment("top-tf-19-all@5.csv").csv({typed: true})
Insert cell
pr50a = FileAttachment("top-pr-50-all@4.csv").csv({typed: true})
Insert cell
tf50a = FileAttachment("top-tf-50-all@5.csv").csv({typed: true})
Insert cell
pr19i = FileAttachment("top-pr-19@5.csv").csv({typed: true})
Insert cell
pr50i = FileAttachment("top-pr-50@5.csv").csv({typed: true})
Insert cell
tf50i = FileAttachment("top-tf-50@5.csv").csv({typed: true})
Insert cell
tf19i = FileAttachment("top-tf-19@5.csv").csv({typed: true})
Insert cell
pr19d = FileAttachment("top-pr-19-dom@3.csv").csv({typed: true})
Insert cell
pr50d = FileAttachment("top-pr-50-dom@2.csv").csv({typed: true})
Insert cell
tf50d = FileAttachment("top-tf-50-dom@4.csv").csv({typed: true})
Insert cell
tf19d = FileAttachment("top-tf-19-dom@3.csv").csv({typed: true})
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more