GDGraph

 view release on metacpan or  search on metacpan

Graph/Data.pm  view on Meta::CPAN

GD::Graph and the module itself, and will most likely not be useful to
you. Many won't even I<seem> useful to you...

=head1 EXAMPLES

  use GD::Graph::Data;
  use GD::Graph::bars;

  my $data = GD::Graph::Data->new();

  $data->read(file => '/data/sales.dat', delimiter => ',');
  $data = $data->copy(wanted => [2, 4, 5]);

  # Add the newer figures from the database
  use DBI;
  # do DBI things, like connecting to the database, statement
  # preparation and execution

  while (@row = $sth->fetchrow_array)
  {
      $data->add_point(@row);
  }

  my $chart = GD::Graph::bars->new();
  my $gd = $chart->plot($data);

or for quick changes to legacy code

  # Legacy code builds array like this
  @data = ( [qw(Jan Feb Mar)], [1, 2, 3], [5, 4, 3], [6, 3, 7] );

  # And we quickly need to do some manipulations on that
  my $data = GD::Graph::Data->new();
  $data->copy_from(\@data);

  # And now do all the new stuff that's wanted.
  while (@foo = bar_baz())
  {
      $data->add_point(@foo);
  }

=head1 METHODS

=head2 $data = GD::Graph::Data->new()

Create a new GD::Graph::Data object.

=cut

# Error constants
use constant ERR_ILL_DATASET    => 'Illegal dataset number';
use constant ERR_ILL_POINT      => 'Illegal point number';
use constant ERR_NO_DATASET     => 'No data sets set';
use constant ERR_ARGS_NO_HASH   => 'Arguments must be given as a hash list';

sub new
{
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self = [];
    bless $self => $class;
    $self->copy_from(@_) or return $self->_move_errors if (@_);
    return $self;
}

sub DESTROY
{
    my $self = shift;
    $self->clear_errors();
}

sub _set_value
{
    my $self = shift;
    my ($nd, $np, $val) = @_;

    # Make sure we have empty arrays in between
    if ($nd > $self->num_sets)
    {
        # XXX maybe do this with splice
        for ($self->num_sets .. $nd - 1)
        {
            push @{$self}, [];
        }
    }
    $self->[$nd][$np] = $val;

    return $self;
}

=head2 $data->set_x($np, $value);

Set the X value of point I<$np> to I<$value>. Points are numbered
starting with 0. You probably will never need this. Returns undef on
failure.

=cut

sub set_x
{
    my $self = shift;
    $self->_set_value(0, @_);
}

=head2 $data->get_x($np)

Get the X value of point I<$np>. See L<"set_x">.

=cut

sub get_x
{
    my $self = shift;
    my $np   = shift;
    return $self->_set_error(ERR_ILL_POINT)
        unless defined $np && $np >= 0;

    $self->[0][$np];
}

=head2 $data->set_y($nd, $np, $value);

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.827 second using v1.00-cache-2.02-grep-82fe00e-cpan-9e6bc14194b )