Published
Edited
Dec 17, 2020
3 forks
6 stars
Insert cell
md`# D3-force beeswarm of mobility`
Insert cell
height = 600
Insert cell
width = 950
Insert cell
chart = {
const svg = d3.create("svg")
.attr("height", height)
.attr("width", width)
const max_pop = d3.max(mobility_data, d => d.pop)
const data = mobility_data.filter(d => d.in_lockdown === "TRUE")
const xScale = d3.scaleLinear().domain([-1, 0]).range([0, width]).clamp(true)
const rScale = d3.scaleSqrt().domain([0, max_pop]).range([0, 8])
const force = d3.forceSimulation(data)
.force('forceX', d3.forceX(d => xScale(d.pct_change)).strength(2))
.force('forceY', d3.forceY(height/2).strength(0.1))
.force('collide', d3.forceCollide(d => rScale(d.pop) + 0.5))
.stop();
const NUM_ITERATIONS = 300;
for (let i = 0; i < NUM_ITERATIONS; ++i) {
force.tick();
};
force.stop();
svg.selectAll("circle")
.data(data)
.enter()
.append("circle")
.attr("cx", d => d.x)
.attr("cy", d => d.y)
.attr("r", d => rScale(d.pop))
.style("fill", "#bdbdbd");
svg.append("g")
.call(d3.axisTop(xScale))
.style("transform", `translateY(${height*0.75}px`)
return svg.node();
}
Insert cell
html`<style>svg { overflow: visible; }</style>`
Insert cell
mobility_data = d3.csv("https://static01.nyt.com/newsgraphics/2020/03/27/coronavirus-behavior-int/d08526cd59a705b40c03e9950174af4a5bb72835/pct_change_04_01.csv")
Insert cell
d3 = require("d3@5")
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