Published
Edited
Mar 9, 2020
Insert cell
md`# Assignment 2`
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
Illinois = FileAttachment("IL_BNDY_County_Py.json").json()
Insert cell
counties = topojson.feature(Illinois, Illinois.objects.IL_BNDY_County_Py)
Insert cell
csv_data = d3.csvParse(await FileAttachment("Illinois_PEP_2018_PEPANNRES.csv").text(),({fips, respop72010
, respop72011}) => [fips, [+respop72010, +respop72011, ((+respop72011) - (+respop72010))/(+respop72010)]])
Insert cell
data = Object.assign(new Map(csv_data), {title: "Population Change"})
Insert cell
change = Array.from(csv_data.values(), d => d[1][2])
Insert cell
colors = [d3.color("#2c7bb6"), d3.color("#abd9e9"),d3.color("#ffffbf"), d3.color("#fdae61"),d3.color("#d7191c")]
Insert cell
naturalbreaks = simple.ckmeans(change, colors.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([-0.01, -0.005, 0, 0.005, 0.01])
.range(colors)
Insert cell
width = 975
Insert cell
height = 610
Insert cell
//Rotate the map sets the longitude of origin for our Albers 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.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(360,20)")
.append(() =>
legend({
color: color,
title: data.title,
width: 260,
tickFormat: ".3%"
})
);

svg.append("g")
.selectAll("path")
.data(counties.features)
.join("path")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("fill", function(d){
var org_fips = d.properties.CO_FIPS.toString()
var fips = d.properties.CO_FIPS.toString()
var numdigits = org_fips.length
if(numdigits == 1){
fips = "1700" + org_fips
}else if(numdigits == 2){
fips = "170" + org_fips
}
else if(numdigits == 3){
fips = "17" + org_fips
}
var mycolor = color(data.get(fips)[2])
return mycolor
})
//.attr("fill", d => color(data.get("17" +d.properties.CO_FIPS)[2]))
.attr("d", path)
.append("title")
//.text(d => "change: " + data.get("17" +d.properties.CO_FIPS)[2]);

return svg.node();
}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

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