Public
Edited
Mar 1, 2023
1 star
Insert cell
Insert cell
Insert cell
Insert cell
{ const svg = d3.create("svg")
.attr("viewBox", [0, 0, 975, 800])

const projection = d3.geoAlbers().scale(2800).translate([1000, 500])
const path = d3.geoPath(projection)


svg.append("g")
.attr("stroke", "lightgrey")
.attr('stroke-width', .2)
.selectAll("path")
.data(voronoi.features.filter(d => d.properties.site.properties.state_abbr === 'UT' ))
.join("path")
.attr("d", path)
.attr('fill', d => color(d.properties.site.properties.risk_area) )
.attr('fill-opacity', .3)


svg.append('g')
.selectAll('city')
.data(cities.filter(d => d.properties.state_abbr === 'UT'))
.join('path')
.attr('transform',d => `translate(${projection(d.geometry.coordinates)[0]},${projection(d.geometry.coordinates)[1]})`)
.attr('d', d => spike(heightScale(d.properties.population) ) )
.attr('fill', d => color(d.properties.risk_area) )
.attr('fill-opacity', .5)
.attr('stroke', d => color(d.properties.risk_area))
.attr('stroke-linejoin', 'round')
.attr('stroke-width', 1)


return svg.node()
}
Insert cell
color = d3.scaleQuantile().domain(cities.map(d => d.properties.risk_area))
.range(['#FFF4EB', '#FDD0A2', '#FD8C3B', '#D84702', '#7F2604'])
Insert cell
color.breaks
Insert cell
spike = (length) => `M${-7 / 2},0L0,${-length}L${7 / 2},0`
Insert cell
heightScale = d3.scaleSqrt().domain([0, d3.max(cities, d => d.properties.population)]).range([1, 250])
Insert cell
data = await FileAttachment("cities@1.json").json()
Insert cell
cities = topojson.feature(data, data.objects.cities).features
Insert cell
cities.filter(d => d.properties.state === 'UT')
Insert cell
voronoi = d3.geoVoronoi(cities).polygons()
Insert cell
projection = d3.geoAlbers().fitSize([width, 600], outline)

//scale(2000).translate([800, 500])
Insert cell
path = d3.geoPath(projection)
Insert cell
d3 = require("d3@7", "d3-geo-voronoi@1")
Insert cell
state_outline = await FileAttachment("states_outline@1.json").json()
Insert cell
outline = topojson.feature(state_outline, state_outline.objects.states_outline)
Insert cell
Insert cell
cities.sort((a, b) => b.properties.risk_area - a.properties.risk_area).map(d => ({name: d.properties.name,
state: d.properties.state_abbr, population: d.properties.population, risk_per_sqm: +d.properties.risk_area?.toFixed(5)}) ).slice(0, 50)
Insert cell
populated = cities.sort((a, b) => b.properties.population - a.properties.population).slice(0, 10)
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