使用 PSTricks 或 Tikz 绘制“特征树”

使用 PSTricks 或 Tikz 绘制“特征树”

我知道 Tikz 和 PSTricks 都提供了许多绘制树的软件包和选项,但我不知道如何绘制特征树,如 Karl Wiegers 指南中所示软件要求(微软出版社,2013 年),因为它实际上不是一棵树,而是由直线连接的三棵小树,直线本身与椭圆相连。有人能帮我吗?

特征树的示例

答案1

我不会使用具有复杂嵌套语法和限制的树库并依赖自动定位,而是会以方便的方式使用标准 TikZ 语法。

  • 为不同类型的节点定义样式
  • 放置主坐标,使用计算语法划分距离
  • 将主要节点放置在这些坐标处
  • 放置更多节点,因为这样看起来好看,请尝试使用相对定位
  • 绘制连接线

由于相对定位和使用计算是逻辑定位,当您调整一些值以完成时,一切都会保持良好的状态。

主要部分-剩余节点可以按照类似的方式添加:

特征图

\documentclass[tikz,border=4mm]{standalone}
\usetikzlibrary{calc,shapes,positioning}
\tikzset{%
  root/.style = { draw, ellipse, fill=blue!10,
                  font = \sffamily\LARGE\bfseries, align = center},
  main/.style = { draw, rounded corners = 6pt, fill=blue!10, align = center,
                  font = \sffamily\Large\bfseries, minimum height = 11ex,
                  inner sep = 6pt},
  end/.style  = { font = \sffamily\bfseries},
}
\newcommand*{\len}{18}
\begin{document}
\begin{tikzpicture}

% Base graph coordinates
\coordinate (A);
\coordinate (B) at ($(A)+(\len,0)$);
\coordinate (D) at ($(A)!.4!(B)$);
\coordinate (E) at ($(A)!.7!(B)$);
\coordinate (F) at ($(D)!.5!(E)$);

% main leaves coordinates
\coordinate (health) at ($(D)+(120:\len/3)$);
\coordinate (order) at ($(E)+(120:\len/3)$);
\coordinate (inventory) at ($(F)+(240:\len/3)$);

% graph lines
\draw (A) -- (B)
      (D) -- (health)
      (E) -- (order)
      (F) -- (inventory);

% root node
\node [root] at (B) {Chemical\\Tracking\\System};

% main leaves nodes
\node [main, above = 0 of health] {Health \&\\Safety};
\node [main, above = 0 of order] {Order\\Chemicals};
\node [main, below = 0 of inventory] {Inventory\\Management};

% end leaves nodes, positioning in relation to existing nodes
% somehow manually, but relative positioning makes changes easier
\node (info)       [end, anchor=west] at ($(A)+(0,0.2*\len)$) {Health \& Safety Info};
\node (compliance) [end, anchor=west] at ($(A)!.6666!(info.west)$) {Compliance Reporting};
\node (exposure)   [end, anchor=west] at ($(A)!.3333!(info.west)$) {Chemical Exposure Report};

\node (laboratories) [end, anchor=west] at ($(A)-(0,0.1*\len)$) {Laboratories};
\node (stockroom) [end, anchor=west] at ($(A)!.5!(laboratories.west)$) {Chemical Stockroom};

\node (incident)  [end] at ($(health)!.6666!(order)-(0,1)$) {Incident Reporting};
\node (request)   [end] at ($(incident)!.4!(F)$) {Chemical Request};
\node (receiving) [end] at ($(inventory)!.5!(E)+(1,0)$) {Receiving};
\node (search)    [end] at ($(order)!.5!(B)+(1,1)$) {Search};

% draw lines
\draw (info.east) -- ($(health)!(info.east)!(D)$);
\draw (compliance.east) -- ($(health)!(compliance.east)!(D)$);
\draw (exposure.east) -- ($(health)!(exposure.east)!(D)$);
\draw (incident.west) -- ($(health)!(incident.west)!(D)$);

\draw (request.east) -- ($(order)!(request.east)!(E)$);
\draw (stockroom.east) -- ($(inventory)!(stockroom.east)!(F)$);
\draw (laboratories.east) -- ($(inventory)!(laboratories.east)!(F)$);
\draw (receiving.west) -- ($(inventory)!(receiving.west)!(F)$);

\draw (search.west) -- ($(order)!(search.west)!(E)$);

\end{tikzpicture}
\end{document}

相关内容