【问题标题】:sortKeys not working with d3.nest()sortKeys 不适用于 d3.nest()
【发布时间】:2015-10-06 20:32:31
【问题描述】:

我想按年份而不是按值对我的 d3 sunburst 的外环进行排序,所以它是按时间顺序排列的。这是我的代码:

    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)
        };

“.sortKeys(d3.ascending)”被忽略。外圈还是按值排列的。

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

更新: 试过这个:

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

更新了我的plunker

看来这可能是有问题的代码。我不知道如何改变它以获得我正在寻找的外环是按时间顺序排列的,但是如果我将 d.values 更改为 d.key,这些值都会搞砸,请参阅内联评论。

        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
        });

此代码来自jsDataV的第7章。相关GitHub

更新了我的plunker

【问题讨论】:

  • 也发布一些示例 csv 数据。
  • 我添加了一个我的csv文件的样本,真实的超过8000行。

标签: javascript csv d3.js sunburst-diagram


【解决方案1】:

我认为这是因为第一次记录的年份是Nan值。如果您看到hierarchy 变量的控制台,您将看到第一条记录的年份为Nan。在第一个记录中的教育之前,您需要删除 , 这样的“,”不会弄乱您的整个 .csv 文件。您可以查看this plnkr 进行现场演示。

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

【讨论】:

  • 嗯,当我将文件更改为 tsv 并使用 d3.tsv 函数时,我得到了与上述相同的结果。我不明白如何查看层次结构的价值,它没有出现在我的控制台中。
  • 您可以按 F12 键,它将显示在控制台选项卡中。关键应该是您的年份值。我的猜测是,对于您的某些行,它给出了“Nan”,这就是为什么它没有按年份排序的原因。看看我编辑的答案。
  • 当我修改您的 plunker 示例以包含逗号时,我确实看到了 NaN。奇怪的是,当我删除逗号或使用 tsv 选项而不是 csv 时,排序仍然不起作用。
  • 是的,我不知道为什么会这样。可能这有帮助stackoverflow.com/questions/30480981/…
【解决方案2】:

已解决:

    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;
        });

关键部分是:

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

【讨论】:

    猜你喜欢
    • 2017-01-17
    • 2018-11-19
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    相关资源
    最近更新 更多