Operating System - HP-UX
1833055 Members
2441 Online
110049 Solutions
New Discussion

Re: Simulating a graph-bar generator

 
SOLVED
Go to solution
Jose Mosquera
Honored Contributor

Simulating a graph-bar generator

Hi pals,

Any smart shell-script to simulate into a text environment a graph-bar chart. Pls imagine the following data colection:

A=8
B=2
C=5
D=0
E=4

Then I'll need obtain something like this:

====
====
====
==== ====
==== ==== ====
==== ==== ====
==== ==== ==== ====
==== ==== ==== ====
A B C D E

Best regards,
JMM
5 REPLIES 5
Jose Mosquera
Honored Contributor

Re: Simulating a graph-bar generator

Pls see attached text in previuos note to see the real output wanted.

BR,

JMM
Chris Wilshaw
Honored Contributor

Re: Simulating a graph-bar generator

Not sure about getting the bars vertically, however;

#!/usr/bin/sh
A=8
B=2
C=5
D=0
E=5
STRING="====="

for LIST in A B C D E
do
COUNT=0
echo "$LIST \c"
while [ $COUNT -lt $LIST ]
do
echo "$STRING \c"
COUNT=`expr $COUNT + 1`
done
echo ""
done

This generates

A ===== ===== ===== ===== ===== ===== ===== =====
B ===== =====
C ===== ===== ===== ===== =====
D
E ===== ===== ===== ===== =====

Hope that's of some use.

Chris
Hein van den Heuvel
Honored Contributor

Re: Simulating a graph-bar generator

SMOP:

in perl:

$data{"A"} = 8;
$data{"B"} = 2;
$data{"C"} = 5;
$data{"D"} = 0;
$data{"E"} = 4;
foreach $key (keys %data) { $max = $data{$key} if ($data{$key} > $max)};
while ($max-- > 0) {
foreach $key (keys %data) {
printf ("%6.6s ", ($data{$key} >= $max)? "====": " ");
}
print "\n";
}
foreach $key (keys %data) {
printf ("%6.6s ", $key);
}
print "\n";

Hein van den Heuvel
Honored Contributor
Solution

Re: Simulating a graph-bar generator

Might as well do it right....

With boundary condition fixed, and sorted key values:

$data{"A"} = 8;
$data{"B"} = 2;
$data{"C"} = 5;
$data{"D"} = 0;
$data{"E"} = 4;
foreach $key (sort keys %data) {
$max = $data{$key} if ($data{$key} > $max);
push @k, $key;
};
while ($max > 0) {
foreach $key (@k) {
printf ("%6.6s ", ($data{$key} >= $max)? "====": " ");
}
print "\n";
$max--;
}
foreach $key (@k) {
printf ("%6.6s ", $key);
}
print "\n";
Nicolas Dumeige
Esteemed Contributor

Re: Simulating a graph-bar generator

To achive the horizontal display you want (not the simpliest !!!) use the terminal capability like

- tput and cups : I have a script wich use this kind of stuff. It's rather slow actually.

- use the C standard library curses.h

If you're successfull, send a feedback on the forum :-D
Good luck.

Nicolas
All different, all Unix