Published
Edited
Sep 23, 2019
1 fork
1 star
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

const {nodes, links} = sankey(data);
svg.append("g")
.attr("stroke", "none")
.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 => d.type === "yes" ? "#0073C8" : "#FDB90B")
.append("title")
.text(d => `${d.name}\n${d.value} patients`);
const link = svg.append("g")
.attr("fill", "none")
.selectAll("g")
.data(links)
.join("g")
.attr("class", "link")
.style("mix-blend-mode", "multiply")
//.style("opacity", 0.65);
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 => d.source.type === "no" ? "#FDC73B" : "#4C9DD8");

gradient.append("stop")
.attr("offset", "100%")
.attr("stop-color", d => d.target.type === "no" ? "#FDC73B" : "#4C9DD8");

link.append("path")
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke", d => d.uid)
.attr("stroke-width", d => Math.max(1, d.width));

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

svg.append("g")
.style("font", "10px sans-serif")
.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}\n${d.value.toLocaleString()} patients`)

return svg.node()
//return nodes
}
Insert cell
color = {
const color = d3.scaleOrdinal(d3.schemeCategory10);
return name => color(name.replace(/ .*/, ""));
}
Insert cell
sankey = {
const sankey = d3.sankey()
.nodeId(d => d.name)
//.nodeSort(inputOrder ? null : undefined)
.nodeWidth(35)
.nodePadding(35)
.extent([[0, 5], [width, height - 5]]);
return ({nodes, links}) => sankey({
nodes: nodes.map(d => Object.assign({}, d)),
links: links.map(d => Object.assign({}, d))
});
}
Insert cell
Insert cell
Insert cell
Insert cell
function process(data) {

//set up graph in same style as original example but empty
let graph = {"nodes" : [], "links" : []};

data.forEach(function (d) {
graph.nodes.push({ "name": d.source, "type": d.source_type });
graph.nodes.push({ "name": d.target, "type": d.target_type });
graph.links.push({ "source": d.source,
"target": d.target,
"value": +d.value });
});
//only keep unique nodes
graph.nodes = Array.from(new Set(graph.nodes.map(d => d.name))).map(name => {return {name: name, type: graph.nodes.find(s => s.name === name).type};});
//graph.nodes.sort((a, b) => a.name.localeCompare(b.name));
return graph
}
Insert cell
html`
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">

<style>
.text {
font-family: "Lato";
}
.link {
opacity: 0.4
}
.link:hover {
opacity: 0.8
}
</style>`
Insert cell
data = process(links_data)
Insert cell
d3 = require("d3@5", "d3-sankey@0.12")
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