Graph.pm

filter: Statementstmt 52.9% 62 / 117 Branchbran 27.7% 15 / 54 Conditioncond 20.0% 3 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 66.6% 10 / 15 Podpod 75.0% 3 / 4 totaltotal 43.4% 93 / 214 CC 33 SCAR 35.9
CC
33
Cov
43%
_range_calc
110.0
burn
55.6
_is_valid_config
12.0
CRAP 36.1
« Graph/TimeSeries.pm ↑ Summary Graph/BarHorizontal.pm »
1 package SVG::TT::Graph;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use vars qw($VERSION $AUTOLOAD);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 $VERSION = '0.12';
10  
11 # set up TT object
12 3 my %config = (
13 POST_CHOMP => 1,
14 INCLUDE_PATH => '/',
15 );
16 3 my $tt = Template->new( \%config );
17  
18 =head1 NAME
19  
20 SVG::TT::Graph - Base object for generating SVG Graphs
21  
22 =head1 SYNOPSIS
23  
24 package SVG::TT::Graph::GRAPH_TYPE
25 use SVG::TT::Graph;
26 use base qw(SVG::TT::Graph);
27  
28 sub _set_defaults {
29 my $self = shift;
30  
31 my %default = (
32 'keys' => 'value',
33 );
34 while( my ($key,$value) = each %default ) {
35 $self->{config}->{$key} = $value;
36 }
37 }
38  
39 sub get_template {
40 my $self = shift;
41 # read in template
42 my $template = 'set the template';
43 return $template;
44 }
45
46 # optional - called when object is created
47 sub _init {
48 my $self = shift;
49 # any testing you want to do.
50
51 }
52
53 1;
54
55 In your script...
56
57 use SVG::TT::Graph::GRAPH_TYPE;
58
59 my $width = '500',
60 my $heigh = '300',
61 my @fields = qw(field_1 field_2 field_3);
62
63 my $graph = SVG::TT::Graph::GRAPH_TYPE->new({
64 # Required for some graph types
65 'fields' => \@fields,
66 # .. other config options
67 'height' => '500',
68 });
69
70 my @data = qw(23 56 32);
71 $graph->add_data({
72 'data' => \@data,
73 'title' => 'Sales 2002',
74 });
75
76 # find a config options value
77 my $config_value = $graph->config_option();
78 # set a config option value
79 $graph->config_option($config_value);
80
81 # All graphs support SVGZ (compressed SVG) if
82 # Compress::Zlib is available.
83 # either 'compress' => 1 config option, or
84 $graph->compress(1);
85
86 print "Content-type: image/svg+xml\r\n";
87 print $graph->burn();
88  
89 =head1 DESCRIPTION
90  
91 This package should be used as a base for creating SVG graphs.
92  
93 See SVG::TT::Graph::Line for an example.
94  
95 =cut
96  
97 sub new {
98 11 ▶ my ($proto,$conf) = @_;
Subroutine new: called CC=5 SCAR=16.7
Pod undocumented
99 11 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0110
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
100 11 my $self = {};
101  
102 11 bless($self, $class);
103  
104 11 ▶ if($self->can('_set_defaults')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('_set_defaults')) { } 11 0 11
105 # Populate with local defaults
106 11 $self->_set_defaults();
107 } else {
108 0 croak "$class should have a _set_defaults method";
109 }
110
111 # overwrite defaults with user options
112 11 ▶ while( my ($key,$value) = each %{$conf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$conf;}) 6 11 17
113 6 $self->{config}->{$key} = $value;
114 }
115  
116 # Allow the inheriting modules to do checks
117 11 ▶ if($self->can('_init')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("_init")) 11 0 11
118 11 $self->_init();
119 }
120
121 6 return $self;
122 }
123  
124 =head1 METHODS
125  
126 =head2 add_data()
127  
128 my @data_sales_02 = qw(12 45 21);
129  
130 $graph->add_data({
131 'data' => \@data_sales_02,
132 'title' => 'Sales 2002',
133 });
134  
135 This method allows you do add data to the graph object.
136 It can be called several times to add more data sets in.
137  
138 =cut
139
140 sub add_data {
141 10 ▶ my ($self, $conf) = @_;
Subroutine add_data: called CC=7 SCAR=20.5
Pod documented
142 # create an array
143 10 ▶ unless(defined $self->{'data'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"data"}) 5 5 10
144 5 my @data;
145 5 $self->{'data'} = \@data;
146 }
147
148 croak 'no fields array ref'
149 unless defined $self->{'config'}->{'fields'}
150 10 ▶ && ref($self->{'config'}->{'fields'}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"config"}{"fields"} and ref $self->{"config"}{"fields"} eq "ARRAY" 0 10 10
Condition33%1/3
defined $self->{"config"}{"fields"} and ref $self->{"config"}{"fields"} eq "ARRAY"
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $self->{"config"}{"fields"} and ref $self->{"config"}{"fields"} eq "ARRAY"
defined $self->{"config"}{"fields"} ref $self->{"config"}{"fields"} eq "ARRAY"
Truth table33%1/3
A and B
A= defined $self->{"config"}{"fields"}B= ref $self->{"config"}{"fields"} eq "ARRAY"
ABresult
0X0
100
111
151  
152 10 ▶ if(defined $conf->{'data'} && ref($conf->{'data'}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"data"} and ref $conf->{"data"} eq "ARRAY") 10 0 10
Condition33%1/3
defined $conf->{"data"} and ref $conf->{"data"} eq "ARRAY"
!ll&&!rl&&r
0010
MC/DC0%0/2
defined $conf->{"data"} and ref $conf->{"data"} eq "ARRAY"
defined $conf->{"data"} ref $conf->{"data"} eq "ARRAY"
Truth table33%1/3
A and B
A= defined $conf->{"data"}B= ref $conf->{"data"} eq "ARRAY"
ABresult
0X0
100
111
153 10 my %new_data;
154 10 @new_data{ map { s/&/&/; $_ } @{$self->{'config'}->{'fields'}}} = @{$conf->{'data'}};
155 10 my %store = (
156 'data' => \%new_data,
157 );
158 10 ▶ $store{'title'} = $conf->{'title'} if defined $conf->{'title'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $conf->{"title"} 10 0 10
159 10 push (@{$self->{'data'}},\%store);
160 10 return 1;
161 }
162 0 return undef;
163 }
164  
165 =head2 clear_data()
166  
167 my $graph->clear_data();
168  
169 This method removes all data from the object so that you can
170 reuse it to create a new graph but with the same config options.
171  
172 =cut
173  
174 sub clear_data {
175 5 ▶ my $self = shift;
Subroutine clear_data: called CC=1 SCAR=0.0
Pod documented
176 5 my @data;
177 5 $self->{'data'} = \@data;
178 }
179  
180 =head2 burn()
181  
182 print $graph->burn();
183  
184 This method processes the template with the data and
185 config which has been set and returns the resulting SVG.
186  
187 This method will croak unless at least one data set has
188 been added to the graph object.
189  
190 =cut
191  
192 sub burn {
193 5 ▶ my $self = shift;
Subroutine burn: called CC=8 SCAR=40.2
Pod documented
194
195 # Check we have at least one data value
196 croak "No data available"
197 5 ▶ unless scalar(@{$self->{'data'}}) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->{"data"};} > 0 0 0 0
198
199 # perform any calculations prior to burn
200 0 ▶ $self->calculations() if $self->can('calculations');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("calculations") 0 0 0
201  
202 0 ▶ croak ref($self) . ' must have a get_template method.'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can("get_template") 0 0 0
203 unless $self->can('get_template');
204
205 0 my $template = $self->get_template();
206
207 my %vals = (
208 'data' => $self->{'data'}, # the data
209 'config' => $self->{'config'}, # the configuration
210 0 'calc' => $self->{'calc'}, # the calculated values
211 'sin' => \&_sin_it,
212 'cos' => \&_cos_it,
213 );
214
215 # euu - hack!! - maybe should just be a method
216 0 $self->{sin} = \&_sin_it;
217 0 $self->{cos} = \&_cos_it;
218
219 0 my $file;
220 0 my $template_responce = $tt->process( \$template, \%vals, \$file );
221  
222 0 ▶ if($tt->error()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tt->error) 0 0 0
223 # require Data::Dumper;
224 0 ▶ croak "Template error: " . $tt->error . "\n" if $tt->error;
Branch: 1 branch
BranchTrueFalseTotal
if $tt->error 0 0 0
225 }
226
227 # compress if required
228 0 ▶ if ($self->{config}->{compress}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"compress"}) 0 0 0
229 0 ▶ if (eval "require Compress::Zlib") {
Branch: 1 branch
BranchTrueFalseTotal
if (eval 'require Compress::Zlib') { } 0 0 0
230 0 return Compress::Zlib::memGzip($file) ;
231
232 } else {
233 0 $file .= "<!-- Compress::Zlib not available for SVGZ:$@ -->";
234 }
235 }
236
237 0 return $file;
238 }
239  
240 sub _sin_it {
241 0 ▶ return sin(shift);
Subroutine _sin_it: not called CC=1 SCAR=6.9
242 }
243  
244 sub _cos_it {
245 0 ▶ return cos(shift);
Subroutine _cos_it: not called CC=1 SCAR=6.9
246 }
247  
248 # Calculate a scaling range and divisions to be aesthetically pleasing
249 # Parameters:
250 # value range
251 # Returns
252 # (revised range, division size, division precision)
253 sub _range_calc () {
254 0 ▶ my $self = shift;
Subroutine _range_calc: not called CC=10 SCAR=47.0
255 0 my $range = shift;
256  
257 0 my ($max,$division);
258 0 my $count = 0;
259 0 my $value = $range;
260
261 0 ▶ if ($value == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value == 0) 0 0 0
262 # Can't do much really
263 0 $division = 0.2;
264 0 $max = 1;
265 0 return ($max,$division,1);
266 }
267
268 0 ▶ if ($value < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 1) { } 0 0 0
269 0 ▶ while ($value < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 1) 0 0 0
270 0 $value *= 10;
271 0 $count++;
272 }
273 0 $division = 1;
274 0 ▶ while ($count--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count--) 0 0 0
275 0 $division /= 10;
276 }
277 0 $max = ceil($range / $division) * $division;
278 }
279 else {
280 0 ▶ while ($value > 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value > 10) 0 0 0
281 0 $value /= 10;
282 0 $count++;
283 }
284 0 $division = 1;
285 0 ▶ while ($count--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count--) 0 0 0
286 0 $division *= 10;
287 }
288 0 $max = ceil($range / $division) * $division;
289 }
290
291 0 ▶ if (int($max / $division) <= 2) {
Branch: 2 branches
BranchTrueFalseTotal
if (int $max / $division <= 2) { } 0 0 0
elsif (int $max / $division <= 5) { } 0 0 0
292 0 $division /= 5;
293 0 $max = ceil($range / $division) * $division;
294 }
295 elsif (int($max / $division) <= 5) {
296 0 $division /= 2;
297 0 $max = ceil($range / $division) * $division;
298 }
299
300 0 ▶ if ($division >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($division >= 1) { } 0 0 0
301 0 $count = 0;
302 }
303 else {
304 0 $count = length($division) - 2;
305 }
306  
307 0 return ($max,$division,$count);
308 }
309  
310 # Returns true if config value exists, is defined and not ''
311 sub _is_valid_config() {
312 0 ▶ my ($self,$name) = @_;
Subroutine _is_valid_config: not called CC=3 SCAR=24.8
313 0 ▶ return ((exists $self->{config}->{$name}) && (defined $self->{config}->{$name}) && ($self->{config}->{$name} ne ''));
Condition0%0/6
exists $self->{'config'}{$name} && defined $self->{'config'}{$name} && $self->{'config'}{$name} ne ''
!ll&&!rl&&r
000
exists $self->{"config"}{$name} and defined $self->{"config"}{$name}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $self->{'config'}{$name} && defined $self->{'config'}{$name} && $self->{'config'}{$name} ne ''
exists $self->{"config"}{$name} defined $self->{"config"}{$name} $self->{'config'}{$name} ne ''
Truth table0%0/4
A and B && C
A= exists $self->{"config"}{$name}B= defined $self->{"config"}{$name}C= $self->{'config'}{$name} ne ''
ABCresult
0XX0
10X0
1100
1111
314 }
315  
316 =head2 config methods
317  
318 my $value = $graph->method();
319 $graph->method($value);
320  
321 This object provides autoload methods for all config
322 options defined in the _set_default method within the
323 inheriting object.
324  
325 See the SVG::TT::Graph::GRAPH_TYPE documentation for a list.
326  
327 =cut
328  
329 ## AUTOLOAD FOR CONFIG editing
330  
331 sub AUTOLOAD {
332 4 ▶ my $name = $AUTOLOAD;
Subroutine AUTOLOAD: called CC=5 SCAR=16.4
333 4 $name =~ s/.*://;
334  
335 4 ▶ croak "No object supplied" unless $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] 0 4 4
336 4 ▶ if(defined $_[0]->{'config'}->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]{'config'}{$name}) { } 3 1 4
337 3 ▶ if(defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) 1 2 3
338 # set the value
339 1 $_[0]->{'config'}->{$name} = $_[1];
340 }
341 3 ▶ return $_[0]->{'config'}->{$name} if defined $_[0]->{'config'}->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[0]{"config"}{$name} 3 0 3
342 0 return undef;
343 } else {
344 1 croak "Method: $name can not be used with " . ref($_[0]);
345 }
346 }
347  
348 # As we have AUTOLOAD we need this
349 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
350 }
351  
352 3 1;
353 __END__
354  
355 =head1 EXAMPLES
356  
357 For examples look at the project home page http://leo.cuckoo.org/projects/SVG-TT-Graph/
358  
359 =head1 EXPORT
360  
361 None by default.
362  
363 =head1 ACKNOWLEDGEMENTS
364  
365 Thanks to Foxtons for letting us put this on CPAN.
366 Todd Caine for heads up on reparsing the template (but not using atm)
367 David Meibusch for TimeSeries and a load of other ideas
368 Thanks for all the patches supplied by Andrew Ruthven and others
369  
370 =head1 AUTHOR
371  
372 Leo Lapworth (LLAP@cuckoo.org) and Stephen Morgan (TT and SVG)
373  
374 =head1 SEE ALSO
375  
376 L<SVG::TT::Graph::Line>,
377 L<SVG::TT::Graph::Bar>,
378 L<SVG::TT::Graph::BarHorizontal>,
379 L<SVG::TT::Graph::BarLine>,
380 L<SVG::TT::Graph::Pie>,
381 L<SVG::TT::Graph::TimeSeries>,
382 L<Compress::Zlib>,
383  
384 =head1 COPYRIGHT
385  
386 Copyright (C) 2003, Leo Lapworth
387  
388 This module is free software; you can redistribute it or
389 modify it under the same terms as Perl itself.
390  
391 =cut
« Graph/TimeSeries.pm ↑ Summary Graph/BarHorizontal.pm »