Published
Edited
Apr 14, 2022
1 fork
Insert cell
Insert cell
Insert cell
data = FileAttachment("emissions.json").json()
Insert cell
Insert cell
Insert cell
d3WithSankey = require("d3@6", "d3-sankey@0.12")
Insert cell
d3WithSankey.sankey()
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
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
mySankey = d3WithSankey.sankey()
.size([width, height])
.nodeId(d => d.index)
.nodePadding(10)
.nodeWidth(10)
.extent([[0, 5], [width, height-5]])
Insert cell
Insert cell
sankeyData = mySankey(data)
Insert cell
sankeyNodes = sankeyData["nodes"];
Insert cell
Insert cell
Insert cell
Insert cell
color = (value) => {
return d3WithSankey.interpolatePuRd(value);
}
Insert cell
sankeyNodes.forEach((node) => {
node.color = color(node.index / sankeyNodes.length);
})
Insert cell
rectangles = {
svg.append("g")
.selectAll("rect")
.data(sankeyNodes)
.join("rect")
.attr("x", d => d.x0 + 1)
.attr("y", d => d.y0)
.attr("width", d => d.x1 - d.x0 - 2)
.attr("height", d => d.y1 - d.y0)
.attr("fill", d => d.color)
.append("title")
.text(d => `${d.name}\n${d.value.toLocaleString()}`);
}
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")
.attr("x", d => d.x0 < width / 2 ? d.x1 + 6 : d.x0 - 6)
.attr("y", d => (d.y1 + d.y0) / 2 + 3)
.attr("text-anchor", d => d.x0 < width / 2 ? "start" : "end")
.text(d => d.name)

}
Insert cell
Insert cell
defs = svg.append('defs');
Insert cell
//code borrowed from https://bl.ocks.org/micahstubbs/3c0cb0c0de021e0d9653032784c035e9
links.style('stroke', (d, i) => {
console.log('d from gradient stroke func', d);

// make unique gradient ids
const gradientID = `gradient${i}`;

const startColor = d.source.color;
const stopColor = d.target.color;

console.log('startColor', startColor);
console.log('stopColor', stopColor);

const linearGradient = defs.append('linearGradient')
.attr('id', gradientID);

linearGradient.selectAll('stop')
.data([
{offset: '10%', color: startColor },
{offset: '90%', color: stopColor }
])
.enter().append('stop')
.attr('offset', d => {
console.log('d.offset', d.offset);
return d.offset;
})
.attr('stop-color', d => {
console.log('d.color', d.color);
return d.color;
});

return `url(#${gradientID})`;
})
Insert cell
container.node()
Insert cell
FileAttachment("emissions.json").json()
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