Tikz 逐层绘图

Tikz 逐层绘图

我想基于这个例子。但我目前停留在这一点上:

  1. 顶层应该左右对齐
  2. 我认为代码可以更好

这是我的 MWE:

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz} 
    \usetikzlibrary{chains}
    \usetikzlibrary{positioning}
\definecolor{mycolor}{RGB}{8,108,131}

\begin{document} 
    \begin{tikzpicture}[
        scale=0.75,
        start chain=1 going below, 
        start chain=2 going right,
        node distance=1mm,
        desc/.style={
            scale=0.75,
            on chain=2,
            rectangle,
            rounded corners,
            draw=black, 
            very thick,
            text centered,
            text width=8cm,
            minimum height=12mm,
            fill=mycolor!30
            },
        1/.style={
            fill=mycolor!10
        },
        2/.style={
            fill=mycolor!30
        },
        3/.style={
            fill=mycolor!50
        },
        4/.style={
            fill=mycolor!70
        },
        5/.style={
            fill=mycolor!70
        },
        level/.style={
            scale=0.75,
            on chain=1,
            minimum height=12mm,
            text width=2cm,
            text centered
        },
        every node/.style={font=\sffamily}
    ]

    % Levels
    \node [level] (Level 5) {Layer 5};
    \node [level] (Level 4) {Layer 4};
    \node [level] (Level 3) {Layer 3};
    \node [level] (Level 2) {Layer 2};
    \node [level] (Level 1) {Layer 1};

    % Descriptions
    \chainin (Level 5); % Start right of Level 5
    \node [desc, 5] (5) {Applications layer};
    \node [ scale=0.75,
            rectangle,
            rounded corners,
            draw=black, 
            very thick,
            text centered,
            text width=8cm,
            minimum height=12mm,
            fill=mycolor!30,
           text width=3.5cm, xshift=2.25cm,above left= of 5] {TLS};
    \node [scale=0.75,
            rectangle,
            rounded corners,
            draw=black, 
            very thick,
            text centered,
            text width=8cm,
            minimum height=12mm,
            fill=mycolor!30,text width=3.5cm, xshift=-4.5cm,above right= of 5]  {TLS in HTTPS};

    \node [desc, 4, continue chain=going below] (4) {Transport layer};
    \node [desc,3] (3) {Network layer};
    \node [desc,2] (2) {Data link layer};
    \node [desc,1] (1) {Physical layer};
    \end{tikzpicture}
\end{document} 

这给了我:

输出

谢谢您的帮助

答案1

在此处输入图片描述

并且相当缩短的代码:

\documentclass[tikz, margin=3mm]{standalone}
\usepackage{tikz}
    \usetikzlibrary{chains,
                    positioning}
\definecolor{mycolor}{RGB}{8,108,131}

\begin{document}
    \begin{tikzpicture}[
node distance = 1mm and 0mm,
       start chain = going below,
  desc/.style args = {#1/#2}{
            rectangle, rounded corners, draw, very thick,
            fill=mycolor!#1,
            text width=8cm, align=center,
            minimum height=12mm,
            label=left:Layer #2,
            on chain
                            },
      level/.style = {
            rectangle, rounded corners, draw, very thick,
            fill=mycolor!30,
            text width=3cm, align=center,
            minimum height=12mm},
              font = \sffamily
                            ]
\node (n5) [desc=70/5]  {Applications layer};
    \node [level,above right=of n5.north west] {TLS};
    \node [level,above  left=of n5.north east] {TLS in HTTPS};
\node   [desc=70/4]     {Transport layer};
\node   [desc=50/3]     {Network layer};
\node   [desc=30/2]     {Data link layer};
\node   [desc=10/1]     {Physical layer};
    \end{tikzpicture}
\end{document}

笔记:

  • 用于定位顶部两个节点的positioning库。它们位于节点西北角左上方应用层以及其东北角右上方
  • 所有其他具有样式的节点desc都放置在链中
  • styledesc有两个参数,第一确定填充的颜色,第二确定层的级别,以标签的形式写到节点上。

相关内容