Published
Edited
May 3, 2020
2 forks
19 stars
Insert cell
Insert cell
map = html`<svg viewBox="0 0 975 610" stroke-miterlimit="1">
<defs>
<path id="${outlineId.id}" d="${path(topojson.mesh(us, states, (a, b) => a === b))}"></path>
<clipPath id="${clipId.id}">
<use xlink:href="${outlineId.href}"></use>
</clipPath>
</defs>
<g clip-path="${clipId}" fill-opacity="0.7">${polygons.features.map(d => svg`
<path d="${project(d)}" fill="${color[d.properties.color % color.length]}"></path>`)}
<path d="${path(topojson.mesh(us, states, (a, b) => a !== b))}" fill="none" stroke="white"></path>
<path d="${project(voronoi.cellMesh())}" fill="none" stroke="black"></path>
</g>
<g text-anchor="middle" font-family="sans-serif" font-size="10">${polygons.features.map(d => svg`
<g transform="translate(${projection(d.properties.sitecoordinates).join(",")})">
<circle r="2"></circle>
<text y="-6">${d.properties.site[2]}</text>
</g>`)}
</g>
</svg>`
Insert cell
neighbors = voronoi.delaunay.neighbors
Insert cell
polygons = {
const polygons = voronoi.polygons(), {features} = polygons;
for (const [i, feature] of features.entries()) {
feature.properties.color = d3.max(neighbors[i], j => features[j].properties.color) + 1 | 0;
}
return polygons;
}
Insert cell
data = d3.csvParse(await FileAttachment("us-state-capitals.csv").text(), ({name, longitude, latitude}) => [+longitude, +latitude, name, undefined])
Insert cell
voronoi = d3.geoVoronoi(data)
Insert cell
us = FileAttachment("states-albers-10m.json").json()
Insert cell
states = ({type: "GeometryCollection", geometries: us.objects.states.geometries.filter(d => d.id !== "02" && d.id !== "15")})
Insert cell
projection = d3.geoAlbers().scale(1300).translate([487.5, 305])
Insert cell
path = d3.geoPath()
Insert cell
project = d3.geoPath(projection)
Insert cell
outlineId = DOM.uid("outline")
Insert cell
clipId = DOM.uid("clip")
Insert cell
color = d3.schemeTableau10
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3-array@2", "d3-dsv@1", "d3-geo@1", "d3-geo-voronoi@1", "d3-scale-chromatic@1")
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