2

I am trying to adapt How to create a multiline timeline to my case.

If I try to get larger scaling factor for x-values (years), i.e. from 0.28 to any higher value

\pgfmathsetmacro\yearscale{0.28} % scaling factor for x-values (years)

I get this error:

Dimension too large.

 
myfile.tex, 77‬
<recently read> \pgf@xx 
                        
l.77 }
      
I can't work with sizes bigger than about 19 feet.
Continue and I'll use the largest value I can.

How can I solve this error ?

Here you are code and resulting plot (that one with scaling factor for x-values (years) equal to 0.28):



\documentclass{article}
\usepackage{tikz, pgfplotstable}
\usetikzlibrary{arrows.meta}
\usepackage[utf8]{inputenc}


% read in table
\pgfplotstableread[col sep=comma]{
label,startyear,endyear
A  ,   2011    ,   2012
B     ,   2016    ,   2018
C,   2018    ,   2022
}\authors

\pgfplotstableread[col sep=comma]{
label,startyear,endyear
D ,   2003    ,   2008
E    ,   2008    ,   2011
F    ,   2012    ,  2014
G ,   2013    ,   2017
}\events

% add color columns
\pgfplotstablecreatecol[create col/set={blue!30}]{color}{\authors}
\pgfplotstablecreatecol[create col/set={red!30}]{color}{\events}

% append events table to authors table
\pgfplotstablevertcat{\authors}{\events}
% sort by startyear
\pgfplotstablesort[sort key=startyear]\sortedtable\authors



\begin{document}


% if you want to look at the table    
%\pgfplotstabletypeset[
% columns/label/.style={string type},
% columns/color/.style={string type}
%]\sortedtable

\noindent\begin{tikzpicture}
\pgfmathsetmacro\yearscale{0.28} % scaling factor for x-values (years)
\pgfmathsetmacro\yscale{0.3} % scaling for y-direction -- effectively vertical distance between bars
\pgfmathsetmacro\barheight{0.25} % height of bars

\pgfplotstablegetrowsof{\sortedtable}
\pgfmathsetmacro{\M}{\pgfplotsretval-1}
\pgfplotstablegetcolsof{\sortedtable}
\pgfmathsetmacro{\N}{\pgfplotsretval-1}

\foreach [count=\i,evaluate=\i as \ylevel using \i*\yscale] \row in {0,...,\M}{

% get entries from table row
\pgfplotstablegetelem{\row}{[index]0}\of\sortedtable
\xdef\txt{\pgfplotsretval}
\pgfplotstablegetelem{\row}{[index]1}\of\sortedtable
\xdef\xstart{\pgfplotsretval}
\pgfplotstablegetelem{\row}{[index]2}\of\sortedtable
\xdef\xend{\pgfplotsretval}
\pgfplotstablegetelem{\row}{[index]3}\of\sortedtable
\xdef\barcolor{\pgfplotsretval}
\xdef\TheLastY{\ylevel}

\ifnum \xend=-1
  % single day entry, draw circle
  \filldraw [draw=black!99!\barcolor,fill=\barcolor] (\xstart*\yearscale,-\ylevel) circle[radius=\barheight/2.5] node[black,right=3pt,font=\scriptsize] {\txt};
\else
  % interval, draw a rectangle
  \fill [\barcolor] (\xstart*\yearscale,-\ylevel-\barheight/2) rectangle (\xend*\yearscale,-\ylevel+\barheight/2);
   % add text label right of the rectangle
  \node [right,font=\scriptsize] at (\xend*\yearscale,-\ylevel) {\txt};
\fi
}

\draw [Stealth-Stealth] 
(2003*\yearscale,0) 
-- (2003*\yearscale,-\TheLastY-\yscale)
-- (2023*\yearscale,-\TheLastY-\yscale);

% add ticks on x-axis
\foreach \year in {2003,2004,...,2022}
   \draw (\year*\yearscale,-\TheLastY-\yscale) ++(0,3pt) -- ++(0,-6pt) node[below] {\year};

\end{tikzpicture}
\end{document}

enter image description here

0

1 Answer 1

2

I suggest

  • subtracting 2000 from every year
  • and using the x key (initially x = 1cm) to scale the whole picture instead of using a \yearscale macro.

I've also changed a few \xdefs to \let since they don't need to be globally defined.

Code

\documentclass{article}
\usepackage{tikz, pgfplotstable}
\usetikzlibrary{arrows.meta}
\usepackage[utf8]{inputenc}

% read in table
\pgfplotstableread[col sep=comma]{
label,startyear,endyear
A, 2011, 2012
B, 2016, 2018
C, 2018, 2022
}\authors

\pgfplotstableread[col sep=comma]{
label,startyear,endyear
D, 2003, 2008
E, 2008, 2011
F, 2012, 2014
G, 2013, 2017
}\events

% add color columns
\pgfplotstablecreatecol[create col/set={blue!30}]{color}{\authors}
\pgfplotstablecreatecol[create col/set={red!30}]{color}{\events}

% append events table to authors table
\pgfplotstablevertcat{\authors}{\events}
% sort by startyear
\pgfplotstablesort[sort key=startyear]\sortedtable\authors

\begin{document}
\noindent\begin{tikzpicture}[x=7mm]
\pgfmathsetmacro\yscale{0.3} % scaling for y-direction 
                             % -- effectively vertical distance between bars
\pgfmathsetmacro\barheight{0.25} % height of bars

\pgfplotstablegetrowsof{\sortedtable}
\pgfmathsetmacro{\M}{\pgfplotsretval-1}

\foreach [count=\i,evaluate=\i as \ylevel using \i*\yscale] \row in {0,...,\M}{
  % get entries from table row
  \pgfplotstablegetelem{\row}{[index]0}\of\sortedtable
  \let\txt\pgfplotsretval
  \pgfplotstablegetelem{\row}{[index]1}\of\sortedtable
  \let\xstart\pgfplotsretval
  \pgfplotstablegetelem{\row}{[index]2}\of\sortedtable
  \let\xend\pgfplotsretval
  \pgfplotstablegetelem{\row}{[index]3}\of\sortedtable
  \let\barcolor\pgfplotsretval
  \xdef\TheLastY{\ylevel}
  
  \ifnum \xend=-1
    % single day entry, draw circle
    \filldraw [draw=black!99!\barcolor,fill=\barcolor] (\xstart-2000,-\ylevel)
      circle[radius=\barheight/2.5*1cm]
      node[black,right=3pt,font=\scriptsize] {\txt};
  \else
    % interval, draw a rectangle
    \fill [\barcolor] (\xstart-2000,-\ylevel-\barheight/2) rectangle
                        (\xend-2000,-\ylevel+\barheight/2);
     % add text label right of the rectangle
    \node [right,font=\scriptsize] at (\xend-2000,-\ylevel) {\txt};
  \fi
}

\draw [Stealth-Stealth] (2003-2000,0) -- (2003-2000,-\TheLastY-\yscale)
                                      -- (2023-2000,-\TheLastY-\yscale);

% add ticks on x-axis
\foreach \year in {2003, 2004, ..., 2022}
  \draw (\year-2000,-\TheLastY-\yscale) ++(0,3pt)
     -- ++(0,-6pt) node[below] {\year};
\end{tikzpicture}
\end{document}
2
  • 1
    The pgfplotstable manual: \pgfplotstablegetelem is quite slow and tables are stored as a sequence of columns. I have an approach that iterates over columns and then rows and stores the values in a key which then can be accessed by a simple macro like \tdrc{<row index>}{<column name>} or \tdrci{<row index>}{<column index>}. It also uses the y key to scale the diagram vertically so that the \row loop variable can simply be used as an y value. Since this is not an answer to this question, I'll provide only a link. Sep 22, 2022 at 22:18
  • amazing, thanks a lot !!!!!!!!!!!!! :-)
    – Ommo
    Sep 22, 2022 at 22:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .