1

am learning d3 at the moment (new to JavaScript too), and am having a bit of trouble getting my nested data to sort by 'rolled up' values. Here is my code fragment:

    d3.csv("flat_data.csv", function (data) {      

        var nested_data = d3.nest()
                .key(function(d) { return d.Field1; })
                .rollup(function(g) { 
                return d3.sum(g, function(v) {return v.Field2; }); 
                })
                .entries(data)
                .sort(function(a,b) {return d3.descending(a.values,b.values);});

        console.log(nested_data); 

Try as I might, nested_data is stubbornly unsorted (no specific error thrown). I would like the array entries(data) to be sorted by the values calculated by the rollup function (which are correct). Have looked at a few questions on SO and they at least one recommends the above syntax. I'm obviously missing something - could someone please help? Thank you!

EDIT:

So on consulting the API reference, maybe the sort method is old? looks like there is a nest.sortValues(comparator) method, if I can work out the correct syntax for my code I'll post here as didn't immediately work but maybe can find the right way to use it (feel free to let me know though!) Thanks again

EDIT2:

Totally foxed by this! tried to employ syntax in http://bl.ocks.org/phoebebright/raw/3176159/ amongst other things to no avail. It'd be fantastic if someone knew how to make it work! Thanks

EDIT3:

sortValues is unsuitable in this case since it sorts the leaves, of which there is only one in the case of a rolledup function. I think mystery solved as per 'answer' below. Thanks.

Mehness
  • 135
  • 1
  • 8

1 Answers1

0

I hope it's ok to answer my own question - but I had been using the first answer to:

d3.js sorting by rollup field

Which was not working. In case this does help anyone, just changing .values to .value in:

            .sort(function(a,b) {return d3.descending(a.value,b.value);});

Fixed the problem and the nested rolled up data is sorted. Thanks

Community
  • 1
  • 1
Mehness
  • 135
  • 1
  • 8
  • 1
    That little detail is easily overlooked. It was a minor change from D3 v3 to v4. See my [answer](http://stackoverflow.com/a/40878485/4235784) to [*"d3 axes behaving strangely with a pow scale"*](/q/33284775). – altocumulus Jan 21 '17 at 21:56
  • Thanks very much will check it out – Mehness Jan 21 '17 at 23:24
  • @altocumulus thanking lucky stars to have thought that could be it, not sure such a guess is spectacularly replicable! Well, as a complete newbie must say the challenge of learning d3 with so many online resources v3 focused, is, erm rewarding :) – Mehness Jan 21 '17 at 23:55