TikZ/Forest:如何为父/子/孙分支节点唯一地着色?

TikZ/Forest:如何为父/子/孙分支节点唯一地着色?

跟随我的上一个问题例如,在这个 MWE 中,如何为每个父/子/孙分支设置一个独特的颜色,使得颜色在向下时逐渐变暗或变亮?

换句话说,如果我想为节点Parent 1Child 1和设定蓝色Grandchild 1,我希望它们的填充颜色分别为blue!80blue!50blue!20。同样,我想为其他分支设定其他颜色,并且各个节点应以相同的方式着色。

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows.blur}
\begin{document}
\newlength\gap
\setlength\gap{10mm}
\begin{forest}
  forked edges,
  for tree={
    draw=blue!80!darkgray,
    fill=blue!80!darkgray!25,
    rounded corners,
    minimum width=(\textwidth-6*\gap)/3,
    minimum height=4ex,
    edge={-Latex},
    font=\sffamily,
    text centered,
    blur shadow,
  },
  where={level()<=1}{%
    parent anchor=children,
    l sep+=10pt,
    s sep'+=10pt,
   }{%
     folder,
     grow'=0,
     l sep+=0pt,% length of edge to grand child
     if level=2{%
       before typesetting nodes={child anchor=north},
       !u.s sep'+=10pt,
       edge path'={%
         (!u.parent anchor) -- ++(0,-10pt) -| (.child anchor)
       },
     }{},
   }
    [Grandparent
        [Parent 1
            [Child 1
                [Grandchild 1]
            ]
        ]
        [Parent 2
            [Child 2
                [Grandchild 2]
            ]
        ]
        [Parent 3
            [Child 3
                [Grandchild 3]
            ]
        ]
    ]
\end{forest}
\end{document}

在此处输入图片描述

答案1

这是一个不同的版本(稍微简单一些):

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows.blur}
\begin{document}
\newlength\gap
\setlength\gap{10mm}
\forestset{parent color/.style args={#1}{
    {fill=#1},
    for tree={fill/.wrap pgfmath arg={#1!##1}{1/level()*80},draw=#1!80!darkgray}},
    root color/.style args={#1}{fill={{#1!60!gray!25},draw=#1!80!darkgray}}
}
\begin{forest}
  forked edges,
  for tree={%
    rounded corners,
    minimum width=(\textwidth-6*\gap)/3,
    minimum height=4ex,
    edge={-Latex},
    font=\sffamily,
    text centered,
    blur shadow,
  },
  where={level()<=1}{%
    parent anchor=children,
    l sep+=10pt,
    s sep'+=10pt,
   }{%
     folder,
     grow'=0,
     l sep+=2pt,% length of edge to grand child
     if level=2{%
       before typesetting nodes={child anchor=north},
       !u.s sep'+=10pt,
       edge path'={%
         (!u.parent anchor) -- ++(0,-10pt) -| (.child anchor)
       },
     }{},
   },
      [Grandparent,root color={brown}
        [Parent 1, parent color={red}
            [Child 1
                [Grandchild 1]
            ]
        ]
        [Parent 2,parent color={green}
            [Child 2
                [Grandchild 2]
            ]
        ]
        [Parent 3,parent color={yellow}
            [Child 3
                [Grandchild 3]
            ]
        ]
    ]
\end{forest}
\end{document}

代码输出

答案2

您可以使用以下样式branch shade=from <colour> to <colour>来为树枝添加阴影。

[编辑以支持诸如保留第二种颜色blue!80规范隐含的颜色。]white

新版本的代码还family tree为树创建了一种样式,该样式应遵循此结构模式。twist=<integer>设置应发生更改的级别,以便更高级别的子级将设置folder样式,即目录样式。默认值为2

正如 Alan Munn 指出的,这实际上与以前相同(例如,也通过.max处理程序隐式地依赖于 nodewalk)。

注意

l sep+=0pt,

对任何东西都没有影响。它给维度增加了一个零长度,这显然等于什么都没有。

然后我们可以写

\begin{forest}
  family tree,
   [Grandparent, left color=cyan, right color=SpringGreen, middle color=Pink, draw=Silver
     [Parent 1, branch shade=from cyan to blue
       [Child 1
         [Grandchild 1]
       ]
     ]
     [Parent 2, branch shade=from Pink to WildStrawberry
       [Child 2
         [Grandchild 2]
       ]
     ]
     [Parent 3, branch shade=from SpringGreen to ForestGreen
       [Child 3
         [Grandchild 3]
       ]
     ]
   ]
\end{forest}

生成由此答案的原始版本中的代码版本创建的树。

彩色树

正如你所看到的,我不知道如何处理根。

或者我们可以写

\begin{forest}
  family tree,
   [Grandparent, fill=darkgray, text=Silver, double=Silver, draw=darkgray
     [Parent 1, branch shade=from blue!80 to blue!20
       [Child 1
         [Grandchild 1]
       ]
     ]
     [Parent 2, branch shade=from WildStrawberry!80 to WildStrawberry!20
       [Child 2
         [Grandchild 2]
       ]
     ]
     [Parent 3, branch shade=from ForestGreen!80 to ForestGreen!20
       [Child 3
         [Grandchild 3]
       ]
     ]
   ]
\end{forest}

隐式地使用white并以不同的方式设置根节点的样式。

隐含使用白色和平原根

完整代码:

\documentclass[border=10pt,multi,tikz,dvipsnames,svgnames,rgb]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{arrows.meta,shadows.blur}
\forestset{
  declare dimen register=gap,
  gap'=10mm,
  declare count register=twist,
  twist'=2,
  family tree/.style={
    forked edges,
    for tree={
      rounded corners,
      minimum width/.wrap pgfmath arg={##1}{(\textwidth-6*(gap))/3},
      minimum height=4ex,
      edge={-Latex},
      font=\sffamily,
      text centered,
      blur shadow,
      edge=thick,
    },
    where={level()<(twist)}{%
      parent anchor=children,
      l sep+=10pt,
      s sep'+=10pt,
    }{%
      folder,
      grow'=0,
      l sep'+=2pt,
      if={level()==(twist)}{%
        before typesetting nodes={child anchor=north},
        !u.s sep'+=10pt,
        edge path'={%
          (!u.parent anchor) -- ++(0,-10pt) -| (.child anchor)
        },
      }{},
    },
  },
  branch shade/.style args={from #1 to #2}{
    before typesetting nodes={
      tempcountc/.max={level}{current,tree},
      tempcountb/.option=level,
      tempcounta=(tempcountc)-(tempcountb)+1,
      temptoksa/.option=name,
      TeX/.wrap pgfmath arg={
        \colorlet{##1col1}{#1}
        \colorlet{##1col2}{#2}
      }{name()},
      for tree={
        rounded corners,
        top color/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb))/(tempcounta))}{(temptoksa)},
        +edge/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb))/(tempcounta))}{(temptoksa)},
        bottom color/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb)+1)/(tempcounta))}{(temptoksa)},
        draw/.wrap 2 pgfmath args={##2col2!##1!##2col1}{100*((level()-(tempcountb)+1)/(tempcounta))}{(temptoksa)},
        thick,
      },
    }
  },
}
\begin{document}
\begin{forest}
  family tree,
   [Grandparent, left color=cyan, right color=SpringGreen, middle color=Pink, draw=Silver
     [Parent 1, branch shade=from cyan to blue
       [Child 1
         [Grandchild 1]
       ]
     ]
     [Parent 2, branch shade=from Pink to WildStrawberry
       [Child 2
         [Grandchild 2]
       ]
     ]
     [Parent 3, branch shade=from SpringGreen to ForestGreen
       [Child 3
         [Grandchild 3]
       ]
     ]
   ]
\end{forest}
\begin{forest}
  family tree,
   [Grandparent, fill=darkgray, text=Silver, double=Silver, draw=darkgray
     [Parent 1, branch shade=from blue!80 to blue!20
       [Child 1
         [Grandchild 1]
       ]
     ]
     [Parent 2, branch shade=from WildStrawberry!80 to WildStrawberry!20
       [Child 2
         [Grandchild 2]
       ]
     ]
     [Parent 3, branch shade=from ForestGreen!80 to ForestGreen!20
       [Child 3
         [Grandchild 3]
       ]
     ]
   ]
\end{forest}
\end{document}

相关内容