1

What's the best way of including a progress bar-like item next to the nodes in a drawing of a network? The idea is that nodes are associated with some quantity in $[0,1]$ which changes across iterations, and which I'd like to depict visually, kind of like this:

enter image description here

One idea, of course, would be to draw a bar chart next to each node, but I'm wondering whether that's overkill and there might be better alternatives.

2

1 Answer 1

3

Something like this?

\documentclass[tikz,border=3,14159]{standalone}
\usetikzlibrary{calc}

\begin{document}
    \begin{tikzpicture}[mynode/.style={circle, draw, minimum width=1cm},node distance=2cm]
        \newcommand{\progbar}[2]
            {
            \def\h{1}
            \def\l{0.3}
            \fill[red] ($(#1)-(0.5*\l,0)$) rectangle ++ (\l,#2*0.01*\h);
            \draw ($(#1)-(0.5*\l,0)$) rectangle ++ (\l,\h);
            \node[above] at ($(#1)+(0,\h)$) {#2\,\%};
            }
            
        
        \node[mynode] (1) {1};
        \node[mynode,right of=1] (2) {2};
        \draw[-stealth] (1)--(2);
        
        \coordinate[left=1cm] (A) at (1.south);
        \coordinate[right=1cm] (B) at (2.south);
        
        \def\va{25} %<--------- define here the value for node 1
        \progbar{A}{\va};
        \pgfmathtruncatemacro\vb{100-\va}
        \progbar{B}{\vb};
    \end{tikzpicture}
\end{document}

progbar next to nodes

EDIT
If you want to see the figures in decimal notation (which makes sense if you're talking bout probabilities), there's some adjustments to do:

\documentclass[tikz,border=3,14159]{standalone}
\usetikzlibrary{calc}

\begin{document}
    \begin{tikzpicture}[mynode/.style={circle, draw, minimum width=1cm},node distance=2cm]
        \newcommand{\progbar}[2]
            {
            \def\h{1}
            \def\l{0.3}
            \fill[red] ($(#1)-(0.5*\l,0)$) rectangle ++ (\l,#2*0.01*\h);
            \draw ($(#1)-(0.5*\l,0)$) rectangle ++ (\l,\h);
            \pgfkeys{/pgf/number format/.cd,fixed,precision=2}
            \pgfmathparse{0.01*#2}\edef\storeresult{\pgfmathresult}%
            \node[above] at ($(#1)+(0,\h)$) {\pgfmathprintnumber\storeresult};
            }
            
        
        \node[mynode] (1) {1};
        \node[mynode,right of=1] (2) {2};
        \draw[-stealth] (1)--(2);
        
        \coordinate[left=1cm] (A) at (1.south);
        \coordinate[right=1cm] (B) at (2.south);
        
        \def\va{84} %<--------- define here the value for node 1
        \progbar{A}{\va};
        \pgfmathtruncatemacro\vb{100-\va}
        \progbar{B}{\vb};
    \end{tikzpicture}

\end{document}

progbar v2

1
  • 3
    That's great, thanks! I snooped around some more after asking and got some approximate results, but it definitely would have taken me longer to figure out how to write the appropriate fill function for the color.
    – Palmy
    Feb 16, 2021 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .