0

I'd like to sort the outer ring of my d3 sunburst by year not by value, so it's in chronological order. Here is my code:

    d3.csv("etds.csv", function(error, dataset) {
        var hierarchy = {
            key: "ETD",
            values: d3.nest()
                .key(function(d) { return d.college; })
                .key(function(d) { return d.dept; })
                .sortKeys(d3.ascending).key(function(d) { return +d.year; })          
                .rollup(function(leaves) {
                    return leaves.length;
                })
                .entries(dataset)
        };

The ".sortKeys(d3.ascending)" is being ignored. The outer ring is still arranged by value.

Sample CSV:

college,dept,year,url
College of Education, Health & Human Development,Education.,2011
College of Letters & Science,Earth Sciences.,2010
College of Letters & Science,Microbiology & Immunology.,2004
College of Letters & Science,Ecology.,1984
College of Letters & Science,Chemistry & Biochemistry.,2008
College of Letters & Science,Mathematical Sciences.,2011
College of Agriculture,Land Resources & Environmental Sciences.,2009
College of Agriculture,Agricultural Economics & Economics.,1996
College of Letters & Science,English.,2007

Update: Tried this:

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

Updated my plunker

It seems that this may be the offending code. I'm not sure how to alter it to get what I'm looking for the outer ring is chronological by year but the values are all screwed up if I change d.values to d.key, see inline comment.

        var partition = d3.layout.partition()
        .children(function(d) {
            return Array.isArray(d.values) ?
                d.values : null;
        })
        .value(function(d) {
            return d.values;  //d.key sorts by year in outer ring
        });

This code is from Chapter 7 of jsDataV.is related GitHub

Updated my plunker

mutanthumb
  • 161
  • 1
  • 13

2 Answers2

0

I think it is because of Nan value in year for the first record. If you see the console for the hierarchy variable you will see that for first record it has year as Nan. You will need to remove , before education in the first record - and such "," are not messing up your entire .csv file. You can check this plnkr for live demo.

college,dept,year,url
College of Education, **Health & Human Development - Education**.,2011
College of Letters & Science,Earth Sciences.,2010
College of Letters & Science,Microbiology & Immunology.,2004
College of Letters & Science,Ecology.,1984
College of Letters & Science,Chemistry & Biochemistry.,2008
College of Letters & Science,Mathematical Sciences.,2011
College of Agriculture,Land Resources & Environmental Sciences.,2009
College of Agriculture,Agricultural Economics & Economics.,1996
College of Letters & Science,English.,2007

enter image description here

somename
  • 978
  • 11
  • 30
  • Hmmm when I changed my file to a tsv, and used the d3.tsv function I got the same results as above. I'm not understanding how to see the value for hierarchy, it wasn't coming up in my console. – mutanthumb Oct 12 '15 at 21:17
  • You can press F12 key it will show up in the console tab. The key should be your year value. My guess is for some of your rows it is giving "Nan" and that is why it is not sorting by year. Look at my edited answer. – somename Oct 12 '15 at 21:26
  • I did see the NaN when I modified your plunker example to include the comma. It's odd that sort is still not working when either I remove the comma or go with the tsv option instead of the csv. – mutanthumb Oct 12 '15 at 21:56
  • Yes I am not sure why this is happening. may be this is helpful http://stackoverflow.com/questions/30480981/d3-js-sorting-by-rollup-field – somename Oct 12 '15 at 23:24
0

Resolved with this:

    var partition =d3.layout.partition()
        .children(function(d) {
            return Array.isArray(d.values) ?
                d.values : null;})
        .value(function(d) {
            return d.values;
        })

        .sort(function(d) {
            return;
        });

The key part being:

            .sort(function(d) {
            return;
        });
mutanthumb
  • 161
  • 1
  • 13