File Coverage

blib/lib/Imager/Graph.pm
Criterion Covered Total %
statement 513 563 91.1
branch 226 342 66.0
condition 26 47 55.3
subroutine 93 97 95.8
pod 57 57 100.0
total 915 1106 82.7


line stmt bran cond sub pod time code
1             package Imager::Graph;
2             require 5.006;
3              
4             =head1 NAME
5              
6             Imager::Graph - Perl extension for producing Graphs using the Imager library.
7              
8             =head1 SYNOPSIS
9              
10             use Imager::Graph::Sub_class;
11             my $chart = Imager::Graph::Sub_class->new;
12             my $img = $chart->draw(data=> \@data, ...)
13             or die $chart->error;
14             $img->write(file => 'image.png');
15              
16             =head1 DESCRIPTION
17              
18             Imager::Graph provides style information to its base classes. It
19             defines the colors, text display information and fills based on both
20             built-in styles and modifications supplied by the user to the draw()
21             method.
22              
23             =over
24              
25             =cut
26              
27 15     15   81 use strict;
  15         23  
  15         671  
28 15     15   202 use vars qw($VERSION);
  15         42  
  15         705  
29 15     15   25505 use Imager qw(:handy);
  15         814604  
  15         141  
30 15     15   18428 use Imager::Fountain;
  15         45677  
  15         593  
31              
32             $VERSION = '0.11';
33              
34             # the maximum recursion depth in determining a color, fill or number
35 15     15   118 use constant MAX_DEPTH => 10;
  15         140  
  15         184222  
36              
37             my $NUM_RE = '(?:[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]\d+?)?)';
38              
39             =item new
40              
41             This is a simple constructor. No parameters required.
42              
43             =cut
44              
45             sub new {
46 26     26 1 38264 bless {}, $_[0];
47             }
48              
49             =item set_graph_size($size)
50              
51             Sets the size of the graph (in pixels) within the image. The size of the image defaults to 1.5 * $graph_size.
52              
53             =cut
54              
55             sub set_graph_size {
56 2     2 1 24 $_[0]->{'custom_style'}->{'size'} = $_[1];
57             }
58              
59             =item set_image_width($width)
60              
61             Sets the width of the image in pixels.
62              
63             =cut
64              
65             sub set_image_width {
66 3     3 1 46 $_[0]->{'custom_style'}->{'width'} = $_[1];
67             }
68              
69             =item set_image_height($height)
70              
71             Sets the height of the image in pixels.
72              
73             =cut
74              
75             sub set_image_height {
76 3     3 1 33 $_[0]->{'custom_style'}->{'height'} = $_[1];
77             }
78              
79             =item add_data_series([8, 6, 7, 5, 3, 0, 9], 'Series Name');
80              
81             Adds a data series to the graph. For L, only one data series can be added.
82              
83             =cut
84              
85             sub add_data_series {
86 5     5 1 50 my $self = shift;
87 5         13 my $data_ref = shift;
88 5         15 my $series_name = shift;
89              
90 5   50     81 my $graph_data = $self->{'graph_data'} || [];
91              
92 5         35 push @$graph_data, { data => $data_ref, series_name => $series_name };
93 5 100       22 if (defined $series_name) {
94 3         7 push @{$self->{'labels'}}, $series_name;
  3         12  
95             }
96              
97 5         16 $self->{'graph_data'} = $graph_data;
98 5         15 return;
99             }
100              
101             sub _get_data_series {
102 315     315   553 my ($self, $opts) = @_;
103              
104             # return the data supplied to draw() if any.
105 315 100       831 if ($opts->{data}) {
106             # one or multiple series?
107 19         40 my $data = $opts->{data};
108 19 50 66     173 if (@$data && ref $data->[0] && ref $data->[0] =~ /ARRAY/) {
      33        
109 0         0 return $data;
110             }
111             else {
112 19         107 return [ { data => $data } ];
113             }
114             }
115              
116 296         1498 return $self->{'graph_data'};
117             }
118              
119             =item set_labels(['label1', 'label2' ... ])
120              
121             Labels the specific data points. For line/bar graphs, this is the x-axis. For pie graphs, it is the label for the wedges.
122              
123             =cut
124              
125             sub set_labels {
126 20     20 1 217 $_[0]->{'labels'} = $_[1];
127             }
128              
129             sub _get_labels {
130 86     86   155 my ($self, $opts) = @_;
131              
132 86 100       375 $opts->{labels}
133             and return $opts->{labels};
134              
135 64         281 return $_[0]->{'labels'}
136             }
137              
138             =item set_title($title)
139              
140             Sets the title of the graph. Requires setting a font.
141              
142             =cut
143              
144             sub set_title {
145 2     2 1 23 $_[0]->{'custom_style'}->{'title'}->{'text'} = $_[1];
146             }
147              
148             =item set_font($font)
149              
150             Sets the font to use for text. Takes an L object.
151              
152             =cut
153              
154             sub set_font {
155 11     11 1 1319 $_[0]->{'custom_style'}->{'font'} = $_[1];
156             }
157              
158             =item set_style($style_name)
159              
160             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.
161              
162             =cut
163              
164             sub set_style {
165 5     5 1 52 $_[0]->{'style'} = $_[1];
166             }
167              
168             sub _get_style {
169 39     39   86 my ($self, $opts) = @_;
170              
171 39 100       150 $opts->{style}
172             and return $opts->{style};
173              
174 34         96 return $self->{'style'};
175             }
176              
177             =item error
178              
179             Returns an error message. Only valid if the draw() method returns false.
180              
181             =cut
182              
183             sub error {
184 10     10 1 5729 $_[0]->{_errstr};
185             }
186              
187             =item draw
188              
189             Creates a new image, draws the chart onto that image and returns it.
190              
191             Optionally, instead of using the api methods to configure your chart,
192             you can supply a C parameter in the format
193             required by that particular graph, and if your graph will use any
194             text, a C parameter
195              
196             You can also supply many different parameters which control the way
197             the graph looks. These are supplied as keyword, value pairs, where
198             the value can be a hashref containing sub values.
199              
200             The C