Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Building graph produces error "Cannot open gdgraph1.png:Permission denied" 1

Status
Not open for further replies.

Tester_V

Technical User
Nov 22, 2019
54
US
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 :)
Capture_dtf5lw.png

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
 
Hi

As far as I see, the only problem is the passing of data arrays. Those are barewords, need references :
Code:
[gray]# plot the data on the graph[/gray]
[b]my[/b] [navy]$gd[/navy] [teal]=[/teal] [navy]$graph[/navy][teal]->[/teal][COLOR=orange]plot[/color][teal]([/teal]
    [teal][[/teal]
        [highlight][teal]\[/teal][navy]@[/navy][/highlight][navy]xsizes[/navy][teal],[/teal]
        [highlight][teal]\[/teal][navy]@[/navy][/highlight][navy]ycounts[/navy]
    [teal]][/teal]
[teal]);[/teal]

Regarding the error message you quoted, sounds like the Perl script has no permission to write in the current directory. Try writing elsewhere.

Tester_V said:
I still cannot find the button for posting codes :)
code-button_tvagn8.png


Feherke.
feherke.github.io
 
Thank you for the replay Feherke!

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

 
Hi

I mean that I only added those [highlight]highlighted[/highlight] characters to your code in the [tt]$graph->plot()[/tt] call and got this gdgraph1.png file :
gdgraph1_afjudx.png



Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top