5

I need to export statistics from a database into PDF files, with a program. The database contains several tests and they need to be displayed as barcharts. Here is an example:

enter image description here

The problem is clear: when xlabels are long, then the chart goes out of the page. However, I cannot tell in advance, how long the labels will be. Sometimes they will be just simple words. Sometimes whome sentences.

The question is: how to calculate the size needed for the labels, and auto-scale the rest of the bar chart to fit on the page horizontally?

\subsection{Irányítási kompetencia}

    %
        \begin{tikzpicture}
\begin{axis}[
        xbar,
            xmin=,
            xmax=61.1111111111,
            width=12cm,
            height={ 1cm + ( 4.0 * 1cm ) },
    symbolic y coords={{Tervezés},{Szervezés},{Döntőképesség},{Ellenőrzés}},
    %ylabel={Irányítási kompetencia}, % This is not really needed, need to save space!
    xlabel={Pontérték \%},
    ytick=data, % Without this, y labels will be written for every "tick". And not for every bar.
    nodes near coords,
    nodes near coords align = {horizontal}
]
\addplot [draw=black, fill=cyan!40!black] coordinates {
    (37.5,{Tervezés})
    (61.1111111111,{Szervezés})
    (31.25,{Döntőképesség})
    (31.25,{Ellenőrzés})

};
\end{axis}
\end{tikzpicture}


%
    %



\subsection{Vezetési kompetencia}

    %
        \begin{tikzpicture}
\begin{axis}[
        xbar,
            xmin=,
            xmax=60.0,
            width=12cm,
            height={ 1cm + ( 11.0 * 1cm ) },
    symbolic y coords={{Megbízhatóság},{Stressztűrés},{Figyelemkoncentráció},{Szabálykövetés},{Önkontroll},{Holisztikus gondolkodás},{Analitikus gondolkodás},{Szervezőkészség},{Alkalmazkodó képesség},{Rendszerező, struktúráló képesség},{Vezetői szerep}},
    %ylabel={Vezetési kompetencia}, % This is not really needed, need to save space!
    xlabel={Pontérték \%},
    ytick=data, % Without this, y labels will be written for every "tick". And not for every bar.
    nodes near coords,
    nodes near coords align = {horizontal}
]
\addplot [draw=black, fill=cyan!40!black] coordinates {
    (55.0,{Megbízhatóság})
    (60.0,{Stressztűrés})
    (30.0,{Figyelemkoncentráció})
    (33.3333333333,{Szabálykövetés})
    (35.0,{Önkontroll})
    (35.0,{Holisztikus gondolkodás})
    (38.8888888889,{Analitikus gondolkodás})
    (59.0909090909,{Szervezőkészség})
    (54.1666666667,{Alkalmazkodó képesség})
    (40.9090909091,{Rendszerező, struktúráló képesség})
    (36.3636363636,{Vezetői szerep})

};
\end{axis}
\end{tikzpicture}

enter image description here

UPDATE: I have also tried to put the whole tizkpicture into \resizebox{\linewidth}{!}{...} but it also scales vertically, and that is bad, because the number of bars in the chart varies. So by "scaling" I mean: specify the width of the chart. What I don't mean is: "scale the chart that was already rendered".

nagylzs
  • 1,225
  • This looks like a job for tikzscale. See http://tex.stackexchange.com/questions/36297/pgfplots-how-can-i-scale-to-text-width – Jake Apr 05 '13 at 06:31
  • Yes, the tikzscale package looks very promising. I see one problem with that: it requires me to include graphics from an external file with the includegraphics macro. Unfortunately, all of these charts are in the same file, and I don't want to put them into separated files. – nagylzs Apr 05 '13 at 09:43

1 Answers1

1

I didn't like the output of \resizebox because the labels got distorted... so I thought changing the width would be better. There might be simpler solutions (using parameters I couldn't find), but here's one. I've used the showframe package to check the margins.

enter image description here

\documentclass{article}

\usepackage{t1enc}
\usepackage{pgfplots}
\usepackage[margin=1cm]{geometry} % demo only

\usepackage{varwidth}
\usepackage{environ}
%\usepackage{showframe}

\newsavebox{\mybox}
\newlength{\mylength}
\NewEnviron{fittedplot}[1]{%
    \begin{lrbox}{\mybox}\begin{tikzpicture}\begin{axis}[#1,width=\textwidth]\BODY\end{axis}\end{tikzpicture}\end{lrbox}%
    \setlength{\mylength}{-\the\wd\mybox}%
    \addtolength{\mylength}{2\textwidth}%
    \begin{tikzpicture}\begin{axis}[#1,width=\mylength]\BODY\end{axis}\end{tikzpicture}%
}

\begin{document}

\noindent
\begin{fittedplot}{
    xbar,
    xmin=,
    xmax=60.0,
    height={ 1cm + ( 11.0 * 1cm ) },
    symbolic y coords={{Megbízhatóság},{Stressztűrés},{Figyelemkoncentráció},{Szabálykövetés},{Önkontroll},{Holisztikus gondolkodás},{Analitikus gondolkodás},{Szervezőkészség},{Alkalmazkodó képesség},{Rendszerező, struktúráló képesség},{Vezetői szerep}},
    %ylabel={Vezetési kompetencia}, % This is not really needed, need to save space!
    xlabel={Pontérték \%},
    ytick=data, % Without this, y labels will be written for every "tick". And not for every bar.
    nodes near coords,
    nodes near coords align = {horizontal}}
\addplot [draw=black, fill=cyan!40!black] coordinates {
    (55.0,{Megbízhatóság})
    (60.0,{Stressztűrés})
    (30.0,{Figyelemkoncentráció})
    (33.3333333333,{Szabálykövetés})
    (35.0,{Önkontroll})
    (35.0,{Holisztikus gondolkodás})
    (38.8888888889,{Analitikus gondolkodás})
    (59.0909090909,{Szervezőkészség})
    (54.1666666667,{Alkalmazkodó képesség})
    (40.9090909091,{Rendszerező, struktúráló képesség})
    (36.3636363636,{Vezetői szerep})
};
\end{fittedplot}

\end{document}
masu
  • 6,571