Public
Edited
Mar 21, 2023
Insert cell
md`# Patients Sent - Parallel Sets`
Insert cell
d3 = require("d3@5", "d3-sankey@0.12")
Insert cell
raw_data = d3.csvParse(await FileAttachment("sent_matrix-2.csv").text(), d3.autoType)
Insert cell
graph = {
const srcNodes = raw_data.columns.map(colName => {return {name: colName+"-src"}});
const dstNodes = raw_data.columns.map(colName => {return {name: colName+"-dst"}});
const nodes = srcNodes.concat(dstNodes);
let links = [];
raw_data.forEach((row,i) => {
for (var k in row) {
links.push({source: raw_data.columns[i]+"-src", target: k+"-dst", value: row[k]})
}
});
links = links.filter(link => link.value > 0);
return {nodes, links}
}
Insert cell
nStates = graph.nodes.length / 2;
Insert cell
height = 600
Insert cell
width = 954
Insert cell
margins = ({top: 5, bottom: 5, left: 30, right: 30});
Insert cell
format = d3.format(",.0f");
Insert cell
sankey = {
const sankey = d3.sankey()
.nodeId(d => d.name)
.nodeAlign(d3[`sankey${align[0].toUpperCase()}${align.slice(1)}`])
.nodeWidth(15)
.nodePadding(10)
.nodeSort(null)
.extent([[margins.left, margins.top], [width - margins.right, height - margins.bottom]]);
return ({nodes, links}) => sankey({
nodes: nodes.map(d => Object.assign({}, d)),
links: links.map(d => Object.assign({}, d))
});
}
Insert cell
sankey(graph)
Insert cell
edgeColor = "path";
Insert cell
align = "justify";
Insert cell
sankey(graph)
Insert cell
color = d3.scaleOrdinal()
.domain(d3.range(nStates))
.range(d3.range(nStates).map(x => d3.interpolatePlasma(x/nStates)))
// interpolateYlGnBu, interpolateSpectral, interpolateRdYlGn, interpolateInferno, interpolateTurbo, interpolatePlasma, interpolateCool, interpolateRainbow
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const {nodes, links} = sankey(graph);
svg.append("text")
.attr("x", width-(margins.right/3))
.attr("y", height/2)
.attr("text-anchor", "middle")
.style("font-family", "Helvetica")
.style("font-size", "14px")
.attr("transform", `rotate(-90,${width-(margins.right/3)},${height/2})`)
.text("Number of people treated");
svg.append("text")
.attr("x", margins.right/2)
.attr("y", height/2)
.attr("text-anchor", "middle")
.style("font-family", "Helvetica")
.style("font-size", "14px")
.attr("transform", `rotate(-90,${margins.right/2},${height/2})`)
.text("Number of people requiring hospitalization");

svg.append("g")
.selectAll("rect")
.data(nodes)
.join("rect")
.attr("x", d => d.x0)
.attr("y", d => d.y0)
.attr("height", d => d.y1 - d.y0)
.attr("width", d => d.x1 - d.x0)
.attr("fill", d => color(d.index<nStates ? d.index : d.index-nStates))
.append("title")
.text(d => `${d.name}\n${format(d.value)}`);

const link = svg.append("g")
.attr("fill", "none")
.attr("stroke-opacity", 0.5)
.selectAll("g")
.data(links)
.join("g")
.style("mix-blend-mode", "multiply");

if (edgeColor === "path") {
const gradient = link.append("linearGradient")
.attr("id", d => (d.uid = DOM.uid("link")).id)
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", d => d.source.x1)
.attr("x2", d => d.target.x0);

gradient.append("stop")
.attr("offset", "0%")
.attr("stop-color", d => color(d.source.index));

gradient.append("stop")
.attr("offset", "100%")
.attr("stop-color", d => color(d.target.index-nStates));
}

link.append("path")
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke", d => edgeColor === "none" ? "#aaa"
: edgeColor === "path" ? d.uid
: edgeColor === "input" ? color(d.index)
: color(d.target))
.attr("stroke-width", d => Math.max(1, d.width));

link.append("title")
.text(d => `${d.source.name} → ${d.target.name}\n${format(d.value)}`);

svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 11)
.selectAll("text")
.data(nodes)
.join("text")
.attr("x", d => d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6)
.attr("y", d => (d.y1 + d.y0) / 2)
.attr("dy", "0.35em")
.attr("text-anchor", d => d.x0 < width / 2 ? "start" : "end")
.text(d => d.name.substring(0,2));

return svg.node();
}
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