Clicker/Data/Series.pm

filter: Statementstmt 94.3% 50 / 53 Branchbran 100% 10 / 10 Conditioncond 88.8% 8 / 9 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 90.9% 10 / 11 Podpod 75.0% 3 / 4 totaltotal 92.3% 85 / 92 CC 11 SCAR 11.8
CC
11
Cov
94%
BUILDARGS
6.0
get_value_for_key
4.0
_build_range
2.0
CRAP 3.2
« Clicker/Decoration/Plot.pm ↑ Summary Clicker/Axis/DivisionType/Exact.pm »
1 package Chart::Clicker::Data::Series;
2 11 $Chart::Clicker::Data::Series::VERSION = '2.89';
3 11 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: A series of key, value pairs representing chart data
6  
7 11 ▶use List::Util qw(max min);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Chart::Clicker::Data::Range;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 has 'keys' => (
12 traits => [ 'Array' ],
13 is => 'rw',
14 isa => 'ArrayRef[Num]',
15 7 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 11 handles => {
17 'add_to_keys' => 'push',
18 'key_count' => 'count'
19 }
20 );
21  
22  
23 11 has 'name' => (
24 is => 'rw',
25 isa => 'Str',
26 predicate => 'has_name'
27 );
28  
29  
30 11 has 'range' => (
31 is => 'rw',
32 isa => 'Chart::Clicker::Data::Range',
33 lazy_build => 1
34 );
35  
36  
37 has 'values' => (
38 traits => [ 'Array' ],
39 is => 'rw',
40 isa => 'ArrayRef[Num|Undef]',
41 7 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 11 handles => {
43 'add_to_values' => 'push',
44 'value_count' => 'count'
45 }
46 );
47  
48 sub _build_range {
49 5 ▶ my ($self) = @_;
Subroutine _build_range: called CC=2 SCAR=6.9
50  
51 5 my $values = $self->values;
52  
53 confess('A series must have values before it can be charted')
54 5 ▶ unless scalar(@{ $values });
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$values;} 1 4 5
55  
56 return Chart::Clicker::Data::Range->new(
57 13 lower => min(grep { defined } @{ $values }),
58 4 upper => max(grep { defined } @{ $values })
59 );
60 }
61  
62 sub BUILDARGS {
63 21 ▶ my ($class, @args) = @_;
Subroutine BUILDARGS: called CC=6 SCAR=17.9
Pod documented
64  
65 21 ▶ if(@args == 1 && (ref($args[0]) eq 'HASH') && !exists($args[0]->{keys})) {
Branch: 2 branches
BranchTrueFalseTotal
if (@args == 1 and ref $args[0] eq 'HASH' and not exists $args[0]{'keys'}) { } 1 20 21
elsif (@args % 2 == 0) { } 17 3 20
Condition83%5/6
@args == 1 and ref $args[0] eq "HASH" and not exists $args[0]{"keys"}
!ll&&!rl&&r
1731
@args == 1 and ref $args[0] eq "HASH"
!ll&&!rl&&r
1704
MC/DC66%2/3
@args == 1 and ref $args[0] eq "HASH" and not exists $args[0]{"keys"}
@args == 1 ref $args[0] eq "HASH" not exists $args[0]{"keys"}
Truth table75%3/4
A and B and not(C)
A= @args == 1B= ref $args[0] eq "HASH"C= not exists $args[0]{"keys"}
ABCresult
0XX0
10X0
1100
1111
66 1 my @keys = sort { $a <=> $b } keys %{ $args[0] };
67 1 my @values = ();
68 1 foreach my $k (@keys) {
69 4 push(@values, $args[0]->{$k})
70 }
71 1 return { keys => \@keys, values => \@values }
72 } elsif(@args % 2 == 0) {
73 17 return { @args };
74 }
75  
76 3 return $args[0];
77 }
78  
79  
80 sub add_pair {
81 0 ▶ my ($self, $key, $value) = @_;
Subroutine add_pair: not called CC=1 SCAR=6.9
Pod documented
82  
83 0 $self->add_to_keys($key);
84 0 $self->add_to_values($value);
85 }
86  
87  
88 sub get_value_for_key {
89 5 ▶ my ($self, $key) = @_;
Subroutine get_value_for_key: called CC=4 SCAR=13.9
Pod documented
90  
91 5 for(0..$self->key_count) {
92 11 my $ikey = $self->keys->[$_];
93 11 ▶ return $self->values->[$_] if defined($ikey) && $ikey == $key;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ikey and $ikey == $key 4 7 11
Condition100%3/3
defined $ikey and $ikey == $key
!ll&&!rl&&r
164
MC/DC100%2/2
defined $ikey and $ikey == $key
defined $ikey $ikey == $key
Truth table100%3/3
A and B
A= defined $ikeyB= $ikey == $key
ABresult
0X0
100
111
94 }
95  
96 # We found nothing, return undef
97 1 return undef;
98 }
99  
100 sub prepare {
101 6 ▶ my ($self) = @_;
Subroutine prepare: called CC=2 SCAR=6.9
Pod undocumented
102  
103 6 ▶ if($self->key_count != $self->value_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->key_count != $self->value_count) 1 5 6
104 1 die('Series key/value counts dont match: '.$self->key_count.' != '.$self->value_count.'.');
105 }
106  
107 5 return 1;
108 }
109  
110 11 __PACKAGE__->meta->make_immutable;
111  
112 11 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
113  
114 11 1;
115  
116 __END__
117  
118 =pod
119  
120 =head1 NAME
121  
122 Chart::Clicker::Data::Series - A series of key, value pairs representing chart data
123  
124 =head1 VERSION
125  
126 version 2.89
127  
128 =head1 SYNOPSIS
129  
130 use Chart::Clicker::Data::Series;
131  
132 my @keys = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
133 my @values = (42, 25, 86, 23, 2, 19, 103, 12, 54, 9);
134  
135 my $series = Chart::Clicker::Data::Series->new({
136 keys => \@keys,
137 value => \@values
138 });
139  
140 # Alternately, if you prefer
141  
142 my $series2 = Chart::Clicker::Data::Series->new({
143 1 => 42,
144 2 => 25,
145 3 => 85,
146 4 => 23,
147 5 => 2,
148 6 => 19,
149 7 => 102,
150 8 => 12,
151 9 => 54,
152 10 => 9
153 });
154  
155 =head1 DESCRIPTION
156  
157 Chart::Clicker::Data::Series represents a series of values to be charted.
158  
159 Despite the name (keys and values) it is expected that all keys and values
160 will be numeric. Values is pretty obvious, but it is important that keys
161 also be numeric, as otherwise we'd have no idea how to order the data.
162  
163 If you want to use text labels for your domain's see L<Chart::Clicker::Axis>'s
164 L<tick_labels> method.
165  
166 =head1 ATTRIBUTES
167  
168 =head2 keys
169  
170 Set/Get the keys for this series.
171  
172 =head2 add_to_keys
173  
174 Adds a key to this series.
175  
176 =head2 name
177  
178 Set/Get the name for this Series
179  
180 =head2 range
181  
182 Returns the L<range|Chart::Clicker::Data::Range> for this series.
183  
184 =head2 values
185  
186 Set/Get the values for this series.
187  
188 =head1 METHODS
189  
190 =head2 key_count
191  
192 Get the count of keys in this series.
193  
194 =head2 add_to_values
195  
196 Add a value to this series.
197  
198 =head2 value_count
199  
200 Get the count of values in this series.
201  
202 =head2 add_pair ($key, $value)
203  
204 Convenience method to add a single key and a single value to the series.
205  
206 =head2 get_value_for_key ($key)
207  
208 Returns the value associated with the specified key. This is necessary
209 because not all series will have values for every key.
210  
211 =head1 AUTHOR
212  
213 Cory G Watson <gphat@cpan.org>
214  
215 =head1 COPYRIGHT AND LICENSE
216  
217 This software is copyright (c) 2016 by Cory G Watson.
218  
219 This is free software; you can redistribute it and/or modify it under
220 the same terms as the Perl 5 programming language system itself.
221  
222 =cut
« Clicker/Decoration/Plot.pm ↑ Summary Clicker/Axis/DivisionType/Exact.pm »