Family tree layout with Dot/GraphViz

前端 未结 6 1402
慢半拍i
慢半拍i 2020-12-22 17:33

I am trying to draw a family tree with Dot and GraphViz.

This is what I currently have:

# just graph set-up
digraph simpsons {
ratio = \"auto\"
minc         


        
6条回答
  •  时光取名叫无心
    2020-12-22 17:56

    I don’t think you can take an arbitrary family tree and auto-generate a dot file where it always looks good in GraphViz.

    But I think you can always make it look good if you:

    • Use the rank=same other answers mentioned to get the 'T' connections desired by the OP
    • Use the ordering trick Brian Blank did to prevent weird lines
    • Assume no second marriages and half-siblings
    • Draw only a subset of the tree that obeys the following rules:
      • Let S be the “center” person
      • If S has siblings, make sure S is to right of all of them.
      • If S has a spouse and the spouse has siblings, make sure the spouse is to the left of all his/her siblings.
      • Don’t show nephews, nieces, aunts or uncles of S or S’s spouse
      • Don’t show spouses of siblings
      • Don’t show spouses of spouse’s siblings
      • Show children of S, but not their spouses or children
      • Show parents of S and parents of spouse

    This will end up showing no more than 3 generations at once, with S in the middle generation.

    In the picture below S=Homer (slightly modified from Brian Blank's version):

    digraph G {
      edge [dir=none];
      node [shape=box];
      graph [splines=ortho];
    
      "Herb"      [shape=box, regular=0, color="blue", style="filled" fillcolor="lightblue"] ;
      "Homer"     [shape=box, regular=0, color="blue", style="bold, filled" fillcolor="lightblue"] ;
      "Marge"     [shape=oval, regular=0, color="red", style="filled" fillcolor="pink"] ;
      "Clancy"    [shape=box, regular=0, color="blue", style="filled" fillcolor="lightblue"] ;
      "Jackeline" [shape=oval, regular=0, color="red", style="filled" fillcolor="pink"] ;
      "Abraham"   [shape=box, regular=0, color="blue", style="filled" fillcolor="lightblue"] ;
      "Mona"      [shape=oval, regular=0, color="red", style="filled" fillcolor="pink"] ;
      "Patty"     [shape=oval, regular=0, color="red", style="filled" fillcolor="pink"] ;
      "Selma"     [shape=oval, regular=0, color="red", style="filled" fillcolor="pink"] ;
      "Bart"      [shape=box, regular=0, color="blue", style="filled" fillcolor="lightblue"] ;
      "Lisa"      [shape=oval, regular=0, color="red", style="filled" fillcolor="pink"] ;
      "Maggie"    [shape=oval, regular=0, color="red", style="filled" fillcolor="pink"] ;
    
      a1 [shape=diamond,label="",height=0.25,width=0.25];
      b1 [shape=circle,label="",height=0.01,width=0.01];
      b2 [shape=circle,label="",height=0.01,width=0.01];
      b3 [shape=circle,label="",height=0.01,width=0.01];
      {rank=same; Abraham -> a1 -> Mona};
      {rank=same; b1 -> b2 -> b3};
      {rank=same; Herb; Homer};
      a1 -> b2
      b1 -> Herb
      b3 -> Homer
    
      p1 [shape=diamond,label="",height=0.25,width=0.25];
      q1 [shape=circle,label="",height=0.01,width=0.01];
      q2 [shape=circle,label="",height=0.01,width=0.01];
      q3 [shape=circle,label="",height=0.01,width=0.01];
      {rank=same; Homer -> p1 -> Marge};
      {rank=same; q1 -> q2 -> q3};
      {rank=same; Bart; Lisa; Maggie};
      p1 -> q2;
      q1 -> Bart;
      q2 -> Lisa;
      q3 -> Maggie;
    
      x1 [shape=diamond,label="",height=0.25,width=0.25];
      y1 [shape=circle,label="",height=0.01,width=0.01];
      y2 [shape=circle,label="",height=0.01,width=0.01];
      y3 [shape=circle,label="",height=0.01,width=0.01];
      {rank=same; Clancy -> x1 -> Jackeline};
      {rank=same; y1 -> y2 -> y3};
      {rank=same; Patty; Selma; Marge};
      x1 -> y2;
      y1 -> Marge;
      y2 -> Patty;
      y3 -> Selma;
    }
    

    This yields the following tree by GraphViz (with annotations I added with Power Point): enter image description here

提交回复
热议问题