I'm working on some sort of org-chart/hierarchy-tree and ran into issues with the edge-alignment. As a guideline I used the family-tree-TeXample. See the TeX.sx-dicussion here.
However, the text in my nodes is a lot longer than the short names in the example. Therefore, starting the edges from the south-anchor of the criteria-nodes produces quite a bit of whitespace.
As a solution to this, I'd like to start the edges at the west-anchor of the criteria-nodes. However, edge from parent path
is set to |-
and thus does not produce a horizontal section before going vertical.
I tried the below of=<node>
, but that didn't help either. The option I'm trying to avoid is setting a coordinate for each edge, in case I'd have to adjust something later. Spreading the tree out horizontally is unfortunately not an option either, because the attribute-count is too high.
I googled a bit to find some pictures of trees that have this edge-style:
The first tree uses an edge style like -|-
.
The second one is also nice, but this anchor does not exist in TikZ-nodes.
The third is better than my current solution, but doesn't look very clean.
Here's the MWE that produces a tree as shown in the picture at the top:
\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}
Bonus question:
The top-node centers itself according to the total number of nodes in all levels. Therefore, it's a bit shifted toward the left. Can I tell TikZ to ignore the attribute-level for the centering? It would look nicer, if the heading would be right above the middle criteria.