Public
Edited
Jan 27, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
//.attr("style", "background-color: #212126");

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

svg.append("g")
.attr("stroke", "#212126")
.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");

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", 16)
//.attr("fill", "#A6ACB0")
.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.0em")
.attr("text-anchor", d => d.x0 < width / 2 ? "start" : "end")
.text(d => d.name + " (" + d3.format(",.0f")(d.sum) + ")");

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(20)
.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(["#A88DF6", "#80C8FE", "#F98084", "#F99E71", "#ffe066"]);
const color = d3.scaleOrdinal(["#b3e6b3", "#d2a679", "#F98084", "#a6a6a6", "#805af2" ]);
//return d => color(d.category === undefined ? d.name : d.category);
return d=> color(d.code);
}
Insert cell
data = {
return {
nodes: [
{ name: "From 2xx", category: "from", code: "2xx", sum: 7115 },
{ name: "From 3xx", category: "from", code: "3xx", sum: 1292 },
{ name: "From 4xx", category: "from", code: "4xx", sum: 96 },
{ name: "From 5xx", category: "from", code: "5xx" , sum: 23},
{ name: "To 2xx", category: "to", code: "2xx", sum: 3563},
{ name: "To 3xx", category: "to", code: "3xx", sum: 4331 },
{ name: "To 4xx", category: "to", code: "4xx" , sum: 624},
{ name: "To 5xx", category: "to", code: "5xx" , sum: 8},
],
links: [
{ source: "From 2xx", target: "To 2xx", value: "3259" },
{ source: "From 2xx", target: "To 3xx", value: "3424" },
{ source: "From 2xx", target: "To 4xx", value: "424" },
{ source: "From 2xx", target: "To 5xx", value: "8" },
{ source: "From 3xx", target: "To 2xx", value: "292" },
{ source: "From 3xx", target: "To 3xx", value: "830" },
{ source: "From 3xx", target: "To 4xx", value: "170" },
{ source: "From 4xx", target: "To 2xx", value: "4" },
{ source: "From 4xx", target: "To 3xx", value: "62" },
{ source: "From 4xx", target: "To 4xx", value: "30" },
{ source: "From 5xx", target: "To 2xx", value: "8" },
{ source: "From 5xx", target: "To 3xx", value: "15" },
],
units: "instances"
}
}
Insert cell
width = 600
Insert cell
height = 400
Insert cell
d3 = require("d3@5", "d3-sankey@0.12")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more