data = {
const links = d3.csvParseRows(source, ([source, target, value, color]) =>
source && target
? {
source,
target,
value: !value || isNaN((value = +value)) ? 1 : value,
color
}
: null
);
const nodeByName = new Map();
for (const link of links) {
if (!nodeByName.has(link.source))
nodeByName.set(link.source, { name: link.source });
if (!nodeByName.has(link.target))
nodeByName.set(link.target, { name: link.target });
}
const nodes = Array.from(nodeByName.values());
nodes.forEach((d) => {
let thisLink = links.filter((e) => e.target === d.name);
if (thisLink.length === 0)
thisLink = links.filter((e) => e.source === d.name);
d.color = thisLink[0].color;
});
return { nodes, links };
}