我一直在玩交叉过滤器,发现它很棒,但最近撞到了墙上。我根本不知道我的问题是否需要交叉过滤器,很高兴听到任何替代解决方案。
我的数据如下所示:
[{
year: "1987",
country: "UK",
product: "pineapple",
tons_available: 10,
tons_sold: 8
},
{ year: "1987", country: "US", product: "pineapple", tons_available: 34, tons_sold: 18},
{ year: "1987", country: "UK", product: "pear", tons_available: 4, tons_sold: 3},
{ year: "1987", country: "US", product: "pear", tons_available: 23, tons_sold: 20},
{ year: "1988", country: "UK", product: "pineapple", tons_available: 12, tons_sold: 3},
{ year: "1988", country: "US", product: "pineapple", tons_available: 56, tons_sold: 6},
{ year: "1988", country: "UK", product: "pear", tons_available: 32, tons_sold: 32},
{ year: "1988", country: "US", product: "pear", tons_available: 31, tons_sold: 8},
and on, and on...]
我想汇总年度数据,并且能够针对“tons_available”和“tons_sold”等已经可用的指标进行汇总。
var by_week = data_to_filter.dimension( function(d) { return d.year; });
var tons_sold_by_week = by_week.group()
.reduceSum(function(d) { return d.tons_sold; });
但是,我找不到在聚合对象之上创建指标的方法。例如,我想创建一个每年的销售率。为此,我需要每年汇总所有字段,然后仅除以:tons_sold/tons_available。
理想情况下,这会给我一个格式为:
[{
year: "1987",
tons_available: 71,
tons_sold: 49
str: 0.69
},
{ year: "1988", tons_available: 131, tons_sold: 49, str: 0.37 },
and on, and on...]
在我看来,聚合对象每年只保留汇总的变量和结果,只有一对键/值。
有没有办法实现我所追求的?
非常感谢!