Published
Edited
May 9, 2021
Fork of Untitled
Insert cell
//Required for Jenks Classifier
simple = require("simple-statistics@7.0.7/dist/simple-statistics.min.js")
Insert cell
//require d3 v5 library
d3 = require("d3@5");
Insert cell
//require topojson-client v3 library
topojson = require("topojson-client@3");
Insert cell
//load json file
countiesIL = FileAttachment("countiesIL.json").json();
Insert cell
//extract json components
counties = topojson.feature(countiesIL, countiesIL.objects.geojson);
Insert cell
//load particular columns and derived columns from csv file
csv_data = d3.csvParse(await FileAttachment("Data@2.csv").text(),({County, c2020, c2030}) => [County, [(+c2030/c2020 - 1)*100]]);
Insert cell
//this is how to take the values of a variable (column) from all variables you created from the csv file
//extract derived column (1), 0 start index
growth20to30 = Array.from(csv_data.values(), d => d[1][0]);
Insert cell
//this is how you map the id column to the values. We will use this for joining with topojson later.
data = Object.assign(new Map(csv_data), {title: ["Projected 2020-2030 % Population Growth"]});
Insert cell
color = d3.scaleThreshold()
.domain([-2, 0, 3.5, 12])
.range(d3.schemeRdBu[5])
Insert cell
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
format = d => `${d}%`
Insert cell
width = 200
Insert cell
height = 400
Insert cell
margin = 0
Insert cell
projection = d3.geoTransverseMercator().rotate([94,0]).fitExtent([[80, 80], [width, height]], counties);
Insert cell
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(0,50)")
.append(() =>
legend({
color: color,
title: data.title,
width: 200,
tickFormat: d => d + "%"
})
);
svg.append("g")
.selectAll("path")
.data(counties.features)
.join("path")
//Line colors
.attr("stroke", "black")
//nothing
.attr("stroke-linejoin", "round")
//width of lines
.attr("stroke-width", 1)
//reading the data
.attr("d", path)
//coloring of data
.attr("fill", d => color(data.get(d.properties.COUNTY_NAM)))
//???
.append("title")
//hover text
.text(d => "Predicted growth in " + d.properties.COUNTY_NAM + " county: " + Math.round(data.get(d.properties.COUNTY_NAM) * 100) / 100 + "%");
return svg.node();
}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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