Public
Edited
Aug 1, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
plotDistricts(districtDayCommunityInfections, dateRange)
Insert cell
Insert cell
plotDistricts(highRiskAreas, dateRange, {
includeTotal: false,
yLabel: "Areas"
})
Insert cell
function plotDistricts(data, dateRange, options = {}) {
options = { yLabel: "Infections", includeTotal: true, ...options };
data = filterRange(data, dateRange);
const yMax = _.max(data.map((d) => d.infections));
// const latestCaseCounts = Object.fromEntries(
// districtNames.map((name) => [
// name,
// d3.greatest(
// data.filter((d) => d.district === name),
// (d) => d.date
// ).infections
// ])
// );
function memoize(fn) {
const cache = new Map();
return (arg) => {
if (cache.has(arg)) return cache.get(arg);
const value = fn(arg);
cache.set(arg, value);
return value;
};
}
const districtInfections = memoize((name) => {
return _.sum(
data.filter((d) => d.district === name).map((d) => d.infections)
);
});
// remove distrincts that don't have any infections
data = data.filter((d) => districtInfections(d.district) > 0);
const dNames = districtNames; //d3.sort(d3.group(data, (d) => d.district).keys());
// add indices, for faceting
const cols = Math.ceil(Math.sqrt(dNames.length));
data = data.map((d) => ({
...d,
index: dNames.indexOf(d.district)
}));
return Plot.plot({
grid: true,
width,
height: 600,
marginBottom: 80,
color: {
// range: ["#282", "#c22"],
// range: ["green", "orange", "red"],
range: ["orange", "red"],
interpolate: "hcl"
},
facet: {
data,
x: (d) => " ".repeat(d.index % cols),
y: (d) => " ".repeat(Math.floor(d.index / cols))
},
x: {
label: "Date",
// tickFormat: "%B %-d",
ticks: 10,
tickRotate: 45
// type: "utc"
},
y: {
label: options.yLabel,
tickFormat: ".0f"
},
marks: [
Plot.frame(),
Plot.areaY(data, {
x: "date",
y: "infections",
curve: "step-before",
fill: (d) => districtInfections(d.district)
}),
// Plot.lineY(data, {
// x: "date",
// y: "infections",
// curve: "step-before",
// stroke: "black"
// }),
Plot.text(data, {
text: (d) =>
options.includeTotal && districtInfections(d.district)
? `${d.district} (${districtInfections(d.district)})`
: d.district,
fontSize: 18,
y: yMax * 0.85
})
]
});
}
Insert cell
dateRange = ({
start: new Date("July 1, 2022"),
end: new Date("July 31, 2022")
})
Insert cell
function filterRange(data, dateRange) {
if (dateRange) {
data = data.filter(({ date }) => date >= dateRange.start);
data = data.filter(({ date }) => date <= dateRange.end);
}
return data;
}
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
Insert cell
districtDayCommunityInfections = ungroupCsv(
"https://docs.google.com/spreadsheets/d/e/2PACX-1vQn1YeYbOv31SCzJs0RlpXETOOKNO4HTn0F5eYT9w_teus0UXKBIxxfJshTuXL9QENK_e5qo2RGPoqZ/pub?output=csv"
)
Insert cell
async function ungroupCsv(url) {
const text = await (await fetch(url)).text();
return (
d3
.csvParse(text, (d) =>
d3.autoType(
// Remove commas
Object.fromEntries(
Object.entries(d).map(([k, v]) => [k, v.replace(/,/g, "")])
)
)
)
// Construct Dates from date strings
.map(({ Date: date, ...districts }) => ({
date: new Date(date + ", 2022"),
...districts
}))
// Subtract one day from each date, in order to translate report dates into infection dates
// .map(({ date, ...districts }) => ({
// date: new Date(date.getTime() - 86400000),
// ...districts
// }))
// The CSV is grouped by date. Ungroup it.
.flatMap(({ date, ...districts }) =>
Object.entries(districts).map(([district, infections]) => ({
date,
district,
infections
}))
)
);
}
Insert cell
districtDayInfections = ungroupCsv(
"https://docs.google.com/spreadsheets/d/e/2PACX-1vSbmGMVrwaU3z6VSi1mN1C4aYjHf49yPnyaYUtw_qNn84yEh0ZgkJmPqLKTkiFrPcpRpcm7p0Q989IB/pub?gid=0&single=true&output=csv"
)
Insert cell
highRiskAreas = {
const data = await ungroupCsv(
"https://docs.google.com/spreadsheets/d/e/2PACX-1vRdldpYPJ-Y0wmnWbZ1PmOdx_Txp5SYovsKzgvkof8bAL_RnOCtDPeMD3aoLSvti9-Ka4DsfEGqxTs5/pub?output=csv",
"areas",
0
);
return data;
// return data.map(({date, district,) => d);
}
Insert cell
Insert cell
mostRecentDate = d3.timeFormat("%b %-d")(
d3.max(districtDayInfections.map((d) => d.date))
)
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