Monday, July 03, 2006

Membuat grafik dng perl dilingkungan Linux


Scriptnya sederhana sekali, kita bisa lihat di helpnya dan bisa cepat diaplikasikan. Sebelum menjalankan atau membuat script ini pastikan GD:Graph lib sudah terinstall di komputer, cara ngistalnya bisa tanya suhu google :)


#!/usr/bin/perl -w

use strict;
use CGI;
use GD::Graph::bars;

my $q = new CGI;
my $graph = new GD::Graph::bars( 1200, 700 );

my @data = read_data("direktory tempat data berupa text file");

$graph->set(

title => "Disk Usage Bar chart (%) ",
x_label => "disk name",
y_label => "disk %",
long_ticks => 2,
y_min_value => 0,
x_label_position => 0.5,
x_labels_vertical => 1,
x_tick_offset => 3,
bar_spacing => 1,
line_width => 1,
show_values => 1,
dclrs => [qw(lgreen lred)],

);

my $gd_image = $graph->plot( \@data );
print $q->header( -type => "image/png", -expires => "-1d" );

binmode STDOUT;
print $gd_image->png;

sub read_data

{

my $fn = shift;
my @d = ();
open(DATA, $fn) || return ();
while ()

{

chomp;

my @row = split / /;
for (my $i = 0; $i <= $#row; $i++)

{

undef $row[$i] if ($row[$i] eq 'undef');
push @{$d[$i]}, $row[$i];

}

}

close (DATA);
return @d;

}

Kalo diexecute hasilnya kurang lebih begini:




0 Comments:

Post a Comment

<< Home