Public
Edited
Nov 23, 2020
1 fork
Insert cell
Insert cell
states
Insert cell
viewof states = {
let value = null;

const svg = d3.create("svg").attr("viewBox", [0, 0, 975, 610]);

svg
.append("g")
.attr("fill", "#eee")
.selectAll("path")
.data(topojson.feature(us, us.objects.states).features)
.enter()
.append("path")
// .attr("fill", colorScheme[Math.floor(Math.random() * 10)])
.attr("opacity", "1.0")
.attr("fill", function(d) {
return colorScheme[Math.floor(d.id / 10)];
})

.attr("d", path)
.on("click", d => {
const node = svg.node();
node.value = value = value === d.id ? null : d.properties.name;
node.dispatchEvent(new CustomEvent("input"));
outline.attr("d", value ? path(d) : null);
outline.attr("fill", colorScheme[Math.floor(d.id / 10)]);
});

svg
.append("path")
.datum(topojson.mesh(us, us.objects.states, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("pointer-events", "none")
.attr("d", path);

const outline = svg
.append("path")
.attr("fill", "none")
.attr("stroke", "black")
.attr("stroke-linejoin", "round")
.attr("pointer-events", "none");

return Object.assign(svg.node(), { value: null });
}
Insert cell
colorScheme = [
"#4e79a7",
"#f28e2c",
"#e15759",
"#76b7b2",
"#59a14f",
"#edc949",
"#af7aa1",
"#ff9da7",
"#9c755f",
"#bab0ab"
]
Insert cell
path = d3.geoPath()
Insert cell
us = FileAttachment("states-albers-10m.json").json()
Insert cell
topojson = require("topojson-client@3")
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