Public
Edited
Jan 9, 2024
1 star
Insert cell
Insert cell
vote_map_animation = {

var path = d3.geoPath(projection);

var svg = d3.create("svg")
.attr("width", width )
.attr("height", height);

return Promise.all([municipalities, election_results, centroids]).then(
([a, b, c]) => {return ready(municipalities, election_results, centroids);} );

function ready(col_mpio, election_results, centroids) {

const g = svg.append("g")

g.selectAll("path")
.data(topojson.feature(col_mpio, col_mpio.objects.col_mpio).features)
.enter()
.append("path")
.attr("class", "municipalities")
.attr("d", path)
.style("fill", d => get_color(d))
.style("stroke", "white")
.style("stroke-width", "0.5");
setInterval(() => {
svg
.selectAll(".municipalities")
.transition()
.delay((d) => {
try{
return get_municipio(d).index * 2;
}
catch (error) {
console.log(d);
}
})
.duration(5000)
.attrTween("d", function (d, i) {
try {
var municipio = get_municipio(d);
return flubber.toCircle(path(d), municipio.x, municipio.y, radiusScale(parseInt(municipio["Población Total"])), {
maxSegmentLength: 2
});
} catch (error) {
console.log(d);
}
});
svg
.selectAll(".municipalities")
.transition()
.delay((d) => {
try{
return get_municipio(d).index * 2 + 10000
}
catch (error) {
console.log(d);
}
})
.duration(5000)
.attrTween("d", function (d, i) {
try {
var municipio = get_municipio(d);
return flubber.fromCircle(municipio.x, municipio.y, radiusScale(parseInt(municipio["Población Total"])), path(d), {
maxSegmentLength: 2
});
} catch (error) {
console.log(d);
};
});
}, 22000);

return md`You may have to wait a minute for the animation below to start: ${svg.node()}`;
}
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
width = 1000;
Insert cell
height = 950;
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
election_results = FileAttachment("yan_basset@27.csv").csv()
Insert cell
municipalities = FileAttachment("municipios_col@4.json").json()
Insert cell
Insert cell
Insert cell
d3 = require("d3@6")
Insert cell
flubber = require('https://unpkg.com/flubber')
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