d3分组和汇总 [英] d3 Grouping and Summarization

查看:73
本文介绍了d3分组和汇总的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON数据对象,该对象由区域,邮政编码和每个邮政编码的项目数量组成。我正在尝试按区域汇总(汇总)数量的总和和记录数量的计数。我目前正在使用d3.js,但如果其他库更适合需要,则可以使用。

I have a JSON data object consisting of zones, zip codes, and quantities of items for each zip code. I am trying to summarize (rollup) a summation of quantities and count of the number of records by zone. I am using d3.js currently, but am open to other libraries if they suit the need better.

原始数据结构的示例:

var test = [  
   {  
      "Name":"One Name",
      "Zip":"75001",
      "Zone":"A",
      "qty":60
   },
   {  
      "Name":"Two Name",
      "Zip":"75003",
      "Zone":"A",
      "qty":40
   },
   {  
      "Name":"Three Name",
      "Zip":"75009",
      "Zone":"B",
      "qty":20
   }
]

这是我想要达到的预期结果:

And here is the desired result I am trying to achieve:

[  
   {  
      "Zone":"A",
      "qtySum":100,
      "cnt":2,
      "values":[  
         {  
            "Name":"One Name",
            "Zip":"75001",
            "Zone":"A",
            "qty":60
         },
         {  
            "Name":"Two Name",
            "Zip":"75003",
            "Zone":"A",
            "qty":40
         }
      ]
   },
   {  
      "Zone":"B",
      "qtySum":20,
      "cnt":1,
      "values":[  
         {  
            "Name":"Three Name",
            "Zip":"75009",
            "Zone":"B",
            "qty":20
         }
      ]
   }
]

我可以实现

d3.nest()
.key(function(d) { return d.Zone; })
.entries(test)

我可以将数据汇总到摘要对象中创建者:

And I can rollup the data into a summary object by:

d3.nest()
.key(function(d) { return d.Zone; })
.rollup(function(v) { return {
        count: v.length,
        total: d3.sum(v, function(d) { return d.qty; }),
    }; 
})
.entries(test);

但是我无法使两者一起工作。我曾考虑过手动遍历摘要对象并将其与原始对象中的相应记录一起填充,但这似乎会产生过多开销。

But I cannot manage to get both to work together. I thought about manually looping through the the summary object and stuffing it with the corresponding records from the original object, but that seems like it would be too much overhead.

推荐答案

您与汇总非常接近。
要添加原始对象,请在返回的对象中添加值:v
然后,使用 map 更改对象并以所需格式返回它。

You was very close with rollup. To add the original objects, add values: v to the returned object. Then, use map to alter the object and return it in the required format.

var test = [  
   {  
      "Name":"One Name",
      "Zip":"75001",
      "Zone":"A",
      "qty":60
   },
   {  
      "Name":"Two Name",
      "Zip":"75003",
      "Zone":"A",
      "qty":40
   },
   {  
      "Name":"Three Name",
      "Zip":"75009",
      "Zone":"B",
      "qty":20
   }
];

var grouped = d3.nest()
  .key(function(d) { return d.Zone; })
  .rollup(function(v) { 
    return {
      cnt: v.length,
      qtySum: d3.sum(v, function(d) { return d.qty; }),
      values: v
    }; 
  })
  .entries(test)
  .map(function(d) {
    return {
      Zone: d.key,
      qtySum: d.value.qtySum,
      cnt: d.value.cnt,
      values: d.value.values
    };
  });

d3.select("#foo").text(JSON.stringify(grouped, null, 2));

<script src="https://d3js.org/d3.v4.min.js"></script>
<pre id="foo"></pre>

这篇关于d3分组和汇总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆