Published
Edited
Nov 10, 2020
3 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
svg = d3.create("svg").attr("viewBox", [0, 0, width, 500])
Insert cell
Insert cell
countries = svg
.append("g")
.selectAll("path")
.data(bn)
.enter()
.append("path")
.attr("d", d3.geoPath().projection(projection))
.attr("fill", "none")
.attr("stroke", "#404040ff")
Insert cell
Insert cell
circles = svg
.append("g")
.selectAll("circle")
.data(n2j.features)
.enter()
.append("circle")
.attr("cx", f => projection(f.geometry.coordinates)[0])
.attr("cy", f => projection(f.geometry.coordinates)[1])
.attr("r", f => 0.000055 * Math.sqrt(f.properties.ar))
.attr("fill", "#ffffff00")
.attr("stroke", "#40404000")
Insert cell
Insert cell
{
circles.append("title").text(
f => `${f.properties.na}
${sizeInd[f.properties.id]} inhabitants in 2018
Variation: ${colInd[f.properties.id]}‰`
);
}
Insert cell
Insert cell
Insert cell
setTimeout(function() {
//hide countries
countries
.transition()
.duration(1000)
.attr("stroke", "#40404000");

//show circles
circles
.transition()
.duration(1000)
.attr("fill", "#ffffff44")
.attr("stroke", "#404040ff");
circles
.on("mouseover", function(rg) {
d3.select(this).attr("fill", "purple");
})
.on("mouseout", function() {
d3.select(this).attr("fill", "#fdbf6faa");
});
}, 1000)
Insert cell
Insert cell
Insert cell
setTimeout(function() {
circles
.transition()
.duration(1500)
.attr("r", f => toRadius(+sizeInd[f.properties.id]))
.attr("fill", f => colorScale(+colInd[f.properties.id]))
.attr("stroke", "#40404030");
circles.on("mouseout", function() {
d3.select(this).attr("fill", f => colorScale(+colInd[f.properties.id]));
});
}, 2500)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
setTimeout(function() {
const simulation = d3
.forceSimulation(n2j.features)
.force("x", d3.forceX().x(f => projection(f.geometry.coordinates)[0]))
.force("y", d3.forceY().y(f => projection(f.geometry.coordinates)[1]))
.force(
"collide",
d3.forceCollide().radius(f => toRadius(+sizeInd[f.properties.id]))
);

//set initial position of the circles
for (const f of n2j.features) {
f.x = projection(f.geometry.coordinates)[0];
f.y = projection(f.geometry.coordinates)[1];
}

simulation.on("tick", () => {
circles.attr("cx", f => f.x).attr("cy", f => f.y);
});

invalidation.then(() => simulation.stop());
}, 3000)
Insert cell
Insert cell
setTimeout(function() {
//circle color
svg
.append("g")
.attr("transform", "translate(20,20)")
.append(() =>
legend({
color: colorScale,
title: "Population change (‰)",
width: 260
})
);

//circle size
const legC = svg
.append("g")
.attr("fill", "#444")
.attr("transform", "translate(50,130)")
.attr("text-anchor", "right")
.style("font", "10px sans-serif")
.selectAll("g")
.data([20e6, 10e6, 1e6])
.join("g");
legC
.append("circle")
.attr("fill", "none")
.attr("stroke", "#444")
.attr("cy", d => -toRadius(d))
.attr("r", toRadius);
legC
.append("text")
.attr("y", d => -10 - 2 * toRadius(d))
.attr("x", 30)
.attr("dy", "1.3em")
.text(d3.format(".1s"));
}, 4500)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
//TODO make it possible to change year and nuts level
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

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