Published
Edited
Mar 9, 2021
Insert cell
md`# Third Exercise on Observable`
Insert cell
d3 = require("d3@5")
Insert cell
topojson = require("topojson-client@3")
Insert cell
Alabama = FileAttachment("Alabama.json").json()
Insert cell
county_features = topojson.feature(Alabama, Alabama.objects.Alabama1)
Insert cell
csv_data = d3.csvParse(await FileAttachment("Alabama.csv").text(),({FIPS, AGE_5_9, POP2010}) => [FIPS, [+AGE_5_9, +AGE_5_9/+POP2010]])
//This is the normalized variable I will be mapping. It was created by diving the population between 5-9 by the total population for each county in Alabama.
Insert cell
childpct = Array.from(csv_data.values(), d => d[1][1])
Insert cell
data = Object.assign(new Map(csv_data), {title: ["Percent Population of Alabama: between 5 & 9"]})
Insert cell
md`# Classification: Natural Breaks`
Insert cell
naturalbreaks = simple.ckmeans(childpct, YlOrBr.length).map(v => v.pop())
Insert cell
md`# Color Scheme`
Insert cell
YlOrBr = [d3.color("#fee391"), d3.color("#fec44f"), d3.color("#fe9929"), d3.color("#d95f0e"),d3.color("#993404")]
Insert cell
color = d3.scaleThreshold()
.domain(naturalbreaks)
.range(YlOrBr)
Insert cell
md`# Creating a Canvas`
Insert cell
width = 900
Insert cell
height = 600
Insert cell
md`# Projecting a Map`
Insert cell
projection = d3.geoTransverseMercator()
.rotate([154, 0])
.center([0, 62])
.fitExtent([[80, 80], [width, height]], county_features);
Insert cell
margin = 100
Insert cell
path = d3.geoPath().projection(projection);
Insert cell
md`# Creating a Choropleth Map`
Insert cell
choropleth = {
const svg = d3.create("svg")
.attr("viewBox", [0, 15, width, height]);
svg.append("g")
.attr("transform", "translate(360,20)")
.append(() =>
legend({
color: color,
title: data.title,
width: 350,
tickFormat: ".2f"
})
);
svg.append("g")
.selectAll("path")
.data(county_features.features)
.join("path")
.attr("stroke", "black")
.attr("stroke-width", 0.5)
.attr("stroke-linejoin", "round")
.attr("fill", d => color(data.get(+d.properties.FIPS)[1]))
.attr("d", path)
.append("title")
.text(d => format(data.get(+d.properties.FIPS)[1] + "%"));
return svg.node();
}
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
format = d => `${d}%`
Insert cell
md`# Annex`
Insert cell
Insert cell
Insert cell
simple = require("simple-statistics@7.0.7/dist/simple-statistics.min.js")
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