2
votes

I have 2 CSV files, one that describes links for a tree (graph.csv - 2 columns: source and target) and other that is a dictionary for the nodes' names (nodes.csv - 2 columns: node_id, node_name). The CSV with the source/target refers the node_id column in the second CSV.

Now I want to use this second CSV to attach the node names (and eventually other properties) to the nodes in the tree. I already figured out how to append text to the node, but I can't figure out how to "cross" the data in the second CSV to append each node's name as text to the respective node.

I got this code working like I wanted it, but right now it only extracts the nodes from the first CSV according to an answer here in SO from Mike Bostock. Right now I can't figure out which node is which and I would like to use the second CSV to identify the nodes.

From what I can tell, this code apparently identifies the unique nodes in graph.csv, determines the root and then builds the tree. If there was a way to read from the first CSV and copy it changing the nodes' IDs with the nodes names in the second CSV, I think that would be the first step, but I can't execute it properly.

Example of the CSVs:

graph.csv:

source,target
1,2
1,3
1,4
2,5
2,6
11,22
14,21
3,7
3,8
4,9
4,10
5,11
5,12
5,13
6,14
6,15
7,16
8,17
9,18
10,19
10,20

nodes.csv:

node_id,node_name
1,Node1
2,Node2
3,Node3
4,Node4
5,Node5
6,Node6
7,Node7
8,Node8
9,Node9
10,Node10
11,Node11
12,Node12
13,Node13
14,Node14
15,Node15
16,Node16
17,Node17
18,Node18
19,Node19
20,Node20
21,Node21
22,Node22

The code so far is:

 

    var margin = {top: 40, right: 40, bottom: 40, left: 40}, width = 1024 - margin.left - margin.right, height = 768 - margin.top - margin.bottom;

    var tree = d3.layout.tree()
        .size([height, width]);

    var svg = d3.select("body").append("svg")
        .attr("width", width + margin.left + margin.right)
        .attr("height", height + margin.top + margin.bottom)
        .append("g")
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

    d3.csv("graph.csv", function(links) {
        var nodesByName = {};

        links.forEach(function(link) {
            var parent = link.source = nodeByName(link.source),
            child = link.target = nodeByName(link.target);
            if (parent.children) parent.children.push(child);
            else parent.children = [child];
        });

    var nodes = tree.nodes(links[0].source);

    svg.selectAll(".link")
        .data(links)
        .enter().append("line")
        .attr("class", "link")
        .attr("x1", function(d) { return d.source.y; })
        .attr("y1", function(d) { return d.source.x; })
        .attr("x2", function(d) { return d.target.y; })
        .attr("y2", function(d) { return d.target.x; });

    svg.selectAll(".node")
        .data(nodes)
            .enter().append("circle")
       .attr("class", "node")
            .attr("r", 10)
            .attr("cx", function(d) { return d.y; })
            .attr("cy", function(d) { return d.x; });

    svg.selectAll("text")     
            .data(nodes)
       .enter().append("text")
       .attr("class", "label")
       .attr("x",function(d) { return (d.y - 25); })
       .attr("y",function(d) { return (d.x + 20); })
       .text("Text");

    function nodeByName(name) {
        return nodesByName[name] || (nodesByName[name] = {name: name});
    }
    });

Does anyone have an idea on how to "relate" the two CSVs and output the respective name in the Text append?

EDIT: I've been looking for techniques to replace the content on an array, based in contents from other array ans I found this beautiful function in PHP that does just what I need: array_replace. Now I need this in Javascript/D3.js but I can's seem to find the exact same solution or kind of an equivalent one. Any ideas?

1

1 Answers

4
votes

I think the easiest way is to map the id to the name directly where you use it, as follows:

svg.selectAll("text")
  [...]
  .text( function(d) { return namesMap[d.name]; });

assuming that namesMap converts node_id to node_name.

Now you just have to create aforementioned namesMap, e.g. like this:

d3.csv("nodes.csv", function(names) {
  var namesMap = {};
  names.forEach(function(d) {
    namesMap[d.node_id] = d.node_name;
  });
  // code using namesMap goes here
});

A slighly modified code (with the csv data loading from <pre> nodes in HTML) is available in this fiddle.