chart = {
const sankeyData = {
nodes: [],
links: []
};
sankeyData.nodes.push({ name: "Total Amount" });
data.forEach(d => {
let targetNode = sankeyData.nodes.find(node => node.name === d.loanorigid);
if (!targetNode) {
targetNode = { name: d.loanorigid };
sankeyData.nodes.push(targetNode);
}
const targetIndex = sankeyData.nodes.indexOf(targetNode);
sankeyData.links.push({
source: 0,
target: targetIndex,
value: d.price
});
});
console.log("Sankey Data:", sankeyData);
const color = d3.scaleOrdinal(d3.schemeCategory10);
const width = 600, height = 400;
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);
const sankey = d3Sankey.sankey()
.nodeWidth(15)
.nodePadding(10)
.extent([[1, 1], [width - 1, height - 5]])
.size([width, height]);
const sankeyGraph = sankey(sankeyData);
svg.append("g")
.selectAll("path")
.data(sankeyGraph.links)
.join("path")
.attr("d", d3Sankey.sankeyLinkHorizontal())
.style("fill", "none")
.style("stroke", d => color(d.target.name))
.style("stroke-opacity", 0.5)
.style("stroke-width", d => Math.max(1, d.width));
svg.append("g")
.selectAll("rect")
.data(sankeyGraph.nodes)
.join("rect")
.attr("x", d => d.x0)
.attr("y", d => d.y0)
.attr("height", d => d.y1 - d.y0)
.attr("width", sankey.nodeWidth())
.style("fill", d => color(d.name));
const formatCurrency = d3.format("$,.2f");
svg.append("g")
.selectAll("text")
.data(sankeyGraph.nodes)
.join("text")
.attr("x", d => ((d.x0 + d.x1) / 2) - 100)
.attr("y", d => (d.y0 + d.y1) / 2)
.text(d => `${d.name} Total: ${formatCurrency(d.value)}`)
.attr("font-size", "13px")
.attr("text-anchor", "middle")
.attr("alignment-baseline", "middle")
.style("fill", "black");
return svg.node();
}