| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. All rights reserved. | 2 * Copyright 2015 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style | 4 * Use of this source code is governed by a BSD-style |
| 5 * license that can be found in the LICENSE file or at | 5 * license that can be found in the LICENSE file or at |
| 6 * https://developers.google.com/open-source/licenses/bsd | 6 * https://developers.google.com/open-source/licenses/bsd |
| 7 */ | 7 */ |
| 8 part of charted.layout; | 8 part of charted.layout; |
| 9 | 9 |
| 10 typedef int SortFunction(HierarchyNode a, HierarchyNode b); | 10 typedef int SortFunction(HierarchyNode a, HierarchyNode b); |
| 11 typedef List ChildrenAccessor(HierarchyNode node); | 11 typedef List ChildrenAccessor(HierarchyNode node); |
| 12 typedef num ValueAccessor(HierarchyNode node); | 12 typedef num ValueAccessor(HierarchyNode node); |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * The hierarchy layout is an abstract layout that is not used directly, but | 15 * The hierarchy layout is an abstract layout that is not used directly, but |
| 16 * instead allows code sharing between multiple hierarchical layouts such as: | 16 * instead allows code sharing between multiple hierarchical layouts such as: |
| 17 * Cluster, Pack, Partition, Tree, and Treemap layout. | 17 * Cluster, Pack, Partition, Tree, and Treemap layout. |
| 18 */ | 18 */ |
| 19 abstract class HierarchyLayout<T extends HierarchyNode> { | 19 abstract class HierarchyLayout<T extends HierarchyNode> { |
| 20 static const ROOT_ROW_INDEX = -1; | 20 static const ROOT_ROW_INDEX = -1; |
| 21 SortFunction sortFunction = hierarchySort; | 21 SortFunction sortFunction = hierarchySort; |
| 22 ChildrenAccessor childrenAccessor = hierarchyChildren; | 22 ChildrenAccessor childrenAccessor = hierarchyChildren; |
| 23 ValueAccessor valueAccessor = hierarchyValue; | 23 ValueAccessor valueAccessor = hierarchyValue; |
| 24 | 24 |
| 25 /// Returns the list of HierarchyNode constructed from the given data and | 25 /// Returns the list of HierarchyNode constructed from the given data and |
| 26 /// parentColumn and valueColumn which is used to construct the hierarchy. | 26 /// parentColumn and valueColumn which is used to construct the hierarchy. |
| 27 /// The returned list of nodes contains the hierarchy with root being the | 27 /// The returned list of nodes contains the hierarchy with root being the |
| 28 /// first element its children in depth first order. | 28 /// first element its children in depth first order. |
| 29 List<T> layout(List rows, int parentColumn, int labelColumn, | 29 List<T> layout( |
| 30 int valueColumn) { | 30 List rows, int parentColumn, int labelColumn, int valueColumn) { |
| 31 List<HierarchyNode> nodeList = []; | 31 List<HierarchyNode> nodeList = []; |
| 32 for (var row in rows) { | 32 for (var row in rows) { |
| 33 nodeList.add(createNode(row[labelColumn], row[valueColumn], 0)); | 33 nodeList.add(createNode(row[labelColumn], row[valueColumn], 0)); |
| 34 } | 34 } |
| 35 | 35 |
| 36 for (var i = 0; i < rows.length; i++) { | 36 for (var i = 0; i < rows.length; i++) { |
| 37 int parentRow = rows[i][parentColumn]; | 37 int parentRow = rows[i][parentColumn]; |
| 38 if (parentRow == ROOT_ROW_INDEX) continue; | 38 if (parentRow == ROOT_ROW_INDEX) continue; |
| 39 var currentNode = nodeList[i]; | 39 var currentNode = nodeList[i]; |
| 40 var parentNode = nodeList[parentRow]; | 40 var parentNode = nodeList[parentRow]; |
| 41 (parentNode.children.isEmpty) ? parentNode.children = [currentNode] : | 41 (parentNode.children.isEmpty) |
| 42 parentNode.children.add(currentNode); | 42 ? parentNode.children = [currentNode] |
| 43 : parentNode.children.add(currentNode); |
| 43 currentNode.parent = parentNode; | 44 currentNode.parent = parentNode; |
| 44 currentNode.depth = parentNode.depth + 1; | 45 currentNode.depth = parentNode.depth + 1; |
| 45 for (var child in currentNode.children) { | 46 for (var child in currentNode.children) { |
| 46 child.depth += 1; | 47 child.depth += 1; |
| 47 } | 48 } |
| 48 } | 49 } |
| 49 | 50 |
| 50 // Reorder the list so that root is the first element and the list contains | 51 // Reorder the list so that root is the first element and the list contains |
| 51 // the hierarchy of nodes in depth first order. | 52 // the hierarchy of nodes in depth first order. |
| 52 var hierarchyNodeList = []; | 53 var hierarchyNodeList = []; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 65 | 66 |
| 66 /// Default accessor method for getting the list of children of the node. | 67 /// Default accessor method for getting the list of children of the node. |
| 67 static List hierarchyChildren(HierarchyNode node) => node.children; | 68 static List hierarchyChildren(HierarchyNode node) => node.children; |
| 68 | 69 |
| 69 /// Default accessor method for getting the value of the node. | 70 /// Default accessor method for getting the value of the node. |
| 70 static num hierarchyValue(HierarchyNode node) => node.value; | 71 static num hierarchyValue(HierarchyNode node) => node.value; |
| 71 | 72 |
| 72 /// Default sorting method for comparing node a and b. | 73 /// Default sorting method for comparing node a and b. |
| 73 static int hierarchySort(HierarchyNode a, HierarchyNode b) => | 74 static int hierarchySort(HierarchyNode a, HierarchyNode b) => |
| 74 b.value - a.value; | 75 b.value - a.value; |
| 75 | |
| 76 } | 76 } |
| 77 | 77 |
| 78 abstract class HierarchyNode { | 78 abstract class HierarchyNode { |
| 79 /// The parent node, or null for the root. | 79 /// The parent node, or null for the root. |
| 80 HierarchyNode parent = null; | 80 HierarchyNode parent = null; |
| 81 | 81 |
| 82 /// The list of children nodes, or null for leaf nodes. | 82 /// The list of children nodes, or null for leaf nodes. |
| 83 List children = []; | 83 List children = []; |
| 84 | 84 |
| 85 /// The label to show for each block of hierarchy | 85 /// The label to show for each block of hierarchy |
| 86 String label = ''; | 86 String label = ''; |
| 87 | 87 |
| 88 /// The node value, as returned by the value accessor. | 88 /// The node value, as returned by the value accessor. |
| 89 dynamic value; | 89 dynamic value; |
| 90 | 90 |
| 91 /// The depth of the node, starting at 0 for the root. | 91 /// The depth of the node, starting at 0 for the root. |
| 92 int depth = 0; | 92 int depth = 0; |
| 93 } | 93 } |
| OLD | NEW |