I tried out Tk with GD somewhile back on linux, but ended up using a web server due to the access required. Here are a couple of snippets for you from my hacking around. I made this work by having GD output to a png file, and then with Tk presenting that file.
The modules used were: Tk, Tk::PNG, and GD::Graph using the
area format (GD::Graph::area).
First making a graph with GD:
##### build a cpu graph...
$poop = 'CPU - Wait IO data from '.$startdate.' - '.$enddate;
# a fancy title for our graph
@data = (\@dateline, \@graphcpu_usr, \@graphcpu_sys, \@graphcpu_wio );
+
# this is an array of arrays. The first one is the X axis
+
# which is the time, the others are the Y axis
my $data = GD::Graph::Data->new() or die GD::Graph::Data->error;
$data->copy_from(\@data);
+
# this tells the graph to get the data from the data array
my $my_graph = new GD::Graph::area(400,300);
+
# and this says what type (area) and it's size
$my_graph->set(
+
# and set up all the particulars...
r_margin => 1,
x_label => 'Time',
y_label => 'CPU Utilization',
title => $poop,
y_min_value => 0,
y_tick_number => 8,
y_label_skip => 1,
x_ticks => 1,
x_label_skip => $x_label_skip,
x_labels_vertical => 1,
x_label_position => 1/2,
cumulate => 1,
transparent => 1,
)or warn $my_graph->error;
open (IMG, ">dbcpu.png") or die "Can't open up the dpcpu graph file";
binmode IMG;
$my_graph->set( dclrs => [ qw(lgreen lblue lred) ] );
+
# here is the colors in the graph
$my_graph->set_legend("usr","sys","wio" );
+
# and the legend text
$my_graph->set_title_font(GD::Font->MediumBold);
print IMG $my_graph->plot($data)->png();
+
# and this plots the image out to a png format
close IMG;
And here is where in TK the graph is displayed:
$fr = $mw->Frame(-relief => 'groove',
-borderwidth => 5)->pack(
-side => 'left',
-expand => 1
);
# the canvas has scroll bars and we sized the viewable part at 625x800
$canvas = $fr->Scrolled('Canvas', -height => 625,
-width =>800,)->pack(
-side => 'right',
-expand => 1
);
# here we define the cpu graph and where we find it
$dbcpu = $canvas->Photo(-format => 'png',
-file => '/home/mjacobs/scripts/perl/dbcpu.png',
-palette => '16/16/16');
# and this actually puts the image on the canvas
$dbcpu_gr = $canvas->createImage(300,250,
-image => $dbcpu,
-tags => "dbcpu",
-anchor => "center");
|