lib/SVG/Sparkline/Utils.pm

filter: Statementstmt 98.5% 138 / 140 Branchbran 90.6% 58 / 64 Conditioncond 84.6% 11 / 13 MC/DCmcdc 75.0% 6 / 8 Subroutinesub 100% 16 / 16 Podpod 100% 10 / 10 totaltotal 95.2% 239 / 251 CC 43 SCAR 19.6
CC
43
Cov
95%
range_mark_to_index
11.1
mark_to_index
11.1
_summarize_xy_pairs
10.1
CRAP 7.1
« script/sparkline.pl ↑ Summary lib/SVG/Sparkline/Whisker.pm »
1 package SVG::Sparkline::Utils;
2  
3 30 ▶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 List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use SVG;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 30 our $VERSION = 1.11;
10  
11 sub format_f
12 {
13 3033 ▶ my $val = sprintf '%.02f', $_[0];
Subroutine format_f: called CC=2 SCAR=6.9
Pod documented
14 3033 $val =~ s/0$//;
15 3033 $val =~ s/\.0$//;
16 3033 ▶ $val = 0 if $val eq '-0';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "-0" 30 3003 3033
17 3033 return $val;
18 }
19  
20 sub calculate_xscale
21 {
22 76 ▶ my ($args, $xrange) = @_;
Subroutine calculate_xscale: called CC=3 SCAR=11.0
Pod documented
23  
24 76 ▶ if( $args->{width} )
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'width'}) { } 8 68 76
25 {
26 8 my $dwidth = $args->{width} - 2*$args->{padx};
27 8 $args->{xscale} = ($dwidth-1) / $xrange;
28 }
29 else
30 {
31 68 ▶ $args->{xscale} ||= 2;
Condition100%2/2
$args->{"xscale"} ||= 2
l!l
563
MC/DC100%1/1
$args->{"xscale"} ||= 2
$args->{"xscale"}
Truth table100%2/2
A
A= $args->{"xscale"}
Aresult
11
00
32 68 my $dwidth = @{$args->{values}} * $args->{xscale} - 1;
33 68 $args->{width} = $dwidth + 2*$args->{padx};
34 }
35 76 return;
36 }
37  
38 sub calculate_yscale_and_offset
39 {
40 76 ▶ my ($args, $yrange, $offset) = @_;
Subroutine calculate_yscale_and_offset: called CC=2 SCAR=6.9
Pod documented
41  
42 # If the data values are all 0, default the range. Any value would be
43 # usable. So, I just pick 1 arbitrarily.
44 76 ▶ $yrange ||= 1;
Condition100%2/2
$yrange ||= 1
l!l
742
MC/DC100%1/1
$yrange ||= 1
$yrange
Truth table100%2/2
A
A= $yrange
Aresult
11
00
45 76 my $height = $args->{height} - 2*$args->{pady};
46 76 $args->{yscale} = -$height / $yrange;
47 76 my $baseline = format_f( -$args->{yscale} * $offset );
48  
49 76 $args->{yoff} = -($baseline+$height+$args->{pady});
50  
51 76 return;
52 }
53  
54 sub xypairs_to_points_str
55 {
56 76 ▶ my ($vals, $xscale, $yscale) = @_;
Subroutine xypairs_to_points_str: called CC=1 SCAR=0.0
Pod documented
57 return join( ' ',
58 785 map { format_f($xscale * $_->[0]) .','. format_f($yscale * $_->[1]) }
59 76 @{$vals}
60 );
61 }
62  
63 sub summarize_values
64 {
65 65 ▶ my ($array) = @_;
Subroutine summarize_values: called CC=3 SCAR=11.0
Pod documented
66 my $desc = {
67 65 min => List::Util::min( @{$array} ),
68 65 max => List::Util::max( @{$array} ),
69 };
70
71 65 ▶ $desc->{min} = 0 if $desc->{min} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $desc->{"min"} > 0 6 59 65
72 65 ▶ $desc->{max} = 0 if $desc->{max} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $desc->{"max"} < 0 8 57 65
73  
74 65 $desc->{range} = $desc->{max}-$desc->{min};
75 65 return $desc;
76 }
77  
78 sub summarize_xy_values
79 {
80 80 ▶ my ($array) = @_;
Subroutine summarize_xy_values: called CC=4 SCAR=13.9
Pod documented
81 80 ▶ return _summarize_xy_pairs( $array ) if 'ARRAY' eq ref $array->[0];
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq ref $array->[0] 6 74 80
82 my $desc = {
83 74 ymin => List::Util::min( @{$array} ),
84 74 ymax => List::Util::max( @{$array} ),
85 xmin => 0,
86 74 xmax => $#{$array},
87 74 xrange => $#{$array},
88 };
89 74 $desc->{base} = 0;
90 74 ▶ $desc->{base} = $desc->{ymin} if $desc->{ymin} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $desc->{"ymin"} > 0 11 63 74
91 74 ▶ $desc->{base} = $desc->{ymax} if $desc->{ymax} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $desc->{"ymax"} < 0 10 64 74
92 74 $desc->{offset} = $desc->{ymin} - $desc->{base};
93  
94 74 $desc->{yrange} = $desc->{ymax}-$desc->{ymin};
95 74 my $i = 0;
96 74 $desc->{vals} = [map { [$i++,$_-$desc->{base}] } @{$array}];
97 74 return $desc;
98 }
99  
100 sub _summarize_xy_pairs
101 {
102 6 ▶ my ($array) = @_;
Subroutine _summarize_xy_pairs: called CC=10 SCAR=23.2
103 6 my $desc = {
104 xmin => $array->[0]->[0],
105 xmax => $array->[-1]->[0],
106 ymin => $array->[0]->[1],
107 ymax => $array->[0]->[1],
108 };
109  
110 6 foreach my $p ( @{$array} )
111 {
112 die "Array element is not a pair.\n"
113 34 ▶ unless 'ARRAY' eq ref $p && 2 == @{$p};
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $p and 2 == @{$p;} 4 30 34
Condition100%3/3
"ARRAY" eq ref $p and 2 == @{$p;}
!ll&&!rl&&r
2230
MC/DC100%2/2
"ARRAY" eq ref $p and 2 == @{$p;}
"ARRAY" eq ref $p 2 == @{$p;}
Truth table100%3/3
A and B
A= "ARRAY" eq ref $pB= 2 == @{$p;}
ABresult
0X0
100
111
114 30 ▶ $desc->{xmin} = $p->[0] if $p->[0] < $desc->{xmin};
Branch: 1 branch
BranchTrueFalseTotal
if $p->[0] < $desc->{"xmin"} 0 30 30
115 30 ▶ $desc->{xmax} = $p->[0] if $p->[0] > $desc->{xmax};
Branch: 1 branch
BranchTrueFalseTotal
if $p->[0] > $desc->{"xmax"} 0 30 30
116 30 ▶ $desc->{ymin} = $p->[1] if $p->[1] < $desc->{ymin};
Branch: 1 branch
BranchTrueFalseTotal
if $p->[1] < $desc->{"ymin"} 4 26 30
117 30 ▶ $desc->{ymax} = $p->[1] if $p->[1] > $desc->{ymax};
Branch: 1 branch
BranchTrueFalseTotal
if $p->[1] > $desc->{"ymax"} 8 22 30
118 }
119 2 $desc->{base} = 0;
120 2 ▶ $desc->{base} = $desc->{ymin} if $desc->{ymin} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $desc->{"ymin"} > 0 0 2 2
121 2 ▶ $desc->{base} = $desc->{ymax} if $desc->{ymax} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $desc->{"ymax"} < 0 0 2 2
122 2 $desc->{offset} = $desc->{ymin} - $desc->{base};
123  
124 2 $desc->{xrange} = $desc->{xmax}-$desc->{xmin};
125 2 $desc->{yrange} = $desc->{ymax}-$desc->{ymin};
126 $desc->{vals} =
127 2 [map { [$_->[0]-$desc->{xmin},$_->[1]-$desc->{base}] } @{$array}];
128 2 return $desc;
129 }
130  
131 sub make_svg
132 {
133 172 ▶ my ($args) = @_;
Subroutine make_svg: called CC=2 SCAR=6.9
Pod documented
134 my $svg = SVG->new(
135 -inline=>1, -nocredits=>1, -raiseerror=>1, -indent=>'', -elsep=>'',
136 width=>$args->{width}, height=>$args->{height},
137 172 viewBox=> join( ' ', @{$args}{qw/xoff yoff width height/} )
138 );
139  
140 172 ▶ if( exists $args->{bgcolor} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"bgcolor"}) 8 164 172
141 {
142 $svg->rect(
143 x => $args->{xoff}-1, y => $args->{yoff}-1,
144 width => $args->{width}+2, height => $args->{height}+2,
145 stroke => 'none', fill => $args->{bgcolor}
146 8 );
147 }
148 172 return $svg;
149 }
150  
151 sub validate_array_param
152 {
153 165 ▶ my ($args, $name) = @_;
Subroutine validate_array_param: called CC=4 SCAR=13.9
Pod documented
154 165 local $Carp::CarpLevel = 2;
155 165 ▶ croak "Missing required '$name' parameter.\n" if !exists $args->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{$name} 5 160 165
156 160 ▶ croak "'$name' must be an array reference.\n" unless 'ARRAY' eq ref $args->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $args->{$name} 8 152 160
157 152 ▶ croak "No values for '$name' specified.\n" unless @{$args->{$name}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$args->{$name};} 5 147 152
158 147 return;
159 }
160  
161 sub range_mark_to_index
162 {
163 14 ▶ my ($type, $index, $values) = @_;
Subroutine range_mark_to_index: called CC=11 SCAR=24.0
Pod documented
164 14 ▶ return 0 if $index eq 'first';
Branch: 1 branch
BranchTrueFalseTotal
if $index eq "first" 3 11 14
165 11 ▶ return $#{$values} if $index eq 'last';
Branch: 1 branch
BranchTrueFalseTotal
if $index eq "last" 2 9 11
166 9 ▶ return $index if $index !~ /\D/ && $index < @{$values};
Branch: 1 branch
BranchTrueFalseTotal
if not $index =~ /\D/ and $index < @{$values;} 3 6 9
Condition66%2/3
not $index =~ /\D/ and $index < @{$values;}
!ll&&!rl&&r
603
MC/DC50%1/2
not $index =~ /\D/ and $index < @{$values;}
not $index =~ /\D/ $index < @{$values;}
Truth table66%2/3
not(A) and B
A= not $index =~ /\D/B= $index < @{$values;}
ABresult
0X0
100
111
167 6 ▶ if( 'high' eq $index )
Branch: 2 branches
BranchTrueFalseTotal
if ('high' eq $index) { } 4 2 6
elsif ('low' eq $index) { } 2 0 2
168 {
169 4 my $high = $values->[0]->[1];
170 4 my $ndx = 0;
171 4 foreach my $i ( 1 .. $#{$values} )
172 {
173 16 ▶ ($high,$ndx) = ($values->[$i]->[1],$i) if $values->[$i]->[1] > $high;
Branch: 1 branch
BranchTrueFalseTotal
if $values->[$i][1] > $high 8 8 16
174 }
175 4 return $ndx;
176 }
177 elsif( 'low' eq $index )
178 {
179 2 my $low = $values->[0]->[0];
180 2 my $ndx = 0;
181 2 foreach my $i ( 1 .. $#{$values} )
182 {
183 8 ▶ ($low,$ndx) = ($values->[$i]->[0],$i) if $values->[$i]->[0] < $low;
Branch: 1 branch
BranchTrueFalseTotal
if $values->[$i][0] < $low 4 4 8
184 }
185 2 return $ndx;
186 }
187  
188 0 croak "'$index' is not a valid mark for $type sparkline";
189 }
190  
191 sub mark_to_index
192 {
193 51 ▶ my ($type, $index, $values) = @_;
Subroutine mark_to_index: called CC=11 SCAR=24.0
Pod documented
194 51 ▶ return 0 if $index eq 'first';
Branch: 1 branch
BranchTrueFalseTotal
if $index eq "first" 9 42 51
195 42 ▶ return $#{$values} if $index eq 'last';
Branch: 1 branch
BranchTrueFalseTotal
if $index eq "last" 9 33 42
196 33 ▶ return $index if $index !~ /\D/ && $index < @{$values};
Branch: 1 branch
BranchTrueFalseTotal
if not $index =~ /\D/ and $index < @{$values;} 15 18 33
Condition66%2/3
not $index =~ /\D/ and $index < @{$values;}
!ll&&!rl&&r
18015
MC/DC50%1/2
not $index =~ /\D/ and $index < @{$values;}
not $index =~ /\D/ $index < @{$values;}
Truth table66%2/3
not(A) and B
A= not $index =~ /\D/B= $index < @{$values;}
ABresult
0X0
100
111
197 18 ▶ if( 'high' eq $index )
Branch: 2 branches
BranchTrueFalseTotal
if ('high' eq $index) { } 9 9 18
elsif ('low' eq $index) { } 9 0 9
198 {
199 9 my $high = $values->[0];
200 9 my $ndx = 0;
201 9 foreach my $i ( 1 .. $#{$values} )
202 {
203 54 ▶ ($high,$ndx) = ($values->[$i],$i) if $values->[$i] > $high;
Branch: 1 branch
BranchTrueFalseTotal
if $values->[$i] > $high 15 39 54
204 }
205 9 return $ndx;
206 }
207 elsif( 'low' eq $index )
208 {
209 9 my $low = $values->[0];
210 9 my $ndx = 0;
211 9 foreach my $i ( 1 .. $#{$values} )
212 {
213 54 ▶ ($low,$ndx) = ($values->[$i],$i) if $values->[$i] < $low;
Branch: 1 branch
BranchTrueFalseTotal
if $values->[$i] < $low 18 36 54
214 }
215 9 return $ndx;
216 }
217  
218 0 croak "'$index' is not a valid mark for $type sparkline";
219 }
220  
221  
222 30 1;
223  
224 __END__
« script/sparkline.pl ↑ Summary lib/SVG/Sparkline/Whisker.pm »