问题
This is actually a strange question. I am working on this https://secure.polisci.ohio-state.edu/faq/d3/zoomabletreemap_code.php and currently trying to pass more than one parameter in the treemap and trying to sum them up the stack, as whats mostly done in zoomable treemap.
The code documented for this change is given as:
// Aggregate the values for internal nodes. This is normally done by the
// treemap layout, but not here because of our custom implementation.
function accumulate(d) {
return d.children
? d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0)
: d.value;
}
But in my approach I have to sum up using more than one parameter, say value and count. I tried to change the same code to add two parameters, but that didnt seem to do the trick, could someone guide me please:
function accumulate(d) {
return d.children
? d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0)
: d.value;
}
function accumulate1(d) {
return d.children
? d.count = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0)
: d.count;
}
As two separate function, and then calling them separately to sum both count and value up the stack, starting from leaf nodes. But this is not working. Could you please guide me?
回答1:
You should be able to sum both values in the accumulate function:
function accumulate(d) {
return d.children
? d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0)
: d.value + d.count;
}
来源:https://stackoverflow.com/questions/17350792/d3-treemap-multiple-parameter-summing-up-the-stack