Examples

Simple

from ascii_graph import Pyasciigraph

test = [('long_label', 423), ('sl', 1234), ('line3', 531),
    ('line4', 200), ('line5', 834)]

graph = Pyasciigraph()

# Simpler example
for line in graph.graph('test 1', test):
    print(line)

# Take also anything castable to utf-8
test = [(1, 423), (2, 1234), (3, 531), ('line4', 200), ('line5', 834)]
for line in graph.graph('test 2', test):
    print(line)

# Example of sort
test.sort(reverse=False, key=lambda tup: tup[1] )
for line in graph.graph('test 2', test):
    print(line)

# Example of reverse sort
test.sort(reverse=True, key=lambda tup: tup[1] )
for line in graph.graph('test 2', test):
    print(line)

print('')
# No label
for line in graph.graph(data=test):
    print(line)

# Different separator length
graph = Pyasciigraph(separator_length=4)
for line in graph.graph(data=test):
    print(line)

Color

#!/bin/env python2

from ascii_graph import Pyasciigraph
from ascii_graph.colors import *

# Simple coloring
test = [('testval0', 600),
        ('testval1', 500, Pur),
        ('testval2', 400, Red),
        ('testval3', 400, Red),
        ('testval4', 300, Gre),
        ('testval5', 200, Yel),
        ('testval6', 100, Cya),
        ('testval7', 50,  Blu) ]

graph = Pyasciigraph()
for line in graph.graph('test graph', test):
    print(line)


# Bold and underline
test = [('testval0', 142),
        ('testval1', 204, BPur),
        ('testval2', 501, URed),
        ('testval3', 103, IRed),
        ('testval4',  29, BIGre),
        ('testval5',  19, UYel),
        ('testval6',  99, ICya),
        ('testval7', 404, BBlu)]
graph = Pyasciigraph()
for line in graph.graph('test graph', test):
    print(line)


# Coloring data according to a pattern (one color each line)
from ascii_graph.colordata import vcolor

test = [('testval0', 600),
        ('testval1', 500),
        ('testval2', 400),
        ('testval3', 400),
        ('testval4', 300),
        ('testval5', 200),
        ('testval6', 100),
        ('testval7', 50 )]

pattern = [Gre, Yel, Red]

data = vcolor(test, pattern)
for line in graph.graph('vcolor test', data):
    print(line)

exit(0)

Multicolor

#!/bin/env python2

from ascii_graph import Pyasciigraph
from ascii_graph.colors import *

test = [('testval0', 600),
           ('testval1', 400, Red),
           ('testval2', [(600, Gre), (500, Blu)]),
           ('testval3', [(200, Yel), (100,)]),
           ('testval4', -170, Cya),
           ('testval5', 50, Blu),
           ('testval6', [(-300, Gre), (-230, Red)]),
           ('testval7', [(-100, Gre), (-230, Red), (200, Yel), (600, Blu)])]

# Multicolor with all the values displayed
graph = Pyasciigraph(separator_length=4)
for line in graph.graph('test graph', test):
    print(line)

# Multicolor with only the max value displayed
graph = Pyasciigraph(separator_length=4, multivalue=False)
for line in graph.graph('test graph mono value', test):
    print(line)

# Coloring data according to thresholds
from ascii_graph.colordata import hcolor

test = [('testval0 (\033[92m+\033[0m)', 600),
        ('testval1 (\033[91m-\033[0m)', 500),
        ('testval2 (\033[92m+\033[0m)', -400),
        ('testval3 (\033[92m+\033[0m)', 400),
        ('testval4 (\033[92m+\033[0m)', 300),
        ('testval5 (\033[91m-\033[0m)', 200),
        ('testval6 (\033[92m+\033[0m)', 100),
        ('testval7 (\033[92m+\033[0m)', 50 )]

thresholds = {
  51:  Gre,
  100: Blu,
  350: Yel,
  500: Red,
}

data = hcolor(test, thresholds)
for line in graph.graph('hcolor test', data):
    print(line)

exit(0)

Human Readable

#!/bin/env python2

from ascii_graph import Pyasciigraph
from ascii_graph.colors import *

test = [('testval0', 123456),
           ('testval1', 918829, Red),
           ('testval2', [(600192, Gre), (500203, Blu)]),
           ('testval3', [(2, Yel), (10,)]),
           ('testval4', 10, Cya),
           ('testval5', 50, Blu),
           ('testval6', [(1001233, Gre), (150000, Red), (100000, Yel), (60, Blu)])]

# Multicolor with all the values displayed
graph = Pyasciigraph(separator_length=4, human_readable='cs')
for line in graph.graph('test graph', test):
    print(line)

# Multicolor with only the max value displayed
graph = Pyasciigraph(separator_length=4, multivalue=False, human_readable='cs')
for line in graph.graph('test graph mono value', test):
    print(line)

# Coloring data according to thresholds
from ascii_graph.colordata import hcolor

test = [('testval0', 6003),
        ('testval1', 900),
        ('testval2', 4920),
        ('testval3', 400),
        ('testval4', 30),
        ('testval5', 200),
        ('testval6', 1025),
        ('testval7', 5023 )]

thresholds = {
  51:  Gre,
  100: Blu,
  350: Yel,
  500: Red,
}

data = hcolor(test, thresholds)
for line in graph.graph('hcolor test', data):
    print(line)

exit(0)

Shell Scripts

#!/bin/sh

# It's a small example of asciigraph usage
# It just displays the load

tput clear
old_width=`tput cols`


# Get number of cores (empiricaly I always try to have load < number of cores)
max=`grep -c processor /proc/cpuinfo`
# Calculate the first threshold (until this threshold, graph is green)
t1=`awk "BEGIN {print $max * 3 / 4}"`
# Calculate the second threshold.
# Between t1 and t2, graph is yellow. 
# After t2, graph is red.
t2=`awk "BEGIN {print $max * 4 / 4}"`

while true
do
    # Get the load
    load_15=`uptime|sed "s/.*:\ //"|cut -d ',' -f 3`
    load_1=`uptime|sed "s/.*:\ //"|cut -d ',' -f 1`
    load_5=`uptime|sed "s/.*:\ //"|cut -d ',' -f 2`

    # Get the width of the term
    width=`tput cols`
    if [ $width -ne $old_width ]
    then
        tput clear
    fi
    old_width=$width

    printf "\
load threshold:$max\n\
load last minute:$load_1\n\
load last 5 minutes:$load_5\n\
load last 15 minutes:$load_15\n" | \
    asciigraph -w $width -l 'loadtop' -c -t $t1 -T $t2

    #refresh every 1 second
    tput cup 0 0
    sleep 1
done
#!/bin/sh

width=`tput cols`

# simple example to graph the du command output

# run du -> refomart a little with awk -> give it to asciigraph
du -d 1 $@ | head -n -1 |\
    awk '{s = ""; for (i = 2; i <= NF; i++) s = s $i " "; printf "%s:%s\n", s, $1 * 1024;}' | \
    asciigraph -l 'Disk Usage' -H -s dec -c -w $width