Chart-Plot
view release on metacpan or search on metacpan
#=====================================================================#
# Chart::Plot -- Front end to GD.pm for plotting two dimensional data #
# by Sanford Morton <smorton@pobox.com> #
#=====================================================================#
# Changes:
# v 0.0 - 08 March 1998
# first version
# v 0.01 - 09 March 1998;
# - _getOM reports >= max instead of >;
# - fixed bug in setData() data check
# v 0.02 - 10 March 1998;
# - changed error handling in setData() (public method) which
# now returns undef on success and sets $self->error ;
# - changed legend to title (public method)
# - adjusted horizontal tick labels up a bit
# v 0.03 - 15 March 1998
# - added colors and dashed line options to dataset graph style
# - added option to pass dataset as two arrays (@xdata, @ydata)
# - added hack for case om == max
# v 0.04 - 15 March 1998
# - added general purpose setGraphOptions()
# v 0.05 - 18 March 1998
# _ added synopsis to pod
# - added getBounds()
# - Hor axis label is set below and right centered or justified.
# - additional vertical offset if title is present; larger font
# v 0.06 - 22 March 1998
# - removed title, offset and axis label methods in favor of
# setGraphOptions()
# - added getBounds()
# v 0.07 - 29 May 1998
# - finally put into standard h2xs form
# - added check for tick step too small
# - changed data validity check to permit scientific notation
# but this invites awful looking tick labels
# v 0.08 - 15 Dec 1998
# - added access to GD object: getGDobject() and data2pxl()
# v 0.09 - 26 July 1999
# - added custom tick labels: xTickLabels, yTickLabels
# - added binmode() to install test and demo script
# v 0.10 - 22 May 2000
# - added @_image_types and image_type() to use gif, jpeg or png
# according to local version of GD; modified draw() and _init()
# v 0.11 - 04 April 2001
# - fixed bug in draw() to enable jpeg's
package Chart::Plot;
$Chart::Plot::VERSION = '0.11';
use GD;
use strict;
#==================#
# class variables #
#==================#
# list of image types supported by GD, currently jpeg, png or gif,
# depending on GD version; initialized in _init()
my @_image_types = ();
#==================#
# public methods #
#==================#
# usage: $plot = new Chart::Plot(); # default 400 by 300 pixels or
# $plot = new Chart::Plot(640, 480);
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
my $GDobject = $img->getGDobject();
my ($GDobject, $black, $white, $red, $green, $blue)
= $img->getGDObject();
In scalar context, this method returns only the reference to the GD
object. It can also return a list containing the reference to the
image object and the colors already created by Chart::Plot for that GD
onject, in the order specified above. If you do not obtain these
colors, you will need to allocate your own colors before drawing,
example below.
When you call the C<draw()> method of Chart::Plot (typically the last
step in your script) any drawing you have done in your script with GD
on the GD object will also be drawn.
Since Chart::Plot works with data values and GD works with pixel
values, you will need the C<data2pxl()> method of Chart::Plot to
translate (x,y) pairs of data values to (px,py) pairs of pixel
values. (You call this method on the Chart::Plot object, not the GD
object.) You must call this method only after all datasets have been
registered with the setData() method, since the graph scaling and this
translation may change with each new dataset.
Here is a brief example which draws small blue circles around each
data point in the chart.
use Chart::Plot;
my $img = Chart::Plot->new;
my @data = qw( 10 11 11 12 12 13 13 14 14 15);
$img->setData (\@data);
# draw circles around each data point, diameter 15 pixels
my $gd = $img->getGDobject;
my $blue = $gd->colorAllocate(0,0,255); # or use $img's blue
my ($px,$py);
for (my $i=0; $i<$#data; $i+=2) {
($px,$py) = $img->data2pxl ($data[$i], $data[$i+1]);
$gd->arc($px,$py,15,15,0,360,$blue);
}
# draw the rest of the chart, and print it
open (OUT,">plot.gif");
binmode OUT;
print OUT $img->draw();
close OUT;
=head1 BUGS AND TO DO
If your data is bunched tightly but far away from the origin, then you
will obtain a better chart if the graph is clipped away from the
origin. I have not yet found a useful way to do this, but I am still
thinking. You may be able to use Custom Tick Labels to improve your
chart in the meantime.
You will probably be unhappy with axis tick labels running together if
you use scientific notation. Controlling tick label formatting and
length for scientific notation seems doable but challenging.
Future versions might incorporate a legend, control of font size, word
wrap and dynamic adjustment of axis labels and title. Better code, a
better pod page.
=head1 AUTHOR
Copyright (c) 1998-2000 by Sanford Morton <smorton@pobox.com> All
rights reserved. This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
This work is dedicated to the memory of Dr. Andrew Morton, who requested it.
I<Requiescat in pace>, my friend.
=head1 SEE ALSO
GD::Graph(1) (formerly GIFgraph(1)) and Chart(1) are other front end
modules to GD(1). All can be found on CPAN.
=cut
( run in 1.410 second using v1.01-cache-2.11-cpan-e1769b4cff6 )