SUM and SORT in nest()

137 views
Skip to first unread message

in...@rheindata.com

unread,
Mar 6, 2015, 8:08:07 AM3/6/15
to d3...@googlegroups.com
    
I am trying to sort my nested data by descending value order with the following code:

    nestedL1Values = d3.nest()
       
.key(function (d) { return d.YEAR; })
       
.rollup(function (values) {
           
return { summe: d3.sum(values, function (d) { return d.REVENUE = +d.REVENUE }) };
       
})
       
.sortValues(function (a, b) { return a.summe - b.summe })
       
.entries(data);

Sorting doesn`t work. How do I need to change my sortValues-function?

Joe Keohan

unread,
Mar 6, 2015, 11:11:47 AM3/6/15
to d3...@googlegroups.com
I've only worked with the d3.nest() method a few times in the past and took a moment to review the following site: http://bl.ocks.org/phoebebright/raw/3176159/   What I did notice was that no examples were provided that used sortValues AFTER a rollup() was called.  So I added a console.log within the sortValues method as so:  .sortValues(function(a,b) { console.log("in sortValues"); return a.summe- b.summe})  and noticed that there was no output so I'm assuming its not kicking that method off for some reason.  The workaround i provided uses .sort() and worked fine the data set provided on that page.   Hope this helps...

var nested_data = d3.nest()
.key(function(d) { return d.status; })
.rollup(function(values) { return { summe: d3.sum(values, function(d) {return d.time })}})
.entries(data)

nested_data.sort(function(a,b) { return a.values["summe"] - b.values["summe"]})

Joe

Simon Breton

unread,
May 19, 2016, 1:29:44 PM5/19/16
to d3-js
Hello guys,

I'm also trying to sort my data in the rollup function. here is my code 

  var newpeople = d3.nest()
       
.key(function(d) { return d.authorname;})
       
.rollup(function(d) {
               
return d3.sum(d, function(g) {return g.linkclicks;});
                           
})
       
.entries(people).forEach(function(currentValue, index, array) {
        tabulate
( currentValue, ["authorname", "linkclicks"] );

But I don't understand why you guys have something like this : "summe: d3.sum" ?

Thanks. 

Joe Keohan

unread,
May 19, 2016, 1:45:18 PM5/19/16
to d3-js
Simon,

summe: d3.sum.... is being used to create a new property called summe which then contains the value result of d3.sum...

Joe
Reply all
Reply to author
Forward
0 new messages