Drawing stacked charts using tikz

\documentclass{standalone}% http://ctan.org/pkg/standalone
\usepackage{calc}
\usepackage{ifthen}
\usepackage{tikz}
\usetikzlibrary{calc,positioning}
\newcommand\helpgrid[3]{ % args: x1 y1, step. The grid starts from (0,0) to (x1,y1) with given step
\draw[help lines, color=gray, dashed] (0,0) grid[step={(#3,#3)}] (#1,#2);
\foreach \x in {0,1,...,#1}
{
\node at (\x,0) {\textbf{\x}};
}
\foreach \y in {0,1,...,#2}
{
\node at (0,\y) {\textbf{\y}};
}
}
\newcounter{a}
\newcounter{b}
\newcounter{c}
\newcommand{\piechart}[6]{
% #1: TP
% #2: TN
% #3: FP
% #4: FN
% #5: center x
% #6: center y
\def \radius{2}
\setcounter{a}{#1 * 360 / (#1+#2+#3+#4)}
\setcounter{b}{\value{a} + #2 * 360 / (#1+#2+#3+#4)}
\setcounter{c}{\value{b} + #3 * 360 / (#1+#2+#3+#4)}
\tikzstyle{every node}=[inner sep=0, font=\scriptsize,fill=white]
\draw[fill=green!40] (#5,#6) -- ++(0:\radius) arc (0:\arabic{a}:\radius) node[midway]{TP:#1} -- cycle;
\draw[fill=cyan!40] (#5,#6) -- ++(\arabic{a}:\radius) arc (\arabic{a}:\arabic{b}:\radius) node[midway]{TN:#2} -- cycle;
\draw[fill=red!40] (#5,#6) -- ++(\arabic{b}:\radius) arc (\arabic{b}:\arabic{c}:\radius) node[midway]{FP:#3} -- cycle;
\draw[fill=orange!40] (#5,#6) -- ++(\arabic{c}:\radius) arc (\arabic{c}:360:\radius) node[midway]{FN:#4} -- cycle;
}
\newcommand{\bubblechart}[6]{
% #1: TP
% #2: TN
% #3: FP
% #4: FN
% #5: center x
% #6: center y
\def\k{2.}
\def\a{sqrt((#1*\k/(#1+#2+#3+#4))/3.14)}
\def\b{sqrt((#2*\k/(#1+#2+#3+#4))/3.14)}
\def\c{sqrt((#3*\k/(#1+#2+#3+#4))/3.14)}
\def\d{sqrt((#4*\k/(#1+#2+#3+#4))/3.14)}
\draw[fill=green!40] (#5,#6) circle (\a);
\draw[fill=cyan!40] ($(#5,#6)+(0.9,0)$) circle (\b);
\draw[fill=red!40] ($(#5,#6)+(2.1,0)$) circle (\c);
\draw[fill=orange!40] ($(#5,#6)+(2.7,0)$) circle (\d);
}
\newcommand{\stackchart}[7]{
% #1: TP
% #2: TN
% #3: FP
% #4: FN
% #5: center x
% #6: label
% #7: scale
\tikzstyle{xlabel}=[inner sep=0,font=\scriptsize,below=.25cm, right=0.5cm, anchor=east,rotate=90]
\tikzstyle{val}=[inner sep=.3,font=\scriptsize]
\tikzstyle{legend}=[draw,inner sep=1,font=\scriptsize]
\def\x{#5}
\def\k{#7}
\pgfmathsetmacro{\a}{(#1/(#1+#2+#3+#4))*\k}
\pgfmathsetmacro{\b}{(#2/(#1+#2+#3+#4))*\k}
\pgfmathsetmacro{\c}{(#3/(#1+#2+#3+#4))*\k}
\pgfmathsetmacro{\d}{(#4/(#1+#2+#3+#4))*\k}
\draw[fill=green!40] (\x,0) node[xlabel] {#6} rectangle ++(1.,\a) ;
\draw[fill=cyan!40] (\x,\a) rectangle ++(1.,\b);
\draw[fill=red!40] (\x,\a+\b) rectangle ++(1.,\c);
\draw[fill=orange!40] (\x,\a+\b+\c) rectangle ++(1.,\d);
% Write raw values:
\node[val,fill=green!40] at (\x+.5, \a) {#1};
\node[val,fill=cyan!40] at (\x+.5, \a+.5*\b) {#2};
\node[val,fill=red!40] at (\x+.5, \a+\b+.5*\c) {#3};
\node[val,fill=orange!40] at (\x+.5, \a+\b+\c+\d) {#4};
% Add a legend
\node[legend,fill=orange!40] (FN) at (0.5,5.5) {FN};
\node[legend,fill=red!40,below=.1cm of FN] (FP) {FP};
\node[legend,fill=cyan!40,below=.1cm of FP] (TN) {TN};
\node[legend,fill=green!40,below=.1cm of TN] (TP) {TP};
}
\begin{document}
\begin{tikzpicture}
\tikzstyle{T}=[inner sep=0,font=\scriptsize,text width=1cm,align=center]
\stackchart{172}{4787}{1750}{54}{3}{Energy}{4}
\stackchart{163}{4652}{1885}{63}{2}{SASD\_DXL}{4}
\stackchart{175}{4167}{2370}{51}{1}{SASD}{4}
\stackchart{169}{3879}{2658}{57}{0}{EucDist}{4}
\def\ybot{-2.3}
\draw (0,\ybot) -- node[midway,below] {Static} (4,\ybot);
\draw (0,6) -- node[midway,above] {Total: 6763} (4,6);
\stackchart{208}{6456}{2408}{55}{6.5}{Energy}{5.4}
\stackchart{207}{5497}{3367}{56}{5.5}{SASD}{5.4}
\stackchart{178}{6182}{2682}{85}{4.5}{EucDist}{5.4}
\draw (4.5,\ybot) -- node[midway,below] {ANM} (7.5,\ybot);
\draw (4.5,6) -- node[midway,above] {Total: 9127} (7.5,6);
% Threshold line:
\node[T] at (-.25,-1.7) {$T$};
\node[T] at (3.5,-1.7) {-11.8\\$\mbox{kcal}\cdot\mbox{mol}^{-1}$};
\node[T] at (2.5,-1.7) {8.6~\AA};
\node[T] at (1.5,-1.7) {29.8~\AA};
\node[T] at (0.5,-1.7) {15.9~\AA};
\node[T] at (7,-1.7) {-11.8\\$\mbox{kcal}\cdot\mbox{mol}^{-1}$};
\node[T] at (6,-1.7) {30.5~\AA};
\node[T] at (5,-1.7) {18.6~\AA};
%\helpgrid{10}{10}{1}
\end{tikzpicture}
\end{document}
view raw charts.tex hosted with ❤ by GitHub

stackedchart

If you want to ask me a question or leave me a message add @bougui505 in your comment.