TreeForm without overlapping

Sometimes I run into the problem of the inability to read labels in TreeForm due to overlay. The following is an example. Can anyone see a way to get rid of the overlap?

 {{4, 5, 6}, {{{{2, 4, 5, 6}, {{{{1, 2, 4}, {}}, {{2, 3, 6}, {}}}}, {{4, 
      5, 6, 8}, {{{4, 7, 8}, {}}, {{6, 8, 9}, {}}}}}} // TreeForm


(source: yaroslavvb.com )

The Belisarius solution helps with the overlay, but loses the tooltips, i.e. compares with

 TreeForm [Hold [
   GraphPlotHighlight [edges: {((_ -> _) | {_ -> _, _}) ...}, 
     hl: {___}: {}, opts: OptionsPattern []]: = 
    Module [{verts, coords, g, sub}, 5]]] 


(source: yaroslavvb.com )

Update answer 11/12 I ended up using the code below (Belisarius code with a little fix)

myTreeForm[exp_] := Module[{tooltipText, i}, tooltipText = Cases[Cases[MakeBoxes[TreeForm@exp, StandardForm], TooltipBox[x__] -> x, 7, Heads -> True], TagBox[x__, y__] -> DisplayForm[First@{x}], Heads -> True]; i = 0; TreeForm[exp, VertexRenderingFunction -> ({Tooltip[ Inset[Rasterize[Text[" " <> ToString@#2 <> " "], Background -> LightBlue], #1], tooltipText[[i++]]]} &)]]; 
+7
graph wolfram-mathematica
source share
2 answers

I have done this before, but have not generalized the result.

  rectOffset = {.25,.1}; fontSize = 10 TreeForm[list, VertexRenderingFunction -> ({White, EdgeForm[Black], Rectangle[#1 - rectOffset, #1 + rectOffset], Black, Text[ Style[#2, fontSize], #1]} &)] 

alt text

Edit using tooltips

Using a “different approach”

The code is dirty, sorry, no time to clean it right now

 rectOffset = {.33, .1}; fontSize = 9; p = Cases[ Cases[MakeBoxes[TreeForm@list, StandardForm], TooltipBox[x__] -> x, 7, Heads -> True], TagBox[x__, y__] -> DisplayForm[First@{x}], Heads -> True]; i = 0; TreeForm[list, VertexRenderingFunction -> ({White, EdgeForm[Black], Rectangle[#1 - rectOffset, #1 + rectOffset], Black, Tooltip[Text[Style[#2, fontSize], #1], p[[i++]]]} &)] 

Exit

alt text

Edit 2

I think this version is better:

 Clear["Global`*"]; list = Hold[ GraphPlotHighlight[edges : {((_ -> _) | {_ -> _, _}) ...}, hl : {___} : {}, opts : OptionsPattern[]] := Module[{verts, coords, g, sub}, 5]]; myTreeForm[exp_] := Module[{ps, tooltipText, i}, ps[text_] := Rasterize[Text[Style[text]], "RasterSize"]; tooltipText = Cases[Cases[MakeBoxes[TreeForm@list, StandardForm], TooltipBox[x__] -> x, 7, Heads -> True], TagBox[x__, y__] -> DisplayForm[First@{x}], Heads -> True]; i = 0; TreeForm[list, EdgeRenderingFunction -> ({Red, Line[#1]} &), VertexRenderingFunction -> ({White, EdgeForm[Black], {}, Black, Tooltip[ Inset[Rasterize[Text[" " <> ToString@#2 <> " "], Background -> LightBlue], #1], tooltipText[[i++]]]} &)] ]; list // myTreeForm 

Exit:

alt text

Change 4 ... and last

Removed the code, removed the false functions and variables that just complicated:

 myTreeForm[list_] := Module[{tooltipText, i}, tooltipText = Cases[Cases[MakeBoxes[TreeForm@list, StandardForm], TooltipBox[x__] -> x, 7, Heads -> True], TagBox[x__, y__] -> DisplayForm[First@{x}], Heads -> True]; i = 0; TreeForm[list, VertexRenderingFunction -> ({Tooltip[Inset[Rasterize[Text[" " <> ToString@#2 <> " "], Background -> LightBlue], #1], tooltipText[[i++]]]} &) ] ]; 

NTN!

+7
source share

It seems that the VertexCoordinateRules option might be the best hope.

0
source share

All Articles