如何用 tikz 绘制这样的图表?

如何用 tikz 绘制这样的图表?

我知道这是一个糟糕的问题,但我不知道该怎么办,因为我没有多少时间了。

我的论文需要这样的图表: 图表示例

我已经尝试使用 tikz 来制作它。首先,我尝试使用 \node 和 \path 库中的 shapes 和 arrows 来制作它,但我做不到。然后我尝试使用 trees 库,但它也没有达到我想要的效果。现在我需要你的帮助。我想这只是几行代码。

这是迄今为止我最好的尝试的代码

\documentclass[a4,border=10pt]{article}
 \usepackage{tikz}
 \usetikzlibrary{shapes,arrows}

 \begin{document}
 % Define block styles
 \tikzstyle{decision} = [diamond, draw, fill=blue!20, 
    text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
 \tikzstyle{block} = [rectangle, draw, fill=blue!20, 
    text width=5em, text centered, minimum height=4em]
 \tikzstyle{line} = [draw, -latex']

 \begin{tikzpicture}[node distance = 2cm, auto]
    % Place nodes
    \node [decision] (outsideThreat) {Bedrohung von außen?};
    \node [block, left of=outsideThreat, below of=outsideThreat] (firewall)  {Firewall};
    \node [decision, right of=outsideThreat, below of=outsideThreat] (knownThreat)  {Bedrohung bekannt?};
    \node [block, below of=knownThreat, left of=knownThreat] (idsAds) {IDS/ADS};
    \node [block, below of=knownThreat, right of=knownThreat] (honeypots)  {Honeypots};
     % Draw edges
    \path [line] (outsideThreat) --(outsideThreat-|firewall) -- node {yes}  (firewall);
     \path [line] (outsideThreat) --(knownThreat|-outsideThreat) -- node {no}  (knownThreat);
    \path [line] (firewall) -- (knownThreat);
    \path [line] (knownThreat) --(knownThreat-|idsAds) -- node {yes} (idsAds);  
    \path [line] (knownThreat) --(honeypots|-knownThreat) -- node {no} (honeypots);
 \end{tikzpicture}
 \end{document}

此代码产生以下输出: 在此处输入图片描述

我无法让防火墙阻止和 Bedrohung bekannt? 决策在水平方向上处于同一水平。并且不知道如何实现上述示例中的精确箭头。

答案1

这是一种方法!

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{shapes.geometric} 
\begin{document}
\begin{tikzpicture}[thick,>=stealth]
\tikzset{box/.style=
   {draw,minimum height=1.2cm,minimum width=3cm}}
\path 
(0,2) node[diamond,draw] (d1) {decision1}
(-5,-2) node[box] (b1) {block1}
(4,-2) node[diamond,draw] (d2) {decision2}
++(-90:1.5) coordinate (d2s) {}
+(4,-2) node[box] (b3) {block3}
+(-3,-1.5) node[box] (b4) {block4};
\draw (0,0)--(d1);
\draw[->] (0,0)-|(d2) node[pos=.25,above]{no};
\draw[->] (0,0)-|(b1) node[pos=.25,above]{yes};
\draw[->] (b1)--(d2) node[midway,above]{some condition};
\draw (d2)--(d2s);
\draw[->] (d2s)-|(b3) node[pos=.25,above]{no};
\draw[->] (d2s)-|(b4) node[pos=.25,above]{yes};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

使用森林,您可以自动化几件事。特别是,您可以将某个节点是否为decision森林的决定权留给它。

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage[edges]{forest}
\usetikzlibrary{shapes.geometric}
\begin{document}
\begin{forest}
forked edges,
for tree={l sep=3em,s sep=3em,edge={-stealth},
 where n children=2{diamond}{rectangle,inner sep=1ex},draw,
 if n=1{edge label={node [pos=0.25, below] {yes} } }{edge label={node [pos=0.25, below] {no} } }}
[decision 1
 [block 1,alias=b1]
 [decision 2,alias=d2
  [block 2]
  [block 3]
 ]
]
\draw[-stealth] (b1) -- (d2) node[midway,above]{pft};
\end{forest}
\end{document}

在此处输入图片描述

答案3

为了好玩,一个简单的pstricks代码:

\documentclass[border=6pt]{standalone}%
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pst-node}
\newcommand{\psstrut}{\rule[-0.55cm]{0pt}{1.25cm}}
\newcommand{\diastrut}{\rule[-0.65cm]{0pt}{1.45cm}}%

\begin{document}

{\sffamily\everymath{\displaystyle}%
\psset{framesep=1pt, arrows=->, arrowinset=0.1,linejoin=1}
\begin{psmatrix}[rowsep=2.5cm, colsep=2cm, emnode=r]% 
%%% Nodes
& \dianode[framesep = -2pt]{d1}{{\diastrut Decision 1}}\\
[name=b1] \psframebox{\makebox[2.5cm]{\psstrut block 1}} & & \dianode[framesep = -2pt]{d2}{{\diastrut Decision 2}}\\
 & [name=b2] \psframebox{\makebox[2.5cm]{\psstrut block 2}} & & [name=b3] \psframebox{\makebox[2.5cm]{\psstrut block 3}}
 %%% Node connections
 \psset{angleA=-90, angleB=90, armA=0.8cm, armB=0.8cm, labelsep=2pt}
 \ncangles{d1}{b1}\nbput{yes}
 \ncangles{d1}{d2}\naput{no}
 \ncline{b1}{d2}\naput{some condition}
 \ncangles{d2}{b2}\nbput{yes}
 \ncangles{d2}{b3}\naput{no}
 \end{psmatrix}}

\end{document} 

在此处输入图片描述

相关内容