Public
Edited
Feb 7, 2023
Fork of D3 U.S. map
1 fork
Insert cell
Insert cell
viewof datafile = Scrubber(datafiles, {
delay: 10,
loop: true,
autoplay: false
})
Insert cell
map = svg`<svg viewBox="0 0 975 610">
<g fill="none" stroke="#000" stroke-linejoin="round" stroke-linecap="round">
<path stroke="#aaa" stroke-width="0.5" d="${path(counties)}"></path>
<path stroke-width="0.5" d="${path(states)}"></path>
<path d="${path(nation)}"></path>
</g>
<g>
${data.map( (d,i) => `<circle
cx=${getLat( d.location )}
cy=${getLng( d.location )}
r=${3}
fill="steelblue"
data-idx=${i}
/>`).join('\n')}
</g>
</svg>`
Insert cell
getLat = (d) => {
let loc = projection([+d.longitude, +d.latitude])
return `${loc && loc[0]}`
}
Insert cell
getLng = (d) => {
let loc = projection([+d.longitude, +d.latitude])
return `${loc && loc[1]}`
}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
endpoint = `https://raw.githubusercontent.com/codecreative/train-tester/main/data/${datafile}`
Insert cell
Insert cell
data = {
if( !data_cache[datafile] ) {
console.log('fetching ', datafile);
let new_data = await fetch( endpoint )
.then((response) => response.json())
.then( json => json.filter( d => d.location ) )
mutable data_cache[datafile] = new_data;
return new_data
} else {
console.log('from cache', datafile)
return data_cache[datafile]
}
}
Insert cell
data_ = fetch( endpoint )
.then((response) => response.json())
.then( json => json.filter( d => d.location ) )
Insert cell
projection = d3.geoAlbersUsa().scale(1300).translate([487.5, 305])
Insert cell
path = d3.geoPath()
Insert cell
states = topojson.mesh(us, us.objects.states, (a, b) => a !== b)
Insert cell
nation = topojson.feature(us, us.objects.nation)
Insert cell
counties = topojson.mesh(us, us.objects.counties, (a, b) => a !== b && (a.id / 1000 | 0) === (b.id / 1000 | 0))
Insert cell
us = FileAttachment("counties-albers-10m.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3-geo@2")
Insert cell
import { Scrubber } from "@mbostock/scrubber"
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