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

Hi monks,

i'm trying to draw a graph using GD::Graph and more or less i got it using piece of code below.

As you can see data of my array are hardcoded in the script, while i would like to read data from external input file and then draw my graph.

Many thanks in advance to anyone that will help me.

Hers's my script:

#!/usr/bin/perl use strict; use GD::Graph::bars3d; use Date::Calc qw(:all); use GD::Graph::Data; my ($d,$m,$y) = (localtime)[3,4,5]; my $mdy = sprintf '%02d-%02d-%02d', $d, $m+1, $y+1900; my $title="IPTV - Video Server Bandwidth Report del "."$mdy"; my @data = ( ["00:00","01:00","02:00","03:00","04:00","05:00","06:00","07:00","08 +:00","09:00","10:00","11:00","12:00","13:00","14:00","15:00","16:00", +"17:00","18:00","19:00","20:00","21:00","22:00","23:00"], [2585.54, 2095.44, 1748.02, 1454.55, 1290.86, 1235.33, 1151.04, 1160 +.86, 1191.82, 1343, 1514.33, 1693.67, 1773.7, 1897.82, 2733.04, 3257. +12, 3375.69, 2949.53, 2573.52, 2139.65, 1697.83, 1989.94, 2746.92, 29 +98.7], ); my $graph = new GD::Graph::bars3d( 1000, 500 ); $graph->set( x_label => 'Intervallo Orario', y_label => 'Traffico in Mbit/s', title => $title, x_labels_vertical => 1, x_label_position => 1/2, y_tick_number => 10, y_label_skip => 1, x_label_skip => 1, x_all_ticks => 1, x_labels_vertical => 1, box_axis => 0, y_long_ticks => 1, dclrs => [ qw(green) ], borderclrs => [ qw(black) ], #white,lgray,gray,dgray,black,lblue,blue,dblue,gold,lyellow,yellow,dye +llow,lgreen,green,dgreen,lred,red,dred,lpurple,purple,dpurple,lorange +,orange,pink,dpink,marine,cyan,lbrown,dbrown show_values => 1, values_vertical => 1, values_space => 15, bar_spacing => 10, ); my $gd = $graph->set_values_font(GD::gdGiantFont); my $gd = $graph->set_title_font(GD::gdGiantFont); my $gd = $graph->set_x_label_font(GD::gdGiantFont); my $gd = $graph->set_y_label_font(GD::gdGiantFont); my $gd = $graph->set_x_axis_font(GD::gdGiantFont); my $gd = $graph->set_y_axis_font(GD::gdGiantFont); my $gd = $graph->plot( \@data ); open(IMG, '>file.jpeg') or die $!; binmode IMG; print IMG $gd->jpeg; close IMG;

Replies are listed 'Best First'.
Re: GD::Graph input file
by toolic (Bishop) on Jul 18, 2011 at 19:01 UTC
    i would like to read data from external input file
    Read Files and I/O and Generation of an ARRAY OF ARRAYS and try to write some code to read in your file. If you still have problems, post your new code plus the contents of your input file.

      Thanks Toolic, let me try some new code based on your input,

Re: GD::Graph input file
by BrowserUk (Patriarch) on Jul 18, 2011 at 19:22 UTC

    Assuming your data files hold the values in the more natural 'lines of pairs' arrangement, something like this might work for you:

    #!/usr/bin/perl use strict; use GD::Graph::bars3d; use Date::Calc qw(:all); use GD::Graph::Data; my ($d,$m,$y) = (localtime)[3,4,5]; my $mdy = sprintf '%02d-%02d-%02d', $d, $m+1, $y+1900; my $title="IPTV - Video Server Bandwidth Report del "."$mdy"; my @data; while( <DATA> ) { my @pair = split; push @{ $data[0] }, $pair[0]; push @{ $data[1] }, $pair[1]; } my $graph = new GD::Graph::bars3d( 1000, 500 ); $graph->set( x_label => 'Intervallo Orario', y_label => 'Traffico in Mbit/s', title => $title, x_labels_vertical => 1, x_label_position => 1/2, y_tick_number => 10, y_label_skip => 1, x_label_skip => 1, x_all_ticks => 1, x_labels_vertical => 1, box_axis => 0, y_long_ticks => 1, dclrs => [ qw(green) ], borderclrs => [ qw(black) ], #white,lgray,gray,dgray,black,lblue,blue,dblue,gold,lyellow,yellow,dye +llow,lgreen,green,dgreen,lred,red,dred,lpurple,purple,dpurple,lorange +,orange,pink,dpink,marine,cyan,lbrown,dbrown show_values => 1, values_vertical => 1, values_space => 15, bar_spacing => 10, ); my $gd = $graph->set_values_font(GD::gdGiantFont); my $gd = $graph->set_title_font(GD::gdGiantFont); my $gd = $graph->set_x_label_font(GD::gdGiantFont); my $gd = $graph->set_y_label_font(GD::gdGiantFont); my $gd = $graph->set_x_axis_font(GD::gdGiantFont); my $gd = $graph->set_y_axis_font(GD::gdGiantFont); my $gd = $graph->plot( \@data ); open(IMG, '>file.jpeg') or die $!; binmode IMG; print IMG $gd->jpeg; close IMG; __DATA__ 00:00 2585.54 01:00 Z095.44 02:00 1748.02 03:00 1454.55 04:00 1290.86 05:00 1235.33 06:00 1151.04 07:00 1160.86 08:00 1191.82 09:00 1343 10:00 1514.33 11:00 1693.67 12:00 1773.7 13:00 1897.82 14:00 2733.04 15:00 3257.12 16:00 3375.69 17:00 2949.53 18:00 2573.52 19:00 2139.65 20:00 1697.83 21:00 1989.94 22:00 2746.92 23:00 2998.7

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Hi Browser, your example work perfectly.

      Many thanks