Chart/GGPlot/Backend.pm

filter: Statementstmt 98.3% 116 / 118 Branchbran 52.3% 22 / 42 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 21 / 21 Podpod 100% 1 / 1 totaltotal 87.9% 160 / 182 CC 22 SCAR 10.0
CC
22
Cov
86%
build
5.1
__ANON__
3.1
__ANON__
3.1
CRAP 2.7
« Chart/GGPlot/HasRequiredAes.pm ↑ Summary Chart/GGPlot/Aes/Functions.pm »
1 package Chart::GGPlot::Backend;
2  
3 # ABSTRACT: Role for backend classes for Chart::GGPlot
4  
5 1 ▶use Chart::GGPlot::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.002003'; # VERSION
9  
10 1 ▶use Chart::GGPlot::Layout;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Chart::GGPlot::Built;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 1 requires 'show';
15 1 requires 'save';
16  
17  
18 6 ▶classmethod build($ggplot) {
Subroutine build: called CC=5 SCAR=16.3
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for classmethod build (expected 2, got ' . @_ . ')') 0 6 6
@_ > 2 and Function::Parameters::_croak('Too many arguments for classmethod build (expected 2, got ' . @_ . ')') 0 6 6
19 # A convient function for debug logging.
20 68 ▶ state $debug_data = sub {
Subroutine __ANON__: called CC=2 SCAR=8.1
21 68 my ($dataframes, $step) = @_;
22 $log->debug( "Backend::build : data $step :\n"
23 0 ▶ . join( "\n", map { $_->string } @$dataframes ) )
Branch: 1 branch
BranchTrueFalseTotal
if $Chart::GGPlot::Backend::log->is_debug 0 68 68
24 if ( $log->is_debug );
25 6 };
26  
27 6 my $plot = $ggplot->clone();
28  
29 6 ▶ if ( $plot->layers->isempty ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plot->layers->isempty) 0 6 6
30 0 $plot->geom_blank();
31 }
32  
33 6 my $layers = $plot->layers;
34  
35 # if layer has data, then use layer's data, else use plot's data.
36 6 ▶ my $layer_data = $layers->map( sub { $_->layer_data( $plot->data ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
37  
38 6 my $scales = $plot->scales;
39  
40 6 my $layout = Chart::GGPlot::Layout->new(
41 facet => $plot->facet,
42 coord => $plot->coordinates
43 );
44 6 my $data = $layout->setup( $layer_data, $plot->data );
45  
46 # Apply function to layer and matching data
47 42 ▶ my $by_layer = fun($f) {
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
@_ < 1 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 42 42
@_ > 1 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 1, got ' . @_ . ')') 0 42 42
48 return [ 0 .. $#$data ]
49 42 ▶ ->map( sub { &$f( $layers->at($_), $data->[$_] ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
50 6 };
51  
52 # Compute aesthetics to produce data with generalised variable names
53 6 ▶ $data = &$by_layer( fun( $l, $d ) { $l->compute_aesthetics( $d, $plot ) } );
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
54 6 $debug_data->($data, 'after compute_aesthetics()');
55  
56 6 ▶ $data = $data->map(sub { $scales->transform_df($_); });
Subroutine __ANON__: called CC=1 SCAR=0.0
57 6 $debug_data->($data, 'after transform_df()');
58  
59 6 ▶ my $scale_x = sub { $scales->get_scales('x') };
Subroutine __ANON__: called CC=1 SCAR=0.0
60 6 ▶ my $scale_y = sub { $scales->get_scales('y') };
Subroutine __ANON__: called CC=1 SCAR=0.0
61 6 $layout->train_position( $data, $scale_x->(), $scale_y->() );
62  
63 # keep raw column on first time of map_position()
64 6 $data = $layout->map_position($data, true);
65 6 $debug_data->($data, 'after map_position()');
66  
67 # store prestats data.
68 6 my $prestats_data = $data->copy;
69  
70 # Apply and map statistics
71 6 ▶ $data = &$by_layer( fun( $l, $d ) { $l->compute_statistic( $d, $layout ) }
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
72 );
73  
74 6 $debug_data->($data, 'after compute_statistic()');
75 6 ▶ $data = &$by_layer( fun( $l, $d ) { $l->map_statistic( $d, $plot ) } );
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
76 6 $debug_data->($data, 'after map_statistic()');
77  
78 # Make sure missing (but required) aesthetics are added
79 6 $scales->add_missing( [qw(x y)] );
80  
81 # Reparameterise geoms from (e.g.) y and width to ymin and ymax
82 6 ▶ $data = &$by_layer( fun( $l, $d ) { $l->compute_geom_1($d); } );
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
83 6 $debug_data->($data, 'after compute_geom_1()');
84  
85 # Apply position adjustments
86 6 ▶ $data = &$by_layer( fun( $l, $d ) { $l->compute_position( $d, $layout ); }
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
87 );
88 6 $debug_data->($data, 'after compute_position()');
89  
90 # Reset position scales, then re-train and map. This ensures that facets
91 # have control over the range of a plot: is it generated from what is
92 # displayed, or does it include the range of underlying data
93 6 $layout->reset_scales();
94  
95 6 $layout->train_position( $data, $scale_x->(), $scale_y->() );
96 6 $layout->setup_panel_params();
97 6 $data = $layout->map_position($data);
98 6 $debug_data->($data, 'after map_position() again');
99  
100 # Train and map non-position scales
101 6 my $npscales = $scales->non_position_scales();
102 6 ▶ if ( $npscales->length > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($npscales->length > 0) 2 4 6
103 2 ▶ $data->map(sub { $npscales->train_df($_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
104 2 ▶ $data = $data->map(sub { $npscales->map_df($_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
105 2 $debug_data->($data, 'after mapping non-position scales');
106 }
107  
108 6 ▶ $data = &$by_layer( fun( $l, $d ) { $l->compute_geom_2($d) } );
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
109 6 $debug_data->($data, 'after compute_geom_2()');
110 6 ▶ $data = &$by_layer( fun( $l, $d ) { $l->finish_statistics($d) } );
Subroutine __ANON__: called CC=3 SCAR=11.2
Branch: 2 branches
BranchTrueFalseTotal
@_ < 2 and Function::Parameters::_croak('Too few arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
@_ > 2 and Function::Parameters::_croak('Too many arguments for fun (anon) (expected 2, got ' . @_ . ')') 0 6 6
111 6 $debug_data->($data, 'after finish_statistics()');
112  
113 6 $data = $layout->finish_data($data);
114 6 $debug_data->($data, 'after finish_data()');
115  
116 6 return Chart::GGPlot::Built->new(
117 data => $data,
118 layout => $layout,
119 plot => $plot,
120 prestats_data => $prestats_data,
121 );
122 1 }
123  
124 1 1;
125  
126 __END__
« Chart/GGPlot/HasRequiredAes.pm ↑ Summary Chart/GGPlot/Aes/Functions.pm »