Public
Edited
Oct 25, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
rescale = d3
.scaleLinear()
.domain(d3.extent(states.features.map(accessor)))
.range([0.001, 10 ** (5 * (1 - exaggeration / 100))])
Insert cell
accessor = ({ id }) => +meta.get(id)[param] || 0
Insert cell
color = d3.scaleSequentialSqrt(
rescale.domain(),
param === "pop_est"
? d3.interpolateBlues
: param === "area"
? d3.interpolateGreens
: d3.interpolateReds
)
Insert cell
inversecolor = d3.scaleSequentialSqrt(
rescale.domain().slice().reverse(),
color.interpolator()
)
Insert cell
Insert cell
world = d3.json(
"https://cdn.jsdelivr.net/npm/us-atlas@3/states-10m.json"
)
Insert cell
fts = d3
.json("https://cdn.jsdelivr.net/npm/us-atlas@3/states-10m.json")
.then(topology => topojson.feature(topology, topology.objects.states))
Insert cell
fts.features
Insert cell
meta = d3.index(fts.features, ({ id }) => id);
Insert cell
Insert cell
world2 = {
if (!world.objects.states)
world.objects.states = world.objects.world_states_data;
if (!world.transform) world.transform = { scale: [1, 1], translate: [0, 0] };
world.objects.states.geometries = world.objects.states.geometries.filter(
(d) => d.id !== "010"
);
return world;
}
Insert cell
Insert cell
topogram = require("topogram")
Insert cell
carto = topogram
.cartogram()
.projection(projection)
.properties((d) => d.properties)
.value((d) => rescale(accessor(d)))
Insert cell
mutable debug = ({})
Insert cell
cartogramFeatures = carto(
world2,
world2.objects.states.geometries
).features.map((d) => {
const geometry = d.geometry;
geometry.coordinates =
geometry.type === "MultiPolygon"
? geometry.coordinates.map((poly) =>
poly.map((ring) => ring.map(projection.invert))
)
: geometry.coordinates.map((ring) => ring.map(projection.invert));
return d;
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// a log error seems more useful?
errors = cartogramFeatures.map((d) => ({
id: d.id,
error: Math.log(areas.get(d.id) / areasSum / (accessor(d) / valuesSum))
}))
Insert cell
Insert cell
errorMedian = d3.median(errors, (d) => Math.abs(d.error))
Insert cell
errorDeviation = d3.deviation(errors, (d) => d.error)
Insert cell
Insert cell
topojson = require("topojson")
Insert cell
d3 = require("d3@7", "d3-geo-projection@3")
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