D3 Treemap multiple parameter summing up the stack

廉价感情. 提交于 2019-12-12 00:37:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!