Published
Edited
Mar 16, 2021
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
MT = FileAttachment("FINALMTMAP (1).json").json()
Insert cell
md` I inserted the topoJSON into the notebook, and created the counties variable. This JSON was converted to WGS84 in the first assignment.`
Insert cell
features = topojson.feature(MT, MT.objects.FINALMTMAP)
Insert cell
Insert cell
csv_data = d3.csvParse(await FileAttachment("MTFoodAccessFINAL (2).csv").text(),({GEOID, TractLOWI, POP2010}) => [GEOID, [+TractLOWI, +TractLOWI/+POP2010]])
Insert cell
data = Object.assign(new Map(csv_data), {title: "Poverty Rate"})
Insert cell
PctLOWI = Array.from(csv_data.values(), d => d[1][1])
Insert cell
Insert cell
YlGnBu = [d3.color("#f1eef6"), d3.color("#d7b5d8"), d3.color("#df65b0"), d3.color("#dd1c77"),d3.color("#980043")]
Insert cell
quantile = d3.scaleQuantile()
.domain(PctLOWI)
.range(YlGnBu)
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(quantile)
.range(YlGnBu)
Insert cell
width = 975
Insert cell
height = 610
Insert cell
margin = 100
Insert cell
md`Map projections are important because the one you choose will distort the areas and shapes of the map. I checked if the projection was the proper one to use for this map, and decided that the transverse mercator projection was fine. `
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]], 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
//Using a path generator to project geometry onto the map
path = d3.geoPath().projection(projection);
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: quantile,
title: data.title,
width: 260,
tickFormat: ".2f"
})
);

svg.append("g")
.selectAll("path")
.data(features.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 => quantile(data.get(d.properties.GEOID)[1]))
.attr("d", path)
.append("title")
.text(d => "The poverty rate for this county is " + data.get(d.properties.GEOID)[1])

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