Tikz 组织结构图

Tikz 组织结构图

我如何对齐以下 tikz 组织结构图的节点?

\documentclass[10pt,letterpaper]{article}
\usepackage[margin=0.5cm, hmargin=0.5cm,vmargin=0.5cm]{geometry}
\usepackage{tikz}
\usepackage{pgfplots}
\usetikzlibrary{trees}
\begin{document}
\begin{center}
\begin{tikzpicture}[auto, node distance = 1cm, thick,
every node/.style = {rectangle, font = \sffamily, white,  
top color = blue!90!black, bottom color = blue!60!black,
text width = 3.5cm, align = center, minimum height = 1cm}]
\node (MAN) {\textbf{Adam Smith}\\CEO};
\node (PROD) [below left = 5cm of MAN] {\textbf{John Doe}};
\node (FIN)     [below = 3cm of MAN]  {\textbf{Steve Sanders}};
\node (RRHH) [below right = 5cm of MAN] {\textbf{Mike Donovan}};
\draw [blue,thick]
 (MAN) -- (FIN)
 (MAN) -| (PROD)
 (MAN) -| (RRHH);
 \end{tikzpicture}
\end{center}
\end{document}

我尝试修改节点距离和节点位置,但它们从未正确对齐,位置从未处于同一水平,并且水平和节点之间的分离非常大。

在此先感谢您的时间!

答案1

虽然您正在加载trees库,但您不会将图表格式化为树。但是,这样做可以避免手动定位所有内容并单独绘制线条的需要。

我特别喜欢 Forest,Ti基于 Z 的绘制树的包,但您也可以使用标准trees库或tikz-qtree其他任何库来执行此操作。

以下是带注释的示例:

\documentclass[border=10pt,multi,tikz]{standalone}
\usepackage[edges]{forest}% load the edges library so we can use forked edges for the tree
\begin{document}
\begin{forest}
  for tree={% apply to entire tree
    edge={thick, draw=blue},% lines blue and thick
    % TikZ keys will be passed through so we can use all the usual styling stuff
    font=\sffamily\bfseries,% default is bold
    text=white,% white text
    top color=blue!90!black,
    bottom color=blue!60!black,
    text width=35mm,
    text centered,
    minimum height=10mm,
    l sep'=20mm,% spread out the levels a bit
    s sep'=10mm,% spread out the siblings a bit
  },
  forked edges,% lines should be squared off: down-acroos-down rather than the default which is as-the-eagle-flies
  % tree specification - notice how concisely the tree can be specified ...
  [Adam Smith\\\textmd{CEO}% override bold default for second line
    [John Doe]
    [Steve Sanders]
    [Mike Donovan]
  ]
\end{forest}
\end{document}

森林版

答案2

Emma 的稍微简化的解决方案:

  • 节点之间的距离由通用选项控制node distance(选择node distance = 1cm and 1.5cm
  • 选项auto被删除
  • 文本颜色使用正确的语法(text=white
  • 粗体文本是全局定义的,对于“CEO”的正常文本的例外是由本地决定的\normalfont

完整的 MWE 是:

\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{positioning}

\begin{document}
\begin{tikzpicture}[
    node distance = 1cm and 1.5cm,
every node/.style = {rectangle, font=\sffamily\bfseries, text=white,
                     top color=blue!90!black, bottom color=blue!60!black,
                     text width=3.5cm, align = center, minimum height = 1cm}
                     ]
\node (MAN)     {Adam Smith\\  \normalfont CEO};
\node (PROD) [below left = of MAN] {John Doe};
\node (FIN)  [below      = of MAN] {Steve Sanders};
\node (RRHH) [below right= of MAN] {Mike Donovan};
%
\draw [blue,thick]  (MAN) -- (FIN)
                    (MAN) -| (PROD)
                    (MAN) -| (RRHH);
    \end{tikzpicture}
\end{document}

得到的有机结构图为:

在此处输入图片描述

答案3

您可以使用below left等中的两个坐标来为below和指定单独的偏移量left

\documentclass[tikz, border=5pt]{standalone}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[auto, node distance = 1cm, thick,
every node/.style = {rectangle, font = \sffamily, white,
top color = blue!90!black, bottom color = blue!60!black,
text width = 3.5cm, align = center, minimum height = 1cm}]
\node (MAN) {\textbf{Adam Smith}\\CEO};
\node (PROD) [below left = 1cm and 2cm of MAN] {\textbf{John Doe}};
\node (FIN)  [below = 1cm of MAN]  {\textbf{Steve Sanders}};
\node (RRHH) [below right = 1cm and 2cm of MAN] {\textbf{Mike Donovan}};
\draw [blue,thick]
 (MAN) -- (FIN)
 (MAN) -| (PROD)
 (MAN) -| (RRHH);
\end{tikzpicture}
\end{document}

在此处输入图片描述

还值得注意的是,默认情况下below left等指定节点边界之间的空间。如果您希望在具有不同大小节点的大图片中进行更好的对齐,您可能需要使用选项on grid,这使得距离指定节点中心之间的空间。

相关内容