如何在绘图中对节点/框进行分层?

如何在绘图中对节点/框进行分层?

我正在尝试制作一个显示系统中元素关系的图表,但我不知道如何分层任何内容。

到目前为止我已经得到:

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}

\begin{document}

\tikzstyle{narrow} = [draw, fill=blue!20, text width=2cm, text centered, minimum height=1cm]
\tikzstyle{wide}   = [draw, fill=orange!20, text width=7.5cm, text centered, minimum height=1.75cm, rounded corners]
\tikzstyle{tall}   = [draw, fill=green!20, text width=1.75cm, text centered, minimum height=7cm, rounded corners]
\tikzstyle{squar} = [draw, fill=yellow!10, text width=4.75cm, text centered, minimum height=3.75cm, rounded corners]

\def\blockdist{5}
\def\edgedist{5}

\begin{tikzpicture}
  \node (lib)     [tall]   {Library};
  \node at (3,-1) (tman)    [narrow] {tmon};
  \node (ksman)   [narrow] [right=.75cm of tman]   {ksmon};
  \node (stsp)    [narrow] [right=.75cm of ksman]  {stosp};
  \node (deamon)  [wide]   [below=.75cm of ksman]  {base};
  \node (imgan)   [narrow] [above=.75cm of tman]   {img-analysis};
  \node (thrman)  [narrow] [above=.75cm of imgan]  {thermal-analysis};
  \node (scrpts)  [squar]  [right=.75cm of thrman] {Scripts};
\end{tikzpicture}

\end{document}

我正在努力完成三件事:

  1. 我怎样才能在“ksmon”和“stosp”节点周围/后面放置一个盒子?
  2. 我怎样才能在上面创建的框和“tmon”节点周围/后面放置一个框?
  3. 如何将节点放入“脚本”节点中或之上?

答案1

我真的不知道你的问题出在哪里...
如果你真的想要位于后面的框tmonksmonstosp在节点之后绘制,你可以使用 PGF 的图层。

代码

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}
\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\tikzset{
    narrow/.style={draw, fill=blue!20, text width=2cm, text centered, minimum height=1cm},
    wide/.style={draw, fill=orange!20, text width=7.5cm, text centered, minimum height=1.75cm, rounded corners},
    tall/.style={draw, fill=green!20, text width=1.75cm, text centered, minimum height=7cm, rounded corners},
    squar/.style={draw, fill=yellow!10, text width=4.75cm, text centered, minimum height=3.75cm, rounded corners}
}

\def\blockdist{5}
\def\edgedist{5}
\begin{document}
\begin{tikzpicture}
  \node (lib)     [tall]   {Library};
  \node at (3,-1) (tman)    [narrow] {tmon};
  \node (ksman)   [narrow] [right=.75cm of tman]   {ksmon};
  \node (stsp)    [narrow] [right=.75cm of ksman]  {stosp};
  \node (deamon)  [wide]   [below=.75cm of ksman]  {base};
  \node (imgan)   [narrow] [above=.75cm of tman]   {img-analysis};
  \node (thrman)  [narrow] [above=.75cm of imgan]  {thermal-analysis};
  \node (scrpts)  [squar]  [right=.75cm of thrman] {Scripts};
  \begin{pgfonlayer}{background}
    \fill[red!25] ([shift={(-.35,.35)}]tman.north west) rectangle ([shift={(.35,-.35)}]stsp.south east);
    \fill[red!50] ([shift={(-.25,.25)}]ksman.north west) rectangle ([shift={(.25,-.25)}]stsp.south east);
  \end{pgfonlayer}
  \node[fill=blue!30,above=.4cm of scrpts.center] (scriptsnode) {Hello, I am a node!};
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述


也许您追求的是更自动化的方式;您可以使用fit库(\usetikzlibrary{fit})来实现,而不是rectangles现在您可以这样做:

  \begin{pgfonlayer}{background}
     \node [fill=red!50, inner sep=.35cm, fit=(tman)(stsp)] {};
     \node [fill=red!25, inner sep=.25cm, fit=(ksman)(stsp)] {};
%    \fill[red!25] ([shift={(-.35,.35)}]tman.north west) rectangle ([shift={(.35,-.35)}]stsp.south east);
%    \fill[red!50] ([shift={(-.25,.25)}]ksman.north west) rectangle ([shift={(.25,-.25)}]stsp.south east);
  \end{pgfonlayer}

相关内容