Published
Edited
Nov 5, 2019
4 stars
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, width * 0.66]);

yield html`<div'>${svg.node()}</div>`;

for (let i = 0; i < centroids.length; i++) {
let r = Infinity;
for (let j of delaunay.neighbors(i)) {
r = Math.min(
r,
Math.sqrt(
Math.pow(centroids[j][0] - centroids[i][0], 2) +
Math.pow(centroids[j][1] - centroids[i][1], 2)
)
);
}
// Create a circle for each county
svg
.append("circle")
.attr("r", r / 2)
.attr('fill', d3.scaleQuantize([0, 10], d3.schemeOrRd[4])(r))
.attr('transform', `translate(${centroids[i]})`);
if (i % 100 === 0) yield html`<div'>${svg.node()}</div>`;
}
yield html`<div'>${svg.node()}</div>`;
}
Insert cell
d3 = require('d3@5') // d3, for rendering to svg and… everything
Insert cell
topojson = require("topojson-client@3") // topojson, for parsing the compressed data
Insert cell
us = FileAttachment("counties-albers-10m.json").json() // the data
Insert cell
path = d3.geoPath().projection(d3.geoIdentity().scale(width / 975)) // project / print method
Insert cell
centroids = topojson
.feature(us, us.objects.counties)
.features.map(path.centroid)
Insert cell
delaunay = d.Delaunay.from(centroids)
Insert cell
d = require('d3-delaunay')
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