我正在尝试使用 Tikz 节点和箭头绘制一个方案。以下是方案代码:
\begin{tikzpicture}[
wide-block/.style={rectangle, draw=black, fill=white, thick, minimum width=4.7cm, minimum height=1cm},
half-wide-block/.style={rectangle, draw=black, fill=white, thick, minimum width=2cm, minimum height=1cm},
group-node/.style={rectangle, draw=black, dashed, inner sep=0.4cm}
]
% fron-end node
\node[wide-block] (lb) {Load balancer};
\node[wide-block] (tm) [below=of lb] {Transaction manager};
% back-end node
\node[half-wide-block] (git) [right=of lb, xshift=1.5cm] {git};
\node[half-wide-block] (hook) [right=of git] {hook};
\node[wide-block] (rm) [right=of tm, xshift=1.5cm] {Resource manager};
\node[half-wide-block] (prop) [below=of rm, xshift=-1.35cm] {Proposer};
\node[half-wide-block] (acc) [right=of prop] {Acceptor};
\node[group-node] (fe) [fit=(lb)(tm), label={Front-ends}] {};
\node[group-node] (be) [fit=(git)(hook)(rm)(prop)(acc), label={Back-ends}] {};
% flow
\draw[->] (lb.west)+(-2,0) -- node {1} (lb.west);
\draw[->] (lb) -- node {2} (git);
\draw[->] (git.east)+(0,0.2) -- node {3} (hook.west)+(0,0.2);
\draw[->] (hook.west)+(0,-0.2) -- node {12} (git.east)+(0,-0.2);
\draw[->] (tm.west) -- node {14} +(-2,0);
\end{tikzpicture}
所有节点均正确渲染,但有些箭头有问题,箭头:
\draw[->] (git.east)+(0,0.2) -- node {3} (hook.west)+(0,0.2);
\draw[->] (hook.west)+(0,-0.2) -- node {12} (git.east)+(0,-0.2);
箭头可能有什么问题?我只想将箭头 3 放在
git
和hook
节点之间的中心线上方,并将 12 线放在此中心下方。
答案1
使用该calc
库,您可以像预期的那样解析节点坐标。
并将图形above
和below
箭头放在一起。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{positioning,fit,calc}
\begin{document}
\begin{tikzpicture}[
wide-block/.style={rectangle, draw=black, fill=white, thick, minimum width=4.7cm, minimum height=1cm},
half-wide-block/.style={rectangle, draw=black, fill=white, thick, minimum width=2cm, minimum height=1cm},
group-node/.style={rectangle, draw=black, dashed, inner sep=0.4cm}
]
% fron-end node
\node[wide-block] (lb) {Load balancer};
\node[wide-block] (tm) [below=of lb] {Transaction manager};
% back-end node
\node[half-wide-block] (git) [right=of lb, xshift=1.5cm] {git};
\node[half-wide-block] (hook) [right=of git] {hook};
\node[wide-block] (rm) [right=of tm, xshift=1.5cm] {Resource manager};
\node[half-wide-block] (prop) [below=of rm, xshift=-1.35cm] {Proposer};
\node[half-wide-block] (acc) [right=of prop] {Acceptor};
\node[group-node] (fe) [fit=(lb)(tm), label={Front-ends}] {};
\node[group-node] (be) [fit=(git)(hook)(rm)(prop)(acc), label={Back-ends}] {};
% flow
\draw[->] ($(lb.west)+(-2,0)$) -- node[above] {1} (lb.west);
\draw[->] (lb) -- node[above] {2} (git);
\draw[->] ($(git.east)+(0,0.2)$) -- node[above] {3} ($(hook.west)+(0,0.2)$);
\draw[->] ($(hook.west)+(0,-0.2)$) -- node[below] {12} ($(git.east)+(0,-0.2)$);
\draw[->] (tm.west) -- node[below] {14} +(-2,0);
\end{tikzpicture}
\end{document}