拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 d3.nestrollup,其中键是向下鉆取到另一列中的类别的列的类别

d3.nestrollup,其中键是向下鉆取到另一列中的类别的列的类别

白鹭 - 2022-02-23 1958 0 0

我想通过键是水果“香蕉”的“型别”来进行汇总。

我有的:

var fruit_cnt = d3.nest()
        .key(function(d) {
            return d.fruit === "banana";
        })
        .rollup(function(o){
            return d3.sum(o, function(d){
                return d.count;
            });
        })
        .entries(dataset)
        .map(function(d){
            return {type: d.key, count: d.value};
        });

我最终得到了键的真伪值,但我希望键是“Chiq”和“Mont”。

资料集:

[
    {
      "fruit": "banana",
      "type": "Chiq",
      "count": 1000
    },
    {
      "fruit": "banana",
      "type": "Mont",
      "count": 200
    },
    {
      "fruit": "watermelon",
      "type": "",
      "count": 100
    },
    {
      "fruit": "grape",
      "type": "",
      "count": 220
    }
  ]

uj5u.com热心网友回复:

您正在将关键方法与过滤器混为一谈:

var fruit_cnt = d3.nest()
        .key(function(d) { return d.type })
        .rollup(function(o){
            return d3.sum(o, function(d){
                return d.count;
            });
        })
        .entries(dataset.filter(function(d) { return d.fruit === "banana" }))
        .map(function(d){
            return {type: d.key, count: d.value};
        })
        
标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *