user2509885
user2509885

Reputation: 45

d3 for loop retrieving data from a list of object

I have a csv file which load into screen . The csv File has following attributes

DATE              MESSAGEID  Status  Source  ParentID  childID
12/21/2013 23:12  123        SEND    A              2
12/21/2013 23:12  456        RECEIVE B              3
12/21/2013 23:12  789        SEND    B              2         4

Now in console I wrote this

var csvdataset;
d3.csv("newFile.csv",function(d,i){csvdataset=i;console.log(csvdataset)});

So it gives me

[Object, Object, Object]

Now if I want to retrieve the message id or any value how do I get those from ? I know if I give

d3.csv("newFile.csv",function(d,i){csvdataset=i;console.log(csvdataset[3])});

I will get the third row but I need some Kind of loop that will execute to retrieve a particular value from an object .

I have tried below things

d3.csv("newFile.csv", function (d, i) {
    csvdataset = i;
}, csvdataset.forEach(csvdataset) {
    alert(csvdataset.SOURCE[csvdataset]);
}
});

d3.csv("newFile.csv", function (d, i) {
    csvdataset = i;
    console.log(i.SOURCESERVICE[i])
});

but nothing is working .

I have another issue . i am generating a json in tree structure as below

[{"Name":"s1","Node ID":"649","DataObject":{"nodeID":"649","nodeName":"s1","timeTaken":"00:06:30","startTime":"2013-12-10 18:06:02"},"Children":[[{"Name":"c1","Node ID":"286","DataObject":{"nodeID":"286","nodeName":"c1","timeTaken":"00:06:27","startTime":"2013-12-10 18:06:04+05:30"},"Children":[[{"Name":"c2","Node ID":"287","DataObject":{"nodeID":"287","nodeName":"c2","timeTaken":"00:00:02","startTime":"2013-12-10 18:06:06+05:30"}},{"Name":"c3","Node ID":"1080","DataObject":{"nodeID":"1080","nodeName":"c3","timeTaken":"00:06:17","startTime":"2013-12-10 18:06:12+05:30"},"Children":[[{"Name":"c4","Node ID":"b2861a2f-75a9-4f95-abcd-1dae54e713bc","DataObject":{"nodeID":"b2861a2f-75a9-4f95-abcd-1dae54e713bc","nodeName":"c4","timeTaken":"00:05:08","startTime":"2013-12-10 18:07:19+05:30"}}]]}]]}]]}]

now i am trying to create a tree in d3 . But i am unable to retrieve the children

    var tree = d3.layout.tree()
   .size([height, width])
     .children(function(d)
        {
            return (!d.Children || d.Children.length === 0) ? null : d.Children;
        });
   d3.xhr("DataMapper?function-name=Tree","application/json", function(error, flare) {
   root = JSON.parse(flare.response);
   root.x0 = height / 2;
   root.y0 = 0;
   console.log(root);
   function collapse(d) {
        if (d.Children) {
          d.Children= d.Children;
          d.Children.forEach(collapse);
          d.Children= null;
           console.log("children "+d.Children);

        }
      }
 });

if i am givingvar nodes = tree.nodes(root); i get a array of nodes in the nodes variable but if i give var links=tree.links(nodes); i am unable to get the links . Could any one explain this .I have provided the json above

Upvotes: 0

Views: 4694

Answers (2)

Christopher Hackett
Christopher Hackett

Reputation: 6192

D3 is designed to be functional where you concentrate on the data not the loops.

var data = [];
d3.csv("newFile.csv", function(csvData){
    data = csvData;
    // Call a function now you have the data ready 
    doSomething(data);
})

function doSomething(d){
d3.select('#target')
    .selectAll('div')
    .data(d)
    .enter()
        .append('div')
        .text(function(d, i){return d.MESSAGEID + ' ' + d.Status;})
 }

For further reading check out this

var data = [];
d3.csv("newFile.csv", function(csvData){
    // Just  array of data which has been parsed from the file
    data = csvData;
    console.log({'02: data just Parsed':data});

    // Say you want to group by Status(could be d.MESSAGEID, d.Source etc)
    data = d3.nest()
        .key(function(d){return d.Status;})
        .map(csvData);
    // Now returns an object with 'RECEIVE':Array[1] & 'SEND':Array[2]
    console.log({'03: Nested by Status':data});

    // Call a function now you have the data ready 
    // doSomething(data);
})


// This returns Array[0] as d3.csv does not block 
console.log({'01: Outside callback': data});

Upvotes: 2

Christopher Chiche
Christopher Chiche

Reputation: 15335

You can try to loop in the d3.csv function defined in the second argument.

So you can for example do the following:

d3.csv("newFile.csv",function(d) {
    d.forEach(function(entry){ alert(entry.name)});
})

Upvotes: 0

Related Questions