MonkeyManChfKiller has asked for the wisdom of the Perl Monks concerning the following question:

How could i graph a changing variable, Awhile ago i made perl script that reads incoming and outgoing data in a server,i would like to graph it. basically every second the script checks ingoing and outgoing kbps. I would like to graph each value as so: Every second it would print a bar like:
# #### ### ## #### #### ### ##### # ##################
After a specific amount of Kbps, (or a specific number) the graph goes flat, as seen above. Each column would be a value, column one in that specific chart would be 5120kbp/s, (or 5120 in the variable) the next is lower and depending on the height that the graph is displaying is the current data. Obviously, at the bottom of the graph i will have a reader like i already have. (Incoming Kbp/'s = 123, Outgoing Kbp/'s 100). Tl:Dr, What im trying to accomplish is graphing numbers depending on the size of the number.

Replies are listed 'Best First'.
Re: Graphing varibles.
by wjw (Priest) on May 18, 2014 at 08:18 UTC
    You might find something to start with here ascii graph.

    Honestly, I just did a google search on "perl dynamic graphing using ascii characters" which brought me to that node as the first hit. Seems to me though (after a quick look) that what was done in that node could be adapted for your use.

    Just a thought...hope it is helpful.

    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results...
Re: Graphing varibles.
by flexvault (Monsignor) on May 18, 2014 at 14:03 UTC

    Welcome MonkeyManChfKiller,

    You may want to use your favorite search engine to search on MRTG which stands for 'Multi Router Traffic Grapher' which besides being written in Perl, also does a great job of graphing all sorts of continuously changing activity. It has been used for bandwidth, users, etc. and uses very little resources. I've used it for at least 15 years and it just keeps on working.

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin

Re: Graphing varibles.
by zentara (Cardinal) on May 18, 2014 at 11:04 UTC
    Here is a start for a display if you are willing to, or can, use Tk. The Tk Canvas widget is very versatile, you can do almost anything you want on it. Remember, Tk requires a X server, or equivalent to run; it's not command line.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $w=20; my $x=0; my $y=0; my %colors = ( 0 => ['black','yellow'], 1 => ['yellow','black'], 2 => ['white','green'], 3 => ['green','white'], 4 => ['grey','red'], 5 => ['red','grey'], 6 => ['blue','white'], 7 => ['white','blue'], 8 => ['orange','grey45'], 9 => ['grey45','orange'], ); my %bardata = ( 0 => rand 200, 1 => rand 200, 2 => rand 200, 3 => rand 200, 4 => rand 200, 5 => rand 200, 6 => rand 200, 7 => rand 200, 8 => rand 200, 9 => rand 200, ); my %bars; my $mw=tkinit; my $c = $mw->Canvas->pack; for (0..9) { $bars{$_} = $c->createRectangle($x,$y,$x+20,$bardata{$_}, -fill=> ${$colors{$_}}[0], ); my $text = $c->createText($x+10,$y+10, -anchor=>'center', -fill => ${$colors{$_}}[1], -text => $_ ); $x+=20; } $mw->Button( -text => "Save", -command => [sub { $c->update; my @capture=(); my ($x0,$y0,$x1,$y1)=$c->bbox('all'); @capture=('-x'=>$x0,'-y'=>$y0,-height=>$y1-$y0,-width=>$x1-$x +0); $c -> postscript(-colormode=>'color', -file=>$0.'.ps', -rotate=>90, -width=>800, -height=>500, @capture); } ] )->pack; $mw->repeat(200, sub{ &update }); MainLoop; ########################################################## sub update{ $x=0; $y=0; %bardata = ( 0 => rand 200, 1 => rand 200, 2 => rand 200, 3 => rand 200, 4 => rand 200, 5 => rand 200, 6 => rand 200, 7 => rand 200, 8 => rand 200, 9 => rand 200, ); for (0..9) { $c->delete( $bars{$_} ); $bars{$_} = $c->createRectangle($x,$y,$x+20,$bardata{$_}, -fill=> ${$colors{$_}}[0], ); my $text = $c->createText($x+10,$y+10, -anchor=>'center', -fill => ${$colors{$_}}[1], -text => $_ ); $x+=20; } }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Graphing varibles.
by karlgoethebier (Abbot) on May 18, 2014 at 08:31 UTC

    Unsure about the specs again. Do you search something like this?

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»