TreeForm没有重叠

时间:2010-11-09 17:54:04

标签: graph wolfram-mathematica

我有时遇到TreeForm中的标签因重叠而无法读取的问题。下面是一个例子,任何人都可以看到摆脱重叠的方法吗?

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

http://yaroslavvb.com/upload/treeform1.png

Belisarius'解决方案有助于重叠,但会丢失工具提示,即与

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

http://yaroslavvb.com/upload/mathematica-tooltip.png

回答更新11/12 我最终使用下面的代码(belisarius'代码带有一个小修复)

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++]]]} &)]];

2 个答案:

答案 0 :(得分:7)

我之前做过这个,但从未概括过结果。

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

alt text

使用工具提示修改

使用“不同的方法”

代码很脏,抱歉没时间立即清理它

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++]]]} &)]  

输出

alt text

编辑2

我认为这个版本更好:

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  

输出:

alt text

编辑4 ...和最后一个

清理代码,删除那些只是为了使事情变得复杂的虚假函数和变量:

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++]]]} &) 
   ]  
 ];   

HTH!

答案 1 :(得分:0)

看起来好像选项VertexCoordinateRules可能是您最大的希望。