Partially coloring cell background with histograms
Update with minimal and maximal values
Minimal values are only usable when negative values are disabled (\negativeValuesfalse
).
Code
\documentclass{article}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{tikz,array,collcell}
\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfdeclarelayer{main}
\pgfsetlayers{background,main}
\newif\ifnegativeValues
\newcommand{\tikzMe}[1]{%
\tikz[baseline]{
\node[anchor=base,text width=\minWidth,align=\alignment,inner sep=0pt,inner xsep=\tabcolsep,outer sep=0pt] (n) {\strut$#1$};
\begin{pgfonlayer}{background}
\ifnegativeValues
\pgfmathparse{#1<0?"red!50":"green!25"}
\edef\color{\pgfmathresult}
\else
\def\color{green!25}
\fi
\pgfmathparse{abs((#1-\minValue)/(\maxValue-\minValue))}
\fill[color=\color] (n.north west) rectangle ($(n.south west)!\pgfmathresult!(n.south east)$);
\end{pgfonlayer}
}
}
%\negativeValuestrue % inserts a minus sign for the minWidth and use red for negative values
\negativeValuesfalse % doesn't insert a minus sign, uses only green
\newcolumntype{H}[3]{%
@{}
>{%
\ifx\\#1\\\def\alignment{right}\else\def\alignment{#1}\fi%
\ifnegativeValues\def\minValue{0.}\else\def\minValue{#2}\fi%
\def\maxValue{#3}%
\def\minWidth{\widthof{$\ifnegativeValues-\fi#3$}}%
\collectcell\tikzMe%
}c<{\endcollectcell}
@{}}
\begin{document}
\begin{tabular}{|H{}{10.}{100.00}|H{}{2.}{105.00}|H{}{0.}{150.00}|}
\hline
10.0 & 2.00 & 0.00 \\
15.49 & 13.82 & 100.00 \\
100.00 & 105.00 & 150.00 \\ \hline
\end{tabular}
\end{document}
Output
Old answer
Code
\documentclass{article}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\usepackage{tikz,array,collcell}
\usetikzlibrary{calc}
\pgfdeclarelayer{background}
\pgfdeclarelayer{main}
\pgfsetlayers{background,main}
\newcommand*{\minWidth}{\widthof{$-100.00$}}
\newcommand*{\maxValue}{100}
\newcommand{\tikzMeL}[1]{\tikzMe{#1}{left}}
\newcommand{\tikzMeC}[1]{\tikzMe{#1}{center}}
\newcommand{\tikzMeR}[1]{\tikzMe{#1}{right}}
\newcommand{\tikzMe}[2]{%
\tikz[baseline]{
\node[anchor=base,text width=\minWidth,align=#2,inner sep=0pt,inner xsep=\tabcolsep,outer sep=0pt] (n) {\strut$#1$};
\begin{pgfonlayer}{background}
\pgfmathparse{#1<0?"red!50":"green!25"}
\edef\color{\pgfmathresult}
\pgfmathparse{abs(#1/\maxValue)}
\fill[color=\color] (n.north west) rectangle ($(n.south west)!\pgfmathresult!(n.south east)$);
\end{pgfonlayer}
}
}
\newcolumntype{L}{@{}>{\collectcell\tikzMeL}c<{\endcollectcell}@{}}
\newcolumntype{C}{@{}>{\collectcell\tikzMeC}c<{\endcollectcell}@{}}
\newcolumntype{R}{@{}>{\collectcell\tikzMeR}c<{\endcollectcell}@{}}
\begin{document}
\begin{tabular}{|L|C|R|} \hline
91.41 & 100.00 & 38.76 \\
-15.49 & -13.82 & -100.00 \\
57.11 & 51.21 & -42.84 \\ \hline
\end{tabular}
\end{document}
Output
Another sans-TikZ alterantive:
\documentclass{article}
\usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\newcommand{\maxnum}{100.00}
\newlength{\maxlen}
\newcommand{\databar}[2][green!25]{%
\settowidth{\maxlen}{\maxnum}%
\addtolength{\maxlen}{\tabcolsep}%
\FPeval\result{round(#2/\maxnum:4)}%
\rlap{\color{green!25}\hspace*{-.5\tabcolsep}\rule[-.05\ht\strutbox]{\result\maxlen}{.95\ht\strutbox}}%
\makebox[\dimexpr\maxlen-\tabcolsep][r]{#2}%
}
\begin{document}
\begin{tabular}{*{3}{|l}|}
\hline
\databar{91.41} & \databar {90.81} & \databar{38.76} \\
\databar{98.75} & \databar {13.82} & \databar{94.62} \\
\databar{57.11} & \databar {51.21} & \databar{42.84} \\
\databar{20.00} & \databar{100.00} & \databar{80.00} \\
\hline
\end{tabular}
\end{document}
You provide the maximum number (as a macro \maxnum
) that is used to calculate the percentage fill. Calculations are performed using fp
.
Using this variation of \databar
provides a fill that leaves a (barely visible) .5\arrayrulewidth
on either side of the table cell. This is just to avoid any kind of artefacts caused by the viewer. The TikZ solution doesn't show this since it draws the coloured boxes in the background (almost as an underlay) of the entire table, so the rules overdraw any overlap:
%...
\newcommand{\databar}[2][green!25]{%
\settowidth{\maxlen}{\maxnum}%
\addtolength{\maxlen}{\dimexpr2\tabcolsep-\arrayrulewidth}%
\FPeval\result{round(#2/\maxnum:4)}%
\rlap{\color{green!25}\hspace*{\dimexpr-\tabcolsep+.5\arrayrulewidth}\rule[-.05\ht\strutbox]{\result\maxlen}{.95\ht\strutbox}}%
\makebox[\dimexpr\maxlen-2\tabcolsep+\arrayrulewidth][r]{#2}%
}
%...