blib/lib/Term/Fabulous/Chart/Scale/Linear.pm

filter: Statementstmt 100% 132 / 132 Branchbran 91.9% 57 / 62 Conditioncond 91.5% 54 / 59 MC/DCmcdc 76.4% 26 / 34 Subroutinesub 100% 21 / 21 Podpod 100% 5 / 5 totaltotal 94.2% 295 / 313 CC 56 SCAR 29.3
CC
56
Cov
96%
fit
35.1
_nice_steps
9.0
_candidate
7.0
CRAP 18.7
« blib/lib/Term/Fabulous/Render/Geometry.pm ↑ Summary blib/lib/Term/Fabulous/Role/Themed.pm »
1 package Term::Fabulous::Chart::Scale::Linear;
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::Linear :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 floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use Term::Fabulous::Chart::Format qw(number_formatter);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶ use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 EPSILON => 1e-9,
22 MOST_TICKS => 10_000, # more never fit an axis; a step that needs them is skipped
23 1 };
24  
25 1 ▶ field $step :param :reader;
Subroutine step: called CC=1 SCAR=0.0
Pod documented
26  
27 1 ▶ method kind () { return 'linear' }
Subroutine kind: called CC=1 SCAR=0.0
Pod documented
28  
29 67 ▶ method position ($value) {
Subroutine position: called CC=1 SCAR=0.0
Pod documented
30 67 my ( $low, $high ) = ( $self->min, $self->max );
31 67 return ( $value - $low ) / ( $high - $low );
32 1 }
33  
34 1 ▶ method value_at ($position) {
Subroutine value_at: called CC=1 SCAR=0.0
Pod documented
35 1 return $self->min + $position * ( $self->max - $self->min );
36 1 }
37  
38 # The nice steps (1, 2, 2.5 and 5 times a power of ten) between two
39 # sizes. Whole-number data takes no steps below 1 and no 2.5 below 25.
40 21 ▶ sub _nice_steps ( $smallest, $largest, $integer ) {
Subroutine _nice_steps: called CC=9 SCAR=22.0
41 21 my @steps;
42 21 foreach my $exponent ( floor( log($smallest) / log(10) ) - 1 .. ceil( log($largest) / log(10) ) + 1 ) {
43 102 foreach my $mantissa ( 1, 2, 2.5, 5 ) {
44 408 my $step = $mantissa * 10**$exponent;
45 408 ▶ next if $integer && ( $step < 1 || ( $mantissa == 2.5 && $step < 25 ) );
Branch: 1 branch
BranchTrueFalseTotal
if $integer and $step < 1 || $mantissa == "2.5" && $step < 25 22 386 408
Condition100%9/9
$integer and $step < 1 || $mantissa == "2.5" && $step < 25
!ll&&!rl&&r
3642222
$step < 1 or $mantissa == "2.5" and $step < 25
l!l&&r!l&&!r
20222
$mantissa == "2.5" and $step < 25
!ll&&!rl&&r
1842
MC/DC100%4/4
$integer and $step < 1 || $mantissa == "2.5" && $step < 25
$integer $step < 1 $mantissa == "2.5" $step < 25
Truth table100%5/5
A and B or C and D
A= $integerB= $step < 1C= $mantissa == "2.5"D= $step < 25
ABCDresult
0XXX0
100X0
10100
11XX1
10111
46 386 ▶ push @steps, $step if $step >= $smallest * ( 1 - EPSILON ) && $step <= $largest * ( 1 + EPSILON );
Branch: 1 branch
BranchTrueFalseTotal
if $step >= $smallest * "0.999999999" and $step <= $largest * "1.000000001" 93 293 386
Condition100%3/3
$step >= $smallest * "0.999999999" and $step <= $largest * "1.000000001"
!ll&&!rl&&r
13016393
MC/DC100%2/2
$step >= $smallest * "0.999999999" and $step <= $largest * "1.000000001"
$step >= $smallest * "0.999999999" $step <= $largest * "1.000000001"
Truth table100%3/3
A and B
A= $step >= $smallest * "0.999999999"B= $step <= $largest * "1.000000001"
ABresult
0X0
100
111
47 }
48 }
49 21 return @steps;
50 }
51  
52 # The multiples of $step from $low to $high; none when there would be
53 # more than MOST_TICKS.
54 95 ▶ sub _multiples ( $low, $high, $step ) {
Subroutine _multiples: called CC=4 SCAR=13.9
55 95 my $first = ceil( $low / $step - EPSILON );
56 95 ▶ return () if floor( $high / $step + EPSILON ) - $first + 1 > MOST_TICKS;
Branch: 1 branch
BranchTrueFalseTotal
if floor($high / $step + "1e-09") - $first + 1 > 10000 1 94 95
57 94 my @values;
58 94 ▶ for ( my $index = $first; $index * $step <= $high + $step * EPSILON; $index++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index * $step <= $high + $step * "1e-09") 586 94 680
59 586 my $value = $index * $step;
60 586 ▶ push @values, abs($value) < $step * EPSILON ? 0 : $value;
Branch: 1 branch
BranchTrueFalseTotal
abs $value < $step * 1e-09 ? : 75 511 586
61 }
62 94 return @values;
63 }
64  
65 # Fits a scale to data and to the cells of an axis. See the POD.
66 27 ▶ method fit :common (%options) {
Subroutine fit: called CC=35 SCAR=35.6
Pod documented
67 27 ▶ my $cells = $options{cells} // croak "$class: fit needs cells";
Condition100%2/2
$options{"cells"} // croak("${class}: fit needs cells")
l!l
261
MC/DC100%1/1
$options{"cells"} // croak("${class}: fit needs cells")
$options{"cells"}
Truth table100%2/2
A
A= $options{"cells"}
Aresult
11
00
68 26 ▶ croak "$class: cells must be a positive integer, got $cells" unless $cells =~ /\A[1-9][0-9]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cells =~ /\A[1-9][0-9]*\z/u 1 25 26
69 25 ▶ my $vertical = ( $options{orientation} // 'vertical' ) eq 'vertical';
Condition100%2/2
$options{"orientation"} // "vertical"
l!l
421
MC/DC100%1/1
$options{"orientation"} // "vertical"
$options{"orientation"}
Truth table100%2/2
A
A= $options{"orientation"}
Aresult
11
00
70 25 ▶ my $align = $options{align} // $vertical;
Condition66%2/3
$options{"align"} // $vertical
l!l&&r!l&&!r
1240
MC/DC0%0/2
$options{"align"} // $vertical
$options{"align"} $vertical
Truth table66%2/3
A // B
A= $options{"align"}B= $vertical
ABresult
1X1
011
000
71 25 my ( $fixed_low, $fixed_high ) = @options{qw(min max)};
72 25 ▶ croak "$class: min ($fixed_low) must be less than max ($fixed_high)" if defined $fixed_low && defined $fixed_high && $fixed_low >= $fixed_high;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fixed_low and defined $fixed_high and $fixed_low >= $fixed_high 1 24 25
Condition100%6/6
defined $fixed_low and defined $fixed_high and $fixed_low >= $fixed_high
!ll&&!rl&&r
2311
defined $fixed_low and defined $fixed_high
!ll&&!rl&&r
2212
MC/DC100%3/3
defined $fixed_low and defined $fixed_high and $fixed_low >= $fixed_high
defined $fixed_low defined $fixed_high $fixed_low >= $fixed_high
Truth table100%4/4
A and B and C
A= defined $fixed_lowB= defined $fixed_highC= $fixed_low >= $fixed_high
ABCresult
0XX0
10X0
1100
1111
73  
74 24 ▶ my ( $low, $high ) = $options{extent} ? $options{extent}->@* : ( 0, 1 );
Branch: 1 branch
BranchTrueFalseTotal
$options{'extent'} ? : 23 1 24
75 24 ▶ ( $low, $high ) = ( List::Util::min( $low, 0 ), List::Util::max( $high, 0 ) ) if $options{zero};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"zero"} 1 23 24
76 24 ▶ $low = $fixed_low if defined $fixed_low;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fixed_low 2 22 24
77 24 ▶ $high = $fixed_high if defined $fixed_high;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fixed_high 1 23 24
78 24 ▶ ( $low, $high ) = _widened( $low, $high, $fixed_low, $fixed_high ) if $high <= $low;
Branch: 1 branch
BranchTrueFalseTotal
if $high <= $low 1 23 24
79  
80 24 ▶ 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; }
l!l&&r!l&&!r
1230
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; }
ABresult
1X1
011
000
81 my @steps
82 = defined $options{step}
83 ? ( $options{step} )
84 24 ▶ : _nice_steps( ( $high - $low ) / List::Util::max( 1, $cells - 1 ), $high - $low, $options{integer} );
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'step'} ? : 3 21 24
85 24 my $wanted_ticks = $options{ticks};
86  
87 24 my $best;
88 24 foreach my $step (@steps) {
89 96 ▶ next if ( $high - $low ) / $step > $cells; # more intervals than cells: no tick could get a cell of its own
Branch: 1 branch
BranchTrueFalseTotal
if ($high - $low) / $step > $cells 1 95 96
90 95 ▶ my $candidate = _candidate( $low, $high, $step, $fixed_low, $fixed_high, $options{nice} // 1 ) // next;
Condition100%4/4
_candidate($low, $high, $step, $fixed_low, $fixed_high, $options{"nice"} // 1) // (next)
l!l
941
$options{"nice"} // 1
l!l
491
MC/DC100%1/1
_candidate($low, $high, $step, $fixed_low, $fixed_high, $options{"nice"} // 1) // (next)
_candidate($low, $high, $step, $fixed_low, $fixed_high, $options{"nice"} // 1)
MC/DC100%1/1
$options{"nice"} // 1
$options{"nice"}
Truth table100%2/2
A
A= _candidate($low, $high, $step, $fixed_low, $fixed_high, $options{"nice"} // 1)
Aresult
11
00
Truth table100%2/2
A
A= $options{"nice"}
Aresult
11
00
91 94 my ( $domain_low, $domain_high, $values ) = @$candidate{qw(low high values)};
92 94 my $intervals = @$values - 1;
93 94 ▶ next if $intervals < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $intervals < 1 1 93 94
94  
95 93 my $format = number_formatter( $options{format}, $step, List::Util::max( map { abs } @$values ) );
96 93 my @labels = map { $format->($_) } @$values;
97 93 my $widest = List::Util::max( map { $measure->($_) } @labels );
98 93 my ( $least, $ideal );
99 93 ▶ if ($vertical) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vertical) { } 71 22 93
100 71 ▶ ( $least, $ideal ) = ( $cells >= 8 ? 2 : 1, $cells >= 8 ? List::Util::min( 6, List::Util::max( 3, $cells / 4 ) ) : 2 );
Branch: 2 branches
BranchTrueFalseTotal
$cells >= 8 ? : 70 1 71
$cells >= 8 ? : 70 1 71
101 }
102 else {
103 22 ( $least, $ideal ) = ( $widest + 2, List::Util::max( $widest + 4, 10 ) );
104 }
105 93 my $headroom = ( ( $domain_high - $domain_low ) - ( $high - $low ) ) / ( $domain_high - $domain_low );
106  
107 # Evenly spaced ticks from end to end land on whole cells when the
108 # axis spans a multiple of the intervals, which may leave a few
109 # cells unused. Ticks that do not span the domain (a fixed end
110 # between two of them) are placed as near as they can be.
111 93 ▶ my $spans_domain = abs( $values->[0] - $domain_low ) < $step * EPSILON && abs( $values->[-1] - $domain_high ) < $step * EPSILON;
Condition100%3/3
abs $values->[0] - $domain_low < $step * 1e-09 && abs $values->[-1] - $domain_high < $step * 1e-09
!ll&&!rl&&r
7383
MC/DC100%2/2
abs $values->[0] - $domain_low < $step * 1e-09 && abs $values->[-1] - $domain_high < $step * 1e-09
abs $values->[0] - $domain_low < $step * 1e-09 abs $values->[-1] - $domain_high < $step * 1e-09
Truth table100%3/3
A && B
A= abs $values->[0] - $domain_low < $step * 1e-09B= abs $values->[-1] - $domain_high < $step * 1e-09
ABresult
0X0
100
111
112 93 ▶ my ( $per_interval, $used )
Branch: 1 branch
BranchTrueFalseTotal
$align && $spans_domain ? : 66 27 93
Condition100%3/3
$align and $spans_domain
!ll&&!rl&&r
171066
MC/DC100%2/2
$align and $spans_domain
$align $spans_domain
Truth table100%3/3
A and B
A= $alignB= $spans_domain
ABresult
0X0
100
111
113 = $align && $spans_domain
114 ? ( floor( ( $cells - 1 ) / $intervals ), $intervals * floor( ( $cells - 1 ) / $intervals ) + 1 )
115 : ( ( $cells - 1 ) * ( $values->[-1] - $values->[0] ) / ( $domain_high - $domain_low ) / $intervals, $cells );
116 93 ▶ next if $per_interval < 1 || ( $per_interval < $least && !defined $options{step} );
Branch: 1 branch
BranchTrueFalseTotal
if $per_interval < 1 or $per_interval < $least and not defined $options{"step"} 29 64 93
Condition83%5/6
$per_interval < 1 or $per_interval < $least and not defined $options{"step"}
l!l&&r!l&&!r
12864
$per_interval < $least and not defined $options{"step"}
!ll&&!rl&&r
64028
MC/DC66%2/3
$per_interval < 1 or $per_interval < $least and not defined $options{"step"}
$per_interval < 1 $per_interval < $least not defined $options{"step"}
Truth table75%3/4
A or B and not(C)
A= $per_interval < 1B= $per_interval < $leastC= not defined $options{"step"}
ABCresult
1XX1
0111
00X0
0100
117 64 my $waste = ( $cells - $used ) / $cells;
118 64 ▶ my $density = defined $wanted_ticks ? abs( @$values - $wanted_ticks ) / $wanted_ticks : abs( log( $per_interval / $ideal ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $wanted_ticks ? : 4 60 64
119 64 ▶ my $score = 3 * $waste + 3 * $headroom + $density + ( @$values < 3 && $cells >= 6 ? 0.5 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
@$values < 3 && $cells >= 6 ? : 9 55 64
Condition66%2/3
@$values < 3 and $cells >= 6
!ll&&!rl&&r
5509
MC/DC50%1/2
@$values < 3 and $cells >= 6
@$values < 3 $cells >= 6
Truth table66%2/3
A and B
A= @$values < 3B= $cells >= 6
ABresult
0X0
100
111
120 $best = { score => $score, step => $step, low => $domain_low, high => $domain_high, used => $used, values => $values, labels => \@labels }
121 64 ▶ if !defined $best || $score < $best->{score} - EPSILON;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $best or $score < $best->{"score"} - "1e-09" 36 28 64
Condition100%3/3
not defined $best or $score < $best->{"score"} - "1e-09"
l!l&&r!l&&!r
211528
MC/DC100%2/2
not defined $best or $score < $best->{"score"} - "1e-09"
not defined $best $score < $best->{"score"} - "1e-09"
Truth table100%3/3
not(A) or B
A= not defined $bestB= $score < $best->{"score"} - "1e-09"
ABresult
1X1
011
000
122 }
123  
124 # Too little room for any two ticks: the two ends.
125 24 ▶ $best //= do {
Condition66%2/3
$best //= do { my $format = number_formatter($options{"format"}, $high - $low, &List::Util::max(abs $low, abs $high)); +{"step", $high - $low, "low", $low, "high", $high, "used", $cells, "values", [$low, $high], "labels", [map({&$format($_);} $low, $high)]} }
l!l&&r!l&&!r
2130
MC/DC0%0/2
$best //= do { my $format = number_formatter($options{"format"}, $high - $low, &List::Util::max(abs $low, abs $high)); +{"step", $high - $low, "low", $low, "high", $high, "used", $cells, "values", [$low, $high], "labels", [map({&$format($_);} $low, $high)]} }
$best do { my $format = number_formatter($options{"format"}, $high - $low, &List::Util::max(abs $low, abs $high)); +{"step", $high - $low, "low", $low, "high", $high, "used", $cells, "values", [$low, $high], "labels", [map({&$format($_);} $low, $high)]} }
Truth table66%2/3
A //= B
A= $bestB= do { my $format = number_formatter($options{"format"}, $high - $low, &List::Util::max(abs $low, abs $high)); +{"step", $high - $low, "low", $low, "high", $high, "used", $cells, "values", [$low, $high], "labels", [map({&$format($_);} $low, $high)]} }
ABresult
1X1
011
000
126 3 my $format = number_formatter( $options{format}, $high - $low, List::Util::max( abs $low, abs $high ) );
127 3 { step => $high - $low, low => $low, high => $high, used => $cells, values => [ $low, $high ], labels => [ map { $format->($_) } $low, $high ] };
128 };
129 24 my @ticks = map { { value => $best->{values}[$_], label => $best->{labels}[$_] } } 0 .. $best->{values}->$#*;
130 24 return $class->new( min => $best->{low}, max => $best->{high}, cells => $cells, used => $best->{used}, step => $best->{step}, ticks => \@ticks );
131 1 }
132  
133 1 ▶ sub _widened ( $low, $high, $fixed_low, $fixed_high ) {
Subroutine _widened: called CC=5 SCAR=17.8
134 1 ▶ my $margin = $low == 0 ? 1 : abs($low) / 10;
Branch: 1 branch
BranchTrueFalseTotal
$low == 0 ? : 0 1 1
135 1 ▶ return ( $low, $low + $margin ) if defined $fixed_low;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fixed_low 0 1 1
136 1 ▶ return ( $high - $margin, $high ) if defined $fixed_high;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fixed_high 0 1 1
137 1 ▶ return $low == 0 ? ( 0, 1 ) : ( $low - $margin, $high + $margin );
Branch: 1 branch
BranchTrueFalseTotal
$low == 0 ? : 0 1 1
138 }
139  
140 95 ▶ sub _candidate ( $low, $high, $step, $fixed_low, $fixed_high, $nice ) {
Subroutine _candidate: called CC=7 SCAR=19.5
141 95 ▶ my $domain_low = defined $fixed_low || !$nice ? $low : floor( $low / $step + EPSILON ) * $step;
Branch: 1 branch
BranchTrueFalseTotal
defined $fixed_low || !$nice ? : 12 83 95
Condition100%3/3
defined $fixed_low or not $nice
l!l&&r!l&&!r
8483
MC/DC100%2/2
defined $fixed_low or not $nice
defined $fixed_low not $nice
Truth table100%3/3
A or not(B)
A= defined $fixed_lowB= not $nice
ABresult
1X1
011
000
142 95 ▶ my $domain_high = defined $fixed_high || !$nice ? $high : ceil( $high / $step - EPSILON ) * $step;
Branch: 1 branch
BranchTrueFalseTotal
defined $fixed_high || !$nice ? : 8 87 95
Condition100%3/3
defined $fixed_high or not $nice
l!l&&r!l&&!r
4487
MC/DC100%2/2
defined $fixed_high or not $nice
defined $fixed_high not $nice
Truth table100%3/3
A or not(B)
A= defined $fixed_highB= not $nice
ABresult
1X1
011
000
143 95 ▶ $domain_high += $step if $domain_high <= $domain_low;
Branch: 1 branch
BranchTrueFalseTotal
if $domain_high <= $domain_low 0 95 95
144 95 my @values = _multiples( $domain_low, $domain_high, $step );
145 95 ▶ return undef unless @values;
Branch: 1 branch
BranchTrueFalseTotal
unless @values 1 94 95
146 94 return { low => $domain_low, high => $domain_high, values => \@values };
147 }
148 }
149  
150 1 1;
151  
152 __END__
« blib/lib/Term/Fabulous/Render/Geometry.pm ↑ Summary blib/lib/Term/Fabulous/Role/Themed.pm »