2010-11-09 40 views
7

我有時會遇到由於重疊而導致TreeForm中的標籤無法讀取的問題。下面是一個例子,任何人都可以看到一種擺脫重疊的方法嗎?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

貝利薩留的解決方案有助於重疊,但失去的工具提示,即與

 
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[[email protected], StandardForm], 
     TooltipBox[x__] -> x, 7, Heads -> True], 
    TagBox[x__, y__] -> DisplayForm[[email protected]{x}], Heads -> True]; 
    i = 0; 
    TreeForm[exp, 
    VertexRenderingFunction -> ({Tooltip[ 
     Inset[Rasterize[Text[" " <> [email protected]#2 <> " "], 
      Background -> LightBlue], #1], tooltipText[[i++]]]} &)]]; 
+0

你在說這個http://stackoverflow.com/q/4091728/421225嗎? – 2010-11-09 21:12:15

+0

該示例的表達式來自此處,當然 – 2010-11-09 22:36:16

+0

有一個['Tooltip'](http://reference.wolfram.com/mathematica/ref/Tooltip.html)指令,我希望您可以用它來照顧,如果你能弄清楚要放入它的字符串... – Cascabel 2010-11-09 22:40:59

回答

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[[email protected], StandardForm], TooltipBox[x__] -> x, 
    7, Heads -> True], TagBox[x__, y__] -> DisplayForm[[email protected]{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[[email protected], StandardForm], 
     TooltipBox[x__] -> x, 7, Heads -> True], 
    TagBox[x__, y__] -> DisplayForm[[email protected]{x}], Heads -> True]; 

    i = 0; 

    TreeForm[list, 
    EdgeRenderingFunction -> ({Red, Line[#1]} &), 
    VertexRenderingFunction -> ({White, EdgeForm[Black], {}, Black, 
     Tooltip[ 
     Inset[Rasterize[Text[" " <> [email protected]#2 <> " "], 
      Background -> LightBlue], #1], tooltipText[[i++]]]} &)] 
    ]; 

list // myTreeForm 

輸出:

alt text

編輯4 ...和最後一個

整理了代碼,刪除在那裏假函數和變量只是使事情變得複雜:

myTreeForm[list_] := Module[{tooltipText, i}, 

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

HTH!

+0

這很有用...一個缺點是它丟失了ToolTip,並在StandardForm中添加了子樹,在編輯中添加了示例 – 2010-11-09 19:06:18

+0

@Yaroslav我不明白「丟失Tooltip和StandardForm中的子樹」是什麼意思。你的預期結果是什麼? – 2010-11-09 21:23:16

+0

添加圖片示例 – 2010-11-09 21:27:05

0

看起來好像選項VertexCoordinateRules可能是您最好的希望。