Public
Edited
Dec 4, 2023
Importers
13 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = (await d3.csv(DATA_URL)).map(d => ({
northernVariant: parseVariantResults(d['Northern\rVariant\rResults']),
southernVariant: parseVariantResults(d['Southern\rVariant Results']),
...d,
date: d3.utcParse('%m/%d/%Y')(d['Sample Date']),
southern: toIntOrNull(d['Southern\r(copies/mL)']),
northern: toIntOrNull(d['Northern\r(copies/mL)']),
southernAvg: toIntOrNull(d['Southern\r7 day avg']),
northernAvg: toIntOrNull(d['Northern\r7 day avg']),
}))
.map(d => ({
...d,
southernLow: d.southern - toIntOrNull(d['Southern\rLow Confidence\rInterval']),
northernLow: d.northern - toIntOrNull(d['Northern\rLow Confidence\rInterval']),
southernHigh: d.southern + toIntOrNull(d['Southern\rHigh Confidence\rInterval']),
northernHigh: d.northern + toIntOrNull(d['Northern\rHigh Confidence\rInterval']),
}))
.map((d, i, arr) => {
const windowStart = Math.max(0, i - 7)
const windowEnd = i
const window = arr.slice(windowStart, windowEnd)
return {
...d,
southernLowAvg: d3.mean(window.filter(d => d.southernLow), d => d.southernLow),
northernLowAvg: d3.mean(window.filter(d => d.northernLow), d => d.northernLow),
southernHighAvg: d3.mean(window.filter(d => d.southernHigh), d => d.southernHigh),
northernHighAvg: d3.mean(window.filter(d => d.northernHigh), d => d.northernHigh),
}
})
// Unfold the data so each system gets its own datapoints
.map((d, i, arr) => (
[
{
system: 'northern',
date: d.date,
concentration: d.northern,
concentrationAvg: d.northernAvg,
ciHigh: d.northernHigh,
ciLow: d.northernLow,
avgHigh: d.northernHighAvg,
avgLow: d.northernLowAvg,
},
{
system: 'southern',
date: d.date,
concentration: d.southern,
concentrationAvg: d.southernAvg,
ciHigh: d.southernHigh,
ciLow: d.southernLow,
avgHigh: d.southernHighAvg,
avgLow: d.southernLowAvg,
}
]
))
.flat()
Insert cell
data
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
milestones = holidays.inRange(...d3.extent(data, d => d.date)).map(h => ({
...h,
date: d3.utcDay.floor(h.date), // Convert dates to UTC
}))
Insert cell
Insert cell
RECENT_DATE_RE = /Biobot Data - samples submitted through (\d{1,2}\/\d{1,2}\/\d{4})/
Insert cell
mwra_page = (await fetch(CORS_PROXY + 'https://www.mwra.com/biobot/biobotdata.htm')).text()
Insert cell
latestSiteUpdate = {
try {
return d3.utcParse('%m/%d/%Y')(mwra_page.match(RECENT_DATE_RE)[1])
} catch {
return null
}
}
Insert cell
Insert cell
Insert cell
SYSTEMS_BY_TOWN = new Map([
['northern', ['Wilmington', 'Reading', 'Bedford', 'Burlington', 'Woburn', 'Stoneham', 'Lexington', 'Winchester', 'Melrose', 'Arlington', 'Medford', 'Malden', 'Waltham', 'Belmont', 'Somerville', 'Everett', 'Revere', 'Chelsea', 'Watertown', 'Cambridge', 'Winthrop']],
['both', ['Newton', 'Boston', 'Brookline', 'Milton']],
['southern', ['Ashland', 'Framingham', 'Natick', 'Wellesley', 'Needham', 'Dedham', 'Quincy', 'Westwood', 'Canton', 'Randolph', 'Braintree', 'Weymouth', 'Hingham', 'Norwod', 'Canton', 'Walpole', 'Stoughton', 'Holbrook', 'Norwood']]
])
Insert cell
systemForTown = t => {
const system = Array.from(SYSTEMS_BY_TOWN.entries())
.find(([system, towns]) => towns.includes(t))

if (!system) return null

return system[0]
}
Insert cell
includedTowns = topojson.merge(
TOWNS,
TOWNS.objects.towns.geometries.filter(t => !!systemForTown(t.properties.TOWN2))
)
Insert cell
mapFillScale = (system, both, none) => {
const colorScale = chart.scale('color')
const color = colorScale.apply(system)
if (color) return color

if (system === 'both') return both
return none
}
Insert cell
colorScale = chart.scale('color')
Insert cell
bothTexture = textures.lines()
.size(8)
.strokeWidth(2.5)
.stroke(colorScale.range[0])
.background(colorScale.range[1])
Insert cell
map = {
const mapWidth = Math.min(600, width)
const map = d3.select(DOM.svg(mapWidth, mapWidth))
map.attr('style', 'max-width: 100%')

const reprojection = d3.geoIdentity().fitExtent([[20, 20], [mapWidth - 20, mapWidth - 20]],includedTowns)
const path = d3.geoPath(reprojection)

map.call(bothTexture)

const townShapes = map.append('g')
.selectAll()
.data(topojson.feature(TOWNS, TOWNS.objects.towns).features)
.join('path')
.attr('d', path)
.attr('fill', d => mapFillScale(systemForTown(d.properties.TOWN2), bothTexture.url(), '#EEE'))
.attr('stroke', '#FFF')
return map
}
Insert cell
Insert cell
Insert cell
latestDate = data => data.filter(d => !!d.concentrationAvg).slice(-1)[0].date
Insert cell
Insert cell
capitalize = s => s.substring(0, 1).toUpperCase() + s.substring(1)
Insert cell
Insert cell
toIntOrNull = d => d === "" ? null : +d
Insert cell
Insert cell
pluralize = (word, count, plural) => count === 1 ? word : (plural || word + 's')
Insert cell
parseVariantResults = str => {
if (str === '') return
const [variant, pctStr] = str.split('\r')
return { [variant]: +pctStr.substring(0, pctStr.length - 1) / 100 }
}
Insert cell
Insert cell
formatRatio = (n, formats = {}) => {
const { numberFormat, lowerFormat, higherFormat, exactString } = Object.assign({}, {
numberFormat: d3.format('d'),
higherFormat: n => `↑${n}× over`,
lowerFormat: n => `↓${n}× below`,
exactString: 'At'
}, formats)
if (n < 0) throw('Ratio cannot be smaller than 0')
if (n < 1) return lowerFormat(numberFormat(1 / n))
if (n > 1) return higherFormat(numberFormat(n))
return exactString
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
holidays = import('https://cdn.skypack.dev/@18f/us-federal-holidays@4.0.0?min')
Insert cell
Insert cell
Insert cell
Insert cell
pctThroughYear = (date) => {
const year = date.getFullYear()
const isLeapYear = (0 == year % 4) && (0 != year % 100) || (0 == year % 400)
return d3.utcDay.count(d3.utcYear.floor(date), d3.utcDay.ceil(date)) / (isLeapYear ? 366 :365)
}
Insert cell
dateFromPct = (pct) => {
const dayOfYear = pct * 365 // will be slightly inaccurate for leap years, fine for our purposes
return d3.utcDay.offset(d3.utcYear.floor(new Date()), dayOfYear)
}
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