I have strings for the X axis labels on a pgf line plot. Sometimes these are long and overlap each other. This question is already addressed on this site but only for static cases. The solution proposed is always either to move the labels around, manually truncate them, change their orientation or something similar.
I however am using this for a report generator where the array of label strings is added dynamically at runtime, so I do not have any information about how many labels there are or how long they are. I also can't angle them because that would change the layout and size of various elements which need to remain constant.
I was hoping there might already be a package or class around that does this. Basically I need to calculate the following (assuming monospace font):
- How much total space is available along the x axis (TS)
- How many labels are there (NL)
- How much space per label (SL)=(TS/NL)
- How big is one rendered character in the current font. (CS)
- How many characters can fit in a label (CL)=(SL/CS)
- Create a list of labels where each label is a truncated version of the original label using only the first (CL) characters.
It is always worth checking if something exists before reinventing the wheel. Here is an example of what the plot currently looks like:
\documentclass[a4paper, landscape, lmargin=0, rmargin=0]{article}
\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}
\usepackage{textcomp}
\usepackage[margin=0.1cm]{geometry}
\pagestyle{empty}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ymax=2.8,
ymin=-2.8,
label style={font=\small},
tick label style={font=\small},
ymajorgrids=true,
xtick distance=1,
xticklabels={,,LR Fascia,LR Fascia - Lt Qtr,Lt Fascia,Lt Fascia - Lt Fdr,Lt Fdr,Lt Qtr,RR Fascia,RR Fascia - Rt Qtr,Rt Fascia,Rt Fascia - Rt Fdr,Rt Fdr,Rt Qtr},
width=20cm,
height=9cm,
ytick style={/pgfplots/major tick length=3mm},
max space between ticks=10,
]
\addplot[
mark=*,
] coordinates {
(1,-0.17)
(2,1.04)
(3,-1.08)
(4,-2.2)
(5,1.12)
(6,-1.12)
(7,-0.11)
(8,0.15)
(9,-0.43)
(10,0.85)
(11,-1.28)
(12,-0.26)
};
\end{axis}
\end{tikzpicture}
\end{document}