0

我刚刚在 Tikz 中完成了我的第一个图表。它看起来像我想要的那样,但对我如何“编码”它不满意:

\begin{tikzpicture}
[node distance=14mm,
 item/.style={rounded corners,rectangle,
   thick,
   minimum width=20mm, minimum height=10mm}]

\node[item,draw=blue!50,fill=blue!20] (stack) {1394 Stack};
\node[item,left=of stack,draw=green!50,fill=green!20,yshift=-9mm] (app1) {Application};
\node[item,left=of stack,draw=green!50,fill=green!20,yshift=9mm] (app2) {Application};
\node[item,right=of stack,draw=orange!50,fill=orange!20] (ohci) {OHCI};
\node[item,right=of ohci,yshift=-15mm,draw=yellow!70,fill=yellow!35] (dev1) {Device};
\node[item,right=of ohci,yshift=0mm,draw=yellow!70,fill=yellow!35] (dev2) {Device};
\node[item,right=of ohci,yshift=15mm,draw=yellow!70,fill=yellow!35] (dev3) {Device};

\draw[thick] (app1) -- (stack)
             (app2) -- (stack)
             (stack) -- (ohci)
             (ohci) -- (dev1)
             (ohci) -- (dev2)
             (ohci) -- (dev3);

\node[xshift=7mm,yshift=1mm] (topUser) at (app1.east |- dev3.north) {};
\node[xshift=7mm,yshift=-1mm,label=above left:User space] (botUser) at (app1.east |- dev1.south) {};
\draw[dashed] (topUser) -- (botUser);

\node[xshift=7mm,yshift=1mm] (topKern) at (stack.east |- dev3.north) {};
\node[xshift=7mm,yshift=-1mm,label=above left:Kernel space,
label=above right:Hardware\phantom{p}] (botKern) at (stack.east |- dev1.south) {};
\draw[dashed] (topKern) -- (botKern);
\end{tikzpicture}

我不舒服的地方是:

我如何手动移动“应用程序”和“设备”节点以yshift将它们彼此分开;我确信必须有一种更优雅的方式来产生一个简单的树状结构

从图片顶部到底部的线(topKern -- botKern和);topUser -- botUser这些在 x 轴上手动对齐,以使用xshift=7mm.

我使用\phantom{p}来确保标签“硬件”与其他两个标签具有相同的基线。

4

1 回答 1

1

要构建树结构,请参阅pgfmanual.pdf使树木生长

对于线条,我将创建代表两个节点中间的节点,然后像您一样使用垂直坐标系。你也可以用它current bounding box来识别“边界”。

要正确对齐基线,请指定text heighttext depth。在您的情况下,例如在 style 中every label。但如你所见,我将标签作为节点在下面......

\begin{tikzpicture}[水平距离=35mm,节点距离=15mm,文字高度=1.5ex,文字深度=0.25ex]

\begin{scope}[每个节点/.style={圆角,矩形,厚,最小宽度=20mm,最小高度=10mm}]
\begin{scope}[level 1/.style={sibling distance=19mm,nodes={fill=green!20,draw=green!50}}]
\node[draw=blue!50,fill=blue!20] (stack) {1394 Stack} [grow=left]
  子 {node (app2) {Application}}
  子{节点(app1){应用程序}};
\结束{范围}

\begin{scope}[level 1/.style={sibling distance=15mm,nodes={fill=yellow!70,draw=yellow!35}}]
\node[right= of stack,draw=orange!50,fill=orange!20] (ohci) {OHCI} [grow=right]
  子{节点{设备}}
  子{节点{设备}}
  子{节点{设备}};
\结束{范围}
\结束{范围}

\node[below=0mm of app1] (用户空间) {用户空间};
\node at (userspace -| stack) (kernel) {Kernel};
\node at (userspace -| ohci) (hardware) {Hardware};

\path (app1) -- (stack) node[coordinate,midway] (between1) {};
\draw (ohci) -- (stack) node[coordinate,midway] (between2) {};

\draw[dashed] (当前边界框.north -| between1) -- (当前边界框.south -| between1);
\draw[dashed] (当前边界框.north -| between2) -- (当前边界框.south -| between2);

\结束{tikzpicture}
于 2010-08-08T10:58:55.087 回答