Tikz 图形绘制分层优化

Tikz 图形绘制分层优化

我正在尝试使用 Tikz 制作分层图。

\documentclass{standalone}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{graphs, graphdrawing, quotes}
\usegdlibrary{layered}

\begin{document}

    \begin{tikzpicture}
        \tikzset{
            font=\small,
            every node/.style = draw,
            rounded corners,
            sibling distance = 0.85cm
        }
        \graph [layered layout]
        {
            { [edge={draw=none}] "1950er" -- "1960er" -- "1970er" -- "1980er" -- "1990er" -- "2000er" -- "2010er"};
            { [same layer] "1950er", "COBOL", "FORTRAN"};
            { [same layer] "1960er", "ALGOL", "LISP", "Simula" };
            { [same layer] "1970er", "PASCAL", "Smalltalk", "C"};
            { [same layer] "1980er", "Objective C", "ADA", "Eiffel", "Object PASCAL", "Erlang", "C++" };
            { [same layer] "1990er", "JAVA" [blue], "Object COBOL", "Python" };
            { [same layer] "2000er", "Scala", "C\#" };
            { [same layer] "2010er", "Dart", "Rust", "Kotlin", "Go"};

            "ALGOL" -- {
                "C" -- {
                    "C++" -- "JAVA" -- {
                        "C\#",
                        "Scala",
                        "Kotlin",
                        "Dart"
                    },
                    "Objective C",
                    "Python",
                    "Go",
                    "Dart"
                }, 
                "PASCAL" -- {
                    "Object PASCAL",
                    "Go"
                },
                "Simula" -- {
                    "C++" -- {
                        "C\#" -- {
                            "Dart",
                            "Rust",
                            "Kotlin"
                        },
                        "Rust",
                        "Dart"
                    },
                    "Smalltalk" -- {
                        "Eiffel",
                        "JAVA",
                        "Objective C",
                        "Object PASCAL",
                        "Object COBOL",
                        "Dart",
                        "Erlang"
                    },
                    "Eiffel" -- "Kotlin"
                },
                "ADA" -- "Python"
            };

            "LISP" -- {
                "Smalltalk",
                "Erlang"
            };

            "COBOL" -- "Object COBOL"; 

            { [edge={draw=none}]
                "COBOL" -- "ALGOL",
                "ALGOL" -- "LISP"
            }
        };
    \end{tikzpicture}
\end{document}

结果如下:

分层图

有没有办法稍微调整自动路由,以减少边和节点的重叠。

答案1

图表

我使用matrix库来控制节点的位置。我还更改了某些行上的节点顺序以理清图表。

代码

\documentclass[11pt, margin=.5cm]{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}
  \matrix (L) [matrix of nodes,
  column sep=3ex, row sep=5ex,
  nodes={%
    draw, rounded corners,
    inner sep=2ex, outer sep=.5ex,
    minimum width=10ex, minimum height=5ex
  },
  empty node/.style={draw=none, fill=none},
  every edge/.style={%
    draw, very thin, -,
    preaction={draw=white, -, line width=1ex}
  }]
  {%
    1950er &&&&&&&&&& |(cobol)| COBOL & FORTRAN \\
    1960er &&& |(simula)| Simula &&& |(algol)| Algol && |(lisp)| LISP  \\
    1970er &&&&& |(c)| C && |(smalltalk)| Smalltalk
    && |(pascal)| PASCAL \\
    1980er & |(eiffel)| Eiffel && |(oc)| Objective C
    && |(c++)| C++ &&&  |(opascal)| Object PASCAL
    && |(ada)| ADA && |(erlang)| Erlang \\
    1990er && |(java)[blue]| JAVA
    &&&&&& |(python)| Python &&& |(ocobol)| Object COBOL \\
    2000er && |(scala)| Scala & |(csharp)| C\# \\
    2010er & |(kotlin)| Kotlin &&& |(rust)| Rust &&& |(dart)| Dart
    && |(go)| Go \\
  };
  \path (cobol) edge (ocobol);
  
  \path (algol) edge (c) edge (pascal) edge (simula) edge (ada);
  \path (c) edge (c++) edge (oc) edge (python) edge (go) edge (dart);
  \path (pascal) edge (opascal) edge (go);
  \path (simula) edge (c++) edge (smalltalk) edge (eiffel);
  \path (ada) edge (python);

  \path (c++) edge (java) edge (csharp) edge (dart) edge (rust);
  \path (java) edge (csharp) edge (scala) edge (kotlin) edge (dart);
  \path (csharp) edge (rust) edge (kotlin) edge (dart);
  
  \path (simula) edge (c++) edge (smalltalk) edge (eiffel);
  \path (smalltalk) edge (eiffel) edge (java) edge (oc)
  edge (opascal) edge (ocobol) edge (dart) edge (erlang);
  \path (eiffel) edge (kotlin);

  \path (ada) edge (python);
  
  \path (lisp) edge (smalltalk) edge (erlang);
\end{tikzpicture}
\end{document}

相关内容