Hi,
I’d like to build a graph using Perl, I know nothing about using GD module but I thought Google will help-I was wrong.
I Googled bunch of examples but it seems they all producing same error “Cannot open gdgraph1.pngermission denied”.
I could use Excel but want to use Perl.My data table is very simple:
Cell Usage
1 4 Days 15:16:38
2 4 Days 13:40:01
3 4 Days 17:20:56
4 4 Days 20:01:30
5 4 Days 05:32:38
6 3 Days 23:06:11
7 1 Days 22:52:54
8 2 Days 11:45:33
9 2 Days 10:57:24
10 2 Days 09:52:05
11 2 Days 12:22:11
12 0 Days 04:39:51
I still cannot find the button for posting codes
Here is a script I Googled that spits the same error:
[sup][/sup]
#!/usr/local/bin/perl
use strict ;
use warnings ;
use diagnostics ;
use Carp;
use GD::Graph::bars;
my %dataset = ( 1 => 3,
2 => 17,
3 => 34,
4 => 23,
5 => 25,
6 => 20,
7 => 12,
8 => 3,
9 => 1
);
# create new image
my $graph = new GD::Graph::bars(600, 300);
# discover maximum values of x and y for graph parameters
my( $xmax) = sort {$b <=> $a} keys %dataset;
my( $ymax) = sort {$b <=> $a} values %dataset;
# how many ticks to put on y axis
my $yticks = int($ymax / 5) + 1;
# define input arrays and enter 0 if undefined x value
my(@xsizes) = (0 .. $xmax);
my(@ycounts) = ();
foreach my $x (@xsizes) {
if ( defined $dataset{$x}) {
push @ycounts, $dataset{$x};
}else{
push @ycounts, 0;
}
}
# set parameters for graph
$graph->set(
transparent => 0,
title => "Summary of mutation data",
x_label => 'Mutants per cluster',
y_label => 'Number of mutants',
x_all_ticks => 1,
y_all_ticks => 0,
y_tick_number => $yticks,
zero_axis => 0,
zero_axis_only => 0,
);
# plot the data on the graph
my $gd = $graph->plot(
[ xsizes,
ycounts
]
);
# output file
my $pngfile = "gdgraph1.png";
unless(open(PNG, ">$pngfile")) {
croak "Cannot open $pngfile:$!\n";
}
# set output file handle PNG to binary stream
# (this is important sometimes, for example doing
# GCI programming on some operating systems
binmode PNG;
# print the image to the output file
print PNG $gd->png;
Thank you for you help!
Tester_V
I’d like to build a graph using Perl, I know nothing about using GD module but I thought Google will help-I was wrong.
I Googled bunch of examples but it seems they all producing same error “Cannot open gdgraph1.pngermission denied”.
I could use Excel but want to use Perl.My data table is very simple:
Cell Usage
1 4 Days 15:16:38
2 4 Days 13:40:01
3 4 Days 17:20:56
4 4 Days 20:01:30
5 4 Days 05:32:38
6 3 Days 23:06:11
7 1 Days 22:52:54
8 2 Days 11:45:33
9 2 Days 10:57:24
10 2 Days 09:52:05
11 2 Days 12:22:11
12 0 Days 04:39:51
I still cannot find the button for posting codes
Here is a script I Googled that spits the same error:
[sup][/sup]
#!/usr/local/bin/perl
use strict ;
use warnings ;
use diagnostics ;
use Carp;
use GD::Graph::bars;
my %dataset = ( 1 => 3,
2 => 17,
3 => 34,
4 => 23,
5 => 25,
6 => 20,
7 => 12,
8 => 3,
9 => 1
);
# create new image
my $graph = new GD::Graph::bars(600, 300);
# discover maximum values of x and y for graph parameters
my( $xmax) = sort {$b <=> $a} keys %dataset;
my( $ymax) = sort {$b <=> $a} values %dataset;
# how many ticks to put on y axis
my $yticks = int($ymax / 5) + 1;
# define input arrays and enter 0 if undefined x value
my(@xsizes) = (0 .. $xmax);
my(@ycounts) = ();
foreach my $x (@xsizes) {
if ( defined $dataset{$x}) {
push @ycounts, $dataset{$x};
}else{
push @ycounts, 0;
}
}
# set parameters for graph
$graph->set(
transparent => 0,
title => "Summary of mutation data",
x_label => 'Mutants per cluster',
y_label => 'Number of mutants',
x_all_ticks => 1,
y_all_ticks => 0,
y_tick_number => $yticks,
zero_axis => 0,
zero_axis_only => 0,
);
# plot the data on the graph
my $gd = $graph->plot(
[ xsizes,
ycounts
]
);
# output file
my $pngfile = "gdgraph1.png";
unless(open(PNG, ">$pngfile")) {
croak "Cannot open $pngfile:$!\n";
}
# set output file handle PNG to binary stream
# (this is important sometimes, for example doing
# GCI programming on some operating systems
binmode PNG;
# print the image to the output file
print PNG $gd->png;
Thank you for you help!
Tester_V