Published
Edited
Aug 25, 2020
Insert cell
Insert cell
Insert cell
Insert cell
nodesExt = graph.nodes.flatMap(n => {
return [
n.is_eyeball && {
name: `${n.id}-eb`,
type: `eb`,
desc: n.name,
weight: n.weight
},
n.location && {
name: `${n.id}-loc`,
type: `loc`,
desc: n.name,
weight: n.weight
},
!n.is_eyeball && !n.location && {
name: `${n.id}-int`,
type: `int`,
desc: String(n.id),
weight: n.weight
}
].filter(l => l)
})
Insert cell
linksExt = graph.links
.map(l => ({
...l,
source: graph.nodes.some(n => n.id === l.source && n.is_eyeball) && `${l.source}-eb` || graph.nodes.some(n => n.id === l.source && n.location) && `${l.source}-loc` || null,
target: nodesExt.some(n => n.name === `${l.target}-loc`) && `${l.target}-loc` || `${l.target}-int`,
value: nodesExt.some(n => n.name === `${l.source}-eb`) && nodesExt.find(n => n.name === `${l.source}-eb`).weight || 1.0,
is_eyeball: graph.nodes.some(n => n.id === l.source && n.is_eyeball)
}))
// .filter(l => (l.source !== l.target) && l.source !== null).slice(0,26)
.filter(l => l.is_eyeball)
Insert cell
[...new Set(linksExt.map(l => l.target))]
Insert cell
data = ({
nodes: nodesExt,
links: linksExt
})
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

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", color)
.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");

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));

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

link.append("path")
.attr("d", d3.sankeyLinkHorizontal())
.attr("stroke", d => edgeColor === "none" ? "#aaa"
: edgeColor === "path" ? d.uid
: edgeColor === "input" ? color(d.source)
: color(d.target))
.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")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.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
sankey = {
const sankey = d3.sankey()
.nodeId(d => d.name)
.nodeAlign(d3[`sankey${align[0].toUpperCase()}${align.slice(1)}`])
.nodeWidth(15)
.nodePadding(10)
.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 format = d3.format(",.0f");
return data.units ? d => `${format(d)} ${data.units}` : format;
}
Insert cell
color = {
const color = d3.scaleOrdinal(d3.schemeCategory10);
return d => color(d.category === undefined ? d.name : d.category);
}
Insert cell
data_orig = {
const links = d3.csvParse(await FileAttachment("energy.csv").text(), d3.autoType);
const nodes = Array.from(new Set(links.flatMap(l => [l.source, l.target])), name => ({name, category: name.replace(/ .*/, "")}));
return {nodes, links, units: "TWh"};
}
Insert cell
hege = await d3.json("https://sg-pub.ripe.net/emile/as-pop-hege-by/hege_clean.json");
Insert cell
graph = await d3.json("https://sg-pub.ripe.net/emile/as-pop-hege-by/graph.json");
Insert cell
width = 954
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