自定义键在滚动数据d3.js中排序

时间:2015-09-29 14:03:28

标签: javascript sorting csv d3.js nested

我尝试使用d3.js对从csv文件中获取的嵌套数据的键进行排序。问题是,在这种情况下,根据d3.ascendingd3.descending进行排序是不可能的,因为我的值是字符串。

我找到了符合我需求的例子:

var priority_order = ['MUST', "SHOULD", 'COULD', 'WISH'];

var nested_data = d3.nest()
    .key(function(d) { return d.status; })
    .sortKeys(d3.ascending)
    .key(function(d) { return d.priority; })
    .sortKeys(function(a,b) { return priority_order.indexOf(a) - priority_order.indexOf(b); })
    .rollup(function(leaves) { return leaves.length; })
    .entries(csv_data);

但是当我使用它时它不起作用。我不能理解function(a,b) { return priority_order.indexOf(a) - priority_order.indexOf(b); }

我使用的代码如下:

var S6_order = ['city 20 000 - 49 999 citizens', 
          'city 50 000 – 99 999 citizens', 
          'city 100 000 – 199 999 citizens', 
          'city 200 000 – 499 999 citizens', 
          'city above 500 000 citizens'];

var S6_sort = d3.nest()
    .key(function(d) { return d.S6; })
    .sortKeys(function(a,b) { return S6_order.indexOf(a) - S6_order.indexOf(b); })
    .rollup(function(leaves) { return leaves.length; })
    .entries(data);

我使用的一段数据:

S2,S6,P35,P2_1,P2_2,P2_3,P2_4,P2_5
2-9 persons,city 20 000 - 49 999 citizens,I like very much,too less,too much,too much,too less,just right
10-49 persons,city 100 000 – 199 999 citizens,I like very much,too much,I don't know,I don't know,I don't know,I don't know
2-9 persons,city 200 000 – 499 999 citizens,I like very much,accurately,too less,too less,too less,just right
10-49 persons,city 50 000 – 99 999 citizens,I do not like neither dislike,too less,too much,too much,too less,too less
10-49 persons,city above 500 000 citizens,I rather like,too less,accurately,accurately,accurately,just right
only me,city above 500 000 citizens,I rather like,too less,too less,accurately,accurately,too less
2-9 persons,city 200 000 – 499 999 citizens,I like very much,accurately,accurately,accurately,accurately,just right
only me,city 50 000 – 99 999 citizens,I rather like,accurately,accurately,accurately,accurately,just right
50-249 persons,city 100 000 – 199 999 citizens,I like very much,accurately,too less,too less,accurately,just right
10-49 persons,city 100 000 – 199 999 citizens,I rather like,too less,accurately,accurately,accurately,just right
2-9 persons,city 100 000 – 199 999 citizens,I rather like,accurately,accurately,too less,accurately,just right
2-9 persons,city 50 000 – 99 999 citizens,I rather like,I don't know,accurately,accurately,too less,just right
only me,city 20 000 - 49 999 citizens,I like very much,accurately,accurately,accurately,I don't know,too much
2-9 persons,city above 500 000 citizens,I like very much,too less,accurately,too much,too less,too less
10-49 persons,city above 500 000 citizens,I like very much,accurately,accurately,accurately,accurately,too less
2-9 persons,city 200 000 – 499 999 citizens,I rather like,accurately,accurately,accurately,accurately,just right
2-9 persons,city 100 000 – 199 999 citizens,I like very much,accurately,accurately,accurately,accurately,just right
10-49 persons,city 50 000 – 99 999 citizens,I do not like neither dislike,accurately,accurately,accurately,accurately,just right
2-9 persons,city 50 000 – 99 999 citizens,I like very much,accurately,accurately,accurately,accurately,just right
2-9 persons,city 100 000 – 199 999 citizens,raczej go nie lubię,too less,too much,too much,I don't know,I don't know
2-9 persons,city above 500 000 citizens,I like very much,too less,accurately,accurately,accurately,too less
2-9 persons,city 50 000 – 99 999 citizens,I rather like,too much,accurately,accurately,accurately,just right
10-49 persons,city above 500 000 citizens,I do not like neither dislike,too much,accurately,too much,accurately,I don't know
50-249 persons,city 200 000 – 499 999 citizens,I rather like,I don't know,accurately,accurately,too less,just right
2-9 persons,city above 500 000 citizens,I rather like,too much,accurately,accurately,too less,just right
2-9 persons,city 200 000 – 499 999 citizens,I rather like,accurately,accurately,accurately,accurately,just right
10-49 persons,city above 500 000 citizens,I rather like,too less,too less,accurately,I don't know,too less
2-9 persons,city 200 000 – 499 999 citizens,I do not like neither dislike,I don't know,I don't know,too much,accurately,I don't know
10-49 persons,city 200 000 – 499 999 citizens,I rather like,too much,accurately,too much,accurately,just right

我用以下内容加载:

d3.csv("data.csv", function(file) {
    data = file.map(function(d) {
        return {
            "S2": d.S2,
            "S6": d.S6,
            "P2_1": d.P2_1,
            "P2_2": d.P2_2,
            "P2_3": d.P2_3,
            "P2_4": d.P2_4,
            "P2_5": d.P2_5
            };
        });    
    });

0 个答案:

没有答案