I am using d3.nest() in order to make a hierarchical object from a CSV file.
Could you please help me understand why the following code does not work. I didn't manage to use the nesting function within a loop, as described below.
I have the following CSV file, taken from the examples on d3 website:
"type1","type2","type3","type4","type5","size"
"flare","analytics","cluster","AgglomerativeCluster","","3938"
"flare","analytics","cluster","CommunityStructure","","3812"
"flare","analytics","cluster","MergeEdge","","743"
"flare","analytics","graph","BetweennessCentrality","","3534"
"flare","analytics","graph","LinkDistance","","5731"
This basic nesting works:
data = data.entries(csv)
.key(function(d) {return d.type1; })
.key(function(d) {return d.type2; })
.key(function(d) {return d.type3; })
.entries(csv);
I want to use an array of values to specify my keys in order to modify them dynamically.
This works:
var data = d3.nest();
var nesting = ["type1","type2","type3"];
data = data.key(function(d) {return d[nesting[0]]; });
data = data.key(function(d) {return d[nesting[1]]; });
data = data.key(function(d) {return d[nesting[2]]; });
data = data.entries(csv);
But it does not work with a loop...
var data = d3.nest();
for(var i=0;i<nesting.length;i++)
{
data = data.key(function(d) {return d[nesting[i]]; });
}
data = data.entries(csv);
I can't understand why the loop version is not working... Maybe I miss something about the d3.nest() capabilities...
Also, I would like to know if there is a way to "skip" a nesting level if there is nothing filled at this level (ie: the "type5" level on all the lines from the extract above). How could I do that?
Thanks a lot for reading!