Building graph produces error "Cannot open gdgraph1.png:Permission denied"
Building graph produces error "Cannot open gdgraph1.png:Permission denied"
(OP)
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.png:Permission 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:
#!/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.png:Permission 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:
#!/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
RE: Building graph produces error "Cannot open gdgraph1.png:Permission denied"
As far as I see, the only problem is the passing of data arrays. Those are barewords, need references :
CODE --> Perl ( fragment )
Regarding the error message you quoted, sounds like the Perl script has no permission to write in the current directory. Try writing elsewhere.
Feherke.
feherke.github.io
RE: Building graph produces error "Cannot open gdgraph1.png:Permission denied"
Do you or anyone knows an example of a working building graphs code, the whole code?
Not just snippets?
It is hard to understand without seen the whole script how it all works.
I do not know what that means exactly, "passing of data arrays. Those are barewords, need references"
Does it mean my data array is my data (see below)
And it needs to be passed as a reference array?
CElls, Usage
1,4 Days 15:16:38
10,2 Days 09:52:05
11,2 Days 12:22:11
12,0 Days 04:39:51
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
RE: Building graph produces error "Cannot open gdgraph1.png:Permission denied"
I mean that I only added those highlighted characters to your code in the $graph->plot() call and got this gdgraph1.png file :
Feherke.
feherke.github.io
RE: Building graph produces error "Cannot open gdgraph1.png:Permission denied"
It is interesting....