如何在 TikZ 年表图中为每个事件画一条垂直线?

如何在 TikZ 年表图中为每个事件画一条垂直线?

我使用的是 Levi Wiseman 的 Tikzchronology包。具体来说,我使用了提供的代码这里并添加了一些自己的命令以避免旋转等。但是,我想添加连接每个事件日期和相应事件标签的垂直线,例如连接“35”和“清算股份”的线。非常感谢您的帮助。

截屏

\documentclass[11pt]{article}
\usepackage{chronology}

%\newcounter{step}\newcounter{stepstart}\newcounter{stepstop}%
%\newcounter{yearstart}\newcounter{yearstop}\newcounter{deltayears}%
%\newlength{\xstart}\newlength{\xstop}%
%\newlength{\unit}\newlength{\timelinewidth}%
%\newsavebox{\timelinebox}%

\renewenvironment{chronology}[5][5]{%
  \setcounter{step}{#1}%
  \setcounter{yearstart}{#2}\setcounter{yearstop}{#3}%
  \setcounter{deltayears}{\theyearstop-\theyearstart}%
  \setlength{\unit}{#4}%
  \setlength{\timelinewidth}{#5}%
  \pgfmathsetcounter{stepstart}%
    {\theyearstart+\thestep-mod(\theyearstart,\thestep)}%
  \pgfmathsetcounter{stepstop}{\theyearstop-mod(\theyearstop,\thestep)}%
  \addtocounter{step}{\thestepstart}%
  \begin{lrbox}{\timelinebox}%
    \begin{tikzpicture}[baseline={(current bounding box.north)}]%
      \draw [|->] (0,0) -- (\thedeltayears*\unit+\unit, 0);%
      \foreach \x in {1,...,\thedeltayears}%
        \draw[xshift=\x*\unit] (0,-.1\unit) -- (0,.1\unit);%
      \addtocounter{deltayears}{1}%
      \foreach \x in {\thestepstart,\thestep,...,\thestepstop}{%
        \pgfmathsetlength\xstop{(\x-\theyearstart)*\unit}%
        \draw[xshift=\xstop] (0,-.3\unit) -- (0,.3\unit);%
        \node at (\xstop,0) [below=.2\unit] {\x};}%
}
{%
    \end{tikzpicture}%
  \end{lrbox}%
  \raisebox{2ex}{\resizebox{\timelinewidth}{!}{\usebox{\timelinebox}}}}%
\renewcommand{\event}[3][e]{%
  \pgfmathsetlength\xstop{(#2-\theyearstart)*\unit}%
    \pgfmathsetlength\xstart{(#1-\theyearstart)*\unit}%
    \draw[fill=black,draw=none,opacity=0.5,rounded corners=.2\unit]%
      (\xstart,-.2\unit) rectangle%
      node[opacity=1,rotate=45,right=.5\unit] {#3} (\xstop,.2\unit);%
}%

\newcommand{\action}[3][e]{%
    \pgfmathsetlength\xstop{(#2-\theyearstart)*\unit}%
    \ifx #1e%
        \draw[fill=red,draw=none,opacity=0.5]%
            (\xstop, 0) circle (.2\unit)%
            node[opacity=1,rotate=0,right=.5\unit] {#3};%
    \else%
        \pgfmathsetlength\xstart{(#1-\theyearstart)*\unit}%
        \draw[fill=red,draw=none,opacity=0.5,rounded corners=.2\unit]%
            (\xstart,-.2\unit) rectangle%
            node[opacity=1,rotate=0,right=-2.0\unit,above=.0\unit] {#3} (\xstop,.2\unit);%
    \fi}%

\newcommand{\multirowevent}[3][e]{%
    \pgfmathsetlength\xstop{(#2-\theyearstart)*\unit}%
    \ifx #1e%
        \draw[fill=black,draw=none,opacity=0.5]%
            (\xstop, 0) circle (.2\unit)%
            node[opacity=1,rotate=0,right=.5\unit] {#3};%
    \else%
        \pgfmathsetlength\xstart{(#1-\theyearstart)*\unit}%
        \draw[fill=black,draw=none,opacity=0.5,rounded corners=.2\unit]%
            (\xstart,-.2\unit) rectangle%
            node[opacity=1,rotate=0,right=-2.0\unit,above=.9\unit] {#3} (\xstop,.2\unit);%
    \fi}%

\newcommand{\agm}[3][e]{%
    \pgfmathsetlength\xstop{(#2-\theyearstart)*\unit}%
    \ifx #1e%
        \draw[fill=black,draw=none,opacity=0.5]%
            (\xstop, 0) circle (.2\unit)%
            node[opacity=1,rotate=0,right=.5\unit] {#3};%
    \else%
        \pgfmathsetlength\xstart{(#1-\theyearstart)*\unit}%
        \draw[fill=black,draw=none,opacity=0.5,rounded corners=.2\unit]%
            (\xstart,-.2\unit) rectangle%
            node[opacity=1,rotate=0,right=-2.0\unit,below=1.0\unit] {#3} (\xstop,.2\unit);%
    \fi}%

\begin{document}
    \begin{figure}[htbp]\centering
    \noindent\begin{chronology}[7]{1}{42}{3ex}{\textwidth}
        \multirowevent[5]{5}{\color{black}\parbox{3cm}{\centering AGM\linebreak Announcement}}
        \multirowevent[14]{14}{\color{black}\parbox{3cm}{\centering Asking for share\linebreak registration}}
        \multirowevent[28]{28}{\color{black}\parbox{3cm}{\centering ``Starting\linebreak registration''}}
        \action[28]{35}{\color{red}Monitoring}
        \multirowevent[35]{35}{\color{black}\parbox{3cm}{\centering Clearing\linebreak Shares}}

        \agm[7]{7}{\color{black}AGM-35}
        \agm[14]{14}{\color{black}AGM-28}
        \agm[21]{21}{\color{black}AGM-21}
        \agm[28]{28}{\color{black}AGM-14}
        \agm[35]{35}{\color{black}AGM-7}
        \agm[42]{42}{\color{black}AGM}
    \end{chronology}
    \caption{\textbf{Timeline AGM registration:} ....}
        \label{fig:timeline}
    \end{figure}
\end{document}

答案1

使用您的代码我无法获得您的结果,因此我删除了right=...命令中的所有节点选项\agm,,\action...

在 中\multirowevent,添加了在所有事件和日期之间绘制垂直线的命令。

\draw[->, shorten >=3pt] (auxnode.south) -- (\xstop,0);

您可以根据自己的喜好调整所有参数。结果如下:

在此处输入图片描述

\documentclass[11pt]{article}
\usepackage{chronology}

\usepackage{chronology} 

%\newcounter{step}\newcounter{stepstart}\newcounter{stepstop}%
%\newcounter{yearstart}\newcounter{yearstop}\newcounter{deltayears}%
%\newlength{\xstart}\newlength{\xstop}%
%\newlength{\unit}\newlength{\timelinewidth}%
%\newsavebox{\timelinebox}%

\renewenvironment{chronology}[5][5]{%
  \setcounter{step}{#1}%
  \setcounter{yearstart}{#2}\setcounter{yearstop}{#3}%
  \setcounter{deltayears}{\theyearstop-\theyearstart}%
  \setlength{\unit}{#4}%
  \setlength{\timelinewidth}{#5}%
  \pgfmathsetcounter{stepstart}%
    {\theyearstart+\thestep-mod(\theyearstart,\thestep)}%
  \pgfmathsetcounter{stepstop}{\theyearstop-mod(\theyearstop,\thestep)}%
  \addtocounter{step}{\thestepstart}%
  \begin{lrbox}{\timelinebox}%
    \begin{tikzpicture}[baseline={(current bounding box.north)}]%
      \draw [|->] (0,0) -- (\thedeltayears*\unit+\unit, 0);%
      \foreach \x in {1,...,\thedeltayears}%
        \draw[xshift=\x*\unit] (0,-.1\unit) -- (0,.1\unit);%
      \addtocounter{deltayears}{1}%
      \foreach \x in {\thestepstart,\thestep,...,\thestepstop}{%
        \pgfmathsetlength\xstop{(\x-\theyearstart)*\unit}%
        \draw[xshift=\xstop] (0,-.3\unit) -- (0,.3\unit);%
        \node at (\xstop,0) [below=.2\unit] {\x};}%
}
{%
    \end{tikzpicture}%
  \end{lrbox}%
  \raisebox{2ex}{\resizebox{\timelinewidth}{!}{\usebox{\timelinebox}}}}%
\renewcommand{\event}[3][e]{%
  \pgfmathsetlength\xstop{(#2-\theyearstart)*\unit}%
    \pgfmathsetlength\xstart{(#1-\theyearstart)*\unit}%
    \draw[fill=black,draw=none,opacity=0.5,rounded corners=.2\unit]%
      (\xstart,-.2\unit) rectangle%
      node[opacity=1,rotate=45,right=.5\unit] {#3} (\xstop,.2\unit);%
}%

\newcommand{\action}[3][e]{%
    \pgfmathsetlength\xstop{(#2-\theyearstart)*\unit}%
    \ifx #1e%
        \draw[fill=red,draw=none,opacity=0.5]%
            (\xstop, 0) circle (.2\unit)%
            node[opacity=1,rotate=0] {#3};%
    \else%
        \pgfmathsetlength\xstart{(#1-\theyearstart)*\unit}%
        \draw[fill=red,draw=none,opacity=0.5,rounded corners=.2\unit]%
            (\xstart,-.2\unit) rectangle%
            node[opacity=1,rotate=0,above=.0\unit] {#3} (\xstop,.2\unit);%
    \fi}%

\newcommand{\multirowevent}[3][e]{%
    \pgfmathsetlength\xstop{(#2-\theyearstart)*\unit}%
    \ifx #1e%
        \draw[fill=black,draw=none,opacity=0.5]%
            (\xstop, 0) circle (.2\unit)%
            node[opacity=1,rotate=0] {#3};%
    \else%
        \pgfmathsetlength\xstart{(#1-\theyearstart)*\unit}%
        \draw[fill=black,draw=none,opacity=0.5,rounded corners=.2\unit]%
            (\xstart,-.2\unit) rectangle%
            node[opacity=1,rotate=0,above=.9\unit] (auxnode) {#3} (\xstop,.2\unit);% <------ inserted nodes name
       \draw[->, shorten >=3pt] (auxnode.south) -- (\xstop,0);% <- draws a vertical line from event to date
    \fi}%

\newcommand{\agm}[3][e]{%
    \pgfmathsetlength\xstop{(#2-\theyearstart)*\unit}%
    \ifx #1e%
        \draw[fill=black,draw=none,opacity=0.5]%
            (\xstop, 0) circle (.2\unit)%
            node[opacity=1,rotate=0, draw] {#3};%
    \else%
        \pgfmathsetlength\xstart{(#1-\theyearstart)*\unit}%
        \draw[fill=black,draw=none,opacity=0.5,rounded corners=.2\unit]%
            (\xstart,-.2\unit) rectangle%
            node[opacity=1,rotate=0,below=1.0\unit] {#3} (\xstop,.2\unit);%
    \fi}%

\begin{document}
    \begin{figure}[htbp]\centering
    \noindent\begin{chronology}[7]{1}{42}{3ex}{\textwidth}
        \multirowevent[5]{5}{\color{black}\parbox{3cm}{\centering AGM\linebreak Announcement}}
        \multirowevent[14]{14}{\color{black}\parbox{3cm}{\centering Asking for share\linebreak registration}}
        \multirowevent[28]{28}{\color{black}\parbox{3cm}{\centering ``Starting\linebreak registration''}}
        \action[28]{35}{\color{red}Monitoring}
        \multirowevent[35]{35}{\color{black}\parbox{3cm}{\centering Clearing\linebreak Shares}}

        \agm[7]{7}{\color{black}AGM-35}
        \agm[14]{14}{\color{black}AGM-28}
        \agm[21]{21}{\color{black}AGM-21}
        \agm[28]{28}{\color{black}AGM-14}
        \agm[35]{35}{\color{black}AGM-7}
        \agm[42]{42}{\color{black}AGM}
    \end{chronology}
    \caption{\textbf{Timeline AGM registration:} ....}
        \label{fig:timeline}
    \end{figure}
\end{document}

相关内容