With the example data
var data = [
{Name: 'Mr A', Spent: 40, Year: 2011, tags: ['a', 'b', 'c']},
{Name: 'Mr B', Spent: 10, Year: 2011, tags: ['c']},
{Name: 'Mr C', Spent: 40, Year: 2011, tags: ['a']},
{Name: 'Mr A', Spent: 70, Year: 2012, tags: ['c', 'b']},
{Name: 'Mr B', Spent: 20, Year: 2012, tags: ['b']},
{Name: 'Mr B', Spent: 50, Year: 2013, tags: ['a', 'b', 'c']},
{Name: 'Mr C', Spent: 30, Year: 2013, tags: ['a', 'b']}
];
I am trying to create a dc.js row chart that would show each unique tag and allow me to reduce the values graphed by each tag. So far I have this code which allows me to reduce the tags to their spent sums:
function reduceAdd(p, v) {
v.tags.forEach (function(val, idx) {
p[val] = (p[val] || 0) + v.Spent;
});
return p;
}
function reduceRemove(p, v) {
v.tags.forEach (function(val, idx) {
p[val] -= v.Spent;
});
return p;
}
function reduceInitial() {
return {};
}
var tagsDim = ndx.dimension(function(d) {return d.tags; } );
tagsGroup = tagsDim.groupAll().reduce(reduceAdd, reduceRemove, reduceInitial);
console.log(tagsGroup.value())
'{ a: 160, b: 210, c: 170 }'
This gives the reduced sums correctly. But, because this is a groupAll object dc.js can't graph it, so I am not quite sure where to go from here. Is it even possible for dc.js to access the tags in the array. Would it be better to calculate the sums a different way?
Aucun commentaire:
Enregistrer un commentaire