| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 26 | 223 | ▶ | croak "Missing arguments hash.\n" unless defined $args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 222 | ▶ | croak "Arguments not supplied as a hash reference.\n" unless 'HASH' eq ref $args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 47 | croak "The value of $arg is not a valid color.\n" | ||||||||||||||||||||||||||||
| 48 | 212 | ▶ | unless _is_color( $self->{$arg} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 66 | 189 | ▶ | unless( $self->{'-sized'} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
exists $valid_parms{$parm} or $class->valid_param($parm)
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)
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
exists $self->{$name} and $self->{$name} <= 0
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
| |||||||||||||||||||||||||||||
| 98 | 866 | ▶ | return if exists $self->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 835 | ▶ | $self->{$name} = $default if defined $default; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
exists $self->{$name} and $self->{$name} < 0
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
| |||||||||||||||||||||||||||||
| 109 | 427 | ▶ | return if exists $self->{$name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 405 | ▶ | $self->{$name} = $default if defined $default; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | croak "'mark' parameter must be an array reference.\n" | ||||||||||||||||||||||||||||
| 122 | 84 | ▶ | unless 'ARRAY' eq ref $self->{mark}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | croak "'mark' array parameter must have an even number of elements.\n" | ||||||||||||||||||||||||||||
| 124 | 83 | ▶ | unless 0 == (@{$self->{mark}}%2); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 82 | my @marks = @{$self->{mark}}; | |||||||||||||||||||||||||||
| 127 | 82 | ▶ | while(@marks) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | { | ||||||||||||||||||||||||||||
| 129 | 84 | my ($index, $color) = splice( @marks, 0, 2 ); | |||||||||||||||||||||||||||
| 130 | 84 | ▶ | croak "'$index' is not a valid mark index.\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | unless $index =~ /^(?:first|last|high|low|\d+)$/; | ||||||||||||||||||||||||||||
| 132 | 77 | ▶ | croak "'$color' is not a valid mark color.\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 142 | 101 | ▶ | return 1 if $color =~ /^#[[:xdigit:]]{6}$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | 99 | ▶ | return 1 if $color =~ /^rgb\(\d+,\d+,\d+\)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | 98 | ▶ | return 1 if $color =~ /^rgb\(\d+%,\d+%,\d+%\)$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | 97 | ▶ | return 1 if $color =~ /^[[:alpha:]]+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 18 | return; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 30 | 1; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||