| 1 | package Chart::GGPlot::Guides; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | # ABSTRACT: The container of guides | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Chart::GGPlot::Class qw(:pdl); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '0.002003'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use List::AllUtils qw(uniq); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Module::Load; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | ▶ | has _guides => (is => 'ro', default => sub { {} }); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 0 | ▶ | my $defined_or = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 0 | my ( $obj, $attr, $or ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | # Have to make $or a coderef. Too bad Perl 5 does not support macros. | ||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | ▶ | unless ( defined $obj->{$attr} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | $obj->{$attr} = $or->(); | |||||||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | # Train each scale in scales and generate the definition of guide. | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | method build ($scales, :$layers, :$labels, :$default_mapping=undef, %rest) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
build: not called CC=18 SCAR=58.3
Pod
documented
Branch: 4 branches
Condition0%0/3
@_ > 2 and (@_ - 2) % 2
MC/DC0%0/2
@_ > 2 and (@_ - 2) % 2
@_ > 2
(@_ - 2) % 2
Truth table0%0/3
A and B
A= @_ > 2B= (@_ - 2) % 2
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | my @gdefs; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | for my $scale ($scales->scales->flatten) { | |||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | for my $output ( $scale->aesthetics->flatten ) { | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | my $guide = $self->get_guide($output) // $scale->guide; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->get_guide($output) // $scale->guide
MC/DC0%0/2
$self->get_guide($output) // $scale->guide
$self->get_guide($output)
$scale->guide
Truth table0%0/3
A // B
A= $self->get_guide($output)B= $scale->guide
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | next unless (defined $guide and $guide ne 'none'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $guide and $guide ne "none"
MC/DC0%0/2
defined $guide and $guide ne "none"
defined $guide
$guide ne "none"
Truth table0%0/3
A and B
A= defined $guideB= $guide ne "none"
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | # Check the validity of guide. | ||||||||||||||||||||||||||||||||||||||||||
| 35 | # If guide is a string, then find the guide object. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | $guide = $self->_validate_guide($guide); | |||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||
| 38 | # Check the consistency of the guide and scale. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | my $guide_available_aes = $guide->available_aes; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ( defined $guide_available_aes | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $guide_available_aes and $guide_available_aes->intersect($scale->aesthetics)->length == 0
MC/DC0%0/2
defined $guide_available_aes and $guide_available_aes->intersect($scale->aesthetics)->length == 0
defined $guide_available_aes
$guide_available_aes->intersect($scale->aesthetics)->length == 0
Truth table0%0/3
A and B
A= defined $guide_available_aesB= $guide_available_aes->intersect($scale->aesthetics)->length == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | and $guide_available_aes->intersect( $scale->aesthetics ) | ||||||||||||||||||||||||||||||||||||||||||
| 42 | ->length == 0 ) | ||||||||||||||||||||||||||||||||||||||||||
| 43 | { | ||||||||||||||||||||||||||||||||||||||||||
| 44 | die sprintf( "Guide %s cannot be used for %s.", | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $guide, join( ', ', @{ $scale->aesthetics } ) ); | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | unless (defined $guide->title) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | $guide->set( 'title', | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$scale->name // $labels->at($output)
MC/DC0%0/2
$scale->name // $labels->at($output)
$scale->name
$labels->at($output)
Truth table0%0/3
A // B
A= $scale->nameB= $labels->at($output)
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | $scale->make_title( $scale->name // $labels->at($output) ) | ||||||||||||||||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | $guide = $guide->train($scale, $output); | |||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | next unless defined $guide; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | List::AllUtils::none { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | my $matched = | |||||||||||||||||||||||||||||||||||||||||
| 60 | $self->_matched_aes( $_, $guide, $default_mapping ); | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | my $show_legend = $_->show_legend; | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | ( $matched->length > 0 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$matched->length > 0 && (!defined($show_legend) || $show_legend)
not defined $show_legend or $show_legend
MC/DC0%0/3
$matched->length > 0 && (!defined($show_legend) || $show_legend)
$matched->length > 0
not defined $show_legend
$show_legend
Truth table0%0/4
A && not(B) or C
A= $matched->length > 0B= not defined $show_legendC= $show_legend
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | and ( not defined $show_legend or $show_legend ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | @$layers | ||||||||||||||||||||||||||||||||||||||||||
| 66 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | if (defined $guide) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | push @gdefs, $guide; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | return \@gdefs; | |||||||||||||||||||||||||||||||||||||||||
| 77 | 2 | } | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | method set($key, $guide) { $self->_guides->{$key} = $guide; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: not called CC=3 SCAR=22.8
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | method get_guide($key) { $self->_guides->{$key}; } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_guide: not called CC=3 SCAR=22.8
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | method guides() { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
guides: not called CC=3 SCAR=24.8
Pod
undocumented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my $guides = $self->_guides; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | return [ map { $guides->{$_} } sort keys %$guides ]; | |||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | } | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | classmethod _validate_guide ($guide) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_guide: not called CC=4 SCAR=30.0
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ($guide->$_DOES('Chart::GGPlot::Guide')) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | return $guide; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | my $class_guide = 'Chart::GGPlot::Guide::' . ucfirst($guide); | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | load $class_guide; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | return $class_guide->new; | |||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | } | |||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | classmethod _matched_aes ($layer, $guide, $defaults) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_matched_aes: not called CC=4 SCAR=30.0
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | my $all = [ | ||||||||||||||||||||||||||||||||||||||||||
| 99 | uniq( | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | @{ $layer->mapping->names }, | |||||||||||||||||||||||||||||||||||||||||
| 101 | @{ | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | ( $layer->inherit_aes ? $defaults : $layer->stat->default_aes ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | ->names | ||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 106 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | my $geom_aes = [ @{ $layer->geom->required_aes }, | |||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | @{ $layer->geom->default_aes->names } ]; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | my $matched = $all->intersect($geom_aes)->intersect( $guide->key->names ); | |||||||||||||||||||||||||||||||||||||||||
| 110 | return $matched->setdiff( | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | [ @{ $layer->geom_params->names }, @{ $layer->aes_params->names } ] ); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 2 | } | |||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||||||||||||||||||||||||