6

I have created a custom timeline plot in tikz, and am aware of other options for timelines but I need mine to be more customizable. A mwe is shown below. I would like to modify this tex file to read in the plot data from a csv file.

Tex file:

\documentclass[12pt,a4paper]{article}
\usepackage{readarray,filecontents}
\usepackage{tikz}

\newcommand\loaddata[1]{\CatchFileDef\loadeddata{#1}{\endlinechar=-1}}

%only necessary for overset
\usetikzlibrary{positioning}
\usepackage{makecell}%
\usetikzlibrary{patterns}

\begin{document}

    \begin{tikzpicture}[node distance =2mm]

       % draw horizontal line
       \draw (0,0) -- (11,0) (baseLine) ;

       % draw vertical lines and label below with months
       \foreach \varXcoord/\varMonth [count=\xx] in {
          0/Jan,
          1/Feb,
          2/Mar,
          3/Apr,
          4/May,
          5/Jun,
          6/Jul,
          7/Aug,
          8/Sep,
          9/Oct,
          10/Nov,
          11/Dec
       }
          \draw (\varXcoord,3pt) -- +(0,-6pt) node (qBaseTick\xx) [below] {\varMonth};

       \fill [pattern=north west lines, pattern color=yellow] (-1,0) rectangle (12.5,4); 

       \node [above left = 0 and 1 of baseLine] 
       {
          \begin{tikzpicture}
             \node [rotate=90, anchor=north] {Location}  
          \end{tikzpicture}
       };

       \fill [pattern=north west lines, pattern color=orange] (-1,4) rectangle (12.5,8); 

       \node [above left = 4 and 1 of baseLine] 
       {
          \begin{tikzpicture}
             \node [rotate=90, anchor=north] {Career}  
          \end{tikzpicture}
       };

       % enter events at a 45 degree angle with a subscript
       \foreach \varXcoord/\varYcoord/\varEvent/\varDate [count=\xx] in {%
          % Location
          0/0/Abbotsford/January 1,
          3/0/Surrey/April 1,
          5/1/Vancouver/June 1,
          6/0/New\ Westminster/July 1,%
          10/0/North\ Vancouver/November 1,
          % Career
          0.75/4/Ended\ High\ School/Jan 26, 
          3.3/4/Started\ Trade\ School/April 10, 
          5.8/4/Ended\ Trade\ School/June 28,
          8/4/Started\ Job\ 1/Sept 1,
          10.8/4/Started\ Job\ 2/Nov 27
       } 
          {
          \draw[<-] (\varXcoord,0) -- (\varXcoord,\varYcoord+0.5);
          \node [above right = \varYcoord and \varXcoord + 0.5 of baseLine, rotate=45, anchor=south west] {\makecell[l]{\small${\varEvent}$\\\tiny \varDate}};
          }

   \end{tikzpicture}

\end{document}

This produces the following result: enter image description here

denilw
  • 3,156
  • This code is not compiling. Can you verify that you used the same code to produce the output shown? – nidhin Oct 29 '18 at 09:28
  • @nidhin: Just run the pdf-viewer, it worked for me so … – current_user Oct 29 '18 at 12:23
  • Okay. Let me try again. I doubted because there is blue colored north west lines in your code but not in the image posted. – nidhin Oct 29 '18 at 12:57
  • @current_user How do you compile that? On my machine it throws errors when compiled with any of the standard compilers pdflatex, xelatex or lualatex. –  Oct 29 '18 at 13:34
  • 2
    You are nesting tikzpictures, which one should not do. And your code throws errors. Please consider fixing these issues. –  Oct 29 '18 at 13:36
  • @marmot the advice to not nest tikzpictures is good, at the time that was the only way I could get the labels to work like I wanted, but thanks for the better alternative. About the blue hatch lines, I think right before I was posting the question, I thought the blue contrasted badly with the plot labels, so I changed it to yellow but forgot to update the code. Thanks again. – denilw Oct 29 '18 at 15:42
  • @marmot: https://imgur.com/a/1vPxSqv – current_user Oct 29 '18 at 19:08

1 Answers1

6

pgfplotstable allows you to read off data from a table. I also got rid off the nested tikzpictures and fixed some errors which appeared at least on my machine.

\documentclass[12pt,a4paper]{article}
\usepackage{filecontents}
\begin{filecontents*}{events.csv}
X,Y,Event,Date
0,0,Abbotsford,January 1
3,0,Surrey,April 1
5,1,Vancouver,June 1
6,0,New\ Westminster,July 1
10,0,North\ Vancouver,November 1
0.75,4,Ended\ High\ School,Jan 26
3.3,4,Started\ Trade\ School,April 10
5.8,4,Ended\ Trade\ School,June 28
8,4,Started\ Job\ 1,Sept 1
10.8,4,Started\ Job\ 2,Nov 27
\end{filecontents*}
\usepackage{tikz}
\usepackage{pgfplotstable}
\pgfplotstableread[col sep=comma]{events.csv}\data
% from https://tex.stackexchange.com/a/445369/121799
\newcommand*{\ReadOutElement}[4]{%
    \pgfplotstablegetelem{#2}{#3}\of{#1}%
    \let#4\pgfplotsretval
}


%only necessary for overset
\usetikzlibrary{positioning}
\usepackage{makecell}%
\usetikzlibrary{patterns}

\begin{document}

    \begin{tikzpicture}[node distance =2mm]

       % draw horizontal line
       \draw (0,0) coordinate (baseLine) -- (11,0);

       % draw vertical lines and label below with months
       \foreach \varXcoord/\varMonth [count=\xx] in {
          0/Jan,
          1/Feb,
          2/Mar,
          3/Apr,
          4/May,
          5/Jun,
          6/Jul,
          7/Aug,
          8/Sep,
          9/Oct,
          10/Nov,
          11/Dec
       }
          \draw (\varXcoord,3pt) -- +(0,-6pt) node (qBaseTick\xx) [below] {\varMonth};

       \fill [pattern=north west lines, pattern color=yellow] (-1,0) rectangle (12.5,4); 

       \node [above left = 2.5 and 1 of baseLine,rotate=90] 
       {
         Location
       };

       \fill [pattern=north west lines, pattern color=orange] (-1,4) rectangle (12.5,8); 

       \node [above left = 7 and 1 of baseLine,rotate=90] 
       {
         Career
       };

      \pgfplotstablegetrowsof{\data}
      \pgfmathtruncatemacro{\rownumber}{\pgfplotsretval-1}
      \foreach \X in {0,...,\rownumber}
      {
          \ReadOutElement{\data}{\X}{X}{\varXcoord}
          \ReadOutElement{\data}{\X}{Y}{\varYcoord}
          \ReadOutElement{\data}{\X}{Event}{\varEvent}
          \ReadOutElement{\data}{\X}{Date}{\varDate}
          \draw[<-] (\varXcoord,0) -- (\varXcoord,\varYcoord+0.5);
          \node [above right = \varYcoord and \varXcoord + 0.5 of baseLine, rotate=45, anchor=south west] {\makecell[l]{\small${\varEvent}$\\\tiny \varDate}};
      }
   \end{tikzpicture}

\end{document}

enter image description here

denilw
  • 3,156