Published
Edited
Mar 9, 2021
Insert cell
Insert cell
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
Insert cell
minnesota = FileAttachment("mn_state_WGS84.json").json()
Insert cell
MNcounties = topojson.feature(minnesota, minnesota.objects.mn_state_WGS84)
Insert cell
Insert cell
csv_data = d3.csvParse(await FileAttachment("Census2010_MN.csv").text(),({FIPS, Hispanic, TotalPop}) => [+FIPS, +Hispanic/+TotalPop])
Insert cell
data = Object.assign(new Map(csv_data), {title: "Percent Hispanic by County"})
Insert cell
hispanicPCT = Array.from(csv_data.values(), d => d[1])
Insert cell
Insert cell
naturalbreaks = simple.ckmeans(hispanicPCT, ColorScheme.length).map(v => v.pop())
Insert cell
quantize = d3.scaleQuantize()
.domain([d3.min(hispanicPCT),d3.max(hispanicPCT)])
Insert cell
manual = [.03, .07, .15, d3.max(hispanicPCT)]
Insert cell
Insert cell
ColorScheme = [d3.color("#ffffb2"), d3.color("#fecc5c"), d3.color("#fd8d3c"), d3.color("#e31a1c")]
Insert cell
color = d3.scaleThreshold()
.domain(manual)
.range(ColorScheme)
Insert cell
Insert cell
width = 610
Insert cell
height = 700
Insert cell
margin = 20
Insert cell
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]], MNcounties);
//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
color(data.get(1))
Insert cell
data.get(67)
Insert cell
Insert cell
choropleth = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

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

svg.append("g")
.selectAll("path")
.data(MNcounties.features)
.join("path")
.attr("stroke", "grey")
.attr("stroke-linejoin", "round")
.attr("stroke-width", .25)
// .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.COUNTYFIPS)))
.attr("d", path)
.append("title")
.text(d => "Percent Hispanic: " + data.get(+d.properties.COUNTYFIPS));

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