TransWikia.com

Progress bar next to node in network

TeX - LaTeX Asked by Willow on April 12, 2021

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.

One Answer

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]
            {
            defh{1}
            defl{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);
        
        defva{25} %<--------- define here the value for node 1
        progbar{A}{va};
        pgfmathtruncatemacrovb{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]
            {
            defh{1}
            defl{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}edefstoreresult{pgfmathresult}%
            node[above] at ($(#1)+(0,h)$) {pgfmathprintnumberstoreresult};
            }
            
        
        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);
        
        defva{84} %<--------- define here the value for node 1
        progbar{A}{va};
        pgfmathtruncatemacrovb{100-va}
        progbar{B}{vb};
    end{tikzpicture}

end{document}

progbar v2

Correct answer by SebGlav on April 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP