sum abstract method
Evaluates the specified value
function for this node and each
descendant in
post-order traversal.
The node.value property of each node is set to the numeric value
returned by the specified function plus the combined value of all
children. The function is passed the node’s data, and must return a
non-negative number. The value
accessor is evaluated for node and
every descendant, including internal nodes; if you only want leaf nodes to
have internal value, then return zero for any node with children.
For example, as an
alternative to
node.count:
root.sum((d) => d.value ? 1 : 0);
You must call node.sum or node.count before invoking a hierarchical layout that requires node.value, such as treemap. For example:
// Construct the treemap layout.
final treemap = Treemap();
treemap.size([width, height]);
treemap.padding(2);
// Sum and sort the data.
root.sum((d) => d.value);
root.sort((a, b) {
var heightDiff = b.height - a.height;
return (heightDiff != 0 ? heightDiff : b.value! - a.value!).sign.toInt();
});
// Compute the treemap layout.
treemap(root);
// Retrieve all descendant nodes.
final nodes = root.descendants();
Since the API supports method chaining, you can also say:
(Treemap()
..size = [width, height]
..constPadding(2))(root
..sum((d) => d.value)
..sort((a, b) {
var heightDiff = b.height - a.height;
return (heightDiff != 0 ? heightDiff : b.value! - a.value!)
.sign
.toInt();
}))
.descendants();
This example assumes that the node data has a value property.
Implementation
@override
sum(value);