lib/SVG/Sparkline.pm

filter: Statementstmt 98.6% 142 / 144 Branchbran 96.1% 50 / 52 Conditioncond 88.8% 8 / 9 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 94.1% 32 / 34 Podpod 100% 4 / 4 totaltotal 96.3% 240 / 249 CC 32 SCAR 13.5
CC
32
Cov
98%
new
8.0
_validate_mark_param
7.0
_is_color
6.0
CRAP 3.9
« lib/SVG/Sparkline/RangeBar.pm ↑ Summary lib/SVG/Sparkline/RangeArea.pm »
1 package SVG::Sparkline;
2  
3 31 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use SVG;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 30 ▶use overload '""' => \&to_string;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 30 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 30 our $VERSION = 1.11;
12  
13 30 my %valid_parms = map { $_ => 1 } qw(
14 -allns color -sized
15 height width xscale yscale pady padx
16 color bgcolor mark values
17 );
18  
19 sub new
20 {
21 225 ▶ my ($class, $type, $args) = @_;
Subroutine new: called CC=8 SCAR=20.8
Pod documented
22 225 ▶ croak "No Sparkline type specified.\n" unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 1 224 225
23 # Use eval to load plugin.
24 224 ▶ eval "use SVG::Sparkline::$type;"; ## no critic (ProhibitStringyEval)
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
25 224 ▶ croak "Unrecognized Sparkline type '$type'.\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 223 224
26 223 ▶ croak "Missing arguments hash.\n" unless defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 1 222 223
27 222 ▶ croak "Arguments not supplied as a hash reference.\n" unless 'HASH' eq ref $args;
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $args 1 221 222
28 221 _no_unrecognized_parameters( $type, $args );
29  
30 my $self = bless {
31 -allns => 0,
32 color => '#000',
33 -sized => 1,
34 221 %{$args},
35 }, $class;
36  
37 221 $self->_validate_pos_param( 'height', 12 );
38 219 $self->_validate_pos_param( 'width', 0 );
39 217 $self->_validate_pos_param( 'xscale' );
40 215 $self->_validate_pos_param( 'yscale' );
41 215 $self->_validate_nonneg_param( 'pady', 1 );
42 214 $self->_validate_nonneg_param( 'padx', 0 );
43 213 $self->_validate_mark_param();
44 203 foreach my $arg (qw/color bgcolor/)
45 {
46 405 ▶ next unless exists $self->{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{$arg} 193 212 405
47 croak "The value of $arg is not a valid color.\n"
48 212 ▶ unless _is_color( $self->{$arg} );
Branch: 1 branch
BranchTrueFalseTotal
unless _is_color($self->{$arg}) 2 210 212
49 }
50  
51 201 $self->{xoff} = -$self->{padx};
52 201 $self->_make( $type );
53  
54 170 return $self;
55 }
56  
57 0 ▶sub get_height { return $_[0]->{height}; }
Subroutine get_height: not called CC=1 SCAR=6.9
Pod documented
58 0 ▶sub get_width { return $_[0]->{width}; }
Subroutine get_width: not called CC=1 SCAR=6.9
Pod documented
59  
60 sub to_string
61 {
62 189 ▶ my ($self) = @_;
Subroutine to_string: called CC=3 SCAR=11.0
Pod documented
63 189 my $str = $self->{_SVG}->xmlify();
64 # Cleanup
65 189 ▶ $str =~ s/ xmlns:(?:svg|xlink)="[^"]+"//g unless $self->{'-allns'};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"-allns"} 187 2 189
66 189 ▶ unless( $self->{'-sized'} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"-sized"}) 6 183 189
67 {
68 # If I try to keep them from being created, default '100%' values
69 # show up instead.
70 6 $str =~ s/(<svg[^>]*) height="[^"]+"/$1/;
71 6 $str =~ s/(<svg[^>]*) width="[^"]+"/$1/;
72 }
73 189 return $str;
74 }
75  
76 sub _make
77 {
78 201 ▶ my ($self, $type) = @_;
Subroutine _make: called CC=1 SCAR=0.0
79 201 $self->{_SVG} = "SVG::Sparkline::$type"->make( $self );
80 170 return;
81 }
82  
83 sub _no_unrecognized_parameters {
84 221 ▶ my ( $type, $args ) = @_;
Subroutine _no_unrecognized_parameters: called CC=4 SCAR=14.1
85 221 my $class = "SVG::Sparkline::$type";
86 221 foreach my $parm (keys %{$args}) {
87 croak "Parameter '$parm' not recognized for '$type'\n"
88 410 ▶ unless exists $valid_parms{$parm} || $class->valid_param( $parm );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid_parms{$parm} or $class->valid_param($parm) 0 410 410
Condition66%2/3
exists $valid_parms{$parm} or $class->valid_param($parm)
l!l&&r!l&&!r
388220
MC/DC0%0/2
exists $valid_parms{$parm} or $class->valid_param($parm)
exists $valid_parms{$parm} $class->valid_param($parm)
Truth table66%2/3
A or B
A= exists $valid_parms{$parm}B= $class->valid_param($parm)
ABresult
1X1
011
000
89 }
90 221 return;
91 }
92  
93 sub _validate_pos_param
94 {
95 872 ▶ my ($self, $name, $default) = @_;
Subroutine _validate_pos_param: called CC=5 SCAR=16.1
96 croak "'$name' must have a positive numeric value.\n"
97 872 ▶ if exists $self->{$name} && $self->{$name} <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$name} and $self->{$name} <= 0 6 866 872
Condition100%3/3
exists $self->{$name} and $self->{$name} <= 0
!ll&&!rl&&r
835316
MC/DC100%2/2
exists $self->{$name} and $self->{$name} <= 0
exists $self->{$name} $self->{$name} <= 0
Truth table100%3/3
A and B
A= exists $self->{$name}B= $self->{$name} <= 0
ABresult
0X0
100
111
98 866 ▶ return if exists $self->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$name} 31 835 866
99  
100 835 ▶ $self->{$name} = $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 410 425 835
101 835 return;
102 }
103  
104 sub _validate_nonneg_param
105 {
106 429 ▶ my ($self, $name, $default) = @_;
Subroutine _validate_nonneg_param: called CC=5 SCAR=16.1
107 croak "'$name' must be a non-negative numeric value.\n"
108 429 ▶ if exists $self->{$name} && $self->{$name} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$name} and $self->{$name} < 0 2 427 429
Condition100%3/3
exists $self->{$name} and $self->{$name} < 0
!ll&&!rl&&r
405222
MC/DC100%2/2
exists $self->{$name} and $self->{$name} < 0
exists $self->{$name} $self->{$name} < 0
Truth table100%3/3
A and B
A= exists $self->{$name}B= $self->{$name} < 0
ABresult
0X0
100
111
109 427 ▶ return if exists $self->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$name} 22 405 427
110  
111 405 ▶ $self->{$name} = $default if defined $default;
Branch: 1 branch
BranchTrueFalseTotal
if defined $default 405 0 405
112 405 return;
113 }
114  
115 sub _validate_mark_param
116 {
117 213 ▶ my ($self) = @_;
Subroutine _validate_mark_param: called CC=7 SCAR=19.5
118  
119 213 ▶ return unless exists $self->{mark};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"mark"} 129 84 213
120  
121 croak "'mark' parameter must be an array reference.\n"
122 84 ▶ unless 'ARRAY' eq ref $self->{mark};
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $self->{"mark"} 1 83 84
123 croak "'mark' array parameter must have an even number of elements.\n"
124 83 ▶ unless 0 == (@{$self->{mark}}%2);
Branch: 1 branch
BranchTrueFalseTotal
unless 0 == @{$self->{"mark"};} % 2 1 82 83
125  
126 82 my @marks = @{$self->{mark}};
127 82 ▶ while(@marks)
Branch: 1 branch
BranchTrueFalseTotal
if (@marks) 84 74 158
128 {
129 84 my ($index, $color) = splice( @marks, 0, 2 );
130 84 ▶ croak "'$index' is not a valid mark index.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $index =~ /^(?:first|last|high|low|\d+)$/ 7 77 84
131 unless $index =~ /^(?:first|last|high|low|\d+)$/;
132 77 ▶ croak "'$color' is not a valid mark color.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _is_color($color) 1 76 77
133 unless _is_color( $color );
134 }
135 74 return;
136 }
137  
138 sub _is_color
139 {
140 314 ▶ my ($color) = @_;
Subroutine _is_color: called CC=6 SCAR=17.9
141 314 ▶ return 1 if $color =~ /^#[[:xdigit:]]{3}$/;
Branch: 1 branch
BranchTrueFalseTotal
if $color =~ /^#[[:xdigit:]]{3}$/ 213 101 314
142 101 ▶ return 1 if $color =~ /^#[[:xdigit:]]{6}$/;
Branch: 1 branch
BranchTrueFalseTotal
if $color =~ /^#[[:xdigit:]]{6}$/ 2 99 101
143 99 ▶ return 1 if $color =~ /^rgb\(\d+,\d+,\d+\)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $color =~ /^rgb\(\d+,\d+,\d+\)$/ 1 98 99
144 98 ▶ return 1 if $color =~ /^rgb\(\d+%,\d+%,\d+%\)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $color =~ /^rgb\(\d+%,\d+%,\d+%\)$/ 1 97 98
145 97 ▶ return 1 if $color =~ /^[[:alpha:]]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $color =~ /^[[:alpha:]]+$/ 79 18 97
146 18 return;
147 }
148  
149 30 1;
150  
151 __END__
« lib/SVG/Sparkline/RangeBar.pm ↑ Summary lib/SVG/Sparkline/RangeArea.pm »