如何使用多个值数组计算每个键的唯一值? / DC.js - Crossfilter - Reductionio
How to count unique value per key with multiple values array ? / DC.js - Crossfilter - Reductio
嗨,我需要用这些类型的数据集制作条形图。
如何像这样每天为每个 productID 生成 exceptionCount:
对于 2011-11-14 日计数 3 发生(001、004、005)等...
尝试了很多方法,如reductio、unique count,但没有找到解决方案。
由于定期导入新数据,productIDs 的数组经常发生变化。
data([
{date: "2011-11-14", quantity: 2, total: 190, tip: 100, type: "tab", productIDs:["001"]},
{date: "2011-11-14", quantity: 2, total: 190, tip: 100, type: "tab", productIDs:["001", "005"]},
{date: "2011-11-14", quantity: 1, total: 300, tip: 200, type: "visa", productIDs:["004" ,"005"]},
{date: "2011-11-15", quantity: 2, total: 90, tip: 0, type: "tab", productIDs:["001", "002"]},
{date: "2011-11-15", quantity: 2, total: 90, tip: 0, type: "tab", productIDs:["005"]},
{date: "2011-11-15", quantity: 2, total: 90, tip: 0, type: "tab", productIDs:["001", "004" ,"005"]},
{date: "2011-11-16", quantity: 1, total: 100, tip: 0, type: "cash", productIDs:["001", "002", "003", "004" ,"005"]},
{date: "2011-11-16", quantity: 2, total: 90, tip: 0, type: "tab", productIDs:["001"]},
{date: "2011-11-16", quantity: 2, total: 90, tip: 0, type: "tab", productIDs:["004" ,"005"]},
{date: "2011-11-17", quantity: 2, total: 90, tip: 0, type: "tab", productIDs:["001", "002", "004" ,"005"]},
{date: "2011-11-17", quantity: 2, total: 200, tip: 0, type: "cash", productIDs:["002"]},
{date: "2011-11-17", quantity: 1, total: 200, tip: 100, type: "visa", productIDs:["004"]}
])
var dim = ndx.dimension(function(d) { return d.date; });
var pgroup = dim.group();
var reducer = reductio()
.exception(function(d) { return d.productIDs;}).exceptionCount(true);
reducer(pgroup);
pgroup.top([Infinity]);
Barchart
.dimension(dim)
.group(pgroup)
.xUnits(dc.units.ordinal)
.x(d3.scale.ordinal())
.elasticY(true)
.brushOn(true)
.valueAccessor(function(p) {
return p.value.exceptionCount;});
感谢您的帮助
不知道reductio是否支持这个;我想这将是该词汇表中的某种异常数组操作。
这是使用直接交叉过滤器和 dc.js:
的一种方法
var dim = ndx.dimension(function(d) { return d.date; });
var pgroup = dim.group().reduce(
function(p, v) { // add
v.productIDs.forEach(x => p[x] = (p[x] || 0) + 1);
return p;
},
function(p, v) { // remove
v.productIDs.forEach(x => { if(--p[x] === 0) delete p[x] });
return p;
},
function() { // init
return {};
}
);
Barchart
.valueAccessor(p => Object.keys(p.value).length)
嗨,我需要用这些类型的数据集制作条形图。
如何像这样每天为每个 productID 生成 exceptionCount:
对于 2011-11-14 日计数 3 发生(001、004、005)等...
尝试了很多方法,如reductio、unique count,但没有找到解决方案。
由于定期导入新数据,productIDs 的数组经常发生变化。
data([
{date: "2011-11-14", quantity: 2, total: 190, tip: 100, type: "tab", productIDs:["001"]},
{date: "2011-11-14", quantity: 2, total: 190, tip: 100, type: "tab", productIDs:["001", "005"]},
{date: "2011-11-14", quantity: 1, total: 300, tip: 200, type: "visa", productIDs:["004" ,"005"]},
{date: "2011-11-15", quantity: 2, total: 90, tip: 0, type: "tab", productIDs:["001", "002"]},
{date: "2011-11-15", quantity: 2, total: 90, tip: 0, type: "tab", productIDs:["005"]},
{date: "2011-11-15", quantity: 2, total: 90, tip: 0, type: "tab", productIDs:["001", "004" ,"005"]},
{date: "2011-11-16", quantity: 1, total: 100, tip: 0, type: "cash", productIDs:["001", "002", "003", "004" ,"005"]},
{date: "2011-11-16", quantity: 2, total: 90, tip: 0, type: "tab", productIDs:["001"]},
{date: "2011-11-16", quantity: 2, total: 90, tip: 0, type: "tab", productIDs:["004" ,"005"]},
{date: "2011-11-17", quantity: 2, total: 90, tip: 0, type: "tab", productIDs:["001", "002", "004" ,"005"]},
{date: "2011-11-17", quantity: 2, total: 200, tip: 0, type: "cash", productIDs:["002"]},
{date: "2011-11-17", quantity: 1, total: 200, tip: 100, type: "visa", productIDs:["004"]}
])
var dim = ndx.dimension(function(d) { return d.date; });
var pgroup = dim.group();
var reducer = reductio()
.exception(function(d) { return d.productIDs;}).exceptionCount(true);
reducer(pgroup);
pgroup.top([Infinity]);
Barchart
.dimension(dim)
.group(pgroup)
.xUnits(dc.units.ordinal)
.x(d3.scale.ordinal())
.elasticY(true)
.brushOn(true)
.valueAccessor(function(p) {
return p.value.exceptionCount;});
感谢您的帮助
不知道reductio是否支持这个;我想这将是该词汇表中的某种异常数组操作。
这是使用直接交叉过滤器和 dc.js:
的一种方法var dim = ndx.dimension(function(d) { return d.date; });
var pgroup = dim.group().reduce(
function(p, v) { // add
v.productIDs.forEach(x => p[x] = (p[x] || 0) + 1);
return p;
},
function(p, v) { // remove
v.productIDs.forEach(x => { if(--p[x] === 0) delete p[x] });
return p;
},
function() { // init
return {};
}
);
Barchart
.valueAccessor(p => Object.keys(p.value).length)