| 1 | package SVG::Sparkline::Whisker; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use SVG; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use SVG::Sparkline::Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 13 | ▶ | use 5.008000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 13 | our $VERSION = 1.11; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | # alias to make calling shorter. | ||||||||||||||||||||||||||||
| 13 | 13 | *_f = *SVG::Sparkline::Utils::format_f; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub valid_param { | ||||||||||||||||||||||||||||
| 16 | 5 | ▶ | return scalar grep { $_[1] eq $_ } qw/gap thick/; | ||||||||||||||||||||||||||
|
Subroutine
valid_param: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | sub make | ||||||||||||||||||||||||||||
| 20 | { | ||||||||||||||||||||||||||||
| 21 | 36 | ▶ | my ($class, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
make: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | # validate parameters | ||||||||||||||||||||||||||||
| 23 | 36 | my @values; | |||||||||||||||||||||||||||
| 24 | 36 | ▶ | croak "Missing required 'values'\n" unless exists $args->{values}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 35 | ▶ | if( 'ARRAY' eq ref $args->{values} ) | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 26 | { | ||||||||||||||||||||||||||||
| 27 | 11 | @values = @{$args->{values}}; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | elsif( !ref $args->{values} ) | ||||||||||||||||||||||||||||
| 30 | { | ||||||||||||||||||||||||||||
| 31 | 23 | my $valstr = $args->{values}; | |||||||||||||||||||||||||||
| 32 | # Convert 1/0 string to a +/- string. | ||||||||||||||||||||||||||||
| 33 | 23 | ▶ | $valstr =~ tr/10/+-/ if $valstr =~ /1/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 23 | @values = split //, $valstr; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | else | ||||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||
| 39 | 1 | croak "Unrecognized type of 'values' data.\n"; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 34 | @values = map { _val( $_ ) } @values; | |||||||||||||||||||||||||||
| 42 | 33 | ▶ | croak "No values specified for 'values'.\n" unless @values; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Figure out the width I want and define the viewBox | ||||||||||||||||||||||||||||
| 45 | 31 | ▶ | my $thick = $args->{thick} || 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'thick'} || 1
MC/DC100%1/1
$args->{'thick'} || 1
$args->{'thick'}
Truth table100%2/2
A
A= $args->{'thick'}
| |||||||||||||||||||||||||||||
| 46 | 31 | ▶ | my $gap = $args->{gap} || 2 * $thick; | ||||||||||||||||||||||||||
|
Condition66%2/3
$args->{'gap'} || 2 * $thick
MC/DC0%0/2
$args->{'gap'} || 2 * $thick
$args->{'gap'}
2 * $thick
Truth table66%2/3
A || B
A= $args->{'gap'}B= 2 * $thick
| |||||||||||||||||||||||||||||
| 47 | 31 | my $space = $thick + $gap; | |||||||||||||||||||||||||||
| 48 | 31 | my $dwidth; | |||||||||||||||||||||||||||
| 49 | 31 | ▶ | if($args->{width}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 4 | $dwidth = $args->{width} - 2*$args->{padx}; | |||||||||||||||||||||||||||
| 52 | 4 | $thick = _f( $dwidth / (3*@values) ); | |||||||||||||||||||||||||||
| 53 | 4 | $gap = _f( 2* $thick ); | |||||||||||||||||||||||||||
| 54 | 4 | $space = 3*$thick; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | else | ||||||||||||||||||||||||||||
| 57 | { | ||||||||||||||||||||||||||||
| 58 | 27 | $dwidth = @values * $space; | |||||||||||||||||||||||||||
| 59 | 27 | $args->{width} = $dwidth + 2*$args->{padx}; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 31 | ▶ | ++$space if $space =~s/\.9\d$//; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 31 | my $height = $args->{height} - 2*$args->{pady}; | |||||||||||||||||||||||||||
| 63 | 31 | my $wheight = $args->{height}/2; | |||||||||||||||||||||||||||
| 64 | 31 | $args->{yoff} = -$wheight; | |||||||||||||||||||||||||||
| 65 | 31 | $wheight -= $args->{pady}; | |||||||||||||||||||||||||||
| 66 | 31 | my $svg = SVG::Sparkline::Utils::make_svg( $args ); | |||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 31 | my $off = _f( $gap/2 ); | |||||||||||||||||||||||||||
| 69 | 31 | my $path = "M$off,0"; | |||||||||||||||||||||||||||
| 70 | 31 | foreach my $v (@values[0..$#values-1]) | |||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | 179 | ▶ | if( $v ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||
| 74 | 122 | my ($u,$d) = ( -$v*$wheight, $v*$wheight ); | |||||||||||||||||||||||||||
| 75 | 122 | $path .= "v${u}m$space,${d}"; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else | ||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||
| 79 | 57 | $path .= "m$space,0"; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 31 | $path .= 'v' . (-$values[-1]*$wheight); | |||||||||||||||||||||||||||
| 83 | 31 | $path = _clean_path( $path ); | |||||||||||||||||||||||||||
| 84 | 31 | $svg->path( 'stroke-width'=>$thick, stroke=>$args->{color}, d=>$path ); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 31 | ▶ | if( exists $args->{mark} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | { | ||||||||||||||||||||||||||||
| 88 | _make_marks( $svg, | ||||||||||||||||||||||||||||
| 89 | thick=>$thick, off=>$off, space=>$space, wheight=>-$wheight, | ||||||||||||||||||||||||||||
| 90 | values=>\@values, mark=>$args->{mark} | ||||||||||||||||||||||||||||
| 91 | 9 | ); | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 29 | return $svg; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | sub _make_marks | ||||||||||||||||||||||||||||
| 97 | { | ||||||||||||||||||||||||||||
| 98 | 9 | ▶ | my ($svg, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_make_marks: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 9 | my @marks = @{$args{mark}}; | |||||||||||||||||||||||||||
| 101 | 9 | ▶ | while(@marks) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||
| 103 | 11 | my ($index,$color) = splice( @marks, 0, 2 ); | |||||||||||||||||||||||||||
| 104 | 11 | $index = _check_index( $index, $args{values} ); | |||||||||||||||||||||||||||
| 105 | 9 | _make_mark( $svg, %args, index=>$index, color=>$color ); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 7 | return; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | sub _make_mark | ||||||||||||||||||||||||||||
| 111 | { | ||||||||||||||||||||||||||||
| 112 | 9 | ▶ | my ($svg, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_make_mark: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 113 | 9 | my $index = $args{index}; | |||||||||||||||||||||||||||
| 114 | 9 | ▶ | return unless $args{values}->[$index]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 8 | my $x = $index * $args{space}+$args{off}; | |||||||||||||||||||||||||||
| 116 | $svg->line( x1=>$x, x2=>$x, y1=>0, y2=>$args{wheight} * $args{values}->[$index], | ||||||||||||||||||||||||||||
| 117 | 'stroke-width'=>$args{thick}, stroke=>$args{color} | ||||||||||||||||||||||||||||
| 118 | 8 | ); | |||||||||||||||||||||||||||
| 119 | 8 | return; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub _check_index | ||||||||||||||||||||||||||||
| 123 | { | ||||||||||||||||||||||||||||
| 124 | 11 | ▶ | my ($index, $values) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_check_index: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 125 | 11 | ▶ | return 0 if $index eq 'first'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 10 | ▶ | return $#{$values} if $index eq 'last'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 9 | ▶ | return $index unless $index =~ /\D/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 2 | die "'$index' is not a valid mark for Whisker sparkline"; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub _val | ||||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||
| 134 | 213 | ▶ | my $val = shift; | ||||||||||||||||||||||||||
|
Subroutine
_val: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 213 | ▶ | return $val <=> 0 if $val =~ /\d/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 114 | ▶ | return $val eq '+' ? 1 : ( $val eq '-' ? -1 : die "Unrecognized character '$val'\n" ); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub _clean_path | ||||||||||||||||||||||||||||
| 141 | { | ||||||||||||||||||||||||||||
| 142 | 37 | ▶ | my ($path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_clean_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 143 | 37 | $path =~ s/((?:m[-.\d]+,[-.\d+]+){2,})/_consolidate_moves( $1 )/eg; | |||||||||||||||||||||||||||
| 144 | # Consolidate initial M with m | ||||||||||||||||||||||||||||
| 145 | 37 | $path =~ s/^M([-.\d]+),([-.\d]+)m([-.\d]+),([-.\d]+)/'M'. _f($1+$3) .','. _f($2+$4)/e; | |||||||||||||||||||||||||||
| 146 | 37 | $path =~ s/m[-.\d]+,[-.\d]+$//; # remove trailing move. | |||||||||||||||||||||||||||
| 147 | 37 | $path =~ s/m0,0(?![.\d])//; | |||||||||||||||||||||||||||
| 148 | 37 | return $path; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | sub _consolidate_moves | ||||||||||||||||||||||||||||
| 152 | { | ||||||||||||||||||||||||||||
| 153 | 47 | ▶ | my ($moves) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_consolidate_moves: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||
| 154 | 47 | my @coords = split /[m,]/, $moves; | |||||||||||||||||||||||||||
| 155 | 47 | shift @coords; # dump empty initial string. | |||||||||||||||||||||||||||
| 156 | 47 | my ($x,$y); | |||||||||||||||||||||||||||
| 157 | 47 | ▶ | while(@coords) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||
| 159 | 100 | my ($lx, $ly) = splice @coords, 0, 2; | |||||||||||||||||||||||||||
| 160 | 100 | $x += $lx; | |||||||||||||||||||||||||||
| 161 | 100 | $y += $ly; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 47 | ▶ | return ($x||$y) ? 'm' . _f($x).',' . _f($y) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$x or $y
MC/DC0%0/2
$x or $y
$x
$y
Truth table33%1/3
A or B
A= $xB= $y
| |||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 13 | 1; | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | __END__ | ||||||||||||||||||||||||||||