function SankeyChart({
nodes,
links
}, {
format = ",",
nodeId = d => d.id,
nodeGroup,
nodeGroups,
nodeLabel,
nodeTitle = d => `${d.id}\n${format(d.value)}`,
nodeAlign = d3Sankey.sankeyLeft,
nodeWidth = 50,
nodePadding = 10,
nodeLabelPadding = 5,
nodeStroke = "currentColor",
nodeStrokeWidth,
nodeStrokeOpacity,
nodeStrokeLinejoin,
linkSource = ({source}) => source,
linkTarget = ({target}) => target,
linkValue = ({value}) => value,
linkPath = straightLine,
linkTitle = d => `${d.source.id} → ${d.target.id}\n${format(d.value)}`,
linkColor = 'layered',
linkStrokeOpacity = 0.5,
linkMixBlendMode = "multiply",
colors = colorScale,
width = 1000,
height = 400,
marginTop = 5,
marginRight = 1,
marginBottom = 5,
marginLeft = 1,
} = {}) {
const LS = d3.map(links, linkSource).map(intern);
const LT = d3.map(links, linkTarget).map(intern);
const LV = d3.map(links, linkValue);
if (nodes === undefined) nodes = Array.from(d3.union(LS, LT), id => ({id}));
const N = d3.map(nodes, nodeId).map(intern);
const G = nodeGroup == null ? null : d3.map(nodes, nodeGroup).map(intern);
// Replace the input nodes and links with mutable objects for the simulation.
nodes = d3.map(nodes, (_, i) => ({id: N[i]}));
links = d3.map(links, (_, i) => ({source: LS[i], target: LT[i], value: LV[i]}));
// Compute default domains.
if (G && nodeGroups === undefined) nodeGroups = G;
// Construct the scales.
const color = nodeGroup == null ? null : d3.scaleOrdinal(nodeGroups, colors);
// Compute the Sankey layout.
const sankey = d3Sankey
.sankey()
.nodeId(({index: i}) => N[i])
.nodeAlign(nodeAlign)
.nodeWidth(nodeWidth)
.nodePadding(nodePadding)
.nodeSort(null)
.iterations(10000)
.linkSort((a, b) => {
const targetIndex = target => nodeData.data.findIndex(node => node.id === target)
if (a.source === b.source) {
return targetIndex(a.target.id) - targetIndex(b.target.id)
}
if (a.target === b.target) {
return targetIndex(a.source.id) - targetIndex(b.source.id)
}
})
.extent([[marginLeft, marginTop], [width - marginRight, height - marginBottom]])
({nodes, links});
// Compute titles and labels using layout nodes, so as to access aggregate values.
if (typeof format !== "function") format = d3.format(format);
const Tl = nodeLabel === undefined ? N : nodeLabel == null ? null : d3.map(nodes, nodeLabel);
const Tt = nodeTitle == null ? null : d3.map(nodes, nodeTitle);
const Lt = linkTitle == null ? null : d3.map(links, linkTitle);
// A unique identifier for clip paths (to avoid conflicts).
const uid = `O-${Math.random().toString(16).slice(2)}`;
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height])
.attr("style", "max-width: 100%; height: auto; height: intrinsic;");
const node = svg.append("g")
.attr("stroke", nodeStroke)
.attr("stroke-width", nodeStrokeWidth)
.attr("stroke-opacity", nodeStrokeOpacity)
.attr("stroke-linejoin", nodeStrokeLinejoin)
.selectAll("rect")
.data(nodes)
.join("rect")
.attr("x", d => d.x0)
.attr("y", d => d.y0)
.attr("height", d => d.y1 - d.y0)//,30)
//.attr("height", d => Math.max(d.y1 - d.y0,30))
.attr("width", d => d.x1 - d.x0);
if (G) node.attr("fill", ({index: i}) => color(G[i]));
if (Tt) node.append("title").text(({index: i}) => Tt[i]);
const link = svg.append("g")
.attr("fill", "none")
.attr("stroke-opacity", linkStrokeOpacity)
.selectAll("g")
.data(links)
.join("g")
.style("mix-blend-mode", linkMixBlendMode);
var totalNodes = 16;
link.append("path")
.attr("d", linkPath)
.attr("stroke", function(d) {
if (linkColor === "layered") {
// Use the color of the target node for the last two nodes
if (d.target.index >= totalNodes - 2) {
return color(G[d.target.index]);
} else {
// Use the color of the source node for other nodes
return color(G[d.source.index]);
}
} else if (linkColor === "source-target") {
return `url(#${uid}-link-${d.index})`;
} else if (linkColor === "source") {
return color(G[d.source.index]);
} else if (linkColor === "target") {
return color(G[d.target.index]);
} else {
// Handle other cases or default behavior here
return linkColor;
}
})
.attr("stroke-width", ({width}) => Math.max(1, width))
.attr('stroke-linejoin', 'bevel')
.call(Lt ? path => path.append("title").text(({index: i}) => Lt[i]) : () => {});
if (Tl) {
svg.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("text")
.data(nodes)
.join("text")
.attr("x", d => (d.x0 + d.x1) / 2)
.attr("y", d => (d.y1 + d.y0) / 2)
.attr("dy", "0.35em")
.attr("text-anchor", "middle")
.text(({ index: i }) => Tl[i])
.selectAll("tspan")
.data(({ index: i }) => Tl[i].split(/\s+/).slice(1))
.join("tspan")
.attr("x", d => (d.x0 + d.x1) / 2) // Center each line horizontally
.attr("dy", "1.1em")
.text(d => d);
}
function intern(value) {
return value !== null && typeof value === "object" ? value.valueOf() : value;
}
// Relative to container/ node rect
node
.append("text")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.attr("x", (d) => (d.x0 < width / 2 ? 6 + (d.x1 - d.x0) : -6)) // +/- 6 pixels relative to container
.attr("y", (d) => (d.y1 - d.y0) / 2) // middle of node
.attr("dy", "0.35em")
.attr("text-anchor", (d) => (d.x0 < width / 2 ? "start" : "end"))
.text((d) => d.name);
// d3V6 events: https://observablehq.com/@d3/d3v6-migration-guide#events
node
.attr("cursor", "move")
.call(d3.drag().on("start", dragStart).on("drag", dragMove));
function dragStart(event, d) {
d.__x = event.x;
d.__y = event.y;
d.__x0 = d.x0;
d.__y0 = d.y0;
d.__x1 = d.x1;
d.__y1 = d.y1;
} //dragStart
function dragMove(event, d) {
d3.select(this).attr("transform", function (d) {
const dx = event.x - d.__x;
const dy = event.y - d.__y;
d.x0 = d.__x0 + dx;
d.x1 = d.__x1 + dx;
d.y0 = d.__y0 + dy;
d.y1 = d.__y1 + dy;
if (d.x0 < 0) {
d.x0 = 0;
d.x1 = nodeWidth;
} // if
if (d.x1 > width) {
d.x0 = width - nodeWidth;
d.x1 = width;
} // if
if (d.y0 < 0) {
d.y0 = 0;
d.y1 = d.__y1 - d.__y0;
} // if
if (d.y1 > height) {
d.y0 = height - (d.__y1 - d.__y0);
d.y1 = height;
} // if
return `translate(${d.x0}, ${d.y0})`;
}); //.attr('transform', function (d) {
// https://github.com/d3/d3-sankey#sankey_update
sankey.update({ nodes, links });
link.selectAll(".link").attr("d", linkPath);
}
return Object.assign(svg.node(), {scales: {color}});
}