Published
Edited
Nov 18, 2021
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
cities = FileAttachment("cities.csv").csv({ typed: true })
Insert cell
cityRadius = (population) => 0.035 * Math.pow(population, 0.44)
Insert cell
pop = cities.find((d) => d.city === highlightedCity).population
Insert cell
albers = d3.geoAlbersUsa().scale(1300).translate([487.5, 305])
Insert cell
scale = {
const ny = cities.find((d) => d.city === "New York");
const sf = cities.find((d) => d.city === "San Francisco");
const nyp = albers([ny.lon, ny.lat]);
const sfp = albers([sf.lon, sf.lat]);
return Math.hypot(nyp[0] - sfp[0], nyp[1] - sfp[1]) / 4100;
}
Insert cell
projection = d3.geoIdentity().fitExtent(
[
[0, 0],
[width, height]
],
counties
)
Insert cell
nation = topojson.feature(us, us.objects.nation)
Insert cell
states = topojson.mesh(us, us.objects.states)
Insert cell
counties = topojson.feature(us, us.objects.counties)
Insert cell
import { us } from "@d3/choropleth"
Insert cell
function map() {
const context = DOM.context2d(width, height);
context.fillStyle = fillBackground;
context.fillRect(0, 0, width, height);
const projection = d3.geoIdentity().fitExtent(
[
[0, 0],
[width, height]
],
nation
);
const path = d3.geoPath(projection, context);

context.beginPath();
path(nation);
context.fillStyle = fillNation;
context.fill();
context.stroke();
context.clip();

context.beginPath();
path(states);
context.lineWidth = 0.75;
context.stroke();

context.lineWidth = 0.25;
for (const { fill, mult, layer } of [
{ fill: fillRegion, mult: 8 },
{ fill: fillZone, mult: 2.5, layer: true },
{ fill: fillCity, mult: 1 }
]) {
context.beginPath();
for (const { lon, lat, population } of cities) {
const r = mult * cityRadius(population) * scale;
const a = albers([lon, lat]);
if (!a) continue;
const p = projection(a);

context.moveTo(p[0] + r, p[1]);
context.arc(...p, r, 0, 2 * Math.PI);
}
context.fillStyle = fill;
context.fill();
context.strokeStyle = fill;
context.stroke();

if (layer) {
context.beginPath();
path(counties);
context.lineWidth = 0.25;
context.strokeStyle = "black";
context.stroke();
}
}

return context.canvas;
}
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