Public
Edited
Mar 1, 2023
1 fork
Insert cell
md`# Assignment 3: Choropleth Mapping
Miah Boyle`
Insert cell
d3 = require("d3@5")
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
simple = require("simple-statistics@7.0.7/dist/simple-statistics.min.js")
Insert cell
format = d => `${d}%`
Insert cell
topojson = require("topojson-client@3")
Insert cell
MN_CAFO = FileAttachment("MinnesotaCensusCAFO_ExportFeature.json").json()
Insert cell
census_features = topojson.feature(MN_CAFO, MN_CAFO.objects.MinnesotaCensusCAFO_ExportFeature)
Insert cell
csv_data = d3.csvParse(await FileAttachment("ej_mpca_census (1).csv").text(),({tractce, totpov, pop185x}) => [tractce, +pop185x/+totpov])
Insert cell
Insert cell
data = Object.assign(new Map(csv_data), {title: "Percent population under 185% poverty level"})
Insert cell
//Array(99).filter(d => d==0)
//Array(99).filter(d=>0)
//Yield_2019.object.array(99).filter(d => d==0)
//Yieldthisone=domain[d3.min(50),d3.max(300)]
Insert cell
//Yield_2019 = Array.from(csv_data.values(), d => d[1]).filter(d => d > 0)
Insert cell
pctPov = Array.from(csv_data.values(), d => d[1]).filter(d => d >= 0)
Insert cell
d3.min(pctPov)
Insert cell
PuBuGn = [d3.color("#d0d1e6"), d3.color("#a6bddb"), d3.color("#67a9cf"), d3.color("#1c9099"),d3.color("#016c59")]
Insert cell
Insert cell
naturalbreaks = simple.ckmeans(pctPov, PuBuGn.length).map(v => v.pop())
Insert cell
Insert cell
nancolor = d3.color("grey")
Insert cell
nullcolorforlegend = d3.scaleOrdinal()
.domain([NaN])
.range([nancolor])
Insert cell
//more information on sequential scales: https://observablehq.com/@d3/sequential-scales
//color = d3.scaleSequentialQuantile([...data.values()], d3.interpolateBlues)

//color = d3.scaleQuantile()
//.domain(Yield_2019)
//.range(["#ffffcc","#a1dab4","#41b6c4","#2c7fb8","#253494"])

color = d3.scaleThreshold()
.domain(naturalbreaks)
.range(PuBuGn)
//color= d3.scaleQuantize()
//.domain(["])
//.range(["#ffffcc", "#cbc9e2", "#9e9ac8","#756bb1","#54278f"])
Insert cell
width = 600
Insert cell
height = 800
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]], census_features);
//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
Insert cell
//Using a path generator to project geometry onto the map
path = d3.geoPath().projection(projection);
Insert cell
choropleth = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.attr("transform", "translate(550,60)")
.append(() =>
legend({
color: nullcolorforlegend,
title: "No Data",
width: 50,
tickFormat: ".1f"
})
);
svg.append("g")
.attr("transform", "translate(250,60)")
.append(() =>
legend({
color: color,
title: data.title,
width: 260,
tickFormat: ".2f"
})
);

svg.append("g")
.selectAll("path")
.data(census_features.features)
.join("path")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 0.1)
.attr("fill", function(d){
if(data.get(d.properties.TRACTCE) >= 0){
return color(data.get(d.properties.TRACTCE));
}
else{
return nancolor;
}
})
// .attr("fill", d => color(data.get(d.properties.NAME)[0]))
.attr("d", path)
.append("title")
.text(d => "Percent Poverty: " + data.get(d.properties.TRACTCE));

return svg.node();
}
Insert cell
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