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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl Graph

Status
Not open for further replies.

JackTheRussel

Programmer
Aug 22, 2006
110
FI
Hi.

I would like to make some charts and graphs in perl.

I have no clue where to start. I noticed this example in internet, but when I run this program nothing happens.

How would I get out some png or jpeg picture by using this example ?

Code:
#!/usr/bin/perl

use GD::Graph::bars;

@data = ( 
    ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"],
    [   11,   12,   15,   16,    3,  1.5,    1,     3,     4],
    [    5,   12,   24,   15,   19,    8,    6,    15,    21],
    [    12,   3,    1,   5,    12,    9,   16,    25,    11],
);

$my_graph = new GD::Graph::bars( );
$my_graph->set( 
        x_label => 'X Label',
        y_label => 'Y label',
        title => 'Stacked Bars (incremental)',
        y_max_value => 50,
        y_tick_number => 10,
        y_label_skip => 2,
        cumulate => 1,
);

$my_graph->set_legend( qw(offset increment more));
my $gd = $my_graph->plot(\@data);

From here I noticed this example.

 
You can do it like this:
Code:
  open(IMG, '>file.png') or die $!;
  binmode IMG;
  print IMG $gd->png;
 
BTW. Does anyone know somekind tutorial pages where are information about Perl-graphs.

Example now I would need information how I can read data from file. I don't want to read data from @table. (example which is above).


And Can I somehow make reasonable pictures where I have time and value data like this:

(notice that time-perioids are really tiny.)

Code:
       [time]      [value]
2007-02-09 13:06:04 1.487
2007-02-09 13:06:05 0.198
2007-02-09 13:06:05 0.167
2007-02-09 13:06:09 8.307
2007-02-09 13:06:09 3.631
2007-02-09 13:11:28 1.491
2007-02-09 13:11:40 0.178
2007-02-09 13:11:40 0.403
2007-02-09 13:11:40 2.513
2007-02-09 13:11:40 1.401
2007-02-09 13:13:32 1.5
2007-02-09 13:13:44 0.18
2007-02-09 13:13:44 0.269
2007-02-09 13:13:48 2.63
2007-02-09 13:13:48 1.829
2007-02-09 13:31:26 1.48
2007-02-09 13:31:38 0.181
2007-02-09 13:31:38 0.27
2007-02-09 13:31:42 2.03
2007-02-09 13:31:42 1.354
2007-02-09 13:33:10 1.494
2007-02-09 13:33:23 0.203
2007-02-09 13:33:23 0.166
2007-02-09 13:33:26 2.541
2007-02-09 13:33:26 1.851
2007-02-09 13:35:56 1.494
2007-02-09 13:36:09 0.176
2007-02-09 13:36:09 0.271

 
Code:
open (DATA, "table.txt");
my @data = <DATA>;
close (DATA);
chomp @data;

my $arrayref = [
   [ "Date", "Time", "Value" ],
];

foreach my $line (@data) {
   my ($date,$time,$value) = split(/\s+/, $line, 3);
   push (@{$arrayref}, [
      $date, $time, $value
   ]);
}

# $arrayref should look like this afterward...
$arrayref = [
   [ "Date",       "Time",     "Value" ],
   [ "2007-02-09", "13:06:04", "1.487" ],
   [ "2007-02-09", "13:06:05", "0.198" ],
   [ "2007-02-09", "13:06:05", "0.167" ],
   #etc......
];

You wouldn't find a tutorial about reading from a file and manipulating data structures when looking for a tutorial on graphing; you'd want to look up tutorials on reading from a file and manipulating data structures. Graphing comes after the author assumes you know how to get the information you want to graph to begin with. ;-)

-------------
Cuvou.com | The NEW Kirsle.net
 
Kirsie thanks..

Hate to ask stupid questions, but I'll have to.

Do I have to use @data or $arrayref when I plot my picture ?

I get error when I try to plot my picture
Code:
my $gd = $my_graph->plot(\@data);

---> Can't call method "png" on an undefined value

Or...
Code:
my $gd = $my_graph->plot(\$arrayref);

---> Can't call method "png" on an undefined value
 
Code:
my $gd = $my_graph->plot($arrayref);

Try removing the backslash, since $arrayref is already a reference to an array (using \@data would've made a reference to @data)

I didn't look up how GD::Graph works, but the code I posted turns your data into a structure similar to the example you posted above

Code:
@data = (
    ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"],
    [   11,   12,   15,   16,    3,  1.5,    1,     3,     4],
    [    5,   12,   24,   15,   19,    8,    6,    15,    21],
    [    12,   3,    1,   5,    12,    9,   16,    25,    11],
);

-------------
Cuvou.com | The NEW Kirsle.net
 
Thank for you help Kirsie.

Now it works !


TorontoJim:

Thanks for advice, but I don't have use for that program.
(browser use).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top