在 Tikz 中自动设置一个接一个的节点

在 Tikz 中自动设置一个接一个的节点

我已经将我的时间表更改为类似于此问题中提出的时间表:时间轴和 tikz

一切都很顺利,但我遇到了一个问题。我不知道如何自动设置节点的位置。

基本上,它们的位置应该是“固定的”,从 (3,0) 开始,然后一个接一个,它们之间有固定的距离(无论它们是否是一行)。之后我会画出线条。我怎样才能告诉 Tikz 自动获取每个文本节点的坐标而不手动设置它?(这意味着删除命令的第二个参数\evento。)

我希望最终结果看起来像问题中的结果或像Tom Bombadil的答案,但使用我的代码:

\documentclass[]{article}
\usepackage[a4paper, margin=1cm]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{rotating}
\usepackage{amsmath}
\usepackage{pgfplots}
\usepackage{tikz}
\usepackage{xparse,expl3}

\usetikzlibrary{fit, arrows,backgrounds,patterns,shapes,shapes.multipart,positioning,calc,decorations.markings}
\pgfplotsset{compat=1.7}
\pagestyle{empty}

\tikzset{
    opera/.style={draw, rectangle, align=center, text width=6cm, fill=white, font=\scriptsize},
    typnode/.style={anchor=north west, text width=12cm, inner sep=0mm},
    data/.style={draw=gray, rectangle, fill=white, font=\scriptsize, inner sep=0.5mm},
    datum/.style={font=\scriptsize, rotate=90, inner sep=1mm},
}

\newcommand*{\evento}[3]{
        \coordinate (A) at (0,{(#1-1600)/10});
        \coordinate (B) at (3,{#2});%
        \coordinate[right=5mm of A] (Z);
        \coordinate[left=1cm of B] (Y);
        \draw (A) -- (Z);
        \draw[-|] (Y) -- (B) node[data, pos=0.55] {#1};
        \draw (Z) -- (Y);
        \node[right=3mm of B, typnode] (D) {#3};
}

\begin{document}
        \footnotesize   
    \begin{tikzpicture}[x=1cm,y=-7mm]
            \centering
       %draw horizontal line   
       \draw[|->, -latex, draw] (0,0) -- (0,39);
       \draw[-, dashed] (0,-0.5) -- (0,0);
       \draw[|->, -latex, draw] (34,0) -- (34,39);
       \draw[-, dashed] (34,-0.5) -- (34,39);

       %draw years
        \foreach \y [evaluate=\y as \xear using int(1600+\y*10)] in {0,1,...,38}{ 
            \draw (0,\y) node[left=2pt,anchor=east,xshift=0,font=\scriptsize] {$\xear$}; 
            \draw (-0.1,\y) -- (0.1,\y);
            }

        \foreach \y [evaluate=\y as \xear using int(1600+\y*10)] in {0,1,...,37}{ 
            \draw (0,\y) node[left=2pt,anchor=east,xshift=0,font=\scriptsize] {}; 
            \draw (0,\y+.5) -- (0.1,\y+.5);
            }

        \evento{1603}{0}{King James VI of Scotland ascends to the English throne, becoming James I of England and uniting the crowns - but not the parliaments - of the two kingdoms};
        \evento{1605}{2}{\textbf{5 November} - Gunpowder Plot: A plot in which Guy Fawkes and other Catholic associates conspired to blow up King James VI and I and the Parliament of England was uncovered.};
        \evento{1618}{3}{Walter Raleigh was executed.};
        \evento{1639}{5}{Bishops' Wars: A war with Scotland began which would last until 1640.};
        \evento{1640}{6}{Long Parliament: The Parliament was convened.};
        \evento{1642}{7}{English Civil War: The war began (see timeline of the English Civil War).};
        \evento{1649}{8}{January Trial and execution of Charles I\\Interregnum began with the First Commonwealth};
        \evento{1653}{9}{The Protectorate began under the Lord Protector Oliver Cromwell};
        \evento{1659}{10}{Second Commonwealth a period of great political instability};
        \evento{1660}{11}{Restoration of the monarchy};
        \evento{1666}{12}{\textbf{2 September} - Great Fire of London: A fire began in London.\\\textbf{6 September} - Great Fire of London: The fire ended.};

    \end{tikzpicture}
\end{document}

答案1

我会在这里使用链。west below键有助于正确对齐节点。它的定义取自我的positioning-plus图书馆

我没有\evento逐一遵循您的宏,但将线条包含在循环中。我已从样式fill=white中删除data,因为它不再位于线条顶部。

s之间的垂直距离typnode通过选项指定node distance,并取自south west前一个节点的和新节点的north west锚点(“ ”)之间的距离。west below

data标有年份的标签( )base left的放置位置typnode使得它们共享同一基线。

你也可以创建一个函数,{(\Year-1600)/10}这样你就可以轻松地更改轴。如何获取线上的坐标的实现可能非常不同。

代码

\documentclass[tikz]{standalone}
\usetikzlibrary{chains}
\makeatletter
\tikzset{west below/.code=\tikz@lib@place@handle@{#1}{north west}{0}{-1}{south west}{1}}
\makeatother
\tikzset{
  typnode/.style={anchor=north west, text width=12cm, inner sep=0mm},
  data/.style={draw=gray, rectangle, font=\scriptsize, inner sep=0.5mm},
}
\begin{document}\footnotesize
\begin{tikzpicture}[x=1cm, y=-7mm]
%draw horizontal line
\foreach \xValue in {0,34}
  \path (\xValue,0) edge[-latex] ++(0,39)
                    edge[dashed] ++ (down:1);

%%draw years
\foreach \y [evaluate=\y as \xear using int(1600+\y*10)] in {0,1,...,38}{ 
  \node[left=2pt,anchor=east,xshift=0,font=\scriptsize] at (0,\y) {$\xear$}; 
  \draw (-0.1,\y) -- (0.1,\y); \draw (0,\y+.5) -- (0.1,\y+.5);
}

\begin{scope}[start chain=ch1 going west below, node distance=+1em]
\foreach \Year/\Text in {%
    1603/{King James VI of Scotland ascends to the English throne, becoming James I of England and uniting the crowns - but not the parliaments - of the two kingdoms},
    1605/{\textbf{5 November} - Gunpowder Plot: A plot in which Guy Fawkes and other Catholic associates conspired to blow up King James VI and I and the Parliament of England was uncovered.},
    1618/{Walter Raleigh was executed.},
    1639/{Bishops' Wars: A war with Scotland began which would last until 1640.},
    1640/{Long Parliament: The Parliament was convened.},
    1642/{English Civil War: The war began (see timeline of the English Civil War).},
    1649/{January Trial and execution of Charles I\\Interregnum began with the First Commonwealth},
    1653/{The Protectorate began under the Lord Protector Oliver Cromwell},
    1659/{Second Commonwealth a period of great political instability},
    1660/{Restoration of the monarchy},
    1666/{\textbf{2 September} - Great Fire of London: A fire began in London.\\\textbf{6 September} - Great Fire of London: The fire ended.}}{
  \node[typnode, at=(right:5cm), on chain=ch1, alias=Text] {\Text};
  \node[data,    base left=+2em of Text, alias=Year] {\Year};
  \draw[-|] (Year.east) -- ++(right:3mm);
  \draw     (Year.west) -- ++(left:3mm)
                        -- ([shift=(right:3mm)] 0,{(\Year-1600)/10})
                        --                     (0,{(\Year-1600)/10});
}
\end{scope}
\end{tikzpicture}
\end{document}

输出

在此处输入图片描述

相关内容