D3 Nest examples

Single level nest

I want to get list of tasks performed by persons

var nested_data = d3.nest()
.key(function(d) { return d.who; })
.entries(data);

Two level nest

I want to get list of tasks performed by persons based on task status

var nested_data = d3.nest()
.key(function(d) { return d.who; })
.key(function(d) { return d.status; })
.entries(data);

Use rollup to count leaves

I want to count tasks performed by persons based on task status

var nested_data = d3.nest()
.key(function(d) { return d.who; })
.key(function(d) { return d.status; })
.rollup(function(leaves) { return leaves.length; })
.entries(data);

Rollup does sums as well

I want to count tasks and times taken by persons based on task status

Can't have two rollups, but can return an object/array

var nested_data = d3.nest()
.key(function(d) { return d.who; })
.key(function(d) { return d.status; })
.rollup(function(leaves) { return {"length": leaves.length, "total_time": d3.sum(leaves, function(d) {return parseFloat(d.time);})} })
.entries(data);

Rollup everything to get a grand total of number of lines

I want to count numbers of records

No key

var nested_data = d3.nest()
.rollup(function(leaves) { return leaves.length; })
.entries(data);

Sorting

Each level can be sorted by key - a simple ascending or descending...

var nested_data = d3.nest()
.key(function(d) { return d.who; }).sortKeys(d3.ascending)
.key(function(d) { return d.status; }).sortKeys(d3.descending)
.rollup(function(leaves) { return leaves.length; })
.entries(data);

Sorting - custom order

Persons, fortuitously, can be sorted in straing ascending order, but Status requires a custom order. Create an list in the order you want and use indexOf to create the order comparaitor function.

var status_order = ['In Progress', "Complete", 'Not Started'];
var nested_data = d3.nest()
.key(function(d) { return d.who; }).sortKeys(d3.ascending)
.key(function(d) { return d.status; }).sortKeys(function(a,b) { return status_order.indexOf(a) - status_order.indexOf(b); })
.rollup(function(leaves) { return leaves.length; })
.entries(data);

Sorting - sort the leaves as well

Use sortValue to sort the leaves - sort by time with smallest first

var status_order = ['In Progress', "Complete", 'Not Started'];
var nested_data = d3.nest()
.key(function(d) { return d.who; }).sortKeys(d3.ascending)
.key(function(d) { return d.status; }).sortKeys(function(a,b) { return status_order.indexOf(a) - status_order.indexOf(b); })
.sortValues(function(a,b) { return parseFloat(a.time) - parseFloat(b.time); } }
.entries(data);

Populate a Select list from csv data

Use nest to get a unique list of people then create a select from it.

var nested_data = d3.nest()
.key(function(d) { return d.who}).sortKeys(d3.ascending)
.rollup(function(leaves) { return leaves.length; })
.entries(data);

var list = d3.select("#ex10").append("select")

list.selectAll("option")
.data(nested_data)
.enter()
.append("option")
.attr("value", function(d) {return d.key;})
.text(function(d) {
return d.key; });



profile for Chandrakant Thakkar at Stack Overflow, Q&A for professional and enthusiast programmers