tags:

views:

135

answers:

4

I am writting a Perl script to create pie graph using GD::Graph::pie with these arrays:

@Array1 = ("A", "B", "C", "D");
$array2 = [
      ['upto 100 values'],
      ['upto 100 values'],
      ['upto 100 values'],
      ['upto 100 values']
    ];

As per my understanding to get this done, I have to create an array with the references of above arrays, like:

 my @graph_data = (\@Array1, @$array2);

I have also tried to use foreach loop but not getting good results. I want to create pie graph with first value in @Array1 against first value in $array2 and second value in @Array1 against second value in $array2 and so on. Also I want put the same title for each graph as per values in @Array1.

eg.
my @graph_data1 = (\@Array1[0], @$array2[0]);

Can anyone please suggest me the better way to do this?

+3  A: 

Before getting into pie charts and stuff like that, I suggest you get yourself updated on basic Perl data structures and references. Please read perlreftut, youl should be able to solve this problem yourself afterwards.

innaM
+2  A: 

I'm not sure I understand what you are trying to do, but this example will produce 3 pie charts, all of them using the same set of categories. I would second Manni's advice: spend some time with perlreftut and perldsc. Also, if you download the GD::Graph module, it provides many examples, including pie charts (see the samples subdirectory).

use strict;
use warnings;
use GD::Graph::pie;

my @categories = qw(foo bar fubb buzz);
my @data = ( 
    [   25,    32,    10,     44 ],  # Data values for chart #1
    [  123,   221,   110,    142 ],  # Data values for chart #2
    [  225,   252,   217,    264 ],  # etc.
);

for my $i (0 .. $#data){
    my $chart = GD::Graph::pie->new;
    my @pie_data = ( \@categories, $data[$i] );
    $chart->plot(\@pie_data);

    open(my $fh, '>', "pie_chart_$i.gif") or die $!;
    binmode $fh;
    print $fh $chart->gd->gif;
    close $fh;
}
FM
+1  A: 

To state in plainer English what the other answers say less directly:

my @graph_data = (\Array1, $@array2);
my @graph_data1 = (\Array1[0], $@array2[0]);

looks mad. You almost certainly mean:

my @graph_data = (\@Array1, $array2);
# you want the first element of each list in the same datastructure?
my @graph_data1 = ([$Array1[0]], [$array2->[0]]); # (['A'], [[..numbers..]])
                                                  # Note *two* [ and ] in 2nd bit
# ... or you want a different datastructure?
my @graph_data1 = ($Array1[0], $array2->[0]); # ('A', [..numbers..])

@Array1 is an array, you want a reference to it, and that would be \@Array1.

$array2 is a reference to an array already. It contains references to arrays, and I assume you want a list containing the reference to the array at index 0. Thus: $array2->[0] is the first indexed element via an array reference, and it's already an array reference.

ijw
A: 

Hi There,

I found the solution of this problem using below code.

my @pairs = map{"$Array1[$_]@$array2[$_],"} 0..$#Array1;

After this the values from array @pairs can be used to create graphs.

Space
I don't see how this solves your problem if you are trying to pass this to GD::Graph's plot() method.
brian d foy