TikZ-tree:水平组织结构图中的边缘对齐

TikZ-tree:水平组织结构图中的边缘对齐

我正在制作某种组织结构图/层次树,遇到了边缘对齐问题。作为指导原则,我使用了家谱-TeXample. 请参阅 TeX.sx 讨论这里

但是,我的节点中的文本比示例中的短名称长很多。因此,从标准节点的南锚点开始的边缘会产生相当多的空白。

为了解决这个问题,我想从标准节点的西锚点开始绘制边。但是,edge from parent path设置为|-,因此在垂直之前不会产生水平部分。

MWE 中的当前边缘对齐

我尝试了below of=<node>,但也没有帮助。我试图避免的选项是为每个边设置一个坐标,以防我以后需要调整某些东西。不幸的是,水平展开树也不是一种选择,因为属性数太高了。

我谷歌了一下,找到了一些具有这种边缘样式的树的图片:

首选 也不错 最后一招

第一的树使用类似的边样式-|-

第二一个也不错,但是这个锚点在 TikZ 节点中不存在。

第三比我当前的解决方案更好,但看起来不太干净。

下面是生成树的 MWE,如上图所示:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\tikzstyle{every node}=[draw=black,thick,anchor=west, minimum height=2.5em]

\begin{document}

\begin{figure}[!htb]
\resizebox{\linewidth}{!}{

\begin{tikzpicture}[
criteria/.style={text centered, text width=2cm, fill=gray!50},
attribute/.style={%
    grow=down, xshift=-1cm,
    text centered, text width=2cm,
    edge from parent path={(\tikzparentnode.south west) |- (\tikzchildnode.west)}},
first/.style    ={level distance=8ex},
second/.style   ={level distance=16ex},
third/.style    ={level distance=24ex},
fourth/.style   ={level distance=32ex},
fifth/.style    ={level distance=40ex},
level 1/.style={sibling distance=10em}]
    % Main Goal
    \node[anchor=south]{SuperLongTitleWithNoMeaning}
    [edge from parent fork down]

    % Criteria and Attributes
    child{node (crit1) [criteria] {Criteria1}
        child[attribute,first]  {node {Attribute\\Number1}}
        child[attribute,second] {node {Attribute2}}
        child[attribute,third]  {node {Attribute3}}
        child[attribute,fourth] {node {Attribute4}}
        child[attribute,fifth]  {node {Another\\Attribute}}}
    %
    child{node [criteria] {Criteria2}
        child[attribute,first]  {node {Attribute1}}
        child[attribute,second] {node {Attribute2}}
        child[attribute,third]  {node {Third\\Criteria}}
        child[attribute,fourth] {node {Longtext-\\criteria}}}
    %
    child{node [criteria] {Criteria3}
        child[attribute,first]  {node {Attribute\\two lines}}
        child[attribute,second] {node {Attribute2}}     
        child[attribute,third]  {node {Attribute3}}}
    %
    child{node [criteria] {Criteria4}
        child[attribute,first]  {node {Attribute1}}
        child[attribute,second] {node {Attribute2}}}
    %
    child{node [criteria] {Criteria5}
        child[attribute,first]  {node {First\\Attribute}}
        child[attribute,second] {node {Attribute2}}
        child[attribute,third]  {node {Third\\Criteria}}
        child[attribute,fourth] {node {Longtext-\\criteria}}};
\end{tikzpicture}}
\caption{This is a nice tree.}
\end{figure}

\end{document}

附加问题:

顶部节点根据所有级别的节点总数居中。因此,它稍微向左移动了一点。我可以告诉 TikZ 忽略居中的属性级别吗?如果标题正好位于中间标准的上方,看起来会更好。

答案1

以下是第二个选项(节点存在)。

您的 MWE 只有两处变化attribute/,style

  1. xshift 需要调整

  2. 您需要的锚点可以表示为一个角度

抱歉,我放弃了奖金。

就是这样——我会根据您的喜好进行最后的调整:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{trees}
\tikzstyle{every node}=[draw=black,thick,anchor=west, minimum height=2.5em]

\begin{document}

\begin{figure}[!htb]
\resizebox{\linewidth}{!}{

\begin{tikzpicture}[
criteria/.style={text centered, text width=2cm, fill=gray!50},
attribute/.style={%
    grow=down, xshift=0cm,
    text centered, text width=2cm,
    edge from parent path={(\tikzparentnode.225) |- (\tikzchildnode.west)}},
first/.style    ={level distance=8ex},
second/.style   ={level distance=16ex},
third/.style    ={level distance=24ex},
fourth/.style   ={level distance=32ex},
fifth/.style    ={level distance=40ex},
level 1/.style={sibling distance=10em}]
    % Main Goal
    \node[anchor=south]{SuperLongTitleWithNoMeaning}
    [edge from parent fork down]

    % Criteria and Attributes
    child{node (crit1) [criteria] {Criteria1}
        child[attribute,first]  {node {Attribute\\Number1}}
        child[attribute,second] {node {Attribute2}}
        child[attribute,third]  {node {Attribute3}}
        child[attribute,fourth] {node {Attribute4}}
        child[attribute,fifth]  {node {Another\\Attribute}}}
    %
    child{node [criteria] {Criteria2}
        child[attribute,first]  {node {Attribute1}}
        child[attribute,second] {node {Attribute2}}
        child[attribute,third]  {node {Third\\Criteria}}
        child[attribute,fourth] {node {Longtext-\\criteria}}}
    %
    child{node [criteria] {Criteria3}
        child[attribute,first]  {node {Attribute\\two lines}}
        child[attribute,second] {node {Attribute2}}     
        child[attribute,third]  {node {Attribute3}}}
    %
    child{node [criteria] {Criteria4}
        child[attribute,first]  {node {Attribute1}}
        child[attribute,second] {node {Attribute2}}}
    %
    child{node [criteria] {Criteria5}
        child[attribute,first]  {node {First\\Attribute}}
        child[attribute,second] {node {Attribute2}}
        child[attribute,third]  {node {Third\\Criteria}}
        child[attribute,fourth] {node {Longtext-\\criteria}}};
\end{tikzpicture}}
\caption{This is a nice tree.}
\end{figure}

\end{document}

在此处输入图片描述

答案2

或者forest解决方案(可选花式着色):

更精美的色彩

\documentclass[tikz,border=20pt]{standalone}
\usepackage{forest}
\usetikzlibrary{shadows}
\forestset{
  dir switch tree 2/.style={
    for tree={
      if level=1{
        edge path={
          \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- ++(0,-1.5em) -| (.child anchor)\forestoption{edge label};
        },
        for descendants={
          child anchor=west,
          align=left,
          edge path={
            \noexpand\path [\forestoption{edge}] (!u.parent anchor) -- ++(-.5em,0) |- (.child anchor)\forestoption{edge label};
          },
          before computing xy={
            l=.5em
          }
        },
        before computing xy={
          l=3em
        },
        for tree={
          parent anchor=west,
          anchor=mid west,
          grow'=0,
          font=\sffamily,
          if n children=0{}{
            delay={
              prepend={[,phantom, calign with current]}
            }
          },
        },
      }{},
    },
  },
}
\tikzset{
  colour me/.style={
      draw=colour#1,
      fill=colour#1!20!white,
      text=colour#1!25!black,
  }
}
\begin{document}
\colorlet{colour0}{blue}
\colorlet{colour1}{green}
\colorlet{colour2}{red}
\colorlet{colour3}{orange}
\colorlet{colour4}{yellow}
\colorlet{colour5}{magenta}
\begin{forest}
  dir switch tree 2,
  for tree={
    if level=0{
      colour me=0
    }{
      if level=1{
        node options/.wrap pgfmath arg={colour me=#1}{n()}
      }{
        node options/.wrap 2 pgfmath args={colour me=#1!#2!black}{n("!u")}{100*(n_children("!u")-n()/2)/(n_children("!u"))}
      }
    },
    rounded corners,
    thick,
    drop shadow,
    edge={thick}
  }
  [Super Long Title With No Meaning
    [Criteria 1
      [Attribute\\Number 1]
      [Attribute 2]
      [Attribute 3]
      [Attribute 4]
      [Another\\Attribute]
    ]
    [Criteria 2
      [Attribute 1]
      [Attribute 2]
      [Third\\Criteria]
      [Longtext-\\criteria]
    ]
    [Criteria 3
      [Attribute\\two lines]
      [Attribute 2]
      [Attribute 3]
    ]
    [Criteria 4
      [Attribute 1]
      [Attribute 2]
    ]
    [Criteria 5
      [First\\Attribute]
      [Attribute 2]
      [Third\\Criteria]
      [Longtext-\\criteria]
    ]
  ]
\end{forest}
\end{document}

相关内容