graph - TreeForm 无重叠

标签 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


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

最佳答案

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

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

哼!

关于graph - TreeForm 无重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4136844/

相关文章:

c++ - 使用 Boost.Graph 求解最长路径

java - JFreeChart x 轴刻度

java - 如何在 Java 中制作 Treelayout 图

c++ - Boost 的伯努利数与 Mathematica 不同

wolfram-mathematica - 如何在 Mathematica 中隐藏评估结果?

grid - Mathematica - "Triangulation"- 在图表上绘制三角形网格

wolfram-mathematica - Mathematica中的 “hand-drawn”对象

java - Java swing 中图形的标记

animation - g.raphael 条形图和更新/动画值

graphics - 为什么在 Mathematica 中使用图形函数时点会消失