我想要矩形节点一个在另一个之上。我尝试使用fit
,它似乎使节点大于输入,因此节点重叠。
这是针对垂直堆叠条形图之类的东西。因此,一个元素必须具有最小尺寸,而其他元素则需要具有相对于该元素的精确尺寸。为了让整个图形尽可能低,我希望最小尺寸刚好能够容纳单个线标签。所有其他高度都是相对于最小尺寸计算的。
因此,我需要使用一个可以让我计算相对大小的库,我不能只将确切的大小作为数字输入。
整个堆栈也位于图形另一部分的右侧,并通过线与其相连。所以我需要节点(具有锚点),不能使用简单的矩形。
\documentclass[a4paper,10pt]{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\begin{document}
\begin{tikzpicture}[x=1pt, y=1pt]
[align=center, node distance=0pt]
\newlength{\distanceFromLeft}
\setlength{\distanceFromLeft}{150pt}
\node[fit={(0,25) (\distanceFromLeft, 50)}, draw, align=center, label=center:placeholder for left part of picture] (placeholder) {};
\newlength{\stackWidth}
\setlength{\stackWidth}{100pt}
\newlength{\smallestHeight}
\setlength{\smallestHeight}{11pt} % to accomodate a 10 pt label
\node[fit={(\distanceFromLeft+25,0) (\distanceFromLeft+25+\stackWidth,160/51*\smallestHeight)}, draw, align=center, label=center:first node -- 160] (first) {}; % The first node represents 160 units
\node[fit={(\distanceFromLeft+25,160/51*\smallestHeight) (\distanceFromLeft+25+\stackWidth,221/51*\smallestHeight)}, draw, align=center, label=center:second node -- 51] (second) {}; % The second node is the smallest, it has 51 units. It starts where the old node ends (height 160/51) and is 51/51 high, so its upper right corner is at height (160 + 51)/51 = 221/51 of the smallest height
\node[fit={(\distanceFromLeft+25,221/51*\smallestHeight) (\distanceFromLeft+25+\stackWidth,426/51*\smallestHeight)}, draw, align=center, label=center:third node -- 206] (third) {}; % The third node represents 206 units
\draw [dashed] (placeholder.south east) -- (first.south west);
\draw [dashed] (placeholder.north east) -- (third.north west);
\end{tikzpicture}
\end{document}
答案1
只需使用positioning
TikZ 库和明确定义的节点样式。
代码
\documentclass[border=2pt,tikz]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\tikzset{block/.style={shape=rectangle, draw, node distance=-1pt, minimum width = 10em, line width=1pt}}
\begin{tikzpicture}
\node[block] (first) {first node};
\node[block,below=of first] {second node};
\end{tikzpicture}
\end{document}
结果
更新 1
positioning
您也可以使用和来获得具有相对定位和可变高度的堆叠节点minimum width/height
。图形的其余部分可以通过在节点锚点之间放置线条来绘制。
\newlength{\stackWidth}
\setlength{\stackWidth}{100pt}
\newlength{\smallestHeight}
\setlength{\smallestHeight}{11pt}
\tikzset{block2/.style={shape=rectangle, draw, node distance=-1pt, minimum width = \stackWidth, line width=1pt, inner sep=0pt}}
\begin{tikzpicture}
\begin{scope}[every node/.style={block2}]
\node[minimum height=\smallestHeight] (second) {second node};
\node[minimum height=3\smallestHeight,below=of second] (first) {first node};
\node[minimum height=4\smallestHeight,above=of second] (third) {third node};
\end{scope}
\end{tikzpicture}
更新 2
从您的代码中,要获取不重叠的节点,请添加\tikzset{every node/.append style={inner sep=0pt}}
。
答案2
我理解,堆栈中的节点高度不同,您可以根据某个物体的相对大小(在提供的图片中写在堆栈中的节点中)来计算,即,您希望获得如下内容:
如果你使用另外两个 TikZ 库,这段代码会非常简单和简洁calc
:chains
\documentclass[tikz, border=3mm]{standalone}
\usetikzlibrary{calc,chains,positioning}
\newlength{\smallestHeight}
\setlength{\smallestHeight}{0.2pt} % <-- define units of nodes heights
\begin{document}
\begin{tikzpicture}[
node distance = 0pt and 22mm,
start chain = A going above,
block/.style = {shape=rectangle, draw, line width=1pt,
minimum height=#1\smallestHeight, minimum width = 100pt,
inner sep=0pt, outer sep=0pt, on chain=A}
]
\node[block=160] {first node - 160}; %A-1
\node[block=51] {second node - 51};
\node[block=206] {third node - 206}; %A-3
%
\node[block=100, left=of $(A-1.north west)!0.5!(A-3.south west)$] {placeholder};
%
\draw[dashed] (A-4.north east) -- (A-3.north west)
(A-4.south east) -- (A-1.south west);
\end{tikzpicture}
\end{document}
答案3
另一个解决方案是使用rectangle split
形状。由于这种形状不考虑minimum height
垂直分割节点,因此\parbox
已使用来定义每个部分的不同高度。
\documentclass[tikz,border=2mm]{standalone}
\usetikzlibrary{positioning,shapes.multipart}
\newcommand{\textbox}{\parbox[c][1cm][c]{3cm}{\centering #1}}
\begin{document}
\begin{tikzpicture}
\node[draw] (ph) {placeholder};
\node[rectangle split, rectangle split parts=3, draw, right=2cm of ph] (mem)
{\parbox[c][1cm][c]{3cm}{\centering third node - 206}
\nodepart{two}second node - 51
\nodepart{three}\parbox[c][1cm][c]{3cm}{\centering first node - 160}};
\draw[dashed] (ph.north east)--(mem.north west);
\draw[dashed] (ph.south east)--(mem.south west);
\end{tikzpicture}
\end{document}