Public
Edited
Nov 13, 2020
Insert cell
Insert cell
Insert cell
applySimulation = (nodes) => {
const simulation = d3.forceSimulation(nodes)
//.force("cx", d3.forceX().x(d => width / 2).strength(0.02))
//.force("cy", d3.forceY().y(d => width * (5/8) / 2).strength(0.02))
.force("x", d3.forceX().x(d => d.properties.centroid ? d.properties.centroid[0] : 0).strength(0.3))
.force("y", d3.forceY().y(d => d.properties.centroid ? d.properties.centroid[1] : 0).strength(0.3))
//.force("charge", d3.forceManyBody().strength(-0.5))
.force("collide", d3.forceCollide().radius(d => d.properties.radius + nodePadding).strength(1))
.stop()

let i = 0;
while (simulation.alpha() > 0.01 && i < 200) {
simulation.tick();
i++;
console.log(`${Math.round(100*i/200)}%`)
}

return simulation.nodes();
}
Insert cell
col = d3.scaleSequential([-1, 1], d3.interpolateRgb("blue", "red"))
Insert cell
spreadCounties = applySimulation(counties)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
//d3.csv("https://gist.githubusercontent.com/simon-pinkmartini/aa7c3fb33ec2c3ecb3a6c2127b0b6ff4/raw/a246f60aedd7fb9ffa2a39fc5511e83cc53b708b/gistfile1.txt")
Insert cell
votes = {
//const url = "https://raw.githubusercontent.com/tonmcg/County_Level_Election_Results_12-16/master/2016_US_County_Level_Presidential_Results.csv";
//const url = "https://gist.githubusercontent.com/simon-pinkmartini/aa7c3fb33ec2c3ecb3a6c2127b0b6ff4/raw/a246f60aedd7fb9ffa2a39fc5511e83cc53b708b/gistfile1.txt";
//const url = "https://gist.githubusercontent.com/simon-pinkmartini/fc9cd30a04081b3052fe5137d87ffdee/raw/d3069fe26ca2067938384e542f71588a37ec8e81/2000-for-map_edited2.csv";
//const csv = FileAttachment("map.csv").csv();
//const csv = await d3.csv(url);
const votes = data
.map(row => ({
id: row.combined_fips.padStart(5, "0"),
count: { total: +row.votes_total, dem: +row.votes_dem, gop: +row.votes_gop },
percent: { dem: +row.per_dem, gop: +row.per_gop },
two_party_ratio: (+row.votes_dem) / ((+row.votes_dem) + (+row.votes_gop)),
color: row.color
}))
.map(row => {
switch (row.id) {
case "02270": // Wade Hampton Census Area was renamed to Kusilvak Census Area (Alaska)
return { ...row, id: "02158" };
case "46113": // Shannon County Census Area was renamed to Oglala Lakota County Census Area (South Dakota)
return { ...row, id: "46102" };
default:
return row;
}
})
return votes;
}
Insert cell
data = FileAttachment("map@1.csv").csv()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
turf = require("@turf/turf@5")
Insert cell
Insert cell
import {slider, radio} from "@jashkenas/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