Public
Edited
Feb 5, 2024
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
L = require('leaflet@1.7.1')
Insert cell
Insert cell
esriLeaflet = require('esri-leaflet@3.0.2')
Insert cell
Insert cell
format = d => `${d}%`
Insert cell
Insert cell
iowa = FileAttachment("iowa_counties_topo.json").json()
Insert cell
counties = topojson.feature(iowa, iowa.objects.iowa_counties)
Insert cell
csv_data = d3.csvParse(await FileAttachment("iowa_counties.csv").text(),({FIPS, MED_AGE}) => [+FIPS, +MED_AGE])
Insert cell
data = Object.assign(new Map(csv_data), {title: "Median Age in Census 2010"})
Insert cell
data.get(19005)
Insert cell
med_age = Array.from(csv_data.values(), d => d[1])
Insert cell
YlGnBu = [d3.color("#ffffcc"), d3.color("#a1dab4"), d3.color("#41b6c4"), d3.color("#2c7fb8"),d3.color("#253494")]
Insert cell
naturalbreaks = simple.ckmeans(med_age, YlGnBu.length).map(v => v.pop())
Insert cell
//more information on sequential scales: https://observablehq.com/@d3/sequential-scales
// color = d3.scaleSequentialQuantile([...data.values()], d3.interpolateBlues)

// color = d3.scaleQuantile()
// .domain(med_age)
// .range()

color = d3.scaleThreshold()
.domain(naturalbreaks)
.range(YlGnBu)
Insert cell
width = 975
Insert cell
height = 610
Insert cell
margin = 100
Insert cell
//Rotate the map sets the longitude of origin for our UTM Zone 15N projection.
//projection = d3.geoTransverseMercator().rotate([94,0]).fitExtent([[80, 80], [width, height]], counties);
//d3 reference for projections: https://github.com/d3/d3-geo/blob/master/README.md

//use the following url for specific projection settings: https://github.com/veltman/d3-stateplane
//Use this code to set up the map projection (if different than geographic projection)

//projection = d3.geoAlbers().fitExtent([[margin, margin], [width - margin, height - margin]], counties)

//projection = d3.geoMercator().fitExtent([[margin, margin], [width - margin, height - margin]], counties)
Insert cell
//Using a path generator to project geometry onto the map
//path = d3.geoPath().projection(projection);
Insert cell
choropleth = {
// Create the main container
let container = DOM.element('div', { style: `width:${width}px;` });

// Create an SVG for the legend within the container
const legendSvg = d3.select(container).append("svg")
.attr("width", width)
.attr("height", 70) // Adjust height as needed for your legend
.append("g")
.attr("transform", "translate(365,20)")
.append(() =>
legend({
color: color,
title: data.title,
width: 260,
tickFormat: ".1f"
})
);

// Create a sub-container for the map
let mapContainer = DOM.element('div', { style: `width:${width}px;height:${width/1.6}px` });
container.appendChild(mapContainer);

// Initialize your map in the mapContainer instead of the main container
let map = L.map(mapContainer).setView([41.81, -93.46], 7);
esriLeaflet.basemapLayer('Gray').addTo(map);
L.svg().addTo(map);

yield container;
const overlay = d3.select(map.getPanes().overlayPane);
const svg = overlay.select('svg');

const g = svg.append('g').attr('class', 'leaflet-zoom-hide');

// Define the projectPoint function inside the update function to ensure it uses the current map state
function update() {
const projectPoint = function(x, y) {
const point = map.latLngToLayerPoint(new L.LatLng(y, x));
this.stream.point(point.x, point.y);
};

const projection = d3.geoTransform({ point: projectPoint });
const pathCreator = d3.geoPath().projection(projection);

const areaPaths = g.selectAll("path")
.data(counties.features)
.join("path")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 1)
.attr("fill", d => color(data.get(+d.properties.FIPS)))
.attr("d", pathCreator)
.append("title")
.text(d => "Median Age: " + data.get(+d.properties.FIPS));

}

map.on("zoom", update);
map.on("move", update);

// Initial call to draw the paths
update();

// return svg.node();
}
Insert cell
data.get(19005)
Insert cell
color(data.get(19005))
Insert cell
color(40)
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