Published
Edited
Mar 9, 2020
Insert cell
md`
# Sankey diagram showing user/role access v2
A role is owned by one and only one user, while can be viewed by multiple
`
Insert cell
chart = {
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 => d.color)
.append("title")
.text(d => d.name);

const link = svg.append("g")
.attr("fill", "none")
.attr("stroke-opacity", 0.6)
.selectAll("g")
.data(links.sort((a,b) => accessHash[b.access].order - accessHash[a.access].order))
.join("g")
.style("mix-blend-mode", "normal");

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

link.append("title")
.text(d => `${d.target.name} ${d.is} ${d.source.name}`);

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.4em")
.attr("text-anchor", d => d.x0 < width / 2 ? "start" : "end")
.text(d => d.name);

return svg.node();
}
Insert cell
accessHash = ({
owner: {color: 'blue', is: 'owned by', order: 0},
visible: {color: 'green', is: 'visible by', order: 1},
hidden: {color: 'gray', is: 'hidden from', order: 2},
})
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', color: 'orange'}));
const userNames = ['John', 'Tom', 'Jack', 'Pam', 'Jill', 'Chad', 'Bill', 'Mike', 'Dana', 'Mary'];
const users = userNames.map((name, id) => ({id: id + 10, name, type: 'user', color: 'red' }));
const nodes = [...roles, ...users];
const n = (name) => nodes.find(n => n.name === name).id;
const l = (source, target, access, value = 1) =>
({
access,
source: n(source),
target: n(target),
value,
is: accessHash[access].is,
color: accessHash[access].color,
});
const links = roleNames.flatMap(role => {
const shuffled = shuffle(userNames).slice(Math.trunc(Math.random()*3));
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, 'owner'),
...vis.map(usr => l(usr, role, 'visible')),
...hid.map(usr => l(usr, role, 'hidden')),
];
});
return { nodes, links}
}
Insert cell
width = 800;
Insert cell
height = 500;
Insert cell
sankey = {
const sankey = d3.sankey()
.nodeId(d => d.id)
.nodeWidth(10)
.nodePadding(10)
.nodeAlign(d3.sankeyJustify)
.linkSort((a,b) => accessHash[a.access].order - accessHash[b.access].order)
.extent([[1, 1], [width - 1, height - 1]]);
return ({nodes, links}) => sankey({
nodes: nodes.map(d => Object.assign({}, d)),
links: links.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