package Imager::Graph; require 5.005; =head1 NAME Imager::Graph - Perl extension for producing Graphs using the Imager library. =head1 SYNOPSIS use Imager::Graph::Sub_class; my $chart = Imager::Graph::Sub_class->new; my $img = $chart->draw(data=> \@data, ...) or die $chart->error; =head1 DESCRIPTION Imager::Graph provides style information to its base classes. It defines the colors, text display information and fills based on both built-in styles and modifications supplied by the user to the draw() method. =over =cut use strict; use vars qw($VERSION); use Imager qw(:handy); use Imager::Fountain; $VERSION = '0.07'; # the maximum recursion depth in determining a color, fill or number use constant MAX_DEPTH => 10; my $NUM_RE = '(?:[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]\d+?)?)'; =item new This is a simple constructor. No parameters required. =cut sub new { bless {}, $_[0]; } =item set_graph_size($size) Sets the size of the graph (in pixels) within the image. The size of the image defaults to 1.5 * $graph_size. =cut sub set_graph_size { $_[0]->{'custom_style'}->{'size'} = $_[1]; } =item set_image_width($width) Sets the width of the image in pixels. =cut sub set_image_width { $_[0]->{'custom_style'}->{'width'} = $_[1]; } =item set_image_height($height) Sets the height of the image in pixels. =cut sub set_image_height { $_[0]->{'custom_style'}->{'height'} = $_[1]; } =item add_data_series([8, 6, 7, 5, 3, 0, 9], 'Series Name'); Adds a data series to the graph. For L, only one data series can be added. =cut sub add_data_series { my $self = shift; my $data_ref = shift; my $series_name = shift; my $graph_data = $self->{'graph_data'} || []; push @$graph_data, { data => $data_ref, series_name => $series_name }; $self->{'graph_data'} = $graph_data; return; } sub _get_data_series { my ($self, $opts) = @_; # return the data supplied to draw() if any. if ($opts->{data}) { # one or multiple series? my $data = $opts->{data}; if (@$data && ref $data->[0] && ref $data->[0] =~ /ARRAY/) { return $data; } else { return [ { data => $data } ]; } } return $self->{'graph_data'}; } =item set_labels(['label1', 'label2' ... ]) Labels the specific data points. For line/bar graphs, this is the x-axis. For pie graphs, it is the label for the wedges. =cut sub set_labels { $_[0]->{'labels'} = $_[1]; } sub _get_labels { my ($self, $opts) = @_; $opts->{labels} and return $opts->{labels}; return $_[0]->{'labels'} } =item set_title($title) Sets the title of the graph. Requires setting a font. =cut sub set_title { $_[0]->{'custom_style'}->{'title'}->{'text'} = $_[1]; } =item set_font($font) Sets the font to use for text. Takes an L object. =cut sub set_font { $_[0]->{'custom_style'}->{'font'} = $_[1]; } =item set_style($style_name) Sets the style to be used for the graph. Imager::Graph comes with several pre-defined styles: fount_lin (default), fount_rad, mono, primary_red, and primary. =cut sub set_style { $_[0]->{'style'} = $_[1]; } sub _get_style { my ($self, $opts) = @_; $opts->{style} and return $opts->{style}; return $self->{'style'}; } =item error Returns an error message. Only valid if the draw() method returns false. =cut sub error { $_[0]->{_errstr}; } =item draw Creates a new image, draws the chart onto that image and returns it. Optionally, instead of using the api methods to configure your chart, you can supply a C parameter in the format required by that particular graph, and if your graph will use any text, a C parameter You can also supply many different parameters which control the way the graph looks. These are supplied as keyword, value pairs, where the value can be a hashref containing sub values. The C