代码之家  ›  专栏  ›  技术社区  ›  Yaroslav Bulatov

无重叠的树形

  •  7
  • Yaroslav Bulatov  · 技术社区  · 15 年前

    {{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++]]]} &)]];
    
    2 回复  |  直到 6 年前
        1
  •  7
  •   Dr. belisarius    15 年前

    我以前也这么做过,但从来没有概括过结果。

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

    啊!

        2
  •  0
  •   High Performance Mark    15 年前

    VertexCoordinateRules 也许是你最大的希望。