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); |
(...skipping 10 matching lines...) Expand all Loading... |
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( | 29 List<T> layout( |
30 List rows, int parentColumn, int labelColumn, int valueColumn) { | 30 List rows, int parentColumn, int labelColumn, int valueColumn) { |
31 List<HierarchyNode> nodeList = []; | 31 List<T> 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) | 41 parentNode.children.add(currentNode); |
42 ? parentNode.children = [currentNode] | |
43 : parentNode.children.add(currentNode); | |
44 currentNode.parent = parentNode; | 42 currentNode.parent = parentNode; |
45 currentNode.depth = parentNode.depth + 1; | 43 currentNode.depth = parentNode.depth + 1; |
46 for (var child in currentNode.children) { | 44 for (var child in currentNode.children) { |
47 child.depth += 1; | 45 child.depth += 1; |
48 } | 46 } |
49 } | 47 } |
50 | 48 |
51 // Reorder the list so that root is the first element and the list contains | 49 // Reorder the list so that root is the first element and the list contains |
52 // the hierarchy of nodes in depth first order. | 50 // the hierarchy of nodes in depth first order. |
53 var hierarchyNodeList = []; | 51 var hierarchyNodeList = <HierarchyNode>[]; |
54 var root = nodeList.where((e) => e.depth == 0).elementAt(0); | 52 var root = nodeList.where((e) => e.depth == 0).elementAt(0); |
55 var children = [root]; | 53 var children = <HierarchyNode>[root]; |
56 while (children.length > 0) { | 54 while (children.length > 0) { |
57 var node = children.removeLast(); | 55 var node = children.removeLast(); |
58 children.addAll(node.children); | 56 children.addAll(node.children); |
59 hierarchyNodeList.add(node); | 57 hierarchyNodeList.add(node); |
60 } | 58 } |
61 | 59 |
62 return hierarchyNodeList; | 60 return hierarchyNodeList; |
63 } | 61 } |
64 | 62 |
65 T createNode(label, value, depth); | 63 T createNode(label, value, depth); |
66 | 64 |
67 /// Default accessor method for getting the list of children of the node. | 65 /// Default accessor method for getting the list of children of the node. |
68 static List hierarchyChildren(HierarchyNode node) => node.children; | 66 static List hierarchyChildren(HierarchyNode node) => node.children; |
69 | 67 |
70 /// Default accessor method for getting the value of the node. | 68 /// Default accessor method for getting the value of the node. |
71 static num hierarchyValue(HierarchyNode node) => node.value; | 69 static num hierarchyValue(HierarchyNode node) => node.value; |
72 | 70 |
73 /// Default sorting method for comparing node a and b. | 71 /// Default sorting method for comparing node a and b. |
74 static int hierarchySort(HierarchyNode a, HierarchyNode b) => | 72 static int hierarchySort(HierarchyNode a, HierarchyNode b) => |
75 b.value - a.value; | 73 b.value - a.value; |
76 } | 74 } |
77 | 75 |
78 abstract class HierarchyNode { | 76 abstract class HierarchyNode { |
79 /// The parent node, or null for the root. | 77 /// The parent node, or null for the root. |
80 HierarchyNode parent = null; | 78 HierarchyNode parent = null; |
81 | 79 |
82 /// The list of children nodes, or null for leaf nodes. | 80 /// The list of children nodes, or null for leaf nodes. |
83 List children = []; | 81 List<HierarchyNode> get children; |
84 | 82 |
85 /// The label to show for each block of hierarchy | 83 /// The label to show for each block of hierarchy |
86 String label = ''; | 84 String label = ''; |
87 | 85 |
88 /// The node value, as returned by the value accessor. | 86 /// The node value, as returned by the value accessor. |
89 dynamic value; | 87 dynamic value; |
90 | 88 |
91 /// The depth of the node, starting at 0 for the root. | 89 /// The depth of the node, starting at 0 for the root. |
92 int depth = 0; | 90 int depth = 0; |
93 } | 91 } |
OLD | NEW |