Published unlisted
Edited
Feb 14, 2021
1 star
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, 975, 610]);

svg.append("path")
.datum(topojson.feature(us, us.objects.nation))
.attr("fill", "#e0e0e0")
.attr("d", path);

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("d", path);

const legend = svg.append("g")
.attr("fill", "#777")
.attr("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("g")
.data(length.ticks(4).slice(1).reverse())
.join("g")
.attr("transform", (d, i) => `translate(${975 - (i + 1) * 18},590)`);

legend.append("path")
.attr("fill", "red")
.attr("fill-opacity", 0.3)
.attr("stroke", "red")
.attr("d", d => spike(length(d)));

legend.append("text")
.attr("dy", "1.3em")
.text(length.tickFormat(4, "s"));

svg.append("g")
.attr("fill", "red")
.attr("fill-opacity", 0.3)
.attr("stroke", "red")
.selectAll("path")
.data(data
.filter(d => d.position)
.sort((a, b) => d3.ascending(a.position[1], b.position[1])
|| d3.ascending(a.position[0], b.position[0])))
.join("path")
.attr("transform", d => `translate(${d.position})`)
.attr("d", d => spike(length(d.value)))
.attr("fill", d => d.colorOverride)
.append("title")
.text(d => `${d.title}
${format(d.value)}`);


return svg.node();
}
Insert cell
Origdata = (await FileAttachment("population.json").json()).slice(1).map(([population, state, county]) => {
const id = state + county;
const feature = features.get(id);
return {
id,
position: feature && path.centroid(feature),
title: feature && feature.properties.name,
value: +population,
colorOverride: "green"
};
})
Insert cell
md`### Trying to convert lon,lat to this projection`
Insert cell
cities = [
{lon: -118.2436849, lat: 34.0522342, name: "Los Angeles", population: 3884307},
{lon: -87.6297982, lat: 41.8781136, name: "Chicago", population: 2718782},
{lon: -95.3698028, lat: 29.7604267, name: "Houston", population: 2195914},
{lon: -75.1652215, lat: 39.9525839, name: "Philadelphia", population: 1553165},
{lon: -112.0740373, lat: 33.4483771, name: "Phoenix", population: 1513367}
];
Insert cell
data=Origdata.slice(0,100).concat(cities.map(x=>({
id: Math.floor(Math.random()*1000000),
position: d3.geoPath(projection).centroid(
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [x.lon, x.lat]
}
}),
title: x.name,
value: +x.population,
colorOverride: "blue",
})))
Insert cell
Insert cell
d3.geoPath(d3.geoAlbersUsa().scale(1300).translate([487.5, 305])).centroid(seattleGeoJSON) // X and Y coordinates
Insert cell
d3.geoPath(d3.geoAlbersUsa().scale(1300).translate([487.5, 305]))(seattleGeoJSON) // and SVG path. See https://github.com/d3/d3-geo/blob/master/README.md#_path
Insert cell
seattleGeoJSON=({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-122.3321, 47.6062] // 47.6062° N, 122.3321° W
},
"properties": {
"name": "Seattle"
}
})
Insert cell
projection = d3.geoAlbersUsa().scale(1300).translate([487.5, 305])
Insert cell
length = d3.scaleLinear([0, d3.max(data, d => d.value)], [0, 200])
Insert cell
features = new Map(topojson.feature(us, us.objects.counties).features.map(d => [d.id, d]))
Insert cell
path = d3.geoPath()
Insert cell
spike = (length, width = 7) => `M${-width / 2},0L0,${-length}L${width / 2},0`
Insert cell
format = d3.format(",.0f")
Insert cell
us = FileAttachment("counties-albers-10m.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@6")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more