Public
Edited
Mar 5
Fork of Untitled
Insert cell
md`# Sankey diagram showing user access to different roles`
Insert cell
viewof check_access = Inputs.checkbox(["owned", "visible", "hidden"], {label: "Select some", value: ["owned", "visible", "hidden"]})
Insert cell
chart = {
const edgeColor = 'none';
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

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

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.5)
.selectAll("g")
.data(links)
.join("g")
.style("mix-blend-mode", "multiply");

link.append("path")
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke", d => d.color)
.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")
.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);

return svg.node();
}
Insert cell
color = {
const color = d3.scaleOrdinal(d3.schemeCategory10);
return name => color(name.replace(/ .*/, ""));
}
Insert cell
format = {
const f = d3.format(",.0f");
return d => `${f(d)} TWh`;
}
Insert cell
accessHash = ({
owned: {color: 'blue'},
visible: {color: 'green'},
hidden: {color: 'gray'},
})
Insert cell
shuffle = {
return (arr) => [...arr].sort((a,b) => Math.random() - 0.5);
}
Insert cell
elm = shuffle(['John', 'Tom', 'Jack', 'Pam', 'Jill', 'Chad', 'Bill', 'Mike', 'Dana', 'Mary'])
Insert cell
graph = {
const roleNames = ['CEO', 'CFO', 'CTO', 'COO', 'CMO'];
const roles = roleNames.map((name, id) => ({id: id + 1, name, type: 'role'}));
const userNames = ['John', 'Tom', 'Jack', 'Pam', 'Jill', 'Chad', 'Bill', 'Mike', 'Dana', 'Mary'];
const users = userNames.map((name, id) => ({id: id + 10, name, type: 'user' }));
const nodes = [...roles, ...users];//, ...accesses];
const n = (name) => nodes.find(n => n.name === name).id;
const l = (source, target, access) => [
{source: n(source), target: n(target), value: 1, color: accessHash[access].color, access},
// {source: n(an(target, access)), target: n(target), value: 1, color: accessHash[access].color},
];

const links = roleNames.flatMap(role => {
const shuffled = shuffle(userNames);
const own = shuffled[0];
const part = Math.trunc(Math.random()*(shuffled.length-2)) + 1;
const vis = shuffled.slice(1, part);
const hid = shuffled.slice(part);
return [
...l(own, role, 'owned'),
...vis.flatMap(usr => l(usr, role, 'visible')),
...hid.flatMap(usr => l(usr, role, 'hidden')),
];
});
return { nodes, links}
}
Insert cell
width = 800;
Insert cell
height = 600;
Insert cell
sankey = {
const sankey = d3.sankey()
.nodeId(d => d.id)
.nodeAlign(d3.sankeyJustify)
.nodeWidth(15)
.nodePadding(10)
.extent([[1, 5], [width - 1, height - 5]]);
return ({nodes, links}) => sankey({
nodes: nodes.map(d => Object.assign({}, d)),
links: links.filter(l => check_access.includes(l.access)).map(d => Object.assign({}, d))
});
}
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