Public
Edited
Apr 14, 2023
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function transform(d) {
const [x, y] = path.centroid(d)

return `
translate(${x},${y})
scale(${size(pop.find(x => x.state === d.name)?.pop_2022 )})
translate(${-x},${-y})
`;
}
Insert cell
size = d3.scaleSqrt()
.domain([0, d3.max(pop, d => d.pop_2022)])
.range([0, 1.2])
Insert cell
Insert cell
Insert cell
simulation = d3.forceSimulation(states)
.force("x", d3.forceX(d=> path.centroid(d)[0]).strength(2))
.force("y", d3.forceY(d => path.centroid(d)[1]).strength(2))
.force("charge", d3.forceManyBody().strength(1000))
.force("collide", d3.forceCollide(d => 1 + sizeScale(pop.find(x => x.state === d.name)?.pop_2022) ))
.stop();
Insert cell
sizeScale = d3.scaleSqrt()
.domain([0, d3.max(pop, d => d.pop_2022)])
.range([0, Math.sqrt(width * height) / 10])
Insert cell
Insert cell
demers = {
for (let i = 0; i < 300; i++){
squareSimulation.tick();
}
const svg = d3.create('svg').attr("width", width).attr("height", height)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round");

// center the squares with the translate function
const node = svg.selectAll('square')
.data(states)
.join('rect')
.attr('width', d => sizeSquare(d) )
.attr('height', d => sizeSquare(d) )
.attr('x', d => d.x )
.attr('y', d => d.y )
.attr('stroke', '#FFF')
.attr('transform', d => `translate(${-(sizeSquare(d) / 2)},${-(sizeSquare(d) / 2)})`)

svg.selectAll('label')
.data(states)
.join('text')
.attr('fill', '#FFF')
.attr('x', d => d.x )
.attr('y', d => d.y )
.attr('text-anchor', 'middle')
.text(d => formatText(d) )


return svg.node()
}
Insert cell
sizeSquare = {
const sizeScale = d3.scaleSqrt()
.domain([0, d3.max(pop, d => d.pop_2022)])
.range([0, Math.sqrt(width * height) / 5])

return (d) => sizeScale(pop.find(x => x.state === d.name)?.pop_2022)
}
Insert cell
squareSimulation = {

const circumscribe = (sideLength) => Math.round((Math.sqrt(2) * sideLength) / 2, 2)

return d3.forceSimulation(states)
.force("x", d3.forceX(d=> path.centroid(d)[0]))
.force("y", d3.forceY(d => path.centroid(d)[1]))
.force("charge", d3.forceManyBody().strength(100))
.force("collide", d3.forceCollide(d => circumscribe(sizeSquare(d)) ) )
.stop() }

Insert cell
formatText = (d) => d.name?.slice(0, 1) + d.name?.slice(d.name.length - 1).toUpperCase()
Insert cell
Insert cell
Insert cell
projection = d3.geoAlbersUsa()
Insert cell
Insert cell
pop = {
const data = await FileAttachment("us_population@3.csv").csv()

return data.map(d => ({state: d.state, pop_2022: +d.pop_2022, pop_2020: +d.pop_2020}))
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
us_unproj = await FileAttachment("us.json").json()
Insert cell
states = {
const outlines = topojson.feature(us_unproj, us_unproj.objects.states).features

return outlines.map(d => ({...d, name: stateNames.find(x => +x.id == d.id )?.properties?.name }))


}
Insert cell
height = 600
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