Click to See Complete Forum and Search --> : Draw a bar graph with GD::Graph::bars


oren.gozlan
04-19-2005, 10:34 AM
Hi .. i was looking for a way to draw charts and bars with perl and came accross: http://www.wdvl.com/Authoring/Languages/Perl/Weave/chart1-1.html

i'm trying to use this example:
1 use strict;
2 use GD::Graph::bars;
3
4 my @data = (
5 ["Jan-01","Feb-01","Mar-01", "Apr-01","May-01",
"Jun-01","Jul-01","Aug-01","Sep-01"],
6 [21,25,33,39,49,48,40,45,15]
7 );
8
9 my $graph = new GD::Graph::bars;
10
11 $graph->set(
12 x_label => 'Month',
13 y_label => 'Revenue ($1,000s)',
14 title => 'Monthly Online Sales for 2001',
15 bar_spacing => 10
16 )
17 or warn $graph->error;
18
19 $graph->plot(\@data) or die $graph->error;
20
21 open(GRAPH,">graph1.jpg") || die "Cannot open graph1.jpg: $!\n";
22 print GRAPH $graph->gd->jpeg(100);

but it seems that the jpeg output is illigal or incoerect ... do you guys have any idea ?

Oren

CyCo
04-20-2005, 11:41 AM
Well, if you just want to create and "view" the image on the fly, this works just fine.
**Note the header**


#!/usr/bin/perl

use CGI;
use strict;
use GD::Graph::bars;

my @data = (
["Jan-01","Feb-01","Mar-01", "Apr-01","May-01",
"Jun-01","Jul-01","Aug-01","Sep-01"],
[21,25,33,39,49,48,40,45,15]
);

my $q = CGI->new;
my $graph = new GD::Graph::bars;

$graph->set(
x_label => 'Month',
y_label => 'Revenue ($1,000s)',
title => 'Monthly Online Sales for 2001',
bar_spacing => 10
) or warn $graph->error;

$graph->plot(\@data) or die $graph->error;

print $q->header(-type=>'image/jpeg'),$graph->gd->jpeg(100);



If you want to save the newly-created image, just print the image to a file.


open(GRAPH,">graph1.jpg") || die "Cannot open graph1.jpg: $!\n";
print GRAPH $graph->gd->jpeg(100);
close(GRAPH);


You can see that I created it on the fly, here (http://cybernetics2000.aboho.com/cgi-bin/graph.pl)