使用一个命令创建的树的边缘叠加

使用一个命令创建的树的边缘叠加

这个问题的标题可能不太准确(我目前还不知道有更好的标题),我愿意接受建议。

我使用命令将树的节点链接在一起[edge from parent fork down]。但我希望这些边缘在需要时可见。我可以visible on按照建议使用命令使顶点消失在这篇文章中,但我无法摆脱边缘。

如果您运行下面的代码,您就会明白我的意思。第一页是完整的树。然后在第 2 页,我想要的是提取树的一部分以供参考。所以我删除了顶点,但正如您所见,一些边仍然存在(树顶部的边),这不是我想要的。我只希望树的所需部分可见。
在第 3 页和第 4 页上,还有其他示例展示了删除顶点时边的问题。

所以确切地说,我希望边不必与顶点同时出现。我试过了,[edge from parent fork down, visible on = ]但在这种情况下,所有的树都消失了。
有什么建议吗?

最后一个问题,更多的是出于好奇。正如你所见,我用了很多次visible on,有没有其他方法可以做到这一点,而不用多次使用相同的命令?

\documentclass{beamer}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary[arrows]
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{calc}
\usetikzlibrary{trees, chains, positioning, through, arrows}
\usetikzlibrary{backgrounds}

\begin{document}

\tikzset{
    invisible/.style={opacity=0},
    visible on/.style={alt=#1{}{invisible}},
    alt/.code args={<#1>#2#3}{%
      \alt<#1>{\pgfkeysalso{#2}}{\pgfkeysalso{#3}} % \pgfkeysalso doesn't change the path
    },
  }

\newcommand\ppbb{path picture bounding box}

\begin{frame}

\begin{tikzpicture}[scale=0.6,
    node distance = 2mm and 2mm,
    start chain = going below,
    base/.style = {rectangle, draw, scale=0.6,
               fill=####1,
               inner sep=0mm,
               minimum height = 9mm, text width=25mm, align=center,
               font=\linespread{.9}\selectfont
               },
    T/.style = {path picture={\fill[blue!50!black]
                ([xshift=-4mm]\ppbb.north east) -| ([yshift=-4mm]\ppbb.north east) -- cycle;
                             }% end of path picture
               },
boxB/.style = {base=####1, text width=20mm},
% on chain
 box/.style = {boxB=####1, on chain},
% tree's parameters
  level distance = 18mm,
sibling distance = 32mm,
  level 2/.style = {sibling distance = 25mm},
  level 3/.style = {sibling distance = 22mm}
                        ]
% first part of image
% start of tree
\node[base=teal!50,visible on=<1>] {SCR}
    [edge from parent fork down]
    child { node[base=teal!30,visible on=<1>]      {Ajustement}}
    child { node[base=teal!30,visible on=<1>]    {BSCR}
        child { node[boxB=teal!10,visible on=<1>]    (Rba)   {Marché}}
        child { node[boxB=teal!10,visible on=<{1,2,4}>]    (Rbb)   {Santé}
            child { node[boxB=blue!20,visible on=<1-4>](Rca)   {SLT}}
            child { node[boxB=blue!20,visible on=<{1,2,4}>](Rcb)   {Catas\-trophe}}
            child { node[boxB=blue!20,visible on=<1-4>]  (Rcc)   {NSLT}}
                }
        child { node[boxB=teal!10,visible on=<1>]    (Rbc)  {Contre-partie}}
        child { node[boxB=teal!10,visible on=<1>]    (Rbd)  {Vie}}
        child { node[boxB=teal!10,visible on=<1>]      (Rbe)  {Non-vie}}
        child { node[boxB=teal!10,visible on=<1>]      (Rbf)  {Actifs incorporels}}
            }
    child { node[base=teal!10,visible on=<1>]  {Opérationnel}};
% Noeud Marché
{[start chain]
\coordinate[on chain,below=of Rba.west |- Rca.south]   (a);
\node[box=blue!20,visible on=<1>] (Rbaa) {Taux d'intérêt};
\node[box=blue!20,visible on=<1>] (Rbab) {Action};
\node[box=blue!20,visible on=<1>] (Rbac) {Immobilier};
\node[box=blue!20,visible on=<1>] (Rbad) {Spread};
\node[box=blue!20,visible on=<1>] (Rbae) {Change};
\node[box=blue!20,visible on=<1>] (Rbaf) {Concen\-tration};
\node[box=blue!20,visible on=<1>] (Rbag) {Illiquidité};
}
% Noeud Santé
{[start chain]
\coordinate[on chain,below=of Rcb.south west]   (b);
\node[box=blue!15,visible on=<1-3>] (Rbbaa)  {Mortalité};
\node[box=blue!15,visible on=<1-3>] (Rbbab)  {Longévité};
\node[box=blue!15,visible on=<1-3>] (Rbbac)  {Incapacité Invalidité};
\node[box=blue!15,visible on=<1-3>] (Rbbad)  {Rachat};
\node[box=blue!15,visible on=<1-3>] (Rbbae)  {Dépenses };
\node[box=blue!15,visible on=<1-3>] (Rbbaf)  {Révision};
}
% Noeud NSLT
{[start chain]
\coordinate[on chain,below=of Rcc.south east -| Rbc.east]   (c);
\node[box=blue!15,visible on=<1-3>] (Rbbca)  {Prime et réserve};
\node[box=blue!15,visible on=<1-3>] (Rbbcb)  {Rachat};
}
% Noeud Vie
{[start chain]
\coordinate[on chain,below left=of Rcc.south -| Rbe.west]    (d);
\node[box=blue!20,visible on=<1>] (Rbda) {Mortalité};
\node[box=blue!20,visible on=<1>] (Rbdb) {Longévité};
\node[box=blue!20,visible on=<1>] (Rbdc) {Incapacité Invalidité};
\node[box=blue!20,visible on=<1>] (Rbdd) {Rachat};
\node[box=blue!20,visible on=<1>] (Rbde) {Dépenses};
\node[box=blue!20,visible on=<1>] (Rbdf) {Révision};
\node[box=blue!20,visible on=<1>] (Rbdg) {Catas\-trophe};
}
% Noeud Non Vie
{[start chain]
\coordinate[on chain,below left=of Rcc.south -| Rbf.west]    (e);
\node[box=blue!20,visible on=<1>] (Rbea)   {Prime et réserve};
\node[box=blue!20,visible on=<1>] (Rbeb)   {Rachat};
\node[box=blue!20,visible on=<1>] (Rbec)   {Catas\-trophe};
}
% lines a
\coordinate[left=of Rbaa.west] (aa);
\visible<1>{\draw   (Rba) -- ++ (0,-0.9) -| (aa) -- (Rbaa);
\foreach \i [remember=\i as \ii (initially a)] in {b,c,...,g}
    \draw (aa |- Rba\ii) |- (Rba\i);}
% lines b
\visible<1-3>{\draw   (Rca) |- (Rbbaa);
\foreach \i [remember=\i as \ii (initially a)] in {b,c,...,f}
    \draw (Rca |- Rbba\ii) |- (Rbba\i);}
% lines d
\visible<1-3>{\draw   (Rcc) |- (Rbbca);
\draw   (Rcc) |- (Rbbcb);}
% lines e
\visible<1>{\draw   (Rbd) |- (Rbda);
\foreach \i [remember=\i as \ii (initially a)] in {b,c,...,g}
    \draw (Rbd |- Rbd\ii) |- (Rbd\i);}
% lines f
\visible<1>{\draw   (Rbe) |- (Rbea);
\foreach \i [remember=\i as \ii (initially a)] in {b,c}
    \draw (Rbe |- Rbe\ii) |- (Rbe\i);}

\node[boxB=teal!10] at (6,-13) {\rule[-0.05cm]{0cm}{0.45cm} module de risque};
\node[boxB=blue!20] at (9,-13) {\rule[-0.15cm]{0cm}{0.45cm} sous-module de risque};

\end{tikzpicture}

\end{frame}
\end{document}

答案1

您可以通过将树与父树分离来控制子树的可见性,如在如何将 tikz 树中的子节点分解为单独的节点元素?

\node (r) {Root}
  child {coordinate (left)}
  child {coordinate {right)}
  ;
\node (c1) at (left) {Left Child};
\node (c2) at (right) {Right Child};

然后您就可以使用\uncover每个单独的\node ...;命令。

然而,对于如此复杂的树,缺点是您必须复制粘贴每个子树,以便 tikz 正确计算大小和自动间距:

\node (r) {Root}
  child {node (left) {\phantom{Left Child}}}
  child {node (right) {\phantom{Right Child}}}
  ;
\node (c1) at (left) {Left Child};
\node (c2) at (right) {Right Child};

我想,也可以编写一个脚本来生成它。

相关内容