Fix hierarchy.revalue.
Post-order traversal alone causes all parent values to be reset to zero *after* accumulating child values. An additional pre-order traversal resets all parent values to zero first, and the post-order traversal can then accumulate child values in their parents. This only affects sticky treemaps.
This commit is contained in:
Родитель
61c568f818
Коммит
58af519423
|
@ -6377,11 +6377,18 @@
|
|||
return hierarchy;
|
||||
};
|
||||
hierarchy.revalue = function(root) {
|
||||
if (value) d3_layout_hierarchyVisitAfter(root, function(node) {
|
||||
var parent;
|
||||
node.value = node.children ? 0 : +value.call(hierarchy, node, node.depth) || 0;
|
||||
if (parent = node.parent) parent.value += node.value;
|
||||
});
|
||||
if (value) {
|
||||
d3_layout_hierarchyVisitBefore(root, function(node) {
|
||||
if (node.children) node.value = 0;
|
||||
});
|
||||
d3_layout_hierarchyVisitAfter(root, function(node) {
|
||||
var parent;
|
||||
if (!node.children) {
|
||||
node.value = +value.call(hierarchy, node, node.depth) || 0;
|
||||
}
|
||||
if (parent = node.parent) parent.value += node.value;
|
||||
});
|
||||
}
|
||||
return root;
|
||||
};
|
||||
return hierarchy;
|
||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -60,11 +60,18 @@ d3.layout.hierarchy = function() {
|
|||
|
||||
// Re-evaluates the `value` property for the specified hierarchy.
|
||||
hierarchy.revalue = function(root) {
|
||||
if (value) d3_layout_hierarchyVisitAfter(root, function(node) {
|
||||
var parent;
|
||||
node.value = node.children ? 0 : +value.call(hierarchy, node, node.depth) || 0;
|
||||
if (parent = node.parent) parent.value += node.value;
|
||||
});
|
||||
if (value) {
|
||||
d3_layout_hierarchyVisitBefore(root, function(node) {
|
||||
if (node.children) node.value = 0;
|
||||
});
|
||||
d3_layout_hierarchyVisitAfter(root, function(node) {
|
||||
var parent;
|
||||
if (!node.children) {
|
||||
node.value = +value.call(hierarchy, node, node.depth) || 0;
|
||||
}
|
||||
if (parent = node.parent) parent.value += node.value;
|
||||
});
|
||||
}
|
||||
return root;
|
||||
};
|
||||
|
||||
|
|
|
@ -26,6 +26,13 @@ suite.addBatch({
|
|||
nodes = h.children(function() { return null; }).nodes({children: [{}]});
|
||||
assert.equal(nodes[0].value, 0);
|
||||
assert.isUndefined(nodes[0].children);
|
||||
},
|
||||
"revalue": function(hierarchy) {
|
||||
var h = hierarchy().sticky(true),
|
||||
nodes = h.nodes({children: [{children: [{value: 1}, {value: 2}]}, {value: 3}]});
|
||||
assert.equal(nodes[0].value, 6);
|
||||
h(nodes[0]); // calls hierarchy.revalue
|
||||
assert.equal(nodes[0].value, 6);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче