TikZ 树上的渐变颜色

TikZ 树上的渐变颜色

Mindmap创建由父/子颜色组成的连接线。是否可以将这种效果应用于普通的父/子树,以使用相应颜色的渐变连接父节点和子节点?在一个基本的示例中

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,trees}
\begin{document}

\begin{tikzpicture}
\node[circle,fill=blue] (par) at (0,0) {Parent}
    [level distance=4cm,sibling angle=25,clockwise from=80]
    child {node[circle,fill=red] (child1) {Child 1}}
    child {node[circle,fill=yellow] (child2) {Child 2}}
    child {node[circle,fill=green] (child3) {Child 3}};
\end{tikzpicture}

\end{document}

在此处输入图片描述

如何制作蓝色...红色渐变线条作为连接父级和第一个子级的线,蓝色...黄色渐变线条作为连接第二个子级的线,等等。

答案1

我不知道如何用树和孩子来做到这一点,但可以这样做:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,trees}
\usetikzlibrary{decorations}
\begin{document}

\makeatletter

\pgfkeys{/pgf/decoration/.cd,
         start color/.store in =\startcolor,
         end color/.store in   =\endcolor
}

\pgfdeclaredecoration{color change}{initial}{
 \state{initial}[width=0pt, next state=line, persistent precomputation={%
   \pgfmathdivide{50}{\pgfdecoratedpathlength}%
   \let\increment=\pgfmathresult%
   \def\x{0}%
 }]{}
 \state{line}[width=.5pt,   persistent postcomputation={%
     \pgfmathadd@{\x}{\increment}%
     \let\x=\pgfmathresult%
   }]{%
   \pgfsetarrows{-}%
   \pgfpathmoveto{\pgfpointorigin}%
   \pgfpathlineto{\pgfqpoint{.75pt}{0pt}}%
   \pgfsetstrokecolor{\endcolor!\x!\startcolor}%
   \pgfusepath{stroke}%
 }
 \state{final}{%
   \pgfpathmoveto{\pgfpointorigin}%
   \color{\endcolor!\x!\startcolor}%
   \pgfusepath{stroke}% 
 }}
\makeatother

\begin{tikzpicture}[every node/.style={circle,minimum width=1cm,font=\small},line width=1.6pt] 
  \node [fill=blue] (P) {Parent};
  \node [fill=red]    (C1) at (0:3)  {Child 1};
  \node [fill=yellow] (C2) at (30:3) {Child 2}; 
  \node [fill=green]  (C3) at (60:3) {Child 3};  
    \draw[line width=2pt, 
          decoration={color change, start color=blue, end color=red},
          decorate] (P) -- (C1) ;
\draw[line width = 2pt, 
      decoration = {color change, start color=blue, end color=yellow}, 
          decorate] (P) -- (C2) ;   
\draw[line width = 2pt, 
      decoration = {color change, start color=blue, end color=green}, 
          decorate] (P) -- (C3) ;   
\end{tikzpicture}

\end{document} 

在此处输入图片描述

相关内容