Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question
    
Could you edit your question title to something more specific, to make it easier for others to find this? –  Jake Jul 21 '12 at 17:10
    
I think that the gradient decreases readability a lot. If your really like it, you should at least make it the other way around so that the right edges are sharp. –  Alfred M. Jul 21 '12 at 19:03
add comment

3 Answers 3

up vote 12 down vote accepted

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

share|improve this answer
    
Thanks this looks great. But what can I do if I want in the bar not the numbers, but e.g. text? So as an example in Probing for ownership I want afterwards a bar with 16% but the text should be "yes" –  RoflcoptrException Jul 20 '12 at 9:42
    
Awesome answer! Used it that way! –  RoflcoptrException Aug 16 '12 at 23:27
add comment

Such thing can easily be made with TikZ:

\documentclass{standalone}
\usepackage{tikz}
\tikzset{
mystyle/.style={inner sep=0pt, inner ysep = .5mm, anchor=west, text height=2mm},
mynum/.style={inner sep=0pt, inner ysep = .5mm, anchor=east}
}

\begin{document}
\newcommand{\myrow}[4]{%
#1 & \begin{tikzpicture}[baseline=-2pt]\node at (0,0)[ fill = red, text width=#2 cm,mystyle] {}; \node[mynum] at (1cm,0){\pgfmathparse{int(round(#2*100))}\pgfmathresult}; \end{tikzpicture}&  %
 \begin{tikzpicture}[baseline=-2pt]\node at (0,0)[ fill = blue, text width=#3 cm,mystyle] {}; \node[mynum] at (1cm,0){\pgfmathparse{int(round(#3*100))}\pgfmathresult}; \end{tikzpicture}&%
  \begin{tikzpicture}[baseline=-2pt]\node at (0,0)[ fill = green, text width=#4 cm,mystyle] {}; \node[mynum] at (1cm,0){\pgfmathparse{int(round(#4*100))}\pgfmathresult}; \end{tikzpicture}\\
}

\begin{tabular}{lp{1cm}p{1cm}p{1cm}}
\myrow{yes}{1}{.5}{.75}
\myrow{no}{.33}{.2}{.9}
\myrow{maybe}{.02}{.2}{.9}
\end{tabular}


\end{document}

There is on thing I have to say about your picture: I as a reviewer would reject that thing. Why? Well look at the scale. It's a rubber scale. That means your projection point changes from cell to cell. Such cells can't be compared and the reader will get a false impression; e.g. 66 % and 52% look exactly the same. Now take a look at the result of mine: 100 % is always 1cm. This means (except small values) the projection is always the same.

enter image description here

share|improve this answer
    
Thanks for the remark. It isn't actually my picture, I just used it as an example. –  RoflcoptrException Jul 20 '12 at 9:01
add comment

See spark, sparklines and sparkln.

EDIT: Just noticed you also asked for Excel solutions. For that see sparklinesforxl on sourceforce (also see sparlinesforxl main site) or use the sparkline functionality built into Excel 2010 .

EDIT 2: Here is an example of using sparklinesforxl in Excel. We used the hbar function here. For example, if cell a1 holds a fraction that we wish to display in light blue then we put the formula =hbar(a1, 15128749) into the cell where we want the bar to appear or to display it in light green use =hbar(a1, 9498256) .

There is a color chart here which can be used to avoid color computations although in this example what I actually did was to start with RGB values of (144, 238, 144) for light green and (173, 216, 230) for light blue and use the formula R + 256 * G + 256^2 * B to get the corresponding numeric values shown in the last paragraph for argument 2.

screenshot

EDIT 3: And here is a sparklines package example:

\documentclass{article}
\usepackage{colortbl}
\usepackage[table]{xcolor}
\usepackage{sparklines}
\begin{document}

\setlength{\sparklinethickness}{5pt}
\begin{center}
\begin{tabular}{lllllll}

& \multicolumn{2}{l}{\bfseries E} & 
    \multicolumn{2}{l}{\bfseries O} & 
    \multicolumn{2}{l}{\bfseries E/O} \\

\bfseries First Line & 
    16\% & \definecolor{sparklinecolor}{RGB}{173,216,230}
    \begin{sparkline}{5} \spark 0.0 0.5 0.16 0.5 / \end{sparkline} &
    30\% & \definecolor{sparklinecolor}{RGB}{173,216,230}
    \begin{sparkline}{5} \spark 0.0 0.5 0.30 0.5 / \end{sparkline} &
    53\% & \definecolor{sparklinecolor}{RGB}{144,238,144}
    \begin{sparkline}{5} \spark 0.0 0.5 0.53 0.5 / \end{sparkline} \\

\bfseries Second Line & 
    10\% & \definecolor{sparklinecolor}{RGB}{173,216,230}
    \begin{sparkline}{5} \spark 0.0 0.5 0.10 0.5 / \end{sparkline} &
    20\% & \definecolor{sparklinecolor}{RGB}{173,216,230}
    \begin{sparkline}{5} \spark 0.0 0.5 0.20 0.5 / \end{sparkline} &
    50\% & \definecolor{sparklinecolor}{RGB}{144,238,144}
    \begin{sparkline}{5} \spark 0.0 0.5 0.50 0.5 / \end{sparkline} \\

\end{tabular}
\end{center}

\end{document}

screenshot

share|improve this answer
1  
Rather than just providing links, perhaps you could work up a small sample document that shows how to use the package? –  Alan Munn Jul 20 '12 at 18:57
    
These all contain examples in their documentation. –  user1189687 Jul 20 '12 at 19:22
2  
The idea of the Stackexchange model is that answers are supposed to be relatively self-contained. Answers with just links are especially discouraged since they can often disappear. –  Alan Munn Jul 20 '12 at 20:46
    
OK. I have added a couple of examples. –  user1189687 Jul 21 '12 at 20:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.