Public
Edited
Feb 25, 2023
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
md`# Data`
Insert cell
topojson = require("topojson-client@3")
Insert cell
Counties = FileAttachment("County2010washWGS84.json").json()
Insert cell
WashingtonCounties = topojson.feature(Counties,Counties.objects.County2010washWGS84)
Insert cell
county2010washwgs84Dbf = FileAttachment("County2010washWGS84.dbf.csv").csv()
Insert cell
csv_data = d3.csvParse(await FileAttachment("County2010washWGS84.dbf.csv").text(),({COUNTYFP10, POPWHITE, POP10}) => [COUNTYFP10, +POPWHITE/+POP10])
Insert cell
wa_csv_data = d3.csvParse(await FileAttachment("ca_age@1.csv").text(),({GEO_ID, NAME, EstimateTotalpopulation, Estimate65yearsandover}) => [+GEO_ID, (+Estimate65yearsandover/+EstimateTotalpopulation)])
Insert cell
percent_over65 = Array.from(ca_csv_data.values(), d => d[1])
Insert cell
new_data = Object.assign(new Map(ca_csv_data), {title: ["percent_over65"]})
Insert cell
md`# Map`
Insert cell
YlGnBu = [d3.color("#ffffcc"), d3.color("#a1dab4"), d3.color("#41b6c4"), d3.color("#2c7fb8"),d3.color("#253494")]
Insert cell
naturalbreaks = simple.ckmeans(percent_over65, YlGnBu.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(naturalbreaks)
.range(YlGnBu)
Insert cell
width = 975
Insert cell
height = 640
Insert cell
margin = 100
Insert cell
projection = d3.geoTransverseMercator().rotate([122,0]).fitExtent([[80, 80], [width, height]], new_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: new_data.title,
width: 260,
tickFormat: ".1f"
})
);

svg.append("g")
.selectAll("path")
.data(new_counties_features.features)
.join("path")
.attr("stroke", "white")
.attr("stroke-linejoin", "round")
.attr("stroke-width", 1)
// .attr("fill", function(d){
// console.log(color(new_data.get(+d.properties.GEOID)))
// return color(new_data.get(+d.properties.GEOID));
// })
.attr("fill", d => color(new_data.get(+d.properties.GEOID)))
.attr("d", path)
.append("title")
.text(d => " percent_over65: " + new_data.get(d.properties.GEO_ID));

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