无重叠的树形

发布于 2024-10-01 13:43:16 字数 1495 浏览 2 评论 0原文

我有时会遇到 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


(来源:yaroslavvb.com

贝利撒留的解决方案有助于解决重叠问题,但丢失工具提示,即与

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


(来源:yaroslavvb.com

答案11/12 更新 我最终使用了下面的代码(贝利萨留的代码进行了一些小修复)

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

I sometimes run into the problem of labels in TreeForm being unreadable because of overlap. An example is below, can anyone see a way to get rid of 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)

Belisarius' solution helps with overlap, but loses Tooltips, ie compare with

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


(source: yaroslavvb.com)

Answer update 11/12
I ended up using code below (belisarius' code with a minor 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++]]]} &)]];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑疤怪 2024-10-08 13:43:16

我以前也这样做过,但从未概括过结果。

    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!

I did this before, but never 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 With Tooltips

Using a "different approach"

Code is dirty, sorry no time to clean it up 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++]]]} &)]  

Output

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  

Output:

alt text

Edit 4 ... and last one

Cleaned up code, remove spurious functions and variables that were there just to complicate things:

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!

高速公鹿 2024-10-08 13:43:16

看来选项 VertexCooperativeRules 可能是您最好的希望。

It looks as if the option VertexCoordinateRules may be your best hope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文