Hi Sevil,
Okay, here is a version that groups by product and has individual bars for quantity, total, total2.
It's a bit more complicated because we need to reduce each field:
var dmsGroup = dms2.group().reduce(
function(p, v) { // add
p.quantity += v.quantity;
p.total += v.total;
p.total2 += v.total2;
return p;
},
function(p, v) { // remove
p.quantity -= v.quantity;
p.total -= v.total;
p.total2 -= v.total2;
return p;
},
function() { // init
return {quantity: 0, total: 0, total2: 0}
});
and then flatten into a group with multikeys:
function flatten_group(group, fields) {
return {
all: () => group.all().flatMap(
({key, value}) => fields.map(
field => ({key: [key, field], value: value[field]})))
}
}
Y positions have also gotten more complicated with three bars per group:
mychart.on('pretransition', chart => {
chart.selectAll('g.row rect')
.attr('y', ({key: [cat, col]}) => {
switch(col) {
case 'quantity': return +3;
case 'total': return 0;
case 'total2': return -3;
}
})
chart.selectAll('g.row text')
.attr('y', ({key: [cat, col]}) => {
switch(col) {
case 'quantity': return 15;
case 'total': return 12;
case 'total2': return 9;
}
})
})
This also handles text positions, which my last example did not.
As a bonus, I've also added selection by group: basically we need all bars within a group to act as one, and apply a filter on just the first field of the key:
mychart.addFilterHandler((filters, filter) => {
Array.prototype.push.apply(filters, fields.map(field => [filter[0], field]))
return filters;
})
mychart.removeFilterHandler((filters, filter) => {
return filters.filter(([cat, col]) => cat !== filter[0]);
})
var _filterHandler = mychart.filterHandler();
mychart.filterHandler((dimension, filters) => {
var keys = {};
filters.forEach(([cat, col]) => keys[cat] = true);
_filterHandler(dimension, Object.keys(keys));
})
It's not possible to select individual bars in this case, since bars represent columns and not rows, so this is the only thing that makes sense.
Cheers,
Gordon