Published
Edited
Apr 21, 2022
1 fork
1 star
Insert cell
Insert cell
container = d3.select(DOM.svg(width, height));
Insert cell
svg3 = container.append("g")
// svg.append("...")
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
container.node()
Insert cell
Insert cell
d3 = require("d3@6")
Insert cell
d3Sankey = require("d3-sankey@0.12")
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = FileAttachment("titanic.json").json()
Insert cell
Insert cell
mySankey = d3Sankey.sankey()
.size(size)
.nodeId(d => d.name)
.nodePadding(nodePadding)
.nodeWidth(nodeWidth)
Insert cell
Insert cell
color = (value) => {
return d3.interpolateRainbow(value);
}
Insert cell
Insert cell
graph = {
const graph = mySankey({...data});
graph.nodes.forEach((node) => {
node.color = color(node.index / graph.nodes.length);
});
return graph;
}
Insert cell
Insert cell
originalData = FileAttachment("titanic.json").json()
Insert cell
data
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
svg2 = {
// Create a container to hold our elements.
const svg = d3.select(DOM.svg(width, height));
// We then add a view to hold the drawings.
const view = svg.append("g")
.classed("view", true)
.attr("transform", `translate(${margin}, ${margin})`);
// Now, we define the nodes using rectangles (rect)
const nodes = view.selectAll("rect.node")
.data(graph.nodes)
.join("rect")
.attr("x", d => d.x0)
.attr("y", d => d.y0)
.attr("width", d => d.x1 - d.x0) // Defining the width by the difference
.attr("height", d => Math.max(1, d.y1 - d.y0)) // Defining the height by the difference
.attr("fill", d => d.color)
.attr("opacity", 0.9);
// Add titles for node hover effects.
nodes.append("title").text(d => `${d.name}\n${(d.value)}`);
// Add text labels.
view.selectAll("text.node")
.data(graph.nodes)
.join("text")
.attr("x", d => d.x1)
.attr("dx", 6)
.attr("y", d => (d.y1 + d.y0) / 2)
.attr("dy", "0.35em")
.attr("fill", "black")
.attr("text-anchor", "start")
.attr("font-size", 10)
.attr("font-family", "Arial, sans-serif")
.text(d => d.name)
.filter(d => d.x1 > width / 2)
.attr("x", d => d.x0)
.attr("dx", -6)
.attr("text-anchor", "end");
// Define the gray links.
const links = view.selectAll("path.link")
.data(graph.links)
.join("path")
.attr("d", d3Sankey.sankeyLinkHorizontal())
.attr("stroke", "black")
.attr("stroke-opacity", 0.1)
.attr("stroke-width", d => Math.max(1, d.width))
.attr("fill", "none");
// Add hover effect on the links.
links.append("title").text(d => `${d.source.name} ${d.target.name}\n${(d.value)}`);
function branchAnimate(evt, node) {
if (node === undefined) node = evt;
let links = view.selectAll("path")
.filter((link) => {
return node.sourceLinks.indexOf(link) !== -1;
});

let nextNodes = [];
links.each((link) => {
nextNodes.push(link.target);
});
links
.transition()
.attr("stroke-opacity", 0.5)
.duration(duration)
.ease(d3.easeLinear)
.attr("stroke-dashoffset", 0)

// What is the difference here?
.transition()
.duration(500)
.attr("stroke-opacity", 0.8);
}
function branchClear() {
links.attr("stroke", "black")
.attr("stroke-opacity", 0.1)
.attr("stroke-width", d => Math.max(1, d.width))
.attr("fill", "none");
}

nodes.on("mouseover", branchAnimate)
.on("mouseout", branchClear);
return svg.node();
}
Insert cell
Insert cell
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