LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2019, 10:16 AM   #1
teckk
Senior Member
 
Registered: Oct 2004
Distribution: FreeBSD Arch
Posts: 2,241

Rep: Reputation: 474Reputation: 474Reputation: 474Reputation: 474Reputation: 474
The power of bash: Creating charts and tables.


No gnuplot needed here. Just bash.

Some examples:
Code:
#! /usr/bin/env bash

#Color chart to compare foreground/background colors in 
#terminal. Terminal needs to be at least 80 char wide.

FG=(' black ' '  red  ' ' green ' ' yellow' '  blue '
    'magenta' '  cyan ' ' white ')
    
BG=('Def' 'Blk' 'Red' 'Grn' 'Yel' 'Blu' 'Mag' 'Cya' 'Wht')

str=$(printf "%74s")
echo "     ┌"${str// /─}"┐"

for b in {0..8}; do 
    bg=$((b+39))

    echo -en "\033[0m ${BG[b]} │ "

    for f in {0..7}; do
        echo -en "\033[${bg}m\033[$((f+30))m ${FG[f]} "
    done

    echo -en "\033[0m │\n\033[0m     │ "

    for f in {0..7}; do
        echo -en "\033[${bg}m\033[1;$((f+30))m ${FG[f]} "
    done

    echo -en "\033[0m │\n\033[0m"
  
    echo "     ├"${str// /─}"┤"
done

echo "     └"${str// /─}"┘"
Code:
#! /usr/bin/env bash

#Color chart to compare foreground/background colors in 
#terminal. Terminal needs to be at least 90 char wide.

clear
T='Text'

FGR=('    m' '   1m' '  30m' '1;30m' '  31m' '1;31m' '  32m' \
    '1;32m' '  33m' '1;33m' '  34m' '1;34m' '  35m' '1;35m' \
    '  36m' '1;36m' '  37m' '1;37m')

BGR=(40m 41m 42m 43m 44m 45m 46m 47m)

printf "%45s %s\n %s\n" "Color chart" 

for FGs in "${FGR[@]}"; do
 
    FG=${FGs// /}
    echo -en " $FGs \033[$FG  $T  "
    
    for BG in "${BGR[@]}"
        do echo -en "\033[$FG\033[$BG  $T  \033[0m"
    done
    
    echo
done
Code:
#! /usr/bin/env bash

#Multiplication table. 
#Terminal needs to be at least 100 char wide.

clear

NUM1=({0..15})
NUM2=({0..16})

printf "%55s %s\n %s\n" "Multiplication table" 
printf "%6s" "${NUM1[@]}"
echo

BGC=(43m 45m 46m 43m 45m 46m 43m 45m 46m 43m 45m \
    46m 43m 45m 46m 43m)

for i in "${NUM1[@]}"; do
    if [ $i -lt 10 ]; then
        echo -en " "$i""
    else
        echo -en ""$i""
    fi
    
    A=0; index=0
    for j in "${BGC[@]}"; do
        A=$((${NUM2[$index]} * $i))
     
        if [ $A -lt 10 ]; then
            echo -en "\033[30m\033["$j"   "$A"  "

        elif [ $A -gt 9 -a $A -lt 100 ]; then
            echo -en "\033[30m\033["$j"  "$A"  "
            
        elif [ $A -gt 99 ]; then
            echo -en "\033[30m\033["$j" "$A"  "
        fi
        
        index=$(($index + 1))
        tput sgr0
    done
    echo
done
Code:
#! /usr/bin/env bash

#Temp conversion table. 
#Terminal needs to be at least 30 lines tall.

clear

NUM1=('DegC' 'DegF' 'DegK')
NUM2=({-10..36..2})

echo "Temp conversion table" 
echo -e "\n"${NUM1[@]}""

index=0
for i in "${NUM2[@]}"; do

    for j in x; do
    
        A=${NUM2[$index]}
        B=$((((${NUM2[$index]} * 9) / 5) + 32))
        C=$((${NUM2[$index]} + 273))
        
        if [ $A -lt -9 ]; then
            echo -en "\033[30m\033[43m"$A"   "$B"  "$C" "
        elif [ $A -gt -9 -a $A -lt 0 ]; then
            echo -en "\033[30m\033[43m "$A"   "$B"  "$C" "
        elif [ $A -gt -1 -a $A -lt 10 ]; then
            echo -en "\033[30m\033[43m  "$A"   "$B"  "$C" "
        elif [ $A -gt 9 -a $A -lt 100 ]; then
            echo -en "\033[30m\033[43m "$A"   "$B"  "$C" "
        fi

        index=$(($index + 1))
    done
    
    tput sgr0
    echo
    
done
 
Old 08-06-2019, 11:14 AM   #2
average_user
Member
 
Registered: Dec 2010
Location: Warsaw, Poland
Distribution: Slackware
Posts: 411

Rep: Reputation: 161Reputation: 161
Nice. You really should add screenshots for marketing purposes:
Attached Thumbnails
Click image for larger version

Name:	multiplication-table.png
Views:	78
Size:	13.3 KB
ID:	31042   Click image for larger version

Name:	temp-table.png
Views:	56
Size:	7.0 KB
ID:	31043  
 
1 members found this post helpful.
Old 08-07-2019, 03:46 AM   #3
josephj
Member
 
Registered: Nov 2007
Location: Northeastern USA
Distribution: kubuntu
Posts: 191

Rep: Reputation: 102Reputation: 102
Nice.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Software recommendation for creating pie charts on RHEL server user5309 Linux - Software 2 06-29-2016 04:34 PM
LXer: Creating geographical charts with EuroOffice Map Chart LXer Syndicated Linux News 0 12-08-2008 07:30 AM
LXer: OOo Basic crash course: Creating charts with Base and Calc LXer Syndicated Linux News 0 07-31-2008 11:20 PM
LXer: Creating charts on Web pages with Java and GChart LXer Syndicated Linux News 0 04-25-2008 11:20 AM
LXer: Creating simple charts with Gambas 2.0 LXer Syndicated Linux News 0 12-12-2007 03:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration