Published
Edited
Feb 23, 2021
1 fork
2 stars
Insert cell
Insert cell
Insert cell
viewof selection = {
const files = new Map([
FileAttachment("THRESHOLD_9.json"),
FileAttachment("THRESHOLD_8.json"),
FileAttachment("THRESHOLD_7.json"),
FileAttachment("THRESHOLD_6.json"),
FileAttachment("THRESHOLD_5.json"),
FileAttachment("THRESHOLD_4.json"),
FileAttachment("THRESHOLD_3.json"),
FileAttachment("THRESHOLD_2.json"),
FileAttachment("THRESHOLD_1.json")
].map(f => [f.name, f]));
const form = select({
description: "Select Dataset",
options: Array.from(files.keys()),
value: "THRESHOLD_6.json"
});
return Object.defineProperty(html`<div>${form}`, 'value', {get() { return files.get(form.value) }});
}
Insert cell
data = selection.json()

Insert cell
chart = {
const svg = d3.select(DOM.svg(width, height))
.style("width", "100%")
.style("height", "100%")
.style("font-size", "9px");

const {nodes, links} = sankey(data);

svg.append("g")
.attr("stroke", "#000")
.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.name))
.append("title")
.text(d => `${d.name}\n${format(d.value)}`)
const link = svg.append("g")
.attr("fill", "none")
.attr("stroke-opacity", 0.35)
.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.name));

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

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

link.append("title")
.text(d => `${d.source.name} → ${d.target.name}\n${format(d.value)}`)
.style("font-size", "9px");

svg.append("g")
.style("font", "24px 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)
.style("font-size", "9px");

return svg.node();
}
Insert cell
Insert cell
viewof align = Object.assign(html`<select>
<option value=left>Left-aligned
<option value=right>Right-aligned
<option value=center>Centered
<option value=justify selected>Justified
</select>`, {
value: new URLSearchParams(html`<a href>`.search).get("align") || "left"
})
Insert cell
sankey = {
const sankey = d3.sankey()
.nodeAlign(d3[`sankey${align[0].toUpperCase()}${align.slice(1)}`])
.nodeWidth(15)
.nodePadding(10)
.nodeSort(null)
.extent([[1, 5], [width - 1, height - 5]]);
return ({nodes, links}) => sankey({
nodes: nodes.map(d => Object.assign({}, d)),
links: links.map(d => Object.assign({}, d))
});
}
Insert cell
format = {
const f = d3.format(",.0f");
return d => `${f(d)}`;
}
Insert cell
color = {
const color = d3.scaleOrdinal(d3.quantize(d3.interpolateHcl("#FFAD05", "#1DA1F2"), 8));
return name => color(name.replace(/ .*/, ""));
}
Insert cell
width = 975
Insert cell
height = 600
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