Public
Edited
Apr 3, 2024
Insert cell
Insert cell
Insert cell
Insert cell
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
Insert cell
schooldistricts = FileAttachment("Iowa_School_Districts_2023_2024@1.json").json()
Insert cell
schooldistricts_features = topojson.feature(schooldistricts, schooldistricts.objects.Iowa_School_Districts_2023_2024)
Insert cell
csv_data = d3.csvParse(await FileAttachment("Iowa_School_Districts_2023_2024.csv").text(),({SchoolDist, Pop2020, SqMiles}) => [SchoolDist, [+Pop2020/+SqMiles]])
Insert cell
data = Object.assign(new Map(csv_data), {title: ["Iowa School Districts 2023-2024", "Population Density"]})
Insert cell
data.get("AKRON WESTFIELD")
Insert cell
popdensity = Array.from(csv_data.values(), d => d[1])
Insert cell
YlGnBu = [d3.color("#f1eef6"), d3.color("#d7b5d8"), d3.color("#df65b0"), d3.color("#dd1c77"),d3.color("#980043")]
Insert cell
naturalbreaks = simple.ckmeans(popdensity, 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 = 610
Insert cell
margin = 100
Insert cell
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(365,20)")
.append(() =>
legend({
color: color,
title: data.title,
width: 260,
tickFormat: ".1f"
})
);

svg.append("g")
.selectAll("path")
.data(schooldistricts_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.SchoolDist)[0]))
// return color(data.get(d.properties.SchoolDist)[0]);
// })
.attr("fill", d => color(data.get(d.properties.SchoolDist)))
.attr("d", path)
.append("title")
.text(d => " Population Density: " + data.get(d.properties.SchoolDist));

return svg.node();
}
Insert cell
Insert cell
data.get("AKRON WESTFIELD")
Insert cell
Insert cell
color(40)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more