我正在尝试使用 TikZ 中的矩阵环境制作图表:
\documentclass[margin=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}
\usetikzlibrary{shapes.arrows}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{shadings}
\usetikzlibrary{matrix}
\usetikzlibrary{fit}
\usetikzlibrary{backgrounds}
\begin{document}
\begin{tikzpicture}[
mymatrix/.style={matrix of nodes, nodes=block,
nodes={align=left, text width=2.75cm}, % New!
column sep=3em,
row sep=2em},
block/.style={draw=blue!80, thick, fill=blue!20,
rounded corners,
minimum width=8em,
minimum height=4em,
align=center, },
vhilit/.style={draw=black, thick, %dotted,
inner sep=1em,
},
]
\matrix[mymatrix, nodes={anchor=center, align=center}] (mx) { % at (11,10)
\node[draw=none, fill=none] (a11) {}; & \node[draw=none, fill=none] (a12) {}; & \node[draw=none, fill=none] (a13) {}; & \node[draw=none, fill=none] (a14) {}; & \node[draw=none, fill=none] (a15) {}; \\
\node[block] (a21) {Listening\\Channel}; & {Reading and\\Transmitting} & {Content\\ Management} & \node[draw=none, fill=none] (a24) {}; & \node[draw=none, fill=none] (a25) {}; \\
{Backoff} & \node[draw=none, fill=none] (a32) {}; & {Collision\\Management} & \node[draw=none, fill=none] (a34) {}; & \node[draw=none, fill=none] (a35) {}; \\
\node[draw=none, fill=none] (a41) {}; & {FSA Frame\\Inicialization} & \node[draw=none, fill=none] (a43) {}; & {Tags State\\Control} & {FSA Frame\\Inicialization} \\
\node[draw=none, fill=none] (a51) {}; & {Slot Inventory\\Time Accounting} & \node[draw=none, fill=none] (a53) {}; & \node[draw=none, fill=none] (a54) {}; & \node[draw=none, fill=none] (a55) {}; \\
\node[draw=none, fill=none] (a61) {}; & {Compute Reader\\Tx/Rx Gain} & \node[draw=none, fill=none] (a63) {}; & {Compute Tag's\\Tx/Rx Gain} & \node[draw=none, fill=none] (a65) {}; \\
\node[draw=none, fill=none] (a71) {}; & {Compute\\Backscattered Power} & \node[draw=none, fill=none] (a73) {}; & {Compute Tag's\\Received Power} & {Tag\\Activation} \\
\node[draw=none, fill=none] (a81) {}; & {Compute\\Interferences} & \node[draw=none, fill=none] (a83) {}; & \node[draw=none, fill=none] (a84) {}; & \node[draw=none, fill=none] (a85) {}; \\
\node[draw=none, fill=none] (a91) {}; & {Compute\\SINR/BER} & \node[draw=none, fill=none] (a93) {}; & \node[draw=none, fill=none] (a94) {}; & \node[draw=none, fill=none] (a95) {}; \\
};
\draw[thick] (a21) -- (mx-3-1);
\draw[thick] (a21) -- (mx-2-2);
\draw[thick] (mx-2-2) -- (mx-4-2);
\draw[thick] (mx-2-2) -- (mx-2-3) node [midway, inner sep=0pt,outer sep=0pt] (mid) {};
\draw[thick] (mid) |- (mx-3-3);
\draw[thick] (mx-4-2) -- (mx-5-2);
\draw[thick] (mx-5-2) -- (mx-6-2);
\draw[thick] (mx-6-2) -- (mx-6-4);
\draw[thick] (mx-6-4) -- (mx-7-4);
\draw[thick] (mx-7-2) -- (mx-7-4);
\draw[thick] (mx-4-4) -- (mx-4-5);
\draw[thick] (mx-4-5) -- (mx-7-5);
\draw[thick] (mx-7-5) -- (mx-7-4);
\draw[thick] (mx-7-2) -- (mx-8-2);
\draw[thick] (mx-8-2) -- (mx-9-2);
% \node [coordinate, at (mx-5-2)] (test1) {};
% \draw[thick] (mx-5-2) -- ({$(mx-5-2)!.5!(a51)$}) |- (mx-9-2);
\draw[thick] (mx-5-2) to [bend right=90] (mx-9-2);
\draw[thick] (a21) to [out=180,in=180] (mx-8-2);
\begin{scope}[on background layer]
\node[vhilit, fit=(a11) (a12) (a13) (a14) (a15), align=center, fill=brown!45, opacity=0.7, text opacity=1, text=black] {{Task Assignment\\Initialization/Close Tasks}};
\node[vhilit, fit=(a21) (mx-2-2) (mx-2-3) (a24) (a25) (mx-3-1) (a32) (mx-3-3) (a34) (a35) ,fill=red!25, opacity=0.7, align=left, text opacity=1] {};
\node[vhilit, fit=(a41) (mx-4-2) (a43) (mx-4-4) (mx-4-5) (a51) (mx-5-2) (a53) (a54) (a55) , fill=green!25, opacity=0.6, align=left, text opacity=1] {\large{FSA}};]
%
\node[vhilit, fit=(a61) (mx-6-2) (a63) (mx-6-4) (a65) (a71) (mx-7-2) (a73) (mx-7-4) (mx-7-5) (a81) (mx-8-2) (a83) (a84) (a85) (a91) (mx-9-2) (a93) (a94) (a95) , fill=pink!30, opacity=0.7, align=left, text opacity=1] {\large{PHY}};
\end{scope}
\draw[blue, thick]({$(mx-2-3)!.5!(a24)$} |- mx-2-3.north) -- ({$(mx-2-3)!.5!(a24)$} |- mx.south);
\end{tikzpicture}
\end{document}
产生:
我想在第一列宽度上增加更多空间来写“LBT”、“FSA”和“PHY”,并在垂直线的两侧留出空间来写“Reader”和“Tag”字样,如下图所示:
此外,我不知道如何使“监听通道”块和“计算干扰”之间的链接变得拐角而不是像上面的手工绘图那样弯曲。
答案1
你的代码太乱了...所以我重新格式化它们并做了一些更改。
主要思想是使用
a/.style={
append after command={
(#1,0) coordinate (left-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn)
}
},
扩大单元格边框。
的效果block/.default
是,每当没有提供值时block
,就会使用默认值。
列宽是列最宽单元格的宽度,因此只需调用一次a=-2
或block=-2
即可扩展该列。
但目的是扩大拟合区域,原来的拟合区域是左上角节点和右下角节点决定的矩形,现在要扩大左边界,最简单的办法就是在左边放一个空节点/坐标左上节点,拟合区域有的节点是空节点,所以用 来a=-2
延长边框,其他的block=-2
就够了(原块风格 与 的结合a=-2
),比 方便多了block, a=-2
。
\documentclass[tikz, border=1cm]{standalone}
\usetikzlibrary{calc, matrix, fit, backgrounds}
\tikzset{
a/.style={
append after command={
(#1,0) coordinate (left-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn)
}
},
block/.style={
draw=blue!80, thick, fill=blue!20,
rounded corners,
align=center,
anchor=center,
a=#1,
},
block/.default=0,
mymatrix/.style={
matrix of nodes,
nodes in empty cells,
nodes={
minimum width=8em,
minimum height=4em,
text width=2.75cm,
},
column sep=3em,
row sep=2em
},
vhilit/.style={
draw=black, thick, %dotted,
inner sep=1em,
rounded corners,
},
}
\begin{document}
\begin{tikzpicture}
\matrix [mymatrix] (mx) {
%% row 1
|[a=-4]|
&&&&\\
%% row 2
|[block=-4]|{Listening\\Channel}
& |[block]|{Reading and\\Transmitting}
& |[block=5]|{Content\\ Management}
&&\\
%% row 3
|[block]|{Backoff}
&& |[block]|{Collision\\Management}
&&\\
%% row 4
|[a=-4]|
& |[block]|{FSA Frame\\Inicialization}
&& |[block]|{Tags State\\Control}
& |[block]|{FSA Frame\\Inicialization}
\\
%% row 5
& |[block]|{Slot Inventory\\Time Accounting}
&&&\\
%% row 6
|[a=-4]|
& |[block]|{Compute Reader\\Tx/Rx Gain}
&& |[block]|{Compute Tag's\\Tx/Rx Gain}
&\\
%% row 7
& |[block]|{Compute\\Backscattered Power}
&& |[block]|{Compute Tag's\\Received Power}
& |[block]|{Tag\\Activation}
\\
%% row 8
& |[block]|{Compute\\Interferences}
&&&\\
%% row 9
& |[block]|{Compute\\SINR/BER}
&&&\\
};
\draw[thick] (mx-2-1) -- (mx-3-1);
\draw[thick] (mx-2-1) -- (mx-2-2);
\draw[thick] (mx-2-2) -- (mx-4-2);
\draw[thick] (mx-2-2) -- coordinate [midway] (mid) (mx-2-3);
\draw[thick] (mid) |- (mx-3-3);
\draw[thick] (mx-4-2) -- (mx-5-2);
\draw[thick] (mx-5-2) -- (mx-6-2);
\draw[thick] (mx-6-2) -- (mx-6-4);
\draw[thick] (mx-6-4) -- (mx-7-4);
\draw[thick] (mx-7-2) -- (mx-7-4);
\draw[thick] (mx-4-4) -- (mx-4-5);
\draw[thick] (mx-4-5) -- (mx-7-5);
\draw[thick] (mx-7-5) -- (mx-7-4);
\draw[thick] (mx-7-2) -- (mx-8-2);
\draw[thick] (mx-8-2) -- (mx-9-2);
\draw[thick] (mx-2-1.west) -- ++(-1, 0) |- (mx-8-2);
\draw[thick] (mx-5-2.west) -- ++(-1, 0) |- (mx-9-2);
\tikzset{
myfont/.style={pos=.01, text=red, font=\bfseries\sffamily\large}
}
\draw[blue, thick]
({$(mx-2-3)!.5!(mx-2-4)$} |- mx-2-3.north) --
node[left, myfont] {READER}
node[right, myfont] {TAG}
({$(mx-2-3)!.5!(mx-2-4)$} |- mx.south);
\begin{scope}[on background layer]
\node[vhilit, fit=(left-1-1)(mx-1-1)(mx-1-5), align=center, fill=brown!45, opacity=0.7, text opacity=1, text=black] {{Task Assignment\\Initialization/Close Tasks}};
\node[vhilit, fit=(left-2-1)(mx-2-1)(mx-3-5) ,fill=red!25, opacity=0.7, align=left, text opacity=1] {\large{LBT}};
\node[vhilit, fit=(left-4-1)(mx-4-1)(mx-5-5) , fill=green!25, opacity=0.6, align=left, text opacity=1] {\large{FSA}};]
\node[vhilit, fit=(left-6-1)(mx-6-1)(mx-9-5) , fill=pink!30, opacity=0.7, align=left, text opacity=1] {\large{PHY}};
\end{scope}
\end{tikzpicture}
\end{document}