我在 tikz 中创建了一个自定义时间线图,并且知道时间线的其他选项,但我需要我的时间线更具可定制性。下面显示了一个 mwe。我想修改这个 tex 文件以从 csv 文件中读取绘图数据。
Tex 文件:
\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}
答案1
pgfplotstable
允许您从表中读取数据。我还删除了嵌套的 tikzpictures 并修复了至少在我的计算机上出现的一些错误。
\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}