D3树映射多参数叠加堆栈

D3 Treemap multiple parameter summing up the stack

本文关键字:叠加 堆栈 参数 映射 D3      更新时间:2023-09-26

这实际上是一个奇怪的问题。我正在做这个https://secure.polisci.ohio-state.edu/faq/d3/zoomabletreemap_code.php目前正试图在树图中传递多个参数,并试图将它们汇总到堆栈中,就像在可缩放树图中所做的那样。

此更改的代码记录如下:

// 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;
}

但在我的方法中,我必须使用多个参数进行总结,比如value和count。我试图更改相同的代码以添加两个参数,但这似乎没有奏效,有人能指导我吗:

   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;
    }

作为两个独立的函数,然后分别调用它们,从叶节点开始,在堆栈中对计数和值进行求和。但这是行不通的。你能给我指路吗?

您应该能够对accumulate函数中的两个值求和:

function accumulate(d) {
  return d.children
    ? d.value = d.children.reduce(function(p, v) { return p + accumulate(v); }, 0)
    : d.value + d.count;
}