Easy plotting comes with Tk. Run the "widget" demo, and look under the Canvas section for "Simple 2-d Plot". Setting up the graph hash marks can be a challenge. See Tk Realtime data aquisition and Tk::Graph
Here is a super simple plot on a Tk canvas. I plot values of sin, but you can substitute your discrete values. Another few examples follow below.
#!/usr/bin/perl -w
use strict;
use Tk;
my($o, $s) = (250, 20);
my($pi, $x, $y) = (3.1415926, 0);
my $mw = MainWindow->new;
my $c = $mw->Canvas(-width => 500, -height => 500);
$c->pack;
$c->createLine(50, 250, 450, 250);
$c->createText(10, 250, -fill => 'blue', -text => 'X');
$c->createLine(250, 50, 250, 450);
$c->createText(250, 10, -fill => 'blue', -text => 'Y');
for ($x = -(3*$pi); $x <= +(3*$pi); $x += 0.1) {
$y = sin($x);
$c->createText( $x*$s+$o, $y*$s+$o, -fill => 'red', -text => ':');
$y = cos($x);
$c->createText( $x*$s+$o, $y*$s+$o, -fill => 'green', -text => '|'
+);
}
MainLoop;
If you want something more professional, try gnuplot. It can be run thru Tk too.
#!/usr/bin/perl
use Tk;
use Tk::LabEntry;
use FileHandle;
use strict;
# echo "plot sin (x)" | gnuplot -persist
my $gnuplot_path = "/usr/bin/gnuplot";
my $top = new MainWindow;
my $function = "sin(x)";
my $funcentry = $top->LabEntry
(-label => 'Function:',
-textvariable => \$function,
-labelPack => [-side => 'left'])->pack;
my $butframe = $top->Frame->pack(-anchor => 'w');
my $plotbutton = $butframe->Button(-text => 'Plot',
-command => \&plot,
)->pack(-side => 'left');
$butframe->Button(-text => 'Quit',
-command => \&quit,
)->pack(-side => 'left');
$top->protocol('WM_DELETE_WINDOW', \&quit);
my $gnuplot = new FileHandle("| $gnuplot_path");
$gnuplot->autoflush(1);
$top->bind("<Return>", sub { $plotbutton->invoke });
MainLoop;
sub plot {
$gnuplot->print("plot $function\n") if $function ne '';
}
sub quit {
$gnuplot->close;
$top->destroy;
}
And here is a neat little snippet I have( I didn't write it ), that uses Tk::Graph.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
use Tk::Graph;
my $Main = MainWindow->new( -title => "Test", -background => "white" )
+;
$Main->geometry( '+100+100' );
my $pollsec = 3;
my $f1 = $Main->Frame()->pack( -side => "left", -fill => "both", -expa
+nd => 1 );
my $g1 = $f1->Graph(
-type => "Line",
-legend => 0,
-headroom => 0,
-foreground => "black",
-debug => 0,
-borderwidth => 2,
-titlecolor => '#435d8d',
-yformat => '%g',
-ylabel => "Mb",
-xformat => "%g",
-xlabel => "Requests",
-barwidth => 15,
-padding => [ 50, 20, -30, 50 ], # Padding [top, right, butt
+om, left]
-printvalue => '%s', # Name: Wert
-linewidth => 2,
-shadow => '#435d8d',
-shadowdepth => 3,
-dots => 1,
-look => 20,
-wire => "#d2e8e4",
-max => 1024,
-ytick => 8,
-xtick => 5,
-config => { Used => { -color => "#2db82a" } },
)->pack(
-side => "bottom",
-expand => 1,
-fill => 'both',
);
update_system_memory( $g1, $pollsec * 1000 );
MainLoop;
#####################################################
sub update_system_memory {
my $wid = shift or return undef;
my $ms = shift or return undef;
my $data = {};
my $total = $data->{ Total } = 1024;
my $free = $data->{ Free } = int rand( 1024 );
my $used = $total - $free;
my $percent = 100 * $used / $total;
my $title = sprintf "\nSystem Memory\nMax: %d %s\nUsed: %d %s",
calc_unit_from_kb( $total * 1024, "Mb" ),
calc_unit_from_kb( $used * 1024, "MB" );
$wid->configure(
-title => $title,
-config => {
Used => {
-color => (
$percent >= 50
? ( $percent >= 90 ? "#ff3333" : "#ffb200" )
: "#2db82a"
)
}
},
);
printf "used=%s\n", $used;
$wid->set( { Used => $used, } );
$wid->after( $ms, [ \&update_system_memory => $wid, $ms ] );
}
sub calc_unit_from_kb {
my $value = shift; # Uebergabe in Kb!
return ( -0, "?" ) unless defined $value;
my $unit = shift;
$unit = uc $unit if defined $unit;
my $u = "";
foreach ( qw/Kb Mb Gb Tb Pb/ ) {
$u = $_;
last if defined $unit and uc( $u ) eq $unit;
last if $value < 1024 and not defined $unit;
$value = $value / 1024;
}
return ( $value, $u );
}
|