| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 24 | if ( $log->is_debug ); | ||||||||||||||
| 25 | 6 | }; | |||||||||||||
| 26 | |||||||||||||||
| 27 | 6 | my $plot = $ggplot->clone(); | |||||||||||||
| 28 | |||||||||||||||
| 29 | 6 | ▶ | if ( $plot->layers->isempty ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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
| |||||||||||||||
| 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__ | ||||||||||||||