Published
Edited
Apr 13, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = FileAttachment("titanic@1.json").json()
Insert cell
Insert cell
Insert cell
Insert cell
margin = ({top: 20, bottom: 20, left: 30, right: 30});
Insert cell
height = 500 - margin.top - margin.bottom;
Insert cell
width = 800 - margin.left - margin.right;
Insert cell
mynode = {
const svg = d3.select(DOM.svg(width, height))
.style("background", "#fff")
.style("width", "100%")
.style("height", "auto")

return svg.node()
}
Insert cell
Insert cell
Insert cell
container = {
return d3WithSankey.select(DOM.svg(width, height))
}
Insert cell
svg = container.append("g")
.style("background", "#fff")
.style("width", "100%")
.style("height", "auto")
Insert cell
Insert cell
Insert cell
d3WithSankey = require("d3@6", "d3-sankey@0.12")
Insert cell
Insert cell
d3WithSankey.sankey()
Insert cell
Insert cell
mySankey = d3WithSankey.sankey()
.size([width, height])
.nodeId(d => d.name)
.nodePadding(10)
.nodeWidth(20)
.extent([[0, 5], [width, height-5]])
Insert cell
Insert cell
Insert cell
sankeyData = mySankey(data)
Insert cell
sankeyNodes = sankeyData["nodes"]; // sankeyData.nodes
Insert cell
Insert cell
Insert cell
Insert cell
color = (value) => {
return d3WithSankey.interpolateBlues(value);
}
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
sankeyNodes.forEach((node) => {
node.color = color(node.index / sankeyNodes.length);
})
Insert cell
rectangles = {
svg.append("g")
.selectAll("rect")
.data(sankeyNodes) // sankeyData.nodes
.join("rect")
// Offset
.attr("x", d => d.x0 + 1)
.attr("y", d => d.y0)

// We find the difference between x1 and x0 so that we can get a width.
// Exercise: Try playing around with this!
.attr("width", d => d.x1 - d.x0 - 2)

// Similarly here, we find the difference.
.attr("height", d => d.y1 - d.y0)
.attr("fill", d => d.color)

// We add the small title when we hover over.
.append("title")
.text(d => `${d.name}\n${d.value.toLocaleString()}`);
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
svg.append("g")
.style("font", "10px sans-serif")
.attr("pointer-events", "none")
.selectAll("text")
.data(sankeyData.nodes)
.join("text")

// If the x0 is less than half the width, put it on the left, else on the right.
.attr("x", d => d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6)
.attr("y", d => (d.y1 + d.y0) / 2) //

// Anchor it at the start or end: what does text-anchor mean?
// Exercise: trying anchoring the text to different properties and see how it changes.
.attr("text-anchor", d => d.x0 < width / 2 ? "start" : "end")
.text(d => d.name)

}
Insert cell
Insert cell
container.node()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
const dataset = {
"nodes": [
{
id: 0,
name: "John Doe"
},
{
id: 1,
name: "Jane Doe"
},
{
id: 2,
name: "Steven Seagall"
}],

"links": [
{
"source": 0,
"target": 1,
"value": 50
},
{
"source": 1,
"target": 2,
"value": 20
}
]
}
}
Insert cell
Insert cell
d3 = require("d3@^5.8")
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