Chart/GGPlot/Geom/Bar.pm

filter: Statementstmt 89.6% 104 / 116 Branchbran 43.5% 27 / 62 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 4 totaltotal 68.7% 152 / 221 CC 36 SCAR 21.5
CC
36
Cov
71%
__ANON__
42.0
__ANON__
8.0
__ANON__
7.7
CRAP 8.6
« Chart/GGPlot/Plot.pm ↑ Summary Chart/GGPlot/Coord.pm »
1 package Chart::GGPlot::Geom::Bar;
2  
3 # ABSTRACT: Class for bar geom
4  
5 5 ▶use Chart::GGPlot::Class qw(:pdl);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use MooseX::Singleton;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 extends qw(Chart::GGPlot::Geom::Rect);
10  
11 5 our $VERSION = '0.002003'; # VERSION
12  
13 5 ▶use Chart::GGPlot::Aes;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 5 ▶use Chart::GGPlot::Layer;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Chart::GGPlot::Util qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Chart::GGPlot::Util::Pod qw(layer_func_pod);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 ▶has '+non_missing_aes' => ( default => sub { [qw(xmin xmax ymin ymax)] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
19  
20 8 ▶classmethod required_aes() { [qw(x y)] }
Subroutine required_aes: called CC=3 SCAR=11.2
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for classmethod required_aes (expected 1, got ' . @_ . ')') 0 8 8
@_ > 1 and Function::Parameters::_croak('Too many arguments for classmethod required_aes (expected 1, got ' . @_ . ')') 0 8 8
21 3 ▶classmethod extra_params() { [qw(na_rm width)] }
Subroutine extra_params: called CC=3 SCAR=11.2
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for classmethod extra_params (expected 1, got ' . @_ . ')') 0 3 3
@_ > 1 and Function::Parameters::_croak('Too many arguments for classmethod extra_params (expected 1, got ' . @_ . ')') 0 3 3
22  
23 5 my $geom_bar_pod = layer_func_pod(<<'EOT');
24  
25 geom_bar(:$mapping=undef, :$data=undef, :$stat='count',
26 :$position='stack', :$width=undef,
27 :$na_rm=false, :$show_legend=undef, :$inherit_aes=true,
28 %rest)
29  
30 The "bar" geom makes the height bar proportional to the number of cases
31 in each group (or if the C<weight> aesthetic is supplied, the sum of the
32 C<weights>).
33 It uses C<stat_count()> by default: it counts the number of cases at
34 each x position.
35  
36 Arguments:
37  
38 =over 4
39  
40 %TMPL_COMMON_ARGS%
41  
42 =item * $width
43  
44 Bar width. By default, set to 90% of the resolution of the data.
45  
46 =back
47  
48 See also L<Chart::GGPlot::Stat::Functions/stat_count>.
49  
50 EOT
51  
52 2 ▶my $geom_bar_code = fun (
Subroutine __ANON__: called CC=7 SCAR=20.4
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and @_ % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun (anon)") 0 2 2
Condition66%2/3
@_ > 0 and @_ % 2
!ll&&!rl&&r
110
MC/DC0%0/2
@_ > 0 and @_ % 2
@_ > 0 @_ % 2
Truth table66%2/3
A and B
A= @_ > 0B= @_ % 2
ABresult
0X0
100
111
53 :$mapping = undef, :$data = undef,
54 :$stat = 'count', :$position = 'stack',
55 :$width = undef, :$na_rm = false,
56 :$show_legend = undef, :$inherit_aes = true,
57 %rest )
58 2 ▶{
Branch: 4 branches
BranchTrueFalseTotal
exists $rest{'stat'} ? : 0 2 2
exists $rest{'position'} ? : 0 2 2
exists $rest{'na_rm'} ? : 0 2 2
exists $rest{'inherit_aes'} ? : 0 2 2
59 2 return Chart::GGPlot::Layer->new(
60 data => $data,
61 mapping => $mapping,
62 stat => $stat,
63 geom => 'bar',
64 position => $position,
65 show_legend => $show_legend,
66 inherit_aes => $inherit_aes,
67 params => {
68 width => $width,
69 na_rm => $na_rm,
70 %rest,
71 },
72 );
73 5 };
74  
75 5 my $geom_histogram_pod = layer_func_pod(<<'EOT');
76  
77 geom_histogram(:$mapping=undef, :$data=undef, :$stat="bin",
78 :$position="stack", :$binwidth=undef, :$bins=undef,
79 :$na_rm=false, :$show_legend=undef, :$inherit_aes=true,
80 %rest)
81  
82 Visualise the distribution of a single continuous variable by dividing
83 the x axis into bins and counting the number of observations in each
84 bin. This "histogram" geom displays the counts with bars.
85  
86 =over 4
87  
88 %TMPL_COMMON_ARGS%
89  
90 =item * $binwidth
91  
92 The width of the bins.
93 Can be specified as a numeric value, or a function that calculates width
94 from x. The default is to use C<$bins> bins that cover the range of the
95 data.
96  
97 =item * $bins
98  
99 Number of bins. Overridden by C<$binwidth>. Defaults to 30.
100  
101 You should always override this C<$bins> or C<$binwidth>, exploring
102 multiple widths to find the best to illustrate the stories in your data.
103  
104 =back
105  
106 See also L<Chart::GGPlot::Stat::Functions/stat_bin>.
107  
108 EOT
109  
110 1 ▶my $geom_histogram_code = fun (
Subroutine __ANON__: called CC=7 SCAR=20.7
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and @_ % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun (anon)") 0 1 1
Condition33%1/3
@_ > 0 and @_ % 2
!ll&&!rl&&r
100
MC/DC0%0/2
@_ > 0 and @_ % 2
@_ > 0 @_ % 2
Truth table33%1/3
A and B
A= @_ > 0B= @_ % 2
ABresult
0X0
100
111
111 :$data = undef, :$mapping = undef,
112 :$stat = "bin", :$position = "stack",
113 :$binwidth = undef, :$bins = undef,
114 :$na_rm = false,
115 :$show_legend = undef, :$inherit_aes = true,
116 %rest )
117 1 ▶{
Branch: 4 branches
BranchTrueFalseTotal
exists $rest{'stat'} ? : 0 1 1
exists $rest{'position'} ? : 0 1 1
exists $rest{'na_rm'} ? : 0 1 1
exists $rest{'inherit_aes'} ? : 0 1 1
118 1 return Chart::GGPlot::Layer->new(
119 data => $data,
120 mapping => $mapping,
121 stat => $stat,
122 geom => 'bar',
123 position => $position,
124 show_legend => $show_legend,
125 inherit_aes => $inherit_aes,
126 params => {
127 binwidth => $binwidth,
128 bins => $bins,
129 na_rm => $na_rm,
130 pad => false,
131 %rest
132 },
133 );
134 5 };
135  
136 5 my $geom_col_pod = layer_func_pod(<<'EOT');
137  
138 geom_col(:$mapping=undef, :$data=undef, :$position="stack",
139 :$width=undef, :$na_rm=false, :$show_legend=undef,
140 :$inherit_aes=true,
141 %rest)
142  
143 Bar plot. Different from geom_bar(), geom_col() uses stat_identity():
144 it leaves the data as is.
145  
146 =over 4
147  
148 %TMPL_COMMON_ARGS%
149  
150 =item * $width
151  
152 Bar width. By default, set to 90% of the resolution of the data.
153  
154 =back
155  
156 EOT
157  
158 0 ▶my $geom_col_code = fun (
Subroutine __ANON__: not called CC=6 SCAR=37.4
Branch: 1 branch
BranchTrueFalseTotal
@_ > 0 and @_ % 2 and Function::Parameters::_croak("Odd number of paired arguments for fun (anon)") 0 0 0
Condition0%0/3
@_ > 0 and @_ % 2
!ll&&!rl&&r
000
MC/DC0%0/2
@_ > 0 and @_ % 2
@_ > 0 @_ % 2
Truth table0%0/3
A and B
A= @_ > 0B= @_ % 2
ABresult
0X0
100
111
159 :$data = undef, :$mapping = undef, :$position = "stack",
160 :$width = undef,
161 :$na_rm = false, :$show_legend = undef, :$inherit_aes = true,
162 %rest )
163 0 ▶{
Branch: 3 branches
BranchTrueFalseTotal
exists $rest{'position'} ? : 0 0 0
exists $rest{'na_rm'} ? : 0 0 0
exists $rest{'inherit_aes'} ? : 0 0 0
164 0 return Chart::GGPlot::Layer->new(
165 data => $data,
166 mapping => $mapping,
167 stat => 'identity',
168 geom => 'bar',
169 position => $position,
170 show_legend => $show_legend,
171 inherit_aes => $inherit_aes,
172 params => {
173 width => $width,
174 na_rm => $na_rm,
175 %rest,
176 },
177 );
178 5 };
179  
180 5 ▶classmethod ggplot_functions() {
Subroutine ggplot_functions: called CC=3 SCAR=11.3
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for classmethod ggplot_functions (expected 1, got ' . @_ . ')') 0 5 5
@_ > 1 and Function::Parameters::_croak('Too many arguments for classmethod ggplot_functions (expected 1, got ' . @_ . ')') 0 5 5
181 return [
182 {
183 5 name => 'geom_bar',
184 code => $geom_bar_code,
185 pod => $geom_bar_pod,
186 },
187 {
188 name => 'geom_histogram',
189 code => $geom_histogram_code,
190 pod => $geom_histogram_pod,
191 },
192 {
193 name => 'geom_col',
194 code => $geom_col_code,
195 pod => $geom_col_pod,
196 },
197 ];
198 5 }
199  
200 1 ▶method setup_data ($data, $params) {
Subroutine setup_data: called CC=5 SCAR=17.2
Pod undocumented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 3 and Function::Parameters::_croak('Too few arguments for method setup_data (expected 3, got ' . @_ . ')') 0 1 1
@_ > 3 and Function::Parameters::_croak('Too many arguments for method setup_data (expected 3, got ' . @_ . ')') 0 1 1
201 1 ▶ unless ( $data->exists('width') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->exists("width")) 0 1 1
202 0 ▶ $data->set( 'width',
Condition0%0/3
$params->at("width") // pdl(resolution($data->at("x"), &false()) * "0.9")
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->at("width") // pdl(resolution($data->at("x"), &false()) * "0.9")
$params->at("width") pdl(resolution($data->at("x"), &false()) * "0.9")
Truth table0%0/3
A // B
A= $params->at("width")B= pdl(resolution($data->at("x"), &false()) * "0.9")
ABresult
1X1
011
000
203 $params->at('width')
204 // pdl( resolution( $data->at('x'), false ) * 0.9 ) );
205 }
206 1 ▶ return $data->transform( {
Branch: 1 branch
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 1 1
207 1 ▶ ymin => fun($col, $df) { pmin($df->at('y'), 0) },
Subroutine __ANON__: called CC=3 SCAR=11.3
Branch: 2 branches
BranchTrueFalseTotal
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 1 1
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 1 1
208 1 ▶ ymax => fun($col, $df) { pmax($df->at('y'), 0) },
Subroutine __ANON__: called CC=3 SCAR=11.3
Branch: 2 branches
BranchTrueFalseTotal
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 1 1
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 1 1
209 1 ▶ xmin => fun($col, $df) { $df->at('x') - $df->at('width') / 2 },
Subroutine __ANON__: called CC=3 SCAR=11.3
Branch: 2 branches
BranchTrueFalseTotal
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 1 1
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 1 1
210 1 ▶ xmax => fun($col, $df) { $df->at('x') + $df->at('width') / 2 },
Subroutine __ANON__: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 1 1
211 width => undef,
212 } );
213 5 }
214  
215 5 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
216  
217 5 1;
218  
219 __END__
« Chart/GGPlot/Plot.pm ↑ Summary Chart/GGPlot/Coord.pm »