Hi, i need to make a barchart with these type of data set.
How to make exceptionCount for each productIDs at each days like this :
For day 2011-11-14 count 3 occurs (001, 004, 005), etc...
Tried many methods, like reductio, unique count, but didn't find solution.
productIDs's array often change because of regular import of new data.
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;});
Thanks for your help
I don't know if reductio supports this; I guess it would be some sort of exception-array operation in that vocabulary.
Here is one way to do it with straight crossfilter and 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)