lib/SVG/Sparkline/RangeBar.pm

filter: Statementstmt 100% 105 / 105 Branchbran 100% 22 / 22 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 100% 148 / 148 CC 16 SCAR 15.7
CC
16
Cov
100%
make
9.0
_zero_height_path
6.0
_make_marks
2.0
CRAP 4.8
« lib/SVG/Sparkline/Bar.pm ↑ Summary lib/SVG/Sparkline.pm »
1 package SVG::Sparkline::RangeBar;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use SVG;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use SVG::Sparkline::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use 5.008000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 our $VERSION = 1.11;
12  
13 # alias to make calling shorter.
14 3 *_f = *SVG::Sparkline::Utils::format_f;
15  
16 sub valid_param {
17 10 ▶ return scalar grep { $_[1] eq $_ } qw/gap thick/;
Subroutine valid_param: called CC=1 SCAR=0.0
Pod documented
18 }
19  
20 sub make
21 {
22 32 ▶ my ($class, $args) = @_;
Subroutine make: called CC=9 SCAR=22.0
Pod documented
23 # validate parameters
24 32 SVG::Sparkline::Utils::validate_array_param( $args, 'values' );
25 croak "'values' must be an array of pairs.\n"
26 28 ▶ if grep { 'ARRAY' ne ref $_ || 2 != @{$_} } @{$args->{values}};
Branch: 2 branches
BranchTrueFalseTotal
if grep {"ARRAY" ne ref $_ or 2 != @{$_;};} @{$$args{"values"};} 1 27 28
"ARRAY" ne ref $_ or 2 != @{$_;} 136 4 140
27 my $vals = SVG::Sparkline::Utils::summarize_values(
28 27 [ map { @{$_} } @{$args->{values}} ]
29 );
30  
31 27 my $height = $args->{height} - 2*$args->{pady};
32 27 my $yscale = -$height / $vals->{range};
33 27 my $baseline = _f(-$yscale*$vals->{min});
34  
35 # Figure out the width I want and define the viewBox
36 27 my $dwidth;
37 27 ▶ my $gap = $args->{gap} || 0;
Condition100%2/2
$args->{'gap'} || 0
l!l
423
MC/DC100%1/1
$args->{'gap'} || 0
$args->{'gap'}
Truth table100%2/2
A
A= $args->{'gap'}
Aresult
11
00
38 27 ▶ $args->{thick} ||= 3;
Condition100%2/2
$args->{"thick"} ||= 3
l!l
621
MC/DC100%1/1
$args->{"thick"} ||= 3
$args->{"thick"}
Truth table100%2/2
A
A= $args->{"thick"}
Aresult
11
00
39 27 my $space = $args->{thick}+$gap;
40 27 ▶ if($args->{width})
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'width'}) { } 1 26 27
41 {
42 1 $dwidth = $args->{width} - $args->{padx}*2;
43 1 $space = _f( $dwidth / @{$args->{values}} );
44 1 $args->{thick} = $space - $gap;
45 }
46 else
47 {
48 26 $dwidth = @{$args->{values}} * $space;
49 26 $args->{width} = $dwidth + 2*$args->{padx};
50 }
51 27 $args->{yoff} = -($baseline+$height+$args->{pady});
52 27 $args->{xscale} = $space;
53 27 my $svg = SVG::Sparkline::Utils::make_svg( $args );
54  
55 27 my $off = _f( $gap/2 );
56 27 my $prev = 0;
57 27 my $path = "M". _f(-$args->{thick}-$off).",0";
58 27 foreach my $v (@{$args->{values}})
59 {
60 # Move from previous x,y to low value
61 136 $path .= 'm'. _f($args->{thick}+$gap) .','. _f($yscale*($v->[0]-$prev));
62 136 my $vert = _f( $yscale * ($v->[1]-$v->[0]) );
63 136 ▶ if($vert)
Branch: 1 branch
BranchTrueFalseTotal
if ($vert) { } 130 6 136
64 {
65 130 $path .= "v${vert}h$args->{thick}v". _f(-$vert)."h-$args->{thick}";
66 }
67 else
68 {
69 6 $path .= _zero_height_path( $args->{thick} );
70 }
71 136 $prev = $v->[0];
72 }
73 27 $path = _clean_path( $path );
74 27 $svg->path( stroke=>'none', fill=>$args->{color}, d=>$path );
75  
76 27 ▶ if( exists $args->{mark} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"mark"}) 9 18 27
77 {
78 _make_marks( $svg,
79 thick=>$args->{thick}, off=>$off,
80 space=>$space, yscale=>$yscale,
81 values=>$args->{values}, mark=>$args->{mark}
82 9 );
83 }
84 27 return $svg;
85 }
86  
87 sub _zero_height_path
88 {
89 7 ▶ my ($thick) = @_;
Subroutine _zero_height_path: called CC=6 SCAR=17.9
90 7 my $path = 'v-0.5';
91 7 my $step = 1;
92 7 ▶ $step = $thick/4 if $thick <= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $thick <= 2 1 6 7
93 7 ▶ $step = 2 if $thick >= 8;
Branch: 1 branch
BranchTrueFalseTotal
if $thick >= 8 1 6 7
94 7 my $num_steps = int( $thick/$step ) - 1;
95 7 my $leftover = $thick-($num_steps*$step);
96 7 foreach my $i (1 .. $num_steps)
97 {
98 19 ▶ $path .= "h${step}v" . ($i%2? 1 :-1);
Branch: 1 branch
BranchTrueFalseTotal
$i % 2 ? : 11 8 19
99 }
100 7 ▶ $path .= "h${leftover}v". ($thick%2?0.5: -0.5) . "h-$thick";
Branch: 1 branch
BranchTrueFalseTotal
$thick % 2 ? : 4 3 7
101 7 return $path;
102 }
103  
104 sub _make_marks
105 {
106 9 ▶ my ($svg, %args) = @_;
Subroutine _make_marks: called CC=2 SCAR=6.9
107
108 9 my @marks = @{$args{mark}};
109 9 ▶ while(@marks)
Branch: 1 branch
BranchTrueFalseTotal
if (@marks) 9 9 18
110 {
111 9 my ($index,$color) = splice( @marks, 0, 2 );
112 9 $index = SVG::Sparkline::Utils::range_mark_to_index( 'RangeBar', $index, $args{values} );
113 9 _make_mark( $svg, %args, index=>$index, color=>$color );
114 }
115 9 return;
116 }
117  
118 sub _make_mark
119 {
120 9 ▶ my ($svg, %args) = @_;
Subroutine _make_mark: called CC=2 SCAR=6.9
121 9 my $index = $args{index};
122 9 my ($lo, $hi) = @{$args{values}->[$index]};
123 9 my $y = _f( $hi * $args{yscale} );
124 9 my $h = _f( ($hi-$lo) * $args{yscale});
125 9 ▶ if($h)
Branch: 1 branch
BranchTrueFalseTotal
if ($h) { } 8 1 9
126 {
127 8 my $x = _f($index * $args{space} + $args{off});
128 $svg->rect( x=>$x, y=>$y,
129 width=>$args{thick}, height=>abs($h),
130 stroke=>'none', fill=>$args{color}
131 8 );
132 }
133 else
134 {
135 1 my $x = _f($index * $args{space} +$args{off});
136 $svg->path(
137 d=>"M$x,$y". _zero_height_path( $args{thick} ),
138 stroke=>'none', fill=>$args{color}
139 1 );
140 }
141 9 return;
142 }
143  
144 sub _clean_path
145 {
146 27 ▶ my ($path) = @_;
Subroutine _clean_path: called CC=1 SCAR=0.0
147 27 $path =~ s/^M([-.\d]+),([-.\d]+)m([-.\d]+),([-.\d]+)/'M'. _f($1+$3) .','. _f($2+$4)/e;
148 27 $path =~ s/h0(?![.\d])//g;
149 27 return $path;
150 }
151  
152 3 1;
153  
154 __END__
« lib/SVG/Sparkline/Bar.pm ↑ Summary lib/SVG/Sparkline.pm »