Public
Edited
Apr 25, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, 975, 610]);

svg.append("g")
.attr("transform", "translate(610,20)")
.append(() => legend({color, title: data.title, width: 260, tickFormat: ".0s"}));

svg.append("g")
.selectAll("path")
.data(topojson.feature(us, us.objects.counties).features)
.join("path")
.attr("fill", d => color(data.get(d.id)))
.attr("stroke", "white")
.attr("d", path)

svg.append("path")
.datum(topojson.mesh(us, us.objects.states, (a, b) => a !== b))
.attr("fill", "none")
.attr("stroke", "grey")
.attr("stroke-linejoin", "round")
.attr("d", path);

return svg.node();
}
Insert cell
** Template Parameters **
Insert cell
file = FileAttachment("Alcohol_Consumption_US_2016.csv")
Insert cell
title = "Drought %"
Insert cell
countyIdColumn = "FIPS"
Insert cell
countyRateColumn = "D4"
Insert cell
dateColumn = "MapDate"
Insert cell
dateFormat = "%Y%m%d"
Insert cell
stepSize = 21
Insert cell
** Data Parsing & Prep**
Insert cell
rawData = d3.csvParse(await file.text())
Insert cell
parseTime = d3.timeParse(dateFormat)
Insert cell
timeAsString = d3.timeFormat(dateFormat)
Insert cell
allDates = rawData.map(element => parseTime(element[dateColumn]))
Insert cell
minDate = d3.min(allDates)
Insert cell
maxDate = d3.max(allDates)
Insert cell
maxDays = d3.timeDay.count(minDate, maxDate)
Insert cell
countyArray = rawData.
filter((elm, index) => timeAsString(allDates[index]) == timeAsString(date)).
map(elm => [elm[countyIdColumn], +elm[countyRateColumn]])
Insert cell
maxScale = d3.max(countyArray.map((elm, idx) => elm[1])) + 1
Insert cell
minScale = d3.min(countyArray.map((elm, idx) => elm[1])) + 1
Insert cell
data =
Object.assign(
new Map(countyArray), {title: title}
)

Insert cell
function toDate(rangeValue) {
minDate + rangeValue
}
Insert cell
** Mostly copied from Bostok's Map **
Insert cell
color = d3.scaleQuantize([minScale, maxScale], d3.schemeReds[9])
Insert cell
path = d3.geoPath()
Insert cell
format = d => `${d}%`
Insert cell
states = new Map(us.objects.Alc_Con(d => [d.id, d.properties]))
Insert cell
us = FileAttachment("Alc_Con.json").json()
Insert cell
topojson = require("topojson-client@3")
Insert cell
d3 = require("d3@6")
Insert cell
import {legend} from "@d3/color-legend"
Insert cell
import {Scrubber} from "@mbostock/scrubber"
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