| 1 | package SVG::Sparkline::Bar; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use SVG; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use List::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use SVG::Sparkline::Utils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use 5.008000; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 10 | our $VERSION = 1.11; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # alias to make calling shorter. | ||||||||||||||||||||||||||||
| 14 | 10 | *_f = *SVG::Sparkline::Utils::format_f; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | sub valid_param { | ||||||||||||||||||||||||||||
| 17 | 5 | ▶ | 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 | 42 | ▶ | my ($class, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
make: called CC=12 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | # validate parameters | ||||||||||||||||||||||||||||
| 24 | 42 | SVG::Sparkline::Utils::validate_array_param( $args, 'values' ); | |||||||||||||||||||||||||||
| 25 | 38 | my $vals = SVG::Sparkline::Utils::summarize_values( $args->{values} ); | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 38 | my $height = $args->{height} - 2*$args->{pady}; | |||||||||||||||||||||||||||
| 28 | # If we get all zeros for data, the range will be 0, and the division will | ||||||||||||||||||||||||||||
| 29 | # fail. Almost anything will be a reasonable range, so arbitrarily choose 1. | ||||||||||||||||||||||||||||
| 30 | 38 | ▶ | my $yscale = -$height / ($vals->{range} || 1); | ||||||||||||||||||||||||||
|
Condition100%2/2
$vals->{'range'} || 1
MC/DC100%1/1
$vals->{'range'} || 1
$vals->{'range'}
Truth table100%2/2
A
A= $vals->{'range'}
| |||||||||||||||||||||||||||||
| 31 | 38 | my $baseline = _f(-$yscale*$vals->{min}); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | # Figure out the width I want and define the viewBox | ||||||||||||||||||||||||||||
| 34 | 38 | my $dwidth; | |||||||||||||||||||||||||||
| 35 | 38 | ▶ | my $gap = $args->{gap} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'gap'} || 0
MC/DC100%1/1
$args->{'gap'} || 0
$args->{'gap'}
Truth table100%2/2
A
A= $args->{'gap'}
| |||||||||||||||||||||||||||||
| 36 | 38 | ▶ | $args->{thick} ||= 3; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{"thick"} ||= 3
MC/DC100%1/1
$args->{"thick"} ||= 3
$args->{"thick"}
Truth table100%2/2
A
A= $args->{"thick"}
| |||||||||||||||||||||||||||||
| 37 | 38 | my $space = $args->{thick}+$gap; | |||||||||||||||||||||||||||
| 38 | 38 | ▶ | if($args->{width}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||
| 40 | 1 | $dwidth = $args->{width} - $args->{padx}*2; | |||||||||||||||||||||||||||
| 41 | 1 | $space = _f( $dwidth / @{$args->{values}} ); | |||||||||||||||||||||||||||
| 42 | 1 | $args->{thick} = $space - $gap; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | else | ||||||||||||||||||||||||||||
| 45 | { | ||||||||||||||||||||||||||||
| 46 | 37 | $dwidth = @{$args->{values}} * $space; | |||||||||||||||||||||||||||
| 47 | 37 | $args->{width} = $dwidth + 2*$args->{padx}; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 38 | $args->{yoff} = -($baseline+$height+$args->{pady}); | |||||||||||||||||||||||||||
| 50 | 38 | $args->{xscale} = $space; | |||||||||||||||||||||||||||
| 51 | 38 | my $svg = SVG::Sparkline::Utils::make_svg( $args ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 38 | my $off = _f( $gap/2 ); | |||||||||||||||||||||||||||
| 54 | 38 | my $prev = 0; | |||||||||||||||||||||||||||
| 55 | 38 | my @pieces; | |||||||||||||||||||||||||||
| 56 | 38 | foreach my $v (@{$args->{values}}) | |||||||||||||||||||||||||||
| 57 | { | ||||||||||||||||||||||||||||
| 58 | 294 | my $curr = _f( $yscale*($v-$prev) ); | |||||||||||||||||||||||||||
| 59 | 294 | ▶ | my $subpath = $curr ? "v${curr}h$args->{thick}" : "h$args->{thick}"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 294 | $prev = $v; | |||||||||||||||||||||||||||
| 61 | 294 | ▶ | if($gap && $curr) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$gap and $curr
MC/DC100%2/2
$gap and $curr
$gap
$curr
Truth table100%3/3
A and B
A= $gapB= $curr
| |||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | 16 | $subpath .= 'v' . _f(-$curr); | |||||||||||||||||||||||||||
| 64 | 16 | $prev = 0; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 294 | push @pieces, $subpath; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 38 | ▶ | push @pieces, 'v' . _f( $yscale*(-$prev) ) if $prev; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 38 | ▶ | my $spacer = $gap ? "h$gap" : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 38 | my $path = "M$off,0" . join( $spacer, @pieces ) . 'z'; | |||||||||||||||||||||||||||
| 71 | 38 | $path = _clean_path( $path ); | |||||||||||||||||||||||||||
| 72 | 38 | $svg->path( stroke=>'none', fill=>$args->{color}, d=>$path ); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 38 | ▶ | if( exists $args->{mark} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||
| 76 | _make_marks( $svg, | ||||||||||||||||||||||||||||
| 77 | thick=>$args->{thick}, off=>$off, | ||||||||||||||||||||||||||||
| 78 | space=>$space, yscale=>$yscale, | ||||||||||||||||||||||||||||
| 79 | values=>$args->{values}, mark=>$args->{mark} | ||||||||||||||||||||||||||||
| 80 | 20 | ); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | 38 | return $svg; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | sub _make_marks | ||||||||||||||||||||||||||||
| 86 | { | ||||||||||||||||||||||||||||
| 87 | 20 | ▶ | my ($svg, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_make_marks: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 20 | my @marks = @{$args{mark}}; | |||||||||||||||||||||||||||
| 90 | 20 | ▶ | while(@marks) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||
| 92 | 20 | my ($index,$color) = splice( @marks, 0, 2 ); | |||||||||||||||||||||||||||
| 93 | 20 | $index = _check_index( $index, $args{values} ); | |||||||||||||||||||||||||||
| 94 | 20 | _make_mark( $svg, %args, index=>$index, color=>$color ); | |||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | 20 | return; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub _make_mark | ||||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 20 | ▶ | my ($svg, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_make_mark: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 102 | 20 | my $index = $args{index}; | |||||||||||||||||||||||||||
| 103 | 20 | my $h = _f($args{values}->[$index] * $args{yscale}); | |||||||||||||||||||||||||||
| 104 | 20 | ▶ | if($h) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||
| 106 | 17 | my $x = _f($index * $args{space} + $args{off}); | |||||||||||||||||||||||||||
| 107 | 17 | ▶ | my $y = $h > 0 ? 0 : $h; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | $svg->rect( x=>$x, y=>$y, | ||||||||||||||||||||||||||||
| 109 | width=>$args{thick}, height=>abs( $h ), | ||||||||||||||||||||||||||||
| 110 | stroke=>'none', fill=>$args{color} | ||||||||||||||||||||||||||||
| 111 | 17 | ); | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | else | ||||||||||||||||||||||||||||
| 114 | { | ||||||||||||||||||||||||||||
| 115 | 3 | my $x = _f(($index+0.5) * $args{space} +$args{off}); | |||||||||||||||||||||||||||
| 116 | $svg->ellipse( cx=>$x, cy=>0, ry=>0.5, rx=>$args{thick}/2, | ||||||||||||||||||||||||||||
| 117 | stroke=>'none', fill=>$args{color} | ||||||||||||||||||||||||||||
| 118 | 3 | ); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | 20 | return; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | sub _check_index | ||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | 20 | ▶ | return SVG::Sparkline::Utils::mark_to_index( 'Bar', @_ ); | ||||||||||||||||||||||||||
|
Subroutine
_check_index: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | sub _clean_path | ||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||
| 130 | 42 | ▶ | my ($path) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_clean_path: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 131 | 42 | $path =~ s!((?:h[\d.]+){2,})!_consolidate_moves( $1 )!eg; | |||||||||||||||||||||||||||
| 132 | 42 | $path =~ s/h0(?![.\d])//g; | |||||||||||||||||||||||||||
| 133 | 42 | return $path; | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | sub _consolidate_moves | ||||||||||||||||||||||||||||
| 137 | { | ||||||||||||||||||||||||||||
| 138 | 9 | ▶ | my ($moves) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_consolidate_moves: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 139 | 9 | my @steps = split /h/, $moves; | |||||||||||||||||||||||||||
| 140 | 9 | shift @steps; # discard empty initial string | |||||||||||||||||||||||||||
| 141 | 9 | return 'h' . _f( List::Util::sum( @steps ) ); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 10 | 1; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||