Published
Edited
Jun 25, 2021
Insert cell
Insert cell
md`*A final version of this notebook can be viewed on my blog [here](https://www.robinlinacre.com/country_energy_usage/).*`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
styles = html`<style>
.vega-embed-wrapper {overflow: hidden}

#vg-tooltip-element table td {
white-space: nowrap;
font-size: 0.8rem;
border-bottom: 0px;
}

#vg-tooltip-element table tr td.key {
max-width:500px;
}

#vg-tooltip-element table {
margin-bottom: 0rem;
line-height: 1rem;
border-collapse: unset;
}

`

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
table_data_to_plot = {
let list_of_countries = Object.keys(countries_to_id_dict);

// LIst of countries is the elements of the geojson
return list_of_countries.map(function(country_key) {
let row = {};
let three_letter_country_key = "";
if (country_key in countries_dict_iso2) {
three_letter_country_key = countries_dict_iso2[country_key].id;
} else {
debugger;
}
row["iso2code"] = country_key;
row["country_name"] = countries_to_id_dict[country_key]["name"];
row["country_id"] = countries_to_id_dict[country_key]["id"];

row["primary_energy_per_capita_kg_oil_eq"] = get_value_if_exists(
pepc_dict,
three_letter_country_key
);
row["total_final_energy_consumption_tj"] = get_value_if_exists(
tfec_dict,
three_letter_country_key
);
row["population"] = get_value_if_exists(
population_dict,
three_letter_country_key
);

let pepcoe = row["primary_energy_per_capita_kg_oil_eq"];
row["primary_energy_per_capita_kwh"] = convert_kg_oil_eq_to_kwh(pepcoe);

let pop = row["population"];
let tfec = row["total_final_energy_consumption_tj"];
row[
"final_energy_consumption_per_capita_kwh"
] = convert_tfec_to_kwh_per_capita(tfec, pop);

return row;
});
}
Insert cell
Insert cell
Insert cell
countries_raw = d3.json(
"https://api.worldbank.org/v2/country?format=json&per_page=1000"
)
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
vegalite = require("@observablehq/vega-lite@0.2")
Insert cell
import {select} from "@robinl/inputs"
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