tile method
Uses squarification as the tiling algorithm for this tree map.
The idea is to present treemap layouts in which the rectangles approximate squares.
For more information about squarify technique, see: Squarified Treemaps: https://www.win.tue.nl/~vanwijk/stm.pdf Squarify algorithm from Charted: https://github.com/google/charted/blob/948ba890fc75e985b80ad0fc826cfc13ff55b633/lib/layout/src/treemap_layout.dart#L156
Implementation
@override
void tile(TreeNode<Object> node) {
final children = node.children;
if (children.isNotEmpty) {
final remainingNodes = Queue.of(children);
final rect = availableLayoutBoundingRect(node);
final analyzer = _SquarifyRatioAnalyzer(_ratio, areaForTreeNode);
var bestScore = double.infinity;
var width = math.min(rect.width, rect.height);
final measure = measureForTreeNode(node);
final scaleFactor = measure == 0 ? 0 : areaForRectangle(rect) / measure;
scaleArea(children, scaleFactor);
while (remainingNodes.isNotEmpty) {
final child = remainingNodes.first;
analyzer.addNode(child);
final score = analyzer.worst(width).toDouble();
// Adding a new child rectangle improves score for the aspect ratio .
if (score <= bestScore) {
remainingNodes.removeFirst();
bestScore = score;
} else {
analyzer.removeLast();
position(analyzer.nodes, rect, width, analyzer.layoutArea);
width = math.min(rect.width, rect.height);
analyzer.reset();
bestScore = double.infinity;
}
}
if (analyzer.nodes.isNotEmpty) {
position(analyzer.nodes, rect, width, analyzer.layoutArea);
analyzer.reset();
}
}
}