| 1 | package Term::Fabulous::Chart::Scale::Category; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use v5.32; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use feature 'signatures'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Object::Pad 0.825; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Term::Fabulous::Chart::Scale; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | class Term::Fabulous::Chart::Scale::Category :isa(Term::Fabulous::Chart::Scale) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use List::Util (); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use POSIX qw(ceil); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use constant SHORTEST_CUT_LABEL => 4; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | field $labels :param; | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | field $band :param = 1; | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | field $offset :param = 0; # cells before the first slot | |||||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | field $pitch :param = undef; # cells per slot; undef: the cells shared evenly | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | method kind () { return 'category' } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
kind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | method is_band () { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_band: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 2 | return $band; | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | method labels () { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
labels: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | return @$labels; | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | ▶ | method count () { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | return scalar @$labels; | |||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # The label of category number $index (from 0); undef past the last. | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 3 | ▶ | method label_at ($index) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
label_at: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | return $labels->[$index]; | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # The position of category number $index (from 0). | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 35 | ▶ | method position ($index) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
position: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 35 | ▶ | my $count = @$labels || 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
@$labels || 1
MC/DC0%0/1
@$labels || 1
@$labels
Truth table50%1/2
A
A= @$labels
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 35 | ▶ | return ( $offset + ( $index + 0.5 ) * $self->slot_cells ) / $self->cells if $band; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 3 | ▶ | return $count > 1 ? $index / ( $count - 1 ) : 0.5; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 3 | ▶ | method value_at ($position) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
value_at: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | my $count = @$labels || 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
@$labels || 1
MC/DC0%0/1
@$labels || 1
@$labels
Truth table50%1/2
A
A= @$labels
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | ▶ | return ( $position * $self->cells - $offset ) / $self->slot_cells - 0.5 if $band; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | return $position * ( $count - 1 ); | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # The cells one category's slot takes. | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 38 | ▶ | method slot_cells () { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
slot_cells: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 38 | ▶ | my $count = @$labels || 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
@$labels || 1
MC/DC0%0/1
@$labels || 1
@$labels
Truth table50%1/2
A
A= @$labels
| |||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 38 | ▶ | return $pitch // $self->cells / $count if $band; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$pitch // $self->cells / $count
MC/DC100%1/1
$pitch // $self->cells / $count
$pitch
Truth table100%2/2
A
A= $pitch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | return $count > 1 ? ( $self->cells - 1 ) / ( $count - 1 ) : $self->cells; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # Every category is a tick. On a horizontal axis whose slots are too | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # narrow for the labels, only every second (third, ...) one is | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # labeled; labels that are only a little too wide are cut by the chart. | ||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 9 | ▶ | method fit :common (%options) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fit: called CC=19 SCAR=29.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 9 | ▶ | my $cells = $options{cells} // croak "$class: fit needs cells"; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options{"cells"} // croak("${class}: fit needs cells")
MC/DC100%1/1
$options{"cells"} // croak("${class}: fit needs cells")
$options{"cells"}
Truth table100%2/2
A
A= $options{"cells"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 8 | ▶ | croak "$class: cells must be a positive integer, got $cells" unless $cells =~ /\A[1-9][0-9]*\z/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 8 | ▶ | my @labels = ( $options{labels} // [] )->@*; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options{"labels"} // []
MC/DC0%0/1
$options{"labels"} // []
$options{"labels"}
Truth table50%1/2
A
A= $options{"labels"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | ▶ | my $band = $options{band} // 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options{"band"} // 1
MC/DC100%1/1
$options{"band"} // 1
$options{"band"}
Truth table100%2/2
A
A= $options{"band"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | my $measure = $options{measure} // sub ($label) { length $label }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.1
Condition66%2/3
$options{"measure"} // sub ($label) {
length $label;
}
MC/DC0%0/2
$options{"measure"} // sub ($label) {
length $label;
}
$options{"measure"}
sub ($label) {
length $label;
}
Truth table66%2/3
A // B
A= $options{"measure"}B= sub ($label) {
length $label;
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 8 | ▶ | my $vertical = ( $options{orientation} // 'horizontal' ) eq 'vertical'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options{"orientation"} // "horizontal"
MC/DC100%1/1
$options{"orientation"} // "horizontal"
$options{"orientation"}
Truth table100%2/2
A
A= $options{"orientation"}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 8 | ▶ | my $count = @labels || 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
@labels || 1
MC/DC0%0/1
@labels || 1
@labels
Truth table50%1/2
A
A= @labels
| |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 8 | ▶ | my $slot = $band ? $cells / $count : ( $count > 1 ? ( $cells - 1 ) / ( $count - 1 ) : $cells ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # Slots of whole cells keep the gaps between bars equal; the cells | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # left over go to both ends, unless that wastes too much room. | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 8 | my ( $pitch, $offset ) = ( undef, 0 ); | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | 8 | ▶ | if ( $band && $slot >= 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$band and $slot >= 1
MC/DC100%2/2
$band and $slot >= 1
$band
$slot >= 1
Truth table100%3/3
A and B
A= $bandB= $slot >= 1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 5 | my $whole = int $slot; | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | 5 | my $spare = $cells - $whole * $count; | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | 5 | ▶ | ( $pitch, $offset, $slot ) = ( $whole, int( $spare / 2 ), $whole ) if $spare <= List::Util::max( 3, $cells / 5 ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 8 | my $widest = List::Util::max( 0, map { $measure->($_) } @labels ); | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | 8 | my $stride = 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 8 | ▶ | if ( !$vertical && $widest + 1 > $slot && $slot < SHORTEST_CUT_LABEL + 1 ) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition83%5/6
not $vertical and $widest + 1 > $slot and $slot < 5
not $vertical and $widest + 1 > $slot
MC/DC66%2/3
not $vertical and $widest + 1 > $slot and $slot < 5
not $vertical
$widest + 1 > $slot
$slot < 5
Truth table75%3/4
not(A) and B and C
A= not $verticalB= $widest + 1 > $slotC= $slot < 5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 2 | ▶ | $stride = ceil( ( List::Util::min( $widest, SHORTEST_CUT_LABEL ) + 1 ) / ( $slot || 1 ) ); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$slot || 1
MC/DC0%0/1
$slot || 1
$slot
Truth table50%1/2
A
A= $slot
| |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | ▶ | elsif ( $vertical && $slot < 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$vertical and $slot < 1
MC/DC50%1/2
$vertical and $slot < 1
$vertical
$slot < 1
Truth table66%2/3
A and B
A= $verticalB= $slot < 1
| |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | $stride = ceil( 1 / $slot ); | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 8 | my @ticks = map { { value => $_, label => $labels[$_] } } grep { $_ % $stride == 0 } 0 .. $#labels; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | 8 | return $class->new( min => 0, max => List::Util::max( 0, $#labels ), cells => $cells, used => $cells, labels => \@labels, band => $band, pitch => $pitch, offset => $offset, ticks => \@ticks ); | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||