@@ -33,11 +33,17 @@ suite =
3333 tree
3434 |> doLayout
3535 |> expectNodesToBeOrdered
36+ , Test . fuzz fuzzHierarchy " Rule 7: Laying out a mirrored tree gives the horizontal flip of the original layout" <|
37+ \ tree ->
38+ Expect . equalLists ( nodeCenters ( mirror tree)) ( flippedNodeCenters tree)
3639
3740 -- sanity checks
3841 , test1
3942 , test2
4043 , test3
44+
45+ -- regression for https://github.com/gampleman/elm-visualization/issues/190
46+ , test190
4147 ]
4248
4349
@@ -169,6 +175,39 @@ doLayout =
169175 Hierarchy . tidy [ Hierarchy . nodeSize ( \ ( _, w, h ) -> ( w, h )) , Hierarchy . parentChildMargin 1 , Hierarchy . peerMargin 1 ]
170176
171177
178+ {- | Reverse the order of every node's children, top to bottom.
179+ -}
180+ mirror : Tree a -> Tree a
181+ mirror t =
182+ Tree . tree ( Tree . label t) ( List . reverse ( List . map mirror ( Tree . children t)))
183+
184+
185+ {- | The sorted multiset of node-center positions. The layout reports left-edge
186+ x (center - width / 2), so we add width / 2 back to recover centers.
187+ -}
188+ nodeCenters : Tree ( Int , Float , Float ) -> List ( Float , Float )
189+ nodeCenters tree =
190+ doLayout tree
191+ |> Tree . toList
192+ |> List . map ( \ n -> ( round3 ( n. x + n. width / 2 ) , round3 n. y ))
193+ |> List . sort
194+
195+
196+ {- | The original layout's node centers, flipped horizontally about x = 0.
197+ -}
198+ flippedNodeCenters : Tree ( Int , Float , Float ) -> List ( Float , Float )
199+ flippedNodeCenters tree =
200+ doLayout tree
201+ |> Tree . toList
202+ |> List . map ( \ n -> ( round3 - ( n. x + n. width / 2 ) , round3 n. y ))
203+ |> List . sort
204+
205+
206+ round3 : Float -> Float
207+ round3 v =
208+ toFloat ( round ( v * 1000 )) / 1000
209+
210+
172211formatTree : FinishedLayout -> String
173212formatTree =
174213 let
@@ -210,3 +249,36 @@ test3 =
210249 Tree . tree ( 0 , 1 , 1 ) [ Tree . tree ( 0 , 1 , 1 ) [ Tree . tree ( 0 , 1 , 10 ) [ Tree . singleton ( 0 , 1 , 1 ) , Tree . singleton ( 0 , 1 , 1 ) ], Tree . tree ( 0 , 1 , 1 ) [ Tree . singleton ( 0 , 1 , 1 ) ], Tree . tree ( 0 , 1 , 10 ) [ Tree . singleton ( 0 , 10 , 1 ) ] ] ]
211250 |> doLayout
212251 |> expectNoOverlapNodes
252+
253+
254+
255+ {-
256+ See <https://github.com/gampleman/elm-visualization/issues/190>.
257+ -}
258+
259+
260+ test190 : Test
261+ test190 =
262+ Test . test " Issue 190: slack between a node's children is distributed evenly" <|
263+ \ () ->
264+ let
265+ leaf =
266+ Tree . singleton ( 0 , 1 , 1 )
267+
268+ xs =
269+ Tree . tree ( 0 , 1 , 1 )
270+ [ Tree . tree ( 0 , 1 , 1 ) [ leaf, leaf, leaf, leaf, leaf, leaf, Tree . tree ( 0 , 1 , 1 ) [ leaf ] ]
271+ , leaf
272+ , leaf
273+ , Tree . tree ( 0 , 1 , 1 ) [ leaf ]
274+ , leaf
275+ , Tree . tree ( 0 , 1 , 1 ) [ Tree . tree ( 0 , 1 , 1 ) [ leaf, leaf, leaf, leaf, leaf, leaf, leaf ] ]
276+ ]
277+ |> doLayout
278+ |> Tree . children
279+ |> List . map ( \ c -> ( Tree . label c) . x)
280+
281+ gaps =
282+ List . map2 ( \ p q -> q - p) xs ( List . drop 1 xs)
283+ in
284+ Expect . equalLists ( List . map round3 gaps) [ 3.067 , 3.067 , 3.067 , 2.4 , 2.4 ]
0 commit comments