SPUG: Graphing with Perl

Christopher A. Nielsen chris at zorinco.com
Fri Jun 7 13:21:06 CDT 2002


I've used GD::Image with great success. It creates .png graphic files.
Here's some examples of it's use. I've created a web-hit statistics
grapher with it.

$im = new GD::Image($maxpngwidth,$maxpngheight);

# allocate some colors
$white = $im->colorAllocate(255,255,255);
$black = $im->colorAllocate(0,0,0);
$red = $im->colorAllocate(255,0,0);

# make the background transparent and interlaced
$im->transparent($white);
$im->interlaced('true');

# Put a black frame around the picture
$im->rectangle(0,0,$maxpngwidth-1,$maxpngheight-1,$black);

# write X legends
$inc=1; $charofs=12;
for ($i=1;$i<$graphunitsx; $i+=$inc) {
        $xpos=$graphoffsetx + ($i * $unitwidth);

        # print "***                    xpos=$xpos\n";

	$im->string(gdSmallFont,$xpos-$charofs,$maxpngheight-20,"$i",$black);
        if ($i>9) { $inc=2; $charofs=15; }

}
# heading
$im->string(gdSmallFont,$graphoffsetx,5,$heading,$black);

# then write out the .png file
open(PNG,">$png") ;

# make sure we are writing to a binary stream
binmode PNG;

# Convert the image to PNG and print it on standard output
print PNG $im->png;
close (PNG);


Cheers,
Chris




On Fri, 7 Jun 2002, Peter Darley wrote:

> Friends,
> 	I'm looking for a good graphing module in perl.  We're doing business,
> rather than scientific graphs, so we're graphing arbitrary data rather than
> functions, and would like to have a lot of control over the look of the
> produced graphs.  I've looked at GD:Graph, which doesn't seem to have the
> flexibility I want, and Imager::Graph, which looks great, but only has pie
> charts.
> 	Can anyone point me toward something?  If I can't find anything I'm
> planning on extending the Imager::Graph module.
> Thanks,
> Peter D.
>

-- 
Christopher Nielsen    chris at ZORINco.com    http://ZORINco.com
______________________________________________________________
 Makers of fine microcontroller products -

     C  O  N  T  R  O  L    Y  O  U  R    W  O  R  L  D



 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list