![如何使用 TikZ 创建此图形?](https://linux22.com/image/316118/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20TikZ%20%E5%88%9B%E5%BB%BA%E6%AD%A4%E5%9B%BE%E5%BD%A2%EF%BC%9F.png)
我想要创建如下图形:
下面是我的代码:
\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\newcommand{\psign}{+}
\newcommand{\ppsign}{++}
\begin{document}
\noindent
\begin{tikzpicture}
\tikzstyle{my node} = [draw,rectangle,minimum width=2.2cm,minimum height=0.8cm, node distance=2cm]
%\draw[black!20] (-0.5cm,-0.5cm) grid (14cm,7cm);
\node [my node] (A) {GDI DDI};
\node [my node] (B) [above of=A] {GDI};
\begin{scope}[xshift=3cm]
\node (C) {Graphics Driver};
\node [my node] (D) [above of=C] {GDI\psign Flat API};
\node [my node] (E) [above of=D] {GDI\psign\ C\ppsign API};
\end{scope}
\begin{scope}[xshift=6cm]
\node [my node] (F) {DX DDI};
\node [my node] (G) [above of=F] {DXGI};
\node [my node] (H) [above of=G] {Direct3D};
\node [my node] (I) [above of=H,minimum width=4cm,right=0pt] {Direct2D};
\node [my node] (J) [right of=I,right=0pt,node distance=3cm] {DirectWrite};
\node [my node] (K) [right of=F, node distance=4cm] {Software Rasterizer};
\end{scope}
\begin{scope}[->,>=triangle 45,semithick]
\path (B) edge (A);
\path (E) edge (D);
\path (H) edge (G);
\path (G) edge (F);
\path (J) edge (I);
\end{scope}
\draw [dashed] (-1.25,1cm) -- (14,1cm);
\end{tikzpicture}
\end{document}
答案1
\documentclass[11pt]{article}
\usepackage{tikz,xcolor}
\usetikzlibrary{arrows,calc,fit,backgrounds}
\definecolor{fcolor}{HTML}{F1F1F1}
\newcommand{\psign}{+}
\newcommand{\ppsign}{++}
\begin{document}
\noindent
\begin{tikzpicture}
\tikzstyle{my node} = [draw,rectangle,minimum width=2.2cm,minimum height=0.8cm, node distance=2cm]
%\draw[black!20] (-0.5cm,-0.5cm) grid (14cm,7cm);
\node [my node] (A) {GDI DDI};
\node [my node] (B) [above of=A] {GDI};
\begin{scope}[xshift=3cm]
\node (C) {Graphics Driver};
\node [my node] (D) [above of=C] {GDI\psign Flat API};
\node [my node] (E) [above of=D] {GDI\psign\ C\ppsign API};
\end{scope}
\begin{scope}[xshift=6cm]
\node [my node] (F) {DX DDI};
\node [my node] (G) [fill=white,above of=F] {DXGI};
\node [my node] (H) [fill=white,above of=G] {Direct3D};
\node [my node] (I) [fill=white,above of=H,minimum width=4.5cm,right=0pt] {Direct2D};
\node [my node] (J) [fill=white,right of=I,right=0pt,node distance=3.5cm] {DirectWrite};
\node [my node] (K) [fill=white,right of=G, node distance=4cm] {Software Rasterizer};
\end{scope}
\begin{scope}[->,>=triangle 45,semithick]
\path (B) edge (A);
\path (E) edge (D);
\path (H) edge (G);
\path (G) edge (F);
\path (J) edge (I);
\path (I.south-|K) edge (K);
\path (I.south-|{$(H.north)!.5!(H.north east)$}) edge ($(H.north)!.5!(H.north east)$);
\end{scope}
\begin{scope}[on background layer]
\node[rectangle,fill=fcolor,fit=(G.south west) (J.north east)] {};
\end{scope}
\draw [dashed] (-1.25,1cm) -- (14,1cm);
\end{tikzpicture}
\end{document}
首先我修复了Software Rasterizer
节点位置right of=G
:
\node [my node] (K) [right of=G, node distance=4cm] {Software Rasterizer};
然后,对于剩下的两个箭头连接,我在edge
s 中添加了这两行:
\path (I.south-|K) edge (K);
\path (I.south-|{$(H.north)!.5!(H.north east)$}) edge ($(H.north)!.5!(H.north east)$);
这里,是节点上位于节点中心正上方的(I.south-|K)
点。 的想法与 相同,但稍微复杂一些。 现在它是节点上位于节点北和东北之间中点正上方的点。Direct2D
Software Rasterizer
(I.south-|{$(H.north)!.5!(H.north east)$})
Direct2D
Direct3D
库可以获取突出显示的五个块fit
,但这次是在范围(on background layer
)内,以避免绘制两次节点。我们选择任意两个最外层节点,如G
和,并分别J
指定它们的最外角(G.south west)
和。(J.north east)
答案2
这可以满足您的要求。请注意,fit
绘制在顶部,如果您需要全色绘制在下方,则可能需要绘制这些节点两次(一次获取用于计算的节点fit
,一次将它们再次绘制到适合区域,您只需将绘制代码存储在宏中并运行该宏两次即可)。
\documentclass[11pt]{article}
\usepackage{tikz}
\usetikzlibrary{arrows,calc}
\usetikzlibrary{positioning,fit}
\newcommand{\psign}{+}
\newcommand{\ppsign}{++}
\begin{document}
\noindent
\begin{tikzpicture}
\tikzstyle{my node} = [draw,rectangle,minimum width=2.2cm,minimum height=0.8cm, node distance=2cm]
%\draw[black!20] (-0.5cm,-0.5cm) grid (14cm,7cm);
\node [my node] (A) {GDI DDI};
\node [my node] (B) [above of=A] {GDI};
\begin{scope}[xshift=3cm]
\node (C) {Graphics Driver};
\node [my node] (D) [above of=C] {GDI\psign Flat API};
\node [my node] (E) [above of=D] {GDI\psign\ C\ppsign API};
\end{scope}
\begin{scope}[xshift=6cm]
\node [my node] (F) {DX DDI};
\node [my node] (G) [above of=F] {DXGI};
\node [my node] (H) [above of=G] {Direct3D};
\node [my node] (I) [above of=H,minimum width=4cm,right=0pt] {Direct2D};
\node [my node] (J) [right of=I,right=0pt,node distance=3cm] {DirectWrite};
\node [my node] (K) [right of=G, node distance=4cm] {Software Rasterizer};
\end{scope}
\begin{scope}[ transform canvas={xshift=1cm},
->,>=triangle 45,semithick]
\draw (I) -- (I |- K.north);
\end{scope}
\begin{scope}[ transform canvas={xshift=-4em},
->,>=triangle 45,semithick]
\draw (I) -- (I |- H.north);
\end{scope}
\begin{scope}[->,>=triangle 45,semithick]
\path (B) edge (A);
\path (E) edge (D);
\path (H) edge (G);
\path (G) edge (F);
\path (J) edge (I);
\end{scope}
\draw [dashed] (-1.25,1cm) -- (14,1cm);
\node [fill=gray,opacity=0.2,dotted,fit = (H) (I) (J) (K) ] {};
\end{tikzpicture}
\end{document}
我认为您可以弄清楚如何在灰色区域上方添加图例。
答案3
稍微修改了一下简化的@daleif 和@ AboAmmar 解决方案(在我看来它们几乎相同:-)):
\documentclass[11pt, border=1mm,
tikz,
preview]{standalone}
\usetikzlibrary{arrows,calc,fit,backgrounds}
\newcommand{\psign}{+}
\newcommand{\ppsign}{++}
\begin{document}
\begin{tikzpicture}[
node distance = 2cm,
every node/.style = {draw, rectangle,
fill=white, % if you liked, otherwise should be omitted
minimum width=2.2cm, minimum height=0.8cm}
]
\node (A) {GDI DDI};
\node (B) [above of=A] {GDI};
\begin{scope}[xshift=3cm]
\node (C) [draw=none] {Graphics Driver};
\node (D) [above of=C] {GDI\psign Flat API};
\node (E) [above of=D] {GDI\psign\ C\ppsign API};
\end{scope}
\begin{scope}[xshift=6cm]
\node (F) {DX DDI};
\node (G) [above of=F] {DXGI};
\node (H) [above of=G] {Direct3D};
\node (I) [above of=H,minimum width=4.5cm,right=0pt] {Direct2D};
\node (J) [right of=I,right=0pt,node distance=3.5cm] {DirectWrite};
\node (K) [right of=G, node distance=4cm] {Software Rasterizer};
\end{scope}
\coordinate (L) at ([xshift=7mm] H.north);
\path[-triangle 45,semithick]
(B) edge (A) (E) edge (D) (H) edge (G)
(G) edge (F) (J) edge (I) (I.south-|K) edge (K)
(I.south -| L) edge (L)
(I.south-|{$(H.north)!.5!(H.north east)$}) -- ($(H.north)!.5!(H.north east)$);
\scoped[on background layer]
\node (M) [rectangle,fill=gray!30,fit=(G.south west) (J.north east)] {};
\coordinate (N) at ($(A.west)!0.5!(B.west)$);
\draw [dashed] (N) -- (N -| M.east);
\end{tikzpicture}
\end{document}
此代码提供: