[Tex/LaTex] charts package for LaTeX

chartspackages

I wanted to include some more or less complex charts into my LaTeX document. I have attached a picture that shows an example how it should like. Is there any charts package in LaTeX that allows me to include such charts in my document? Or do I have to do that with Excel or any other tool?

enter image description here
Picture from the paper: The secret life of bugs: Going past the errors and omissions in software repositories

Best Answer

Here is my solution, always based on TikZ.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{backgrounds,shadings}
\newcommand{\barwidth}{2}
\newcommand{\barheight}{0.5}
\newcommand{\thescale}{100}

\newcommand{\setscale}[1]{\renewcommand{\thescale}{#1}}

\newcommand{\shadebar}[2][]{
\node[rectangle,minimum width=\barwidth cm,minimum height=\barheight cm,](a){};
\node[left] at(a.east){\ensuremath{#2\%}};
\ifnum#2=0
\def\shadebarwidth{0.01/\barwidth}
\else
\def\shadebarwidth{(#2/\thescale)*\barwidth}
\fi
\begin{pgfonlayer}{background}
\node[left color=#1,right color=white,draw=white,minimum width=\shadebarwidth cm,minimum height=\barheight cm,right]at (a.west){};
\end{pgfonlayer}
}

\begin{document}
\setscale{20}
\noindent
\tikz{\shadebar[green]{16}}\\
\tikz{\shadebar[red]{10}}\\
\setscale{100}
\tikz{\shadebar[blue]{60}}\\
\tikz{\shadebar[green]{100}}

\vspace{5cm}
\begin{tabular}{lp{\barwidth cm}p{\barwidth cm}p{\barwidth cm}}
Probing for ownership &\setscale{20}\tikz[baseline=-0.5ex]{\shadebar[blue]{16}} &\setscale{70}\tikz[baseline=-0.5ex]{\shadebar[blue]{69}} & \setscale{55}\tikz[baseline=-0.5ex]{\shadebar[green]{52}}\\
Summit &\setscale{20}\tikz[baseline=-0.5ex]{\shadebar[blue]{1}} &\setscale{70}\tikz[baseline=-0.5ex]{\shadebar[blue]{2}} & \setscale{55}\tikz[baseline=-0.5ex]{\shadebar[green]{50}}\\
Probing for expertise &\setscale{20}\tikz[baseline=-0.5ex]{\shadebar[blue]{17}} &\setscale{70}\tikz[baseline=-0.5ex]{\shadebar[blue]{34}} & \setscale{55}\tikz[baseline=-0.5ex]{\shadebar[green]{49}}\\
\end{tabular}
\end{document}

enter image description here

Basically, it allows you to define bars in which you can customize the width, the color and the scale. This because, you may wonder to change scale factor for your charts.

INTRODUCING LABELS

To introduce labels instead of simply numbers here is a solution:

\newcommand{\labeledshadebar}[3][]{
\node[rectangle,minimum width=\barwidth cm,minimum height=\barheight cm,](a){};
\node[left] at(a.east){#3};
\ifnum#2=0
\def\shadebarwidth{0.01/\barwidth}
\else
\def\shadebarwidth{(#2/\thescale)*\barwidth}
\fi
\begin{pgfonlayer}{background}
\node[left color=#1,right color=white,draw=white,minimum width=\shadebarwidth cm,minimum height=\barheight cm,right]at (a.west){};
\end{pgfonlayer}
}

The new command should be used as:

\tikz{\labeledshadebar[green]{16}{yes}}For example, changing the `document` content into:

\begin{document}
\setscale{20}
\noindent
\tikz{\shadebar[green]{16}}\\
\tikz{\labeledshadebar[green]{16}{yes}}\\
\tikz{\shadebar[red]{10}}\\
\setscale{100}
\tikz{\shadebar[blue]{60}}\\
\tikz{\labeledshadebar[blue]{60}{no}}\\
\tikz{\shadebar[green]{100}}

\vspace{2cm}
\begin{tabular}{lp{\barwidth cm}p{\barwidth cm}p{\barwidth cm}}
Probing for ownership &\setscale{20}\tikz[baseline=-0.5ex]{\shadebar[blue]{16}} &\setscale{70}\tikz[baseline=-0.5ex]{\shadebar[blue]{69}} & \setscale{55}\tikz[baseline=-0.5ex]{\shadebar[green]{52}}\\
Summit &\setscale{20}\tikz[baseline=-0.5ex]{\shadebar[blue]{1}} &\setscale{70}\tikz[baseline=-0.5ex]{\shadebar[blue]{2}} & \setscale{55}\tikz[baseline=-0.5ex]{\shadebar[green]{50}}\\
Probing for expertise &\setscale{20}\tikz[baseline=-0.5ex]{\shadebar[blue]{17}} &\setscale{70}\tikz[baseline=-0.5ex]{\shadebar[blue]{34}} & \setscale{55}\tikz[baseline=-0.5ex]{\shadebar[green]{49}}\\
\end{tabular}
\end{document}

it is possible to get:

enter image description here

Xparse implementation

To make things easier and to have just one command, it is possible to redefine the above commands by means of an xparse definition:

\NewDocumentCommand{\shadebar}{O{blue} m o}{%
\node[rectangle,minimum width=\barwidth cm,minimum height=\barheight cm,](a){};
\IfNoValueTF{#3}{%true
\node[left] at(a.east){\ensuremath{#2\%}};
}
{%false
\node[left] at(a.east){#3};
}
\ifnum#2=0
\def\shadebarwidth{0.01/\barwidth}
\else
\def\shadebarwidth{(#2/\thescale)*\barwidth}
\fi
\begin{pgfonlayer}{background}
\node[left color=#1,right color=white,draw=white,minimum width=\shadebarwidth cm,minimum height=\barheight cm,right]at (a.west){};
\end{pgfonlayer}
}

Now the last example with labels could be simplified; in order to allow the customization inside the document of the \barwidth and the \barheight, I inserted two dedicated commands \setbarwidth and \setbarheight.

\documentclass{article}
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{backgrounds,shadings}
\newcommand{\barwidth}{2}
\newcommand{\barheight}{0.5}
\newcommand{\thescale}{100}

% To allow customization inside the document
\newcommand{\setscale}[1]{\renewcommand{\thescale}{#1}}
\newcommand{\setbarwidth}[1]{\renewcommand{\barwidth}{#1}}
\newcommand{\setbarheight}[1]{\renewcommand{\barheight}{#1}}

\NewDocumentCommand{\shadebar}{O{blue} m o}{%
\node[rectangle,minimum width=\barwidth cm,minimum height=\barheight cm,](a){};
\IfNoValueTF{#3}{%true
\node[left] at(a.east){\ensuremath{#2\%}};
}
{%false
\node[left] at(a.east){#3};
}
\ifnum#2=0
\def\shadebarwidth{0.01/\barwidth}
\else
\def\shadebarwidth{(#2/\thescale)*\barwidth}
\fi
\begin{pgfonlayer}{background}
\node[left color=#1,right color=white,draw=white,minimum width=\shadebarwidth cm,minimum height=\barheight cm,right]at (a.west){};
\end{pgfonlayer}
}

\begin{document}
\setbarwidth{3}
\setbarheight{0.75}
\setscale{20}
\noindent
\tikz{\shadebar[green]{16}}\\
\tikz{\shadebar[orange]{16}[yes]}\\
\tikz{\shadebar[red]{10}}\\
\setbarwidth{3.5}
\setbarheight{1}
\setscale{100}
\tikz{\shadebar{60}[no]}\\
\tikz{\shadebar[violet]{60}{no}}\\
\tikz{\shadebar[green!80!black]{100}}

\vspace{2cm}

\setbarwidth{2}
\setbarheight{0.5}
\begin{tabular}{lp{\barwidth cm}p{\barwidth cm}p{\barwidth cm}}
Probing for ownership &\setscale{20}\tikz[baseline=-0.5ex]{\shadebar{16}} &\setscale{70}\tikz[baseline=-0.5ex]{\shadebar{69}} & \setscale{55}\tikz[baseline=-0.5ex]{\shadebar[green]{52}}\\
Summit &\setscale{20}\tikz[baseline=-0.5ex]{\shadebar{1}} &\setscale{70}\tikz[baseline=-0.5ex]{\shadebar{2}} & \setscale{55}\tikz[baseline=-0.5ex]{\shadebar[green]{50}}\\
Probing for expertise &\setscale{20}\tikz[baseline=-0.5ex]{\shadebar{17}} &\setscale{70}\tikz[baseline=-0.5ex]{\shadebar{34}} & \setscale{55}\tikz[baseline=-0.5ex]{\shadebar[green]{49}}\\
\end{tabular}
\end{document}

enter image description here