Published
Edited
Dec 9, 2020
1 fork
Insert cell
Insert cell
highres = false
Insert cell
metrics = [
"total_icu_beds_7_day_avg",
"total_staffed_adult_icu_beds_7_day_avg",
"icu_beds_used_7_day_avg"
]
Insert cell
week = 0
Insert cell
function value(d) {
// percent icu used out of total on average
return (+d["icu_beds_used_7_day_avg"] / +d["total_icu_beds_7_day_avg"]) * 100;
}
Insert cell
hospitalDots = {
let canvas = d3
.create("canvas")
.attr("width", width)
.attr("height", height)
.node();
let ctx = canvas.getContext("2d");
ctx.globalCompositeOperation = "lighter";

scaleCanvas(canvas, ctx);

let projection = d3
.geoAlbersUsa()
.fitSize([width - 10, height], countyShapes);
// .geoEquirectangular()
// .fitWidth(width * 0.98, land)
// .translate([width / 2, height / 2]);

let path = d3
.geoPath()
.projection(projection)
.context(ctx);

ctx.fillStyle = "#111";
ctx.fillRect(0, 0, width, width);

ctx.fillStyle = "#555";
let r = 1;
// let r = 10;
hospitals
.filter(d => {
let events = eventsByHospital.get(d.hospital_pk);
if (events) return value(events[week]) > 75;
})
.sort(function(a, b) {
let eventsA = eventsByHospital.get(a.hospital_pk);
let eventsB = eventsByHospital.get(b.hospital_pk);
if (!eventsA || !eventsB) return -1;
return value(eventsA[week]) - value(eventsB[week]);
})
.forEach(d => {
let p = projection([d.LONGITUDE, d.LATITUDE]);

// total_icu_beds_7_day_avg
// icu_beds_used_7_day_avg
// total_beds_7_day_avg
let events = eventsByHospital.get(d.hospital_pk);
// console.log(d, events);
// if (!events || !events.length) return;
if (events) {
let alpha = metricNormalize(value(events[week]));
r = metricRadius(value(events[week]));
let c = d3.rgb(colorScale(alpha));
// let c = d3.rgb("#fff");
ctx.fillStyle = `rgba(${[c.r, c.g, c.b]}, ${alpha}`;
}
// ctx.fillStyle = "#fff";

if (p) ctx.fillRect(p[0], p[1], r, r);
});

return canvas;
}
Insert cell
metric = metrics[0]
Insert cell
// colorScale = d3.scaleSequential(d3.interpolateGreys)
colorScale = d3.scaleSequential(d3.interpolateMagma)
Insert cell
extent = d3.extent(eventsByHospital, d => value(d[1][week]))
Insert cell
metricNormalize = d3
.scaleLinear()
.range([0, 1])
// .domain(extent)
.domain([0, 101])
Insert cell
metricRadius = d3
.scaleSqrt()
.range([0, 5])
// .domain(extent)
.domain([70, 101])
Insert cell
eventsByHospital.get("010108")
Insert cell
import { countyShapes } from "@enjalot/us-county-datasets"
Insert cell
import { scaleCanvas } from "@john-guerra/canvas-retina-display"
Insert cell
Insert cell
Insert cell
hospitals = FileAttachment("hospitals-maplarge.csv").csv()
Insert cell
Insert cell
hospitalsByPK = new Map(hospitals.map(d => [d.hospital_pk, d]))
Insert cell
Insert cell
hhs = FileAttachment(
"slimmed-reported_hospital_capacity_admissions_facility-level_weekly_average_timeseries_20201207.csv"
).csv()
Insert cell
hhs.filter(d => d.hospital_pk == "010108")
Insert cell
// how many events per hospital (seems 18)
d3.rollup(hhs, v => v.length, d => d.hospital_pk)
Insert cell
eventsByHospital = d3.group(hhs, d => d.hospital_pk)
Insert cell
// how many events per county
d3.rollup(hhs, v => v.length, d => hospitalsByPK.get(d.hospital_pk).fips_code)
Insert cell
d3 = require("d3@6", "d3-geo", "d3-geo-projection@3")
Insert cell
Insert cell
/*
mkdir data
curl https://healthdata.gov/sites/default/files/reported_hospital_capacity_admissions_facility-level_weekly_average_timeseries_20201207.csv > data/reported_hospital_capacity_admissions_facility-level_weekly_average_timeseries_20201207.csv
npm install d3
node slim.js
*/

/*
let fs = require("fs")
let d3 = require("d3")

let dir = "data/"
let filename = "reported_hospital_capacity_admissions_facility-level_weekly_average_timeseries_20201207.csv"
let txt = fs.readFileSync(dir + filename).toString().slice(1) // weird character at begin of file
let data = d3.csvParse(txt)

let todelete = "hospital_name,address,city,state,zip,fips_code,hospital_subtype,ccn,is_metro_micro".split(",")
Object.keys(data[0]).forEach(key => {
if(key.indexOf("previous_day") >= 0) {
todelete += "," + key
}
})
todelete = todelete.split(",")
console.log("todelete", todelete)

let slimmed = data
.filter(d => !!d.address)
// .filter(d => !!d.fips_code)
.map(function(d) {
let obj = {...d}
Object.keys(obj).forEach(key => {
let v = obj[key]
if(v == "-999999" || v == "-999999.0") obj[key] = ""
})

// we can look up all these fields in the hospitals geocoded map
todelete.forEach(del => {
delete obj[del]
})
return obj
})

console.log("writing slimmed full file")
fs.writeFileSync(dir + "slimmed-" + filename, d3.csvFormat(slimmed))

// // let byStateFips = d3.rollup(data, v => v.length, d => d.fips_code.slice(0,2))
let byStateFips = d3.group(data, d => d.fips_code.slice(0,2))

let states = Array.from(byStateFips.keys())

states.forEach(state => {
let d = byStateFips.get(state)
console.log(state, d.length)
fs.writeFileSync(dir + state + "-slimmed-" + filename, d3.csvFormat(d))
})
*/
Insert cell
import { world } from "@d3/world-map"
Insert cell
topojson = require("topojson-client@3")
Insert cell
land = topojson.feature(world, world.objects.land)
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