This code prepares node and edge data for hierarchical visualization by establishing parent-child relationships and propagating branch information, effectively structuring the data as a tree.
npm run import -- "Format d3 tree"
function formatNodes(nodes, edges) {
var nodeMap = {};
nodes.forEach(function (x) {
if (typeof x.size == 'undefined') {
x.size = 1;
}
nodeMap[x.name] = x;
});
edges.forEach(function (x) {
if (typeof nodeMap[x.source].children == 'undefined') {
nodeMap[x.source].children = [];
}
nodeMap[x.target].parent = nodeMap[x.source];
nodeMap[x.source].children.push(nodeMap[x.target]);
// return { parent: nodeMap[x.target], source: nodeMap[x.source], target: nodeMap[x.target] };
});
edges.forEach(function (x) {
if (typeof nodeMap[x.target].branch != 'undefined' && nodeMap[x.target].branch != ''
&& typeof nodeMap[x.source].branch == 'undefined' || nodeMap[x.source].branch == '') {
nodeMap[x.source].branch = nodeMap[x.target].branch;
}
});
return nodes;
};
module.exports = formatNodes;
/**
* Formats node and edge data for graph representation.
* @param {Object[]} nodes - Array of node objects.
* @param {Object[]} edges - Array of edge objects.
* @returns {Object[]} Formatted nodes with added properties.
*/
function formatNodes(nodes, edges) {
// Create a node map for efficient lookup by name
const nodeMap = {};
// Initialize node properties and add to map
nodes.forEach((node) => {
if (typeof node.size === 'undefined') {
node.size = 1;
}
nodeMap[node.name] = node;
});
// Establish parent-child relationships between nodes
edges.forEach((edge) => {
// Get the source and target nodes from the map
const sourceNode = nodeMap[edge.source];
const targetNode = nodeMap[edge.target];
// Handle target node's parent and children
if (!targetNode.parent) {
targetNode.parent = sourceNode;
}
if (!sourceNode.children) {
sourceNode.children = [];
}
sourceNode.children.push(targetNode);
});
// Transfer branch information from target to source nodes
edges.forEach((edge) => {
const sourceNode = nodeMap[edge.source];
const targetNode = nodeMap[edge.target];
// Only transfer branch if it's not already set or empty
if (targetNode.branch && (!sourceNode.branch || sourceNode.branch === '')) {
sourceNode.branch = targetNode.branch;
}
});
// Return the formatted nodes
return nodes;
}
module.exports = formatNodes;
This code processes a set of nodes and edges to prepare them for hierarchical visualization, likely a tree-like structure.
Here's a breakdown:
Initialization:
nodeMap
object to store nodes with additional properties.Node Processing:
nodes
array.size
property is missing, it sets it to 1.nodeMap
using its name
as the key.Edge Processing:
edges
array.children
array, it creates one.parent
property of the target node to the source node.Branch Propagation:
edges
array again.branch
property and the source node doesn't, it assigns the target node's branch
to the source node.Return:
nodes
array, now enriched with parent-child relationships and potentially branch information.In essence, this code transforms raw node and edge data into a hierarchical structure suitable for visualization as a tree diagram.