home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Graph.pm < prev    next >
Encoding:
Perl POD Document  |  2003-07-01  |  44.3 KB  |  1,623 lines

  1. #==========================================================================
  2. #              Copyright (c) 1995-2000 Martien Verbruggen
  3. #--------------------------------------------------------------------------
  4. #
  5. #   Name:
  6. #       GD::Graph.pm
  7. #
  8. #   Description:
  9. #       Module to create graphs from a data set drawing on a GD::Image
  10. #       object
  11. #
  12. #       Package of a number of graph types:
  13. #       GD::Graph::bars
  14. #       GD::Graph::hbars
  15. #       GD::Graph::lines
  16. #       GD::Graph::points
  17. #       GD::Graph::linespoints
  18. #       GD::Graph::area
  19. #       GD::Graph::pie
  20. #       GD::Graph::mixed
  21. #
  22. # $Id: Graph.pm,v 1.53 2003/07/01 04:56:57 mgjv Exp $
  23. #
  24. #==========================================================================
  25.  
  26. #
  27. # GD::Graph
  28. #
  29. # Parent class containing data all graphs have in common.
  30. #
  31.  
  32. package GD::Graph;
  33.  
  34. ($GD::Graph::prog_version) = '$Revision: 1.53 $' =~ /\s([\d.]+)/;
  35. $GD::Graph::VERSION = '1.43';
  36.  
  37. use strict;
  38. use GD;
  39. use GD::Text::Align;
  40. use GD::Graph::Data;
  41. use GD::Graph::Error;
  42. use Carp;
  43.  
  44. @GD::Graph::ISA = qw(GD::Graph::Error);
  45.  
  46. # Some tools and utils
  47. use GD::Graph::colour qw(:colours);
  48.  
  49. my %GDsize = ( 
  50.     'x' => 400, 
  51.     'y' => 300 
  52. );
  53.  
  54. my %Defaults = (
  55.  
  56.     # Set the top, bottom, left and right margin for the chart. These 
  57.     # margins will be left empty.
  58.     t_margin      => 0,
  59.     b_margin      => 0,
  60.     l_margin      => 0,
  61.     r_margin      => 0,
  62.  
  63.     # Set the factor with which to resize the logo in the chart (need to
  64.     # automatically compute something nice for this, really), set the 
  65.     # default logo file name, and set the logo position (UR, BR, UL, BL)
  66.     logo          => undef,
  67.     logo_resize   => 1.0,
  68.     logo_position => 'LR',
  69.  
  70.     # Do we want a transparent background?
  71.     transparent   => 1,
  72.  
  73.     # Do we want interlacing?
  74.     interlaced    => 1,
  75.  
  76.     # Set the background colour, the default foreground colour (used 
  77.     # for axes etc), the textcolour, the colour for labels, the colour 
  78.     # for numbers on the axes, the colour for accents (extra lines, tick
  79.     # marks, etc..)
  80.     bgclr         => 'white',   # background colour
  81.     fgclr         => 'dblue',   # Axes and grid
  82.     boxclr        => undef,     # Fill colour for box axes, default: not used
  83.     accentclr     => 'gray',    # bar, area and pie outlines.
  84.  
  85.     labelclr      => 'dblue',   # labels on axes
  86.     axislabelclr  => 'dblue',   # values on axes
  87.     legendclr     => 'dblue',   # Text for the legend
  88.     textclr       => 'dblue',   # All text, apart from the following 2
  89.  
  90.     valuesclr     => 'dblue',   # values printed above the points
  91.     
  92.     # data set colours
  93.     dclrs => [qw(lred lgreen lblue lyellow lpurple cyan lorange)], 
  94.  
  95.     # number of pixels to use as text spacing
  96.     text_space    => 4,
  97.  
  98.     # These have undefined values, but are here so that the set method
  99.     # knows about them:
  100.     title       => undef,
  101. );
  102.  
  103. sub _has_default { 
  104.     my $self = shift;
  105.     my $attr = shift || return;
  106.     exists $Defaults{$attr} 
  107. }
  108.  
  109. #
  110. # PUBLIC methods, documented in pod.
  111. #
  112. sub new  # ( width, height ) optional;
  113. {
  114.     my $type = shift;
  115.     my $self = {};
  116.     bless $self, $type;
  117.  
  118.     if (@_) 
  119.     {
  120.         # If there are any parameters, they should be the size
  121.         return GD::Graph->_set_error(
  122.             "Usage: GD::Graph::<type>::new(width, height)") unless @_ >= 2;
  123.  
  124.         $self->{width} = shift;
  125.         $self->{height} = shift;
  126.     } 
  127.     else 
  128.     {
  129.         # There were obviously no parameters, so use defaults
  130.         $self->{width} = $GDsize{'x'};
  131.         $self->{height} = $GDsize{'y'};
  132.     }
  133.  
  134.     # Initialise all relevant parameters to defaults
  135.     # These are defined in the subclasses. See there.
  136.     $self->initialise() or return;
  137.  
  138.     return $self;
  139. }
  140.  
  141. sub get
  142. {
  143.     my $self = shift;
  144.     my @wanted = map $self->{$_}, @_;
  145.     wantarray ? @wanted : $wanted[0];
  146. }
  147.  
  148. sub set
  149. {
  150.     my $self = shift;
  151.     my %args = @_;
  152.     my $w = 0;
  153.  
  154.     foreach (keys %args) 
  155.     { 
  156.         # Enforce read-only attributes.
  157.         /^width$/ || /^height$/ and do 
  158.         {
  159.             $self->_set_warning("Read-only attribute '$_' not set");
  160.             $w++;
  161.             next;
  162.         };
  163.  
  164.         $self->{$_} = $args{$_}, next if $self->_has_default($_); 
  165.  
  166.         $w++;
  167.         $self->_set_warning("No attribute '$_'");
  168.     }
  169.  
  170.     return $w ? undef : "No problems";
  171. }
  172.  
  173. # Generic routine to instantiate GD::Text::Align objects for text
  174. # attributes
  175. sub _set_font
  176. {
  177.     my $self = shift;
  178.     my $name = shift;
  179.  
  180.     if (! exists $self->{$name})
  181.     {
  182.         $self->{$name} = GD::Text::Align->new($self->{graph}, 
  183.             valign => 'top',
  184.             halign => 'center',
  185.         ) or return $self->_set_error("Couldn't set font");
  186.     }
  187.  
  188.     $self->{$name}->set_font(@_);
  189. }
  190.  
  191. sub set_title_font # (fontname, size)
  192. {
  193.     my $self = shift;
  194.     $self->_set_font('gdta_title', @_);
  195. }
  196.  
  197. sub set_text_clr # (colour name)
  198. {
  199.     my $self = shift;
  200.     my $clr  = shift;
  201.  
  202.     $self->set(
  203.         textclr       => $clr,
  204.         labelclr      => $clr,
  205.         axislabelclr  => $clr,
  206.         valuesclr     => $clr,
  207.     );
  208. }
  209.  
  210. sub plot
  211. {
  212.     # ABSTRACT
  213.     my $self = shift;
  214.     $self->die_abstract("sub plot missing,");
  215. }
  216.  
  217. # Set defaults that apply to all graph/chart types. 
  218. # This is called by the default initialise methods 
  219. # from the objects further down the tree.
  220.  
  221. sub initialise
  222. {
  223.     my $self = shift;
  224.  
  225.     foreach (keys %Defaults) 
  226.     {
  227.         $self->set($_ => $Defaults{$_});
  228.     }
  229.  
  230.     $self->open_graph()                     or return;
  231.     $self->set_title_font(GD::Font->Large)  or return;
  232. }
  233.  
  234.  
  235. # Check the integrity of the submitted data
  236. #
  237. # Checks are done to assure that every input array 
  238. # has the same number of data points, it sets the variables
  239. # that store the number of sets and the number of points
  240. # per set, and kills the process if there are no datapoints
  241. # in the sets, or if there are no data sets.
  242.  
  243. sub check_data # \@data
  244. {
  245.     my $self = shift;
  246.     my $data = shift;
  247.  
  248.     $self->{_data} = GD::Graph::Data->new($data) 
  249.         or return $self->_set_error(GD::Graph::Data->error);
  250.     
  251.     $self->{_data}->make_strict;
  252.  
  253.     $self->{_data}->num_sets > 0 && $self->{_data}->num_points > 0
  254.         or return $self->_set_error('No data sets or points');
  255.     
  256.     if ($self->{show_values})
  257.     {
  258.         # If this isn't a GD::Graph::Data compatible structure, then
  259.         # we'll just use the data structure.
  260.         #
  261.         # XXX We should probably check a few more things here, e.g.
  262.         # similarity between _data and show_values.
  263.         #
  264.         my $ref = ref($self->{show_values});
  265.         if (! $ref || ($ref ne 'GD::Graph::Data' && $ref ne 'ARRAY'))
  266.         {
  267.             $self->{show_values} = $self->{_data}
  268.         }
  269.         elsif ($ref eq 'ARRAY')
  270.         {
  271.             $self->{show_values} =
  272.                 GD::Graph::Data->new($self->{show_values})
  273.                 or return $self->_set_error(GD::Graph::Data->error);
  274.         }
  275.     }
  276.  
  277.     return $self;
  278. }
  279.  
  280. # Open the graph output canvas by creating a new GD object.
  281.  
  282. sub open_graph
  283. {
  284.     my $self = shift;
  285.     return $self->{graph} if exists $self->{graph};
  286.     $self->{graph} = GD::Image->new($self->{width}, $self->{height});
  287. }
  288.  
  289. # Initialise the graph output canvas, setting colours (and getting back
  290. # index numbers for them) setting the graph to transparent, and 
  291. # interlaced, putting a logo (if defined) on there.
  292.  
  293. sub init_graph
  294. {
  295.     my $self = shift;
  296.  
  297.     $self->{bgci} = $self->set_clr(_rgb($self->{bgclr}));
  298.     $self->{fgci} = $self->set_clr(_rgb($self->{fgclr}));
  299.     $self->{tci}  = $self->set_clr(_rgb($self->{textclr}));
  300.     $self->{lci}  = $self->set_clr(_rgb($self->{labelclr}));
  301.     $self->{alci} = $self->set_clr(_rgb($self->{axislabelclr}));
  302.     $self->{acci} = $self->set_clr(_rgb($self->{accentclr}));
  303.     $self->{valuesci} = $self->set_clr(_rgb($self->{valuesclr}));
  304.     $self->{legendci} = $self->set_clr(_rgb($self->{legendclr}));
  305.     $self->{boxci} = $self->set_clr(_rgb($self->{boxclr})) 
  306.         if $self->{boxclr};
  307.  
  308.     $self->{graph}->transparent($self->{bgci}) if $self->{transparent};
  309.     $self->{graph}->interlaced($self->{interlaced});
  310.  
  311.     # XXX yuck. This doesn't belong here.. or does it?
  312.     $self->put_logo();
  313.  
  314.     return $self;
  315. }
  316.  
  317. sub _read_logo_file
  318. {
  319.     my $self = shift;
  320.     my $gdimport = 'newFrom' . ucfirst($self->export_format);
  321.     my $glogo;
  322.     local (*LOGO);
  323.  
  324.     open(LOGO, $self->{logo}) or return;
  325.     binmode(LOGO);
  326.     unless ( $glogo = GD::Image->$gdimport(\*LOGO) ) 
  327.     {
  328.         carp "Problems reading $self->{logo}"; 
  329.         return;
  330.     }
  331.  
  332.     return $glogo;
  333. }
  334.  
  335. # read in the logo, and paste it on the graph canvas
  336.  
  337. sub put_logo
  338. {
  339.     my $self = shift;
  340.     return unless defined $self->{logo};
  341.  
  342.     my $glogo = $self->_read_logo_file() or return;
  343.  
  344.     my ($x, $y);
  345.     my $r = $self->{logo_resize};
  346.  
  347.     my $r_margin = (defined $self->{r_margin_abs}) ? 
  348.         $self->{r_margin_abs} : $self->{r_margin};
  349.     my $b_margin = (defined $self->{b_margin_abs}) ? 
  350.         $self->{b_margin_abs} : $self->{b_margin};
  351.  
  352.     my ($w, $h) = $glogo->getBounds;
  353.     LOGO: for ($self->{logo_position}) {
  354.         /UL/i and do {
  355.             $x = $self->{l_margin};
  356.             $y = $self->{t_margin};
  357.             last LOGO;
  358.         };
  359.         /UR/i and do {
  360.             $x = $self->{width} - $r_margin - $w * $r;
  361.             $y = $self->{t_margin};
  362.             last LOGO;
  363.         };
  364.         /LL/i and do {
  365.             $x = $self->{l_margin};
  366.             $y = $self->{height} - $b_margin - $h * $r;
  367.             last LOGO;
  368.         };
  369.         # default "LR"
  370.         $x = $self->{width} - $r_margin - $r * $w;
  371.         $y = $self->{height} - $b_margin - $r * $h;
  372.         last LOGO;
  373.     }
  374.     $self->{graph}->copyResized($glogo, 
  375.         $x, $y, 0, 0, $r * $w, $r * $h, $w, $h);
  376. }
  377.  
  378. # Set a colour to work with on the canvas, by rgb value. 
  379. # Return the colour index in the palette
  380.  
  381. sub set_clr # GD::Image, r, g, b
  382. {
  383.     my $self = shift; 
  384.     return unless @_;
  385.     my $gd = $self->{graph};
  386.  
  387.     # All of this could potentially be done by using colorResolve
  388.     # The problem is that colorResolve doesn't return an error
  389.     # condition (-1) if it can't allocate a color. Instead it always
  390.     # returns 0.
  391.  
  392.     # Check if this colour already exists on the canvas
  393.     my $i = $gd->colorExact(@_);
  394.     # if not, allocate a new one, and return its index
  395.     $i = $gd->colorAllocate(@_) if $i < 0;
  396.     # if this fails, we should use colorClosest.
  397.     $i = $gd->colorClosest(@_)  if $i < 0;
  398.  
  399.     # TODO Deal with antialiasing here?
  400.     if (0 && $self->can("setAntiAliased"))
  401.     {
  402.     $self->setAntiAliased($i);
  403.     eval "$i = gdAntiAliased";
  404.     }
  405.  
  406.     return $i;
  407. }
  408.  
  409. # Set a temporary colour that can be used with fillToBorder
  410. sub _set_tmp_clr
  411. {
  412.     my $self = shift; 
  413.     # XXX Error checks!
  414.     $self->{graph}->colorAllocate(0,0,0);
  415. }
  416.  
  417. # Remove the temporary colour
  418. sub _rm_tmp_clr
  419. {
  420.     my $self = shift; 
  421.     return unless @_;
  422.     # XXX Error checks?
  423.     $self->{graph}->colorDeallocate(shift);
  424. }
  425.  
  426. # Set a colour, disregarding wether or not it already exists. This may
  427. # be necessary where one wants the same colour to have a different
  428. # index, as in pie slices of the same color as the edge.
  429. # Note that this could be cleaned up after needed, but we won't do that.
  430.  
  431. sub set_clr_uniq # GD::Image, r, g, b
  432. {
  433.     my $self = shift; 
  434.     return unless @_;
  435.     $self->{graph}->colorAllocate(@_); 
  436. }
  437.  
  438. # Return an array of rgb values for a colour number
  439.  
  440. sub pick_data_clr # number
  441. {
  442.     my $self = shift;
  443.     _rgb($self->{dclrs}[$_[0] % @{$self->{dclrs}} - 1]);
  444. }
  445.  
  446. # contrib "Bremford, Mike" <mike.bremford@gs.com>
  447. sub pick_border_clr # number
  448. {
  449.     my $self = shift;
  450.  
  451.     ref $self->{borderclrs} ?
  452.         _rgb($self->{borderclrs}[$_[0] % @{$self->{borderclrs}} - 1]) :
  453.         _rgb($self->{accentclr});
  454. }
  455.  
  456. sub gd 
  457. {
  458.     my $self = shift;
  459.     return $self->{graph};
  460. }
  461.  
  462. sub export_format
  463. {
  464.     my $proto = shift;
  465.     my @f = grep { GD::Image->can($_) && 
  466.                    do { 
  467.             my $g = GD::Image->new(5,5);
  468.             $g->colorAllocate(0,0,0);
  469.             $g->$_() 
  470.            };
  471.         } qw(gif png jpeg xbm xpm gd gd2);
  472.     wantarray ? @f : $f[0];
  473. }
  474.  
  475. # The following method is undocumented, and will not be supported as
  476. # part of the interface. There isn't really much reason to do so.
  477. sub import_format
  478. {
  479.     my $proto = shift;
  480.     # For now, exclude xpm, as it's buggy
  481.     my @f = grep { GD::Image->can("newFrom\u$_") } qw(gif png jpeg xbm gd gd2);
  482.     wantarray ? @f : $f[0];
  483. }
  484.  
  485. sub can_do_ttf
  486. {
  487.     my $proto = shift;
  488.     return GD::Text->can_do_ttf;
  489. }
  490.  
  491. # DEBUGGING
  492. # data_dump obsolete now, use Data::Dumper
  493.  
  494. sub die_abstract
  495. {
  496.     my $self = shift;
  497.     my $msg = shift;
  498.     # ABSTRACT
  499.     confess
  500.         "Subclass (" .
  501.         ref($self) . 
  502.         ") not implemented correctly: " .
  503.         (defined($msg) ? $msg : "unknown error");
  504. }
  505.  
  506. "Just another true value";
  507.  
  508. __END__
  509.  
  510. =head1 NAME
  511.  
  512. GD::Graph - Graph Plotting Module for Perl 5
  513.  
  514. =head1 SYNOPSIS
  515.  
  516. use GD::Graph::moduleName;
  517.  
  518. =head1 DESCRIPTION
  519.  
  520. B<GD::Graph> is a I<perl5> module to create charts using the GD module.
  521. The following classes for graphs with axes are defined:
  522.  
  523. =over 4
  524.  
  525. =item C<GD::Graph::lines>
  526.  
  527. Create a line chart.
  528.  
  529. =item C<GD::Graph::bars> and C<GD::Graph::hbars>
  530.  
  531. Create a bar chart with vertical or horizontal bars.
  532.  
  533. =item C<GD::Graph::points>
  534.  
  535. Create an chart, displaying the data as points.
  536.  
  537. =item C<GD::Graph::linespoints>
  538.  
  539. Combination of lines and points.
  540.  
  541. =item C<GD::Graph::area>
  542.  
  543. Create a graph, representing the data as areas under a line.
  544.  
  545. =item C<GD::Graph::mixed>
  546.  
  547. Create a mixed type graph, any combination of the above. At the moment
  548. this is fairly limited. Some of the options that can be used with some
  549. of the individual graph types won't work very well. Multiple bar
  550. graphs in a mixed graph won't display very nicely, i.e. they cannot be
  551. put next to each other.
  552.  
  553. =back
  554.  
  555. Additional types:
  556.  
  557. =over 4
  558.  
  559. =item C<GD::Graph::pie>
  560.  
  561. Create a pie chart.
  562.  
  563. =back
  564.  
  565. =head1 EXAMPLES
  566.  
  567. See the samples directory in the distribution, and read the Makefile
  568. there.
  569.  
  570. =head1 USAGE
  571.  
  572. Fill an array of arrays with the x values and the values of the data
  573. sets.  Make sure that every array is the same size, otherwise
  574. I<GD::Graph> will complain and refuse to compile the graph.
  575.  
  576.   @data = ( 
  577.     ["1st","2nd","3rd","4th","5th","6th","7th", "8th", "9th"],
  578.     [    1,    2,    5,    6,    3,  1.5,    1,     3,     4],
  579.     [ sort { $a <=> $b } (1, 2, 5, 6, 3, 1.5, 1, 3, 4) ]
  580.   );
  581.  
  582. If you don't have a value for a point in a certain dataset, you can
  583. use B<undef>, and the point will be skipped.
  584.  
  585. Create a new I<GD::Graph> object by calling the I<new> method on the
  586. graph type you want to create (I<chart> is I<bars>, I<hbars>,
  587. I<lines>, I<points>, I<linespoints>, I<mixed> or I<pie>).
  588.  
  589.   my $graph = GD::Graph::chart->new(400, 300);
  590.  
  591. Set the graph options. 
  592.  
  593.   $graph->set( 
  594.       x_label           => 'X Label',
  595.       y_label           => 'Y label',
  596.       title             => 'Some simple graph',
  597.       y_max_value       => 8,
  598.       y_tick_number     => 8,
  599.       y_label_skip      => 2 
  600.   ) or die $my_graph->error;
  601.  
  602. and plot the graph.
  603.  
  604.   my $gd = $my_graph->plot(\@data) or die $my_graph->error;
  605.  
  606. Then do whatever your current version of GD allows you to do to save the
  607. file. For versions of GD older than 1.19, you'd do something like:
  608.  
  609.   open(IMG, '>file.gif') or die $!;
  610.   binmode IMG;
  611.   print IMG $gd->gif;
  612.   close IMG;
  613.  
  614. and for newer versions (1.20 and up) you'd write
  615.  
  616.   open(IMG, '>file.png') or die $!;
  617.   binmode IMG;
  618.   print IMG $gd->png;
  619.  
  620. or
  621.  
  622.   open(IMG, '>file.gd2') or die $!;
  623.   binmode IMG;
  624.   print IMG $gd->gd2;
  625.  
  626. Then there's also of course the possibility of using a shorter
  627. version (for each of the export functions that GD supports):
  628.  
  629.   print IMG $my_graph->plot(\@data)->gif;
  630.   print IMG $my_graph->plot(\@data)->png;
  631.   print IMG $my_graph->plot(\@data)->gd;
  632.   print IMG $my_graph->plot(\@data)->gd2;
  633.  
  634. If you want to write something that doesn't require your code to 'know'
  635. whether to use gif or png, you could do something like:
  636.  
  637.   if ($gd->can('png')) { # blabla }
  638.  
  639. or you can use the convenience method C<export_format>:
  640.  
  641.   my $format = $my_graph->export_format;
  642.   open(IMG, ">file.$format") or die $!;
  643.   binmode IMG;
  644.   print IMG $my_graph->plot(\@data)->$format();
  645.   close IMG;
  646.  
  647. or for CGI programs:
  648.  
  649.   use CGI qw(:standard);
  650.   #...
  651.   my $format = $my_graph->export_format;
  652.   print header("image/$format");
  653.   binmode STDOUT;
  654.   print $my_graph->plot(\@data)->$format();
  655.  
  656. (the parentheses after $format are necessary, to help the compiler
  657. decide that you mean a method name there)
  658.  
  659. See under L<"SEE ALSO"> for references to other documentation,
  660. especially the FAQ.
  661.  
  662. =head1 METHODS
  663.  
  664. =head2 Methods for all graphs
  665.  
  666. =over 4
  667.  
  668. =item GD::Graph::chart-E<gt>new([width,height])
  669.  
  670. Create a new object $graph with optional width and heigth. 
  671. Default width = 400, default height = 300. I<chart> is either
  672. I<bars>, I<lines>, I<points>, I<linespoints>, I<area>, I<mixed> or
  673. I<pie>.
  674.  
  675. =item $graph-E<gt>set_text_clr(I<colour name>)
  676.  
  677. Set the colour of the text. This will set the colour of the titles,
  678. labels, and axis labels to I<colour name>. Also see the options
  679. I<textclr>, I<labelclr> and I<axislabelclr>.
  680.  
  681. =item $graph-E<gt>set_title_font(font specification)
  682.  
  683. Set the font that will be used for the title of the chart.
  684. See L<"FONTS">.
  685.  
  686. =item $graph-E<gt>plot(I<\@data>)
  687.  
  688. Plot the chart, and return the GD::Image object.
  689.  
  690. =item $graph-E<gt>set(attrib1 =E<gt> value1, attrib2 =E<gt> value2 ...)
  691.  
  692. Set chart options. See OPTIONS section.
  693.  
  694. =item $graph-E<gt>get(attrib1, attrib2)
  695.  
  696. Returns a list of the values of the attributes. In scalar context
  697. returns the value of the first attribute only.
  698.  
  699. =item $graph-E<gt>gd()
  700.  
  701. Get the GD::Image object that is going to be used to draw on. You can do
  702. this either before or after calling the plot method, to do your own
  703. drawing.
  704.  
  705. Note that if you draw on the GD::Image object before calling the plot
  706. method that you are responsible for making sure that the background
  707. colour is correct and for setting transparency.
  708.  
  709. =item $graph-E<gt>export_format()
  710.  
  711. Query the export format of the GD library in use.  In scalar context, it
  712. returns 'gif', 'png' or undefined, which is sufficient for most people's
  713. use. In a list context, it returns a list of all the formats that are
  714. supported by the current version of GD. It can be called as a class or
  715. object method
  716.  
  717. =item $graph-E<gt>can_do_ttf()
  718.  
  719. Returns true if the current GD library supports TrueType fonts, False
  720. otherwise. Can also be called as a class method or static method.
  721.  
  722. =back
  723.  
  724.  
  725.  
  726. =head2 Methods for Pie charts
  727.  
  728. =over 4
  729.  
  730. =item $graph-E<gt>set_label_font(font specification)
  731.  
  732. =item $graph-E<gt>set_value_font(font specification)
  733.  
  734. Set the font that will be used for the label of the pie or the 
  735. values on the pie.
  736. See L<"FONTS">.
  737.  
  738. =back
  739.  
  740.  
  741. =head2 Methods for charts with axes.
  742.  
  743. =over 4
  744.  
  745. =item $graph-E<gt>set_x_label_font(font specification)
  746.  
  747. =item $graph-E<gt>set_y_label_font(font specification)
  748.  
  749. =item $graph-E<gt>set_x_axis_font(font specification)
  750.  
  751. =item $graph-E<gt>set_y_axis_font(font specification)
  752.  
  753. =item $graph-E<gt>set_values_font(font specification)
  754.  
  755. Set the font for the x and y axis label, the x and y axis
  756. value labels, and for the values printed above the data points.
  757. See L<"FONTS">.
  758.  
  759. =item $graph-E<gt>get_hotspot($dataset, $point)
  760.  
  761. B<Experimental>:
  762. Return a coordinate specification for a point in a dataset. Returns a
  763. list. If the point is not specified, returns a list of array references
  764. for all points in the dataset. If the dataset is also not specified,
  765. returns a list of array references for each data set. 
  766. See L<"HOTSPOTS">.
  767.  
  768. =item $graph-E<gt>get_feature_coordinates($feature_name)
  769.  
  770. B<Experimental>:
  771. Return a coordinate specification for a certain feature in the chart.
  772. Currently, features that are defined are I<axes>, the coordinates of
  773. the rectangle within the axes; I<x_label>, I<y1_label> and
  774. I<y2_label>, the labels printed along the axes, with I<y_label>
  775. provided as an alias for I<y1_label>; and I<title> which is the title
  776. text box.
  777. See L<"HOTSPOTS">.
  778.  
  779. =back
  780.  
  781.  
  782. =head1 OPTIONS
  783.  
  784. =head2 Options for all graphs
  785.  
  786. =over 4
  787.  
  788. =item width, height
  789.  
  790. The width and height of the canvas in pixels
  791. Default: 400 x 300.
  792. B<NB> At the moment, these are read-only options. If you want to set
  793. the size of a graph, you will have to do that with the I<new> method.
  794.  
  795. =item t_margin, b_margin, l_margin, r_margin
  796.  
  797. Top, bottom, left and right margin of the canvas. These margins will be
  798. left blank.
  799. Default: 0 for all.
  800.  
  801. =item logo
  802.  
  803. Name of a logo file. Generally, this should be the same format as your
  804. version of GD exports images in. At the moment there is no support for
  805. reading gd format files or xpm files.
  806. Default: no logo.
  807.  
  808. =item logo_resize, logo_position
  809.  
  810. Factor to resize the logo by, and the position on the canvas of the
  811. logo. Possible values for logo_position are 'LL', 'LR', 'UL', and
  812. 'UR'.  (lower and upper left and right). 
  813. Default: 'LR'.
  814.  
  815. =item transparent
  816.  
  817. If set to a true value, the produced image will have the background
  818. colour marked as transparent (see also option I<bgclr>).  Default: 1.
  819.  
  820. =item interlaced
  821.  
  822. If set to a true value, the produced image will be interlaced.
  823. Default: 1.
  824.  
  825. =back
  826.  
  827. =head2 Colours
  828.  
  829. =over 4
  830.  
  831. =item bgclr, fgclr, boxclr, accentclr, shadowclr
  832.  
  833. Drawing colours used for the chart: background, foreground (axes and
  834. grid), axis box fill colour, accents (bar, area and pie outlines), and
  835. shadow (currently only for bars).
  836.  
  837. All colours should have a valid value as described in L<"COLOURS">,
  838. except boxclr, which can be undefined, in which case the box will not be
  839. filled. 
  840.  
  841. =item shadow_depth
  842.  
  843. Depth of a shadow, positive for right/down shadow, negative for left/up
  844. shadow, 0 for no shadow (default).
  845. Also see the C<shadowclr> and C<bar_spacing> options.
  846.  
  847. =item labelclr, axislabelclr, legendclr, valuesclr, textclr
  848.  
  849. Text Colours used for the chart: label (labels for the axes or pie),
  850. axis label (misnomer: values printed along the axes, or on a pie slice),
  851. legend text, shown values text, and all other text.
  852.  
  853. All colours should have a valid value as described in L<"COLOURS">.
  854.  
  855. =item dclrs (short for datacolours)
  856.  
  857. This controls the colours for the bars, lines, markers, or pie slices.
  858. This should be a reference to an array of colour names as defined in
  859. L<GD::Graph::colour> (S<C<perldoc GD::Graph::colour>> for the names available).
  860.  
  861.     $graph->set( dclrs => [ qw(green pink blue cyan) ] );
  862.  
  863. The first (fifth, ninth) data set will be green, the next pink, etc.
  864.  
  865. A colour can be C<undef>, in which case the data set will not be drawn.
  866. This can be useful for cumulative bar sets where you want certain data
  867. series (often the first one) not to show up, which can be used to
  868. emulate error bars (see examples 1-7 and 6-3 in the distribution).
  869.  
  870. Default: [ qw(lred lgreen lblue lyellow lpurple cyan lorange) ] 
  871.  
  872. =item borderclrs
  873.  
  874. This controls the colours of the borders of the bars data sets. Like
  875. dclrs, it is a reference to an array of colour names as defined in
  876. L<GD::Graph::colour>.
  877. Setting a border colour to C<undef> means the border will not be drawn.
  878.  
  879. =item cycle_clrs
  880.  
  881. If set to a true value, bars will not have a colour from C<dclrs> per
  882. dataset, but per point. The colour sequence will be identical for each
  883. dataset. Note that this may have a weird effect if you are drawing more
  884. than one data set. If this is set to a value larger than 1 the border
  885. colour of the bars will cycle through the colours in C<borderclrs>.
  886.  
  887. =item accent_treshold
  888.  
  889. Not really a colour, but it does control a visual aspect: Accents on
  890. bars are only drawn when the width of a bar is larger than this number
  891. of pixels. Accents inside areas are only drawn when the horizontal
  892. distance between points is larger than this number.
  893. Default 4
  894.  
  895. =back
  896.  
  897. =head2 Options for graphs with axes.
  898.  
  899. options for I<bars>, I<lines>, I<points>, I<linespoints>, I<mixed> and 
  900. I<area> charts.
  901.  
  902. =over 4
  903.  
  904. =item x_label, y_label
  905.  
  906. The labels to be printed next to, or just below, the axes. Note that if
  907. you use the two_axes option that you need to use y1_label and y2_label.
  908.  
  909. =item long_ticks, tick_length
  910.  
  911. If I<long_ticks> is a true value, ticks will be drawn the same length
  912. as the axes.  Otherwise ticks will be drawn with length
  913. I<tick_length>. if I<tick_length> is negative, the ticks will be drawn
  914. outside the axes.  Default: long_ticks = 0, tick_length = 4.
  915.  
  916. These attributes can also be set for x and y axes separately with
  917. x_long_ticks, y_long_ticks, x_tick_length and y_tick_length.
  918.  
  919. =item x_ticks
  920.  
  921. If I<x_ticks> is a true value, ticks will be drawm for the x axis.
  922. These ticks are subject to the values of I<long_ticks> and
  923. I<tick_length>.  Default: 1.
  924.  
  925. =item y_tick_number
  926.  
  927. Number of ticks to print for the Y axis. Use this, together with
  928. I<y_label_skip> to control the look of ticks on the y axis.
  929. Default: 5.
  930.  
  931. =item y_number_format
  932.  
  933. This can be either a string, or a reference to a subroutine. If it is
  934. a string, it will be taken to be the first argument to an sprintf,
  935. with the value as the second argument:
  936.  
  937.     $label = sprintf( $s->{y_number_format}, $value );
  938.  
  939. If it is a code reference, it will be executed with the value as the
  940. argument:
  941.  
  942.     $label = &{$s->{y_number_format}}($value);
  943.  
  944. This can be useful, for example, if you want to reformat your values
  945. in currency, with the - sign in the right spot. Something like:
  946.  
  947.     sub y_format
  948.     {
  949.         my $value = shift;
  950.         my $ret;
  951.  
  952.         if ($value >= 0)
  953.         {
  954.             $ret = sprintf("\$%d", $value * $refit);
  955.         }
  956.         else
  957.         {
  958.             $ret = sprintf("-\$%d", abs($value) * $refit);
  959.         }
  960.  
  961.         return $ret;
  962.     }
  963.  
  964.     $my_graph->set( 'y_number_format' => \&y_format );
  965.  
  966. (Yes, I know this can be much shorter and more concise)
  967.  
  968. Default: undef.
  969.  
  970. =item x_label_skip, y_label_skip
  971.  
  972. Print every I<x_label_skip>th number under the tick on the x axis, and
  973. every I<y_label_skip>th number next to the tick on the y axis.
  974. Default: 1 for both.
  975.  
  976. =item x_tick_offset
  977.  
  978. When x_label_skip is used, this will skip the first x_tick_offset values
  979. in the labels before starting to print. Let me give an example. If you
  980. have a series of X labels like
  981.  
  982.   qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)
  983.  
  984. and you set x_label_skip to 3, you will see ticks on the X axis for Jan,
  985. Apr, Jul, Oct and Dec. This is not always what is wanted. If you set
  986. x_tick_offset to 1, you get Feb, May, Aug, Nov and Dec, and if you set
  987. it to 2, you get Mar, Jun Sep and Dec, and this last one definitely
  988. looks better. A combination of 6 and 5 also works nice for months. 
  989.  
  990. Note that the value for x_tick_offset is periodical. This means that it
  991. will have the same effect for each nteger n in x_tick_offset + n *
  992. x_label_skip.
  993.  
  994. =item x_all_ticks
  995.  
  996. Force a print of all the x ticks, even if x_label_skip is set to a value
  997. Default: 0.
  998.  
  999. =item x_label_position
  1000.  
  1001. Controls the position of the X axis label (title). The value for this
  1002. should be between 0 and 1, where 0 means aligned to the left, 1 means
  1003. aligned to the right, and 1/2 means centered. 
  1004. Default: 3/4
  1005.  
  1006. =item y_label_position
  1007.  
  1008. Controls the position of both Y axis labels (titles). The value for
  1009. this should be between 0 and 1, where 0 means aligned to the bottom, 1
  1010. means aligned to the top, and 1/2 means centered. 
  1011. Default: 1/2
  1012.  
  1013. =item x_labels_vertical
  1014.  
  1015. If set to a true value, the X axis labels will be printed vertically.
  1016. This can be handy in case these labels get very long.
  1017. Default: 0.
  1018.  
  1019. =item x_plot_values, y_plot_values
  1020.  
  1021. If set to a true value, the values of the ticks on the x or y axes
  1022. will be plotted next to the tick. Also see I<x_label_skip,
  1023. y_label_skip>.  Default: 1 for both.
  1024.  
  1025. =item box_axis
  1026.  
  1027. Draw the axes as a box, if true.
  1028. Default: 1.
  1029.  
  1030. =item no_axes
  1031.  
  1032. Draw no axes at all. If this is set to undef, all axes are drawn. If
  1033. it is set to 0, the zero axis will be drawn, I<for bar charts only>.
  1034. If this is set to a true value, no axes will be drawns at all. Value
  1035. labels on the axes and ticks will also not be drawn, but axis lables
  1036. are drawn.
  1037. Default: undef.
  1038.  
  1039. =item two_axes
  1040.  
  1041. Use two separate axes for the first and second data set. The first
  1042. data set will be set against the left axis, the second against the
  1043. right axis. If this is set to a true value, trying to use anything
  1044. else than 2 datasets will generate an error.  
  1045.  
  1046. Note that if you use this option, that you need to use y1_label and
  1047. y2_label, instead of just y_label, if you want the two axes to have
  1048. different labels. The same goes for some other options starting with the
  1049. letter 'y' and an underscore.
  1050.  
  1051. Default: 0.
  1052.  
  1053. =item zero_axis
  1054.  
  1055. If set to a true value, the axis for y values of 0 will always be
  1056. drawn. This might be useful in case your graph contains negative
  1057. values, but you want it to be clear where the zero value is. (see also
  1058. I<zero_axis_only> and I<box_axes>).
  1059. Default: 0.
  1060.  
  1061. =item zero_axis_only
  1062.  
  1063. If set to a true value, the zero axis will be drawn (see
  1064. I<zero_axis>), and no axis at the bottom of the graph will be drawn.
  1065. The labels for X values will be placed on the zero exis.
  1066. Default: 0.
  1067.  
  1068. =item y_max_value, y_min_value
  1069.  
  1070. Maximum and minimum value displayed on the y axis. If two_axes is a
  1071. true value, then y1_min_value, y1_max_value (for the left axis),
  1072. and y2_min_value, y2_max_value (for the right axis) take precedence
  1073. over these.
  1074.  
  1075. The range (y_min_value..y_max_value) has to include all the values of
  1076. the data points, or I<GD::Graph> will die with a message.
  1077.  
  1078. For bar and area graphs, the range (y_min_value..y_max_value) has to
  1079. include 0. If it doesn't, the values will be adapted before attempting
  1080. to draw the graph.
  1081.  
  1082. Default: Computed from data sets.
  1083.  
  1084. =item axis_space
  1085.  
  1086. This space will be left blank between the axes and the tick value text.
  1087. Default: 4.
  1088.  
  1089. =item text_space
  1090.  
  1091. This space will be left open between text elements and the graph (text
  1092. elements are title and axis labels.
  1093.  
  1094. Default: 8.
  1095.  
  1096. =item cumulate
  1097.  
  1098. If this attribute is set to a true value, the data sets will be
  1099. cumulated. This means that they will be stacked on top of each other. A
  1100. side effect of this is that C<overwrite> will be set to a true value.
  1101.  
  1102. Notes: This only works for bar and area charts at the moment.
  1103.  
  1104. If you have negative values in your data sets, setting this option might
  1105. produce odd results. Of course, the graph itself would be quite
  1106. meaningless.
  1107.  
  1108. =item overwrite
  1109.  
  1110. If set to 0, bars of different data sets will be drawn next to each
  1111. other. If set to 1, they will be drawn in front of each other.
  1112. Default: 0.
  1113.  
  1114. Note: Setting overwrite to 2 to produce cumulative sets is deprecated,
  1115. and may disappear in future versions of GD::Graph.
  1116. Instead see the C<cumulate> attribute.
  1117.  
  1118. =item correct_width
  1119.  
  1120. If this is set to a true value and C<x_tick_number> is false, then the
  1121. width of the graph (or the height for rotated graphs like
  1122. C<GD::Graph::hbar>) will be recalculated to make sure that each data
  1123. point is exactly an integer number of pixels wide. You probably never
  1124. want to fiddle with this.
  1125.  
  1126. When this value is true, you will need to make sure that the number of
  1127. data points is smaller than the number of pixels in the plotting area of
  1128. the chart. If you get errors saying that your horizontal size if too
  1129. small, you may need to manually switch this off, or consider using
  1130. something else than a bar type for your chart.
  1131.  
  1132. Default: 1 for bar, calculated at runtime for mixed charts, 0 for others.
  1133.  
  1134. =back
  1135.  
  1136. =head2 Plotting data point values with the data point
  1137.  
  1138. Sometimes you will want to plot the value of a data point or bar above
  1139. the data point for clarity. GD::Graph allows you to control this in a
  1140. generic manner, or even down to the single point.
  1141.  
  1142. =over 4
  1143.  
  1144. =item show_values
  1145.  
  1146. Set this to 1 to display the value of each data point above the point or
  1147. bar itself. No effort is being made to ensure that there is enough space
  1148. for the text.
  1149.  
  1150. Set this to a GD::Graph::Data object, or an array reference of the same
  1151. shape, with the same dimensions as your data object that you pass in to
  1152. the plot method. The reason for this option is that it allows you to
  1153. make a copy of your data set, and selectively set points to C<undef> to
  1154. disable plotting of them.
  1155.  
  1156.   my $data = GD::Graph::Data->new( 
  1157.     [ [ 'A', 'B', 'C' ], [ 1, 2, 3 ], [ 11, 12, 13 ] ]);
  1158.   my $values = $data->copy;
  1159.   $values->set_y(1, 1, undef);
  1160.   $values->set_y(2, 0, undef);
  1161.  
  1162.   $graph->set(show_values => $values);
  1163.   $graph->plot($data);
  1164.  
  1165. Default: 0.
  1166.  
  1167. =item values_vertical
  1168.  
  1169. If set to a true value, the values will be printed vertically, instead
  1170. of horizontally. This can be handy if the values are long numbers.
  1171. Default: 0.
  1172.  
  1173. =item values_space
  1174.  
  1175. Space to insert between the data point and the value to print.
  1176. Default: 4.
  1177.  
  1178. =item values_format
  1179.  
  1180. How to format the values for display. See y_number_format for more
  1181. information.
  1182. Default: undef.
  1183.  
  1184. =back
  1185.  
  1186. =head2 Options for graphs with a numerical X axis
  1187.  
  1188. First of all: GD::Graph does B<not> support numerical x axis the way it
  1189. should. Data for X axes should be equally spaced. That understood:
  1190. There is some support to make the printing of graphs with numerical X
  1191. axis values a bit better, thanks to Scott Prahl. If the option
  1192. C<x_tick_number> is set to a defined value, GD::Graph will attempt to
  1193. treat the X data as numerical.
  1194.  
  1195. Extra options are:
  1196.  
  1197. =over 4
  1198.  
  1199. =item x_tick_number
  1200.  
  1201. If set to I<'auto'>, GD::Graph will attempt to format the X axis in a
  1202. nice way, based on the actual X values. If set to a number, that's the
  1203. number of ticks you will get. If set to undef, GD::Graph will treat X
  1204. data as labels.
  1205. Default: undef.
  1206.  
  1207. =item x_min_value, x_max_value
  1208.  
  1209. The minimum and maximum value to use for the X axis.
  1210. Default: computed.
  1211.  
  1212. =item x_number_format
  1213.  
  1214. See y_number_format
  1215.  
  1216. =item x_label_skip
  1217.  
  1218. See y_label_skip
  1219.  
  1220. =back
  1221.  
  1222.  
  1223. =head2 Options for graphs with bars
  1224.  
  1225. =over 4
  1226.  
  1227. =item bar_width
  1228.  
  1229. The width of a bar in pixels. Also see C<bar_spacing>.  Use C<bar_width>
  1230. If you want to have fixed-width bars, no matter how wide the chart gets.
  1231. Default: as wide as possible, within the constraints of the chart size
  1232. and C<bar_spacing> setting.
  1233.  
  1234. =item bar_spacing
  1235.  
  1236. Number of pixels to leave open between bars. This works well in most
  1237. cases, but on some platforms, a value of 1 will be rounded off to 0.
  1238. Use C<bar_spacing> to get a fixed amount of space between bars, with
  1239. variable bar widths, depending on the width of the chart.  Note that if
  1240. C<bar_width> is also set, this setting will be ignored, and
  1241. automatically calculated.  Default: 0
  1242.  
  1243. =back
  1244.  
  1245. =head2 Options for graphs with lines
  1246.  
  1247. =over 4
  1248.  
  1249. =item line_types
  1250.  
  1251. Which line types to use for I<lines> and I<linespoints> graphs. This
  1252. should be a reference to an array of numbers:
  1253.  
  1254.     $graph->set( line_types => [3, 2, 4] );
  1255.  
  1256. Available line types are 1: solid, 2: dashed, 3: dotted, 4:
  1257. dot-dashed.
  1258.  
  1259. Default: [1] (always use solid)
  1260.  
  1261. =item line_type_scale
  1262.  
  1263. Controls the length of the dashes in the line types. default: 6.
  1264.  
  1265. =item line_width
  1266.  
  1267. The width of the line used in I<lines> and I<linespoints> graphs, in pixels.
  1268. Default: 1.
  1269.  
  1270. =item skip_undef
  1271.  
  1272. For all other axes graph types, the default behaviour is (by their
  1273. nature) to not draw a point when the Y value is C<undef>. For line
  1274. charts the point gets skipped as well, but the line is drawn between the
  1275. points n-1 to n+1 directly. If C<skip_undef> has a true value, there
  1276. will be a gap in the chart where a Y value is undefined.
  1277.  
  1278. Note that a line will not be drawn unless there are I<at least two>
  1279. consecutive data points exist that have a defined value. The following
  1280. data set will only plot a very short line towards the end if
  1281. C<skip_undef> is set:
  1282.  
  1283.   @data = (
  1284.     [ qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct ) ],
  1285.     [ 1, undef, 2, undef, 3, undef, 4, undef, 5, 6 ]
  1286.   );
  1287.  
  1288. This option is useful when you have a consecutive gap in your data, or
  1289. with linespoints charts. If you have data where you have intermittent
  1290. gaps, be careful when you use this.
  1291. Default value: 0
  1292.  
  1293. =back
  1294.  
  1295. =head2 Options for graphs with points
  1296.  
  1297. =over 4
  1298.  
  1299. =item markers
  1300.  
  1301. This controls the order of markers in I<points> and I<linespoints>
  1302. graphs.  This should be a reference to an array of numbers:
  1303.  
  1304.     $graph->set( markers => [3, 5, 6] );
  1305.  
  1306. Available markers are: 1: filled square, 2: open square, 3: horizontal
  1307. cross, 4: diagonal cross, 5: filled diamond, 6: open diamond, 7:
  1308. filled circle, 8: open circle, 9: horizontal line, 10: vertical line.
  1309. Note that the last two are not part of the default list.
  1310.  
  1311. Default: [1,2,3,4,5,6,7,8]
  1312.  
  1313. =item marker_size
  1314.  
  1315. The size of the markers used in I<points> and I<linespoints> graphs,
  1316. in pixels.  Default: 4.
  1317.  
  1318. =back
  1319.  
  1320. =head2 Options for mixed graphs
  1321.  
  1322. =over 4
  1323.  
  1324. =item types
  1325.  
  1326. A reference to an array with graph types, in the same order as the
  1327. data sets. Possible values are:
  1328.  
  1329.   $graph->set( types => [qw(lines bars points area linespoints)] );
  1330.   $graph->set( types => ['lines', undef, undef, 'bars'] );
  1331.  
  1332. values that are undefined or unknown will be set to C<default_type>.
  1333.  
  1334. Default: all set to C<default_type>
  1335.  
  1336. =item default_type
  1337.  
  1338. The type of graph to draw for data sets that either have no type set,
  1339. or that have an unknown type set.
  1340.  
  1341. Default: lines
  1342.  
  1343. =back
  1344.  
  1345. =head2 Graph legends (axestype graphs only)
  1346.  
  1347. At the moment legend support is minimal.
  1348.  
  1349. B<Methods>
  1350.  
  1351. =over 4
  1352.  
  1353. =item $graph-E<gt>set_legend(I<@legend_keys>);
  1354.  
  1355. Sets the keys for the legend. The elements of @legend_keys correspond
  1356. to the data sets as provided to I<plot()>.
  1357.  
  1358. If a key is I<undef> or an empty string, the legend entry will be skipped.
  1359.  
  1360. =item $graph-E<gt>set_legend_font(I<font name>);
  1361.  
  1362. Sets the font for the legend text (see L<"FONTS">).
  1363. Default: GD::gdTinyFont.
  1364.  
  1365. =back
  1366.  
  1367. B<Options>
  1368.  
  1369. =over 4
  1370.  
  1371. =item legend_placement
  1372.  
  1373. Where to put the legend. This should be a two letter key of the form:
  1374. 'B[LCR]|R[TCB]'. The first letter indicates the placement (I<B>ottom or
  1375. I<R>ight), and the second letter the alignment (I<L>eft,
  1376. I<R>ight, I<C>enter, I<T>op, or I<B>ottom).
  1377. Default: 'BC'
  1378.  
  1379. If the legend is placed at the bottom, some calculations will be made
  1380. to ensure that there is some 'intelligent' wrapping going on. if the
  1381. legend is placed at the right, all entries will be placed below each
  1382. other.
  1383.  
  1384. =item legend_spacing
  1385.  
  1386. The number of pixels to place around a legend item, and between a
  1387. legend 'marker' and the text.
  1388. Default: 4
  1389.  
  1390. =item legend_marker_width, legend_marker_height
  1391.  
  1392. The width and height of a legend 'marker' in pixels.
  1393. Defaults: 12, 8
  1394.  
  1395. =item lg_cols
  1396.  
  1397. If you, for some reason, need to force the legend at the bottom to
  1398. have a specific number of columns, you can use this.
  1399. Default: computed
  1400.  
  1401. =back
  1402.  
  1403.  
  1404. =head2 Options for pie graphs
  1405.  
  1406. =over 4
  1407.  
  1408. =item 3d
  1409.  
  1410. If set to a true value, the pie chart will be drawn with a 3d look.
  1411. Default: 1.
  1412.  
  1413. =item pie_height
  1414.  
  1415. The thickness of the pie when I<3d> is true.
  1416. Default: 0.1 x height.
  1417.  
  1418. =item start_angle
  1419.  
  1420. The angle at which the first data slice will be displayed, with 0 degrees
  1421. being "6 o'clock".
  1422. Default: 0.
  1423.  
  1424. =item suppress_angle
  1425.  
  1426. If a pie slice is smaller than this angle (in degrees), a label will not
  1427. be drawn on it. Default: 0.
  1428.  
  1429. =item label
  1430.  
  1431. Print this label below the pie. Default: undef.
  1432.  
  1433. =back
  1434.  
  1435. =head1 COLOURS
  1436.  
  1437. All references to colours in the options for this module have been
  1438. shortened to clr. The main reason for this was that I didn't want to
  1439. support two spellings for the same word ('colour' and 'color')
  1440.  
  1441. Wherever a colour is required, a colour name should be used from the
  1442. package L<GD::Graph::colour>. S<C<perldoc GD::Graph::colour>> should give
  1443. you the documentation for that module, containing all valid colour
  1444. names. I will probably change this to read the systems rgb.txt file if 
  1445. it is available.
  1446.  
  1447. =head1 FONTS
  1448.  
  1449. Depending on your version of GD, this accepts both GD builtin fonts or
  1450. the name of a TrueType font file. In the case of a TrueType font, you
  1451. must specify the font size. See L<GD::Text> for more details and other
  1452. things, since all font handling in GD::Graph is delegated to there.
  1453.  
  1454. Examples:
  1455.  
  1456.     $my_graph->set_title_font('/fonts/arial.ttf', 18);
  1457.     $my_graph->set_legend_font(gdTinyFont);
  1458.     $my_graph->set_legend_font(
  1459.         ['verdana', 'arial', gdMediumBoldFont], 12)
  1460.  
  1461. (The above discussion is based on GD::Text 0.65. Older versions have
  1462. more restrictive behaviour).
  1463.  
  1464. =head1 HOTSPOTS
  1465.  
  1466. I<Note that this is an experimental feature, and its interface may, and
  1467. likely will, change in the future. It currently does not work for area
  1468. charts or pie charts.>
  1469.  
  1470. GD::Graph keeps an internal set of coordinates for each data point and
  1471. for certain features of a chart, like the title and axis labels. This
  1472. specification is very similar to the HTML image map specification, and
  1473. in fact exists mainly for that purpose. You can get at these hotspots
  1474. with the C<get_hotspot> method for data point, and
  1475. C<get_feature_coordinates> for the chart features. 
  1476.  
  1477. The <get_hotspot> method accepts two optional arguments, the number of
  1478. the dataset you're interested in, and the number of the point in that
  1479. dataset you're interested in. When called with two arguments, the
  1480. method returns a list of one of the following forms:
  1481.  
  1482.   'rect', x1, y1, x2, y2
  1483.   'poly', x1, y1, x2, y2, x3, y3, ....
  1484.   'line', xs, ys, xe, ye, width
  1485.  
  1486. The parameters for C<rect> are the coordinates of the corners of the
  1487. rectangle, the parameters for C<poly> are the coordinates of the
  1488. vertices of the polygon, and the parameters for the C<line> are the
  1489. coordinates for the start and end point, and the line width.  It should
  1490. be possible to almost directly translate these lists into HTML image map
  1491. specifications.
  1492.  
  1493. If the second argument to C<get_hotspot> is omitted, a list of
  1494. references to arrays will be returned. This list represents all the
  1495. points in the dataset specified, and each array referred to is of the
  1496. form outlined above.
  1497.  
  1498.   ['rect', x1, y1, x2, y2 ], ['rect', x1, y1, x2, y2], ...
  1499.  
  1500. if both arguments to C<get_hotspot> are omitted, the list that comes
  1501. back will contain references to arrays for each data set, which in
  1502. turn contain references to arrays for each point.
  1503.  
  1504.   [
  1505.     ['rect', x1, y1, x2, y2 ], ['rect', x1, y1, x2, y2], ...
  1506.   ],
  1507.   [
  1508.     ['line', xs, ys, xe, ye, w], ['line', xs, ys, xe, ye, w], ...
  1509.   ],...
  1510.  
  1511. The C<get_feature> method, when called with the name of a feature,
  1512. returns a single array reference with a type and coordinates as
  1513. described above. When called with no arguments, a hash reference is
  1514. returned with the keys being all the currently defined and set
  1515. features, and the values array references with the type and
  1516. coordinates for each of those features.
  1517.  
  1518. =head1 ERROR HANDLING
  1519.  
  1520. GD::Graph objects inherit from the GD::Graph::Error class (not the
  1521. other way around), so they behave in the same manner. The main feature
  1522. of that behaviour is that you have the error() method available to get
  1523. some information about what went wrong. The GD::Graph methods all
  1524. return undef if something went wrong, so you should be able to write
  1525. safe programs like this:
  1526.  
  1527.   my $graph = GD::Graph->new()        or die GD::Graph->error;
  1528.   $graph->set( %attributes )        or die $graph->error;
  1529.   $graph->plot($gdg_data)        or die $graph->error;
  1530.  
  1531. More advanced usage is possible, and there are some caveats with this
  1532. error handling, which are all explained in L<GD::Graph::Error>.
  1533.  
  1534. Unfortunately, it is almost impossible to gracefully recover from an
  1535. error in GD::Graph, so you really should get rid of the object, and
  1536. recreate it from scratch if you want to recover. For example, to
  1537. adjust the correct_width attribute if you get the error "Horizontal
  1538. size too small" or "Vertical size too small" (in the case of hbar),
  1539. you could do something like:
  1540.  
  1541.   sub plot_graph
  1542.   {
  1543.       my $data    = shift;
  1544.       my %attribs = @_;
  1545.       my $graph   = GD::Graph::bars->new()
  1546.                               or die GD::Graph->error;
  1547.       $graph->set(%attribs)     or die $graph->error;
  1548.       $graph->plot($data)       or die $graph->error;
  1549.   }
  1550.   
  1551.   my $gd;
  1552.   eval { $gd = plot_graph(\@data, %attribs) };
  1553.   if ($@)
  1554.   {
  1555.       die $@ unless $@ =~ /size too small/;
  1556.       $gd = plot_graph(\@data, %attribs, correct_width => 0);
  1557.   }
  1558.  
  1559. Of course, you could also adjust the width this way, and you can check
  1560. for other errors.
  1561.  
  1562. =head1 NOTES
  1563.  
  1564. As with all Modules for Perl: Please stick to using the interface. If
  1565. you try to fiddle too much with knowledge of the internals of this
  1566. module, you could get burned. I may change them at any time.
  1567.  
  1568. =head1 BUGS
  1569.  
  1570. GD::Graph objects cannot be reused. To create a new plot, you have to
  1571. create a new GD::Graph object.
  1572.  
  1573. Rotated charts (ones with the X axis on the left) can currently only be
  1574. created for bars. With a little work, this will work for all others as
  1575. well. Please, be patient :)
  1576.  
  1577. =head1 AUTHOR
  1578.  
  1579. Martien Verbruggen E<lt>mgjv@tradingpost.com.auE<gt>
  1580.  
  1581. =head2 Copyright
  1582.  
  1583. GIFgraph: Copyright (c) 1995-1999 Martien Verbruggen.
  1584. Chart::PNGgraph: Copyright (c) 1999 Steve Bonds.
  1585. GD::Graph: Copyright (c) 1999 Martien Verbruggen.
  1586.  
  1587. All rights reserved. This package is free software; you can redistribute
  1588. it and/or modify it under the same terms as Perl itself.
  1589.  
  1590. =head2 Acknowledgements
  1591.  
  1592. Thanks to Steve Bonds for releasing Chart::PNGgraph, and keeping the
  1593. code alive when GD reached version 1.20, and I didn't have time to do
  1594. something about it.
  1595.  
  1596. Thanks to the following people for contributing code, or sending me
  1597. fixes:
  1598. Dave Belcher,
  1599. Steve Bonds,
  1600. Mike Bremford,
  1601. Damon Brodie,
  1602. Gary Deschaines,
  1603. brian d foy,
  1604. Edwin Hildebrand,
  1605. Ari Jolma,
  1606. Tim Meadowcroft,
  1607. Honza Pazdziora,
  1608. Scott Prahl,
  1609. Ben Tilly,
  1610. Vegard Vesterheim,
  1611. Jeremy Wadsack.
  1612.  
  1613. And some people whose real name I don't know, and whose email address
  1614. I'd rather not publicise without their consent.
  1615.  
  1616. =head1 SEE ALSO
  1617.  
  1618. L<GD::Graph::FAQ>, 
  1619. L<GD::Graph::Data>, 
  1620. L<GD::Graph::Error>,
  1621. L<GD::Graph::colour>
  1622.  
  1623.