Public
Edited
Apr 5
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
format = d => `${d}%`
Insert cell
Insert cell
Minnesota = FileAttachment("Minnesota_2020_Counties_WGS84.json").json()
Insert cell
counties = topojson.feature(Minnesota, Minnesota.objects.Minnesota_2020_Counties_WGS84)
Insert cell
csv_data = d3.csvParse(await FileAttachment("Minnesota_2020_county.csv").text(),({GISJOIN, U7J001, U7J003}) => [GISJOIN, +U7J003/+U7J001])
Insert cell
data = Object.assign(new Map(csv_data), {title: "Black and African American Population Percentage in Minnesota"})
Insert cell
data.get("G2700010")
Insert cell
Black_pct = Array.from(csv_data.values(), d => d[1])
Insert cell
PnkSlmRd = [d3.color("#fee5d9"), d3.color("#fcae91"), d3.color("#fb6a4a"), d3.color("#de2d26"), d3.color("#a50f15")]
Insert cell
quantile = d3.scaleQuantile()
.domain(Black_pct)
.range(["#fee5d9", "#fcae91", "#fb6a4a", "#de2d26", "#a50f15"])
Insert cell
//more information on sequential scales: https://observablehq.com/@d3/sequential-scales
// color = d3.scaleSequentialQuantile([...data.values()], d3.interpolateBlues)

color = d3.scaleQuantile()
.domain(Black_pct)
.range(PnkSlmRd)

//color = d3.scaleThreshold()
// .domain(quantile)
// .range(PnkSlmRd)
Insert cell
width = 720
Insert cell
height = 560
Insert cell
margin = 150
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://d3js.org/d3-geo

//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.geoConicConformal().parallels([45 + 37 / 60, 47 + 3 / 60])
// .rotate([94 + 15 / 60, 0]).fitExtent([[80, 80], [width, height]], 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 = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.attr("transform", "translate(365,20)")
.append(() =>
legend({
color: color,
title: data.title,
width: 260,
tickFormat: ".1%"
})
);

svg.append("g")
.selectAll("path")
.data(counties.features)
.join("path")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 1)
// .attr("fill", function(d){
// console.log(color(data.get(d.properties.FIPS)[0]))
// return color(data.get(d.properties.FIPS)[0]);
// })
.attr("fill", d => color(data.get(d.properties.GISJOIN)))
.attr("d", path)
.append("title")
.text(d => " Black or African American Percentage: " + data.get(d.properties.GISJOIN));

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