简体   繁体   English

在d3.v4中排序嵌套数据

[英]sorting nested data in d3.v4

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. 我现在正在学习d3(也是JavaScript的新手),并且在使用“累计”值对嵌套数据进行排序时遇到了一些麻烦。 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). 尽可能地尝试,nested_data固执未分类(没有抛出特定错误)。 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. 看了几个关于SO的问题,他们至少推荐了上面的语法。 I'm obviously missing something - could someone please help? 我显然错过了一些东西 - 请有人帮忙吗? Thank you! 谢谢!

EDIT: 编辑:

So on consulting the API reference, maybe the sort method is old? 那么在咨询API参考时,排序方法可能是旧的吗? 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 看起来有一个nest.sortValues(比较器)方法,如果我能为我的代码找出正确的语法,我会在这里发布,因为没有立即工作,但也许可以找到正确的方法来使用它(随意让我知道了!)再次感谢

EDIT2: EDIT2:

Totally foxed by this! 完全被这个骗了! tried to employ syntax in http://bl.ocks.org/phoebebright/raw/3176159/ amongst other things to no avail. 试图在http://bl.ocks.org/phoebebright/raw/3176159/中使用语法等等无济于事。 It'd be fantastic if someone knew how to make it work! 如果有人知道如何让它发挥作用,那就太棒了! Thanks 谢谢

EDIT3: 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. sortValues在这种情况下是不合适的,因为它对叶子进行排序,在rolledup函数的情况下只有一个叶子。 I think mystery solved as per 'answer' below. 我认为神秘的问题按照下面的“答案”解决了。 Thanks. 谢谢。

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 d3.js按汇总字段排序

Which was not working. 哪个不起作用。 In case this does help anyone, just changing .values to .value in: 如果这对任何人都有帮助,只需将.values更改为.value:

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

Fixed the problem and the nested rolled up data is sorted. 修复了问题,并对嵌套的累计数据进行了排序。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM