graph = {
let paths = data
.filter((d) => importers.has(d.target))
.map((d) => Object.assign({}, d));
paths.forEach((d) => (d.stops = []));
const nodes = Array.from(
new Set(paths.flatMap((d) => [d.source, ...d.stops, d.target])),
(iso) => {
const [x, y] = centroid(iso);
return { id: iso, x, y };
}
);
let links = [],
linkValues = new Map();
for (let path of paths) {
const stops = [path.source, ...path.stops, path.target];
let pairs = d3
.pairs(stops)
.map(([source, target]) => ({ source, target, value: path.value }));
for (let pair of pairs) {
const key = `${pair.source} - ${pair.target}`;
const value = linkValues.get(key) || 0;
if (!value) links.push(pair);
linkValues.set(key, value + pair.value);
}
}
links.forEach((d) => (d.value = linkValues.get(`${d.source} - ${d.target}`)));
return { nodes, links };
}