NAME

PDF::Reuse::SimpleChart - Produce simple charts with PDF::Reuse

SYNOPSIS

 use PDF::Reuse::SimpleChart;
 use PDF::Reuse;
 use strict;

 prFile('myFile.pdf');
 my $s = PDF::Reuse::SimpleChart->new();

 $s->columns(qw(Month  January February  Mars  April  May June July));
 $s->add(      'Riga',     314,     490,  322,  -965, 736, 120, 239);
 $s->add(      'Helsinki', 389,    -865, -242,     7, 689, 294, 518);
 $s->add(      'Stockholm',456,    -712,  542,   367, 742, 189, 190);
 $s->add(      'Oslo',     622,     533,  674,  1289, 679, -56, 345);

 $s->draw(x     => 10,
          y     => 200,
          yUnit => '1000 Euros',
          type  => 'bars');
 prEnd();

(In this case, type could also have been 'totalbars','percentbars', 'lines' or 'area'.)

DESCRIPTION

To draw simple charts with the help of PDF::Reuse. Currently there are 5 types: 'bars', 'totalbars','percentbars', 'lines' and 'area'.

Methods

new

my $s = PDF::Reuse::SimpleChart->new();

Constructor. Mandatory.

You can also create a clone of an object like this:

my $clone = $s->new(); 

columns

$s->columns( qw(unit column1 column2 .. columnN));

Defines what you want to write along the x-axis. The first value will be put to the right of the arrow of the axis. It could be the "unit" of the columns.

add

$s->add('name', value1, value2, ..., valueN);

To define data for the graph.

The name will be put to the right of the graph. It will be the identifier (case sensitive) of the series, so you can add new values afterwards. (Then the values also have to come in exactly the same order.)

The values can be numbers with '-' and '.'. You can also have '' or undef to denote a missing value. If the values contain other characters, the series is interpreted as 'columns'.

If you have a text file with a simple 2-dimensional table, like the one here below, you can use each line as parameters to the method. (The value in the upper left corner will refer to the columns to the right, not to the names under it.)

Month   January February Mars  April  May  June  July
Riga        314    490    322   -965  736   120   239
Helsinki    389   -865   -242      7  689   294   518
Stockholm   456   -712    542    367  742   189   190
Oslo        622    533    674   1289  679   -56   345

ex.:

 use PDF::Reuse::SimpleChart;
 use PDF::Reuse;
 use strict;
   
 prFile('myFile.pdf');
 my $s = PDF::Reuse::SimpleChart->new();
 
 open (INFILE, "textfile.txt") || die "Couldn't open textfile.txt, $!\n";
 while (<INFILE>)
 {  my @list = m'(\S+)'og;
    $s->add(@list) if (scalar @list) ;
 }
 close INFILE; 

 $s->draw(x     => 10,
          y     => 200,
          yUnit => '1000 Euros',
          type  => 'bars');
 prEnd();

draw

This method does the actual "plotting" of the graph. The parameters are

x

x-coordinate of the lower left corner in pixels, where the graph is going to be drawn. The actual graph comes still a few pixels to the right.

y

y-coordinate of the lower left corner in pixels, where the graph is going to be drawn. The actual graph comes still a few pixels higher up.

width

Width of the graph. 450 by default. Long texts might end up outside.

height

Height of the graph. 450 by default.

size

A number to resize the graph, with lines, texts and everything

(If you change the size of a graph with the parameters width and height, the font sizes, distances between lines etc. are untouched, but with 'size' they also change.)

xsize

A number to resize the graph horizontally, with lines, texts and everything

ySize

A number to resize the graph vertically, with lines, texts and everything

type

By default: 'bars'. Can also be 'totalbars', percentbars', 'lines' and 'area', (you could abbreviate to the first character if you want). They are more or less freely interchangeable.

When you have 'lines' or 'area', you get vertical lines. They show where the values of the graph are significant. The values between these points are possible, but of course not necessarily true. It is an illustration.

yUnit

What to write above the y-axis

rotate

Look at the documentation for prForm in PDF::Reuse for details. Also texts are rotated, so most often it is not a good idea to use this parameter.

background

Three RGB numbers ex. '0.95 0.95 0.95'.

noUnits

If this parameter is equal to 1, no units are written.

title

color

$s->color( ('1 1 0', '0 1 1', '0 1 0', '1 0 0', '0 0 1'));

To define colors to be used. The parameter to this method is a list of RGB numbers.

EXAMPLE

This is not a real case. Everything is just invented. It is here to show how to use the module. If you want the graphs to be less 'dramatic' change the type to lines or any of the other types.

It might be easier to compare the individual offices with 'bars', totalbars', or 'lines'.

use PDF::Reuse::SimpleChart;
use PDF::Reuse;
use strict;
  
prFile('myFile.pdf');
prCompress(1);
my $s = PDF::Reuse::SimpleChart->new();

###########
# Money in
###########

$s->columns( qw(Month   January February Mars  April  May  June  July));
$s->add(     qw(Riga        436   790     579   1023   964  520    390));
$s->add(     qw(Helsinki    529   630     789    567   570   94    180));
$s->add(     qw(Stockholm   469   534     642    767   712  399    190));
$s->add(     qw(Oslo        569   833     967   1589   790  158    345));

$s->draw(x      => 45,
         y      => 455,
         yUnit  => '1000 Euros',
         type   => 'area',
         title  => 'Money In',
         height => 300,
         width  => 460);

###################################
# Costs are to be shown separately
###################################

my $costs = PDF::Reuse::SimpleChart->new();

$costs->columns( qw(Month   January February Mars April  May  June  July));
$costs->add(     qw(Riga        -316  -290  -376   -823 -243  -320  -509));
$costs->add(     qw(Helsinki    -440  -830  -989   -671 -170  -394  -618));
$costs->add(     qw(Stockholm   -218  -345  -242   -467 -412  -299  -590));
$costs->add(     qw(Oslo        -369  -343  -567   -589 -390  -258  -459));

$costs->draw(x      => 45,
             y      => 55,
             yUnit  => '1000 Euros',
             type   => 'area',
             title  => 'Costs',
             height => 300,
             width  => 460);

####################################
# The costs are added to 'money in'
####################################

$s->add( qw(Riga        -316  -290  -376   -823 -243  -320  -509));
$s->add( qw(Helsinki    -440  -830  -989   -671 -170  -394  -618));
$s->add( qw(Stockholm   -218  -345  -242   -467 -412  -299  -590));
$s->add( qw(Oslo        -369  -343  -567   -589 -390  -258  -459));

prPage();

$s->draw(x     => 45,
         y     => 455,
         yUnit => '1000 Euros',
         type  => 'area',
         title => 'Profit');

########
# Taxes
########

$s->add( qw(Riga        -116  -90   -179   -230  -43  -20  -90));
$s->add( qw(Helsinki     40   -130  -190   -32   -70  -30  -18));
$s->add( qw(Stockholm    28   -45   -42    -107  -92  -99  -90));
$s->add( qw(Oslo        -169  -43   -67    -189  -190 -58  -59));

$s->draw(x     => 45,
         y     => 55,
         yUnit => '1000 Euros',
         type  => 'area',
         title => 'After Tax');

 prEnd(); 

SEE ALSO

PDF::Reuse
PDF::Reuse::Tutorial

AUTHOR

Lars Lundberg, <elkelund @ worldonline . se>

COPYRIGHT AND LICENSE

Copyright 2004 by Lars Lundberg

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

DISCLAIMER

You get this module free as it is, but nothing is guaranteed to work, whatever implicitly or explicitly stated in this document, and everything you do, you do at your own risk - I will not take responsibility for any damage, loss of money and/or health that may arise from the use of this module.