| 1 | package Term::Fabulous::Chart::Scale::Log; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 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::Log :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 format_value); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use constant EPSILON => 1e-9; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 1 | ▶ | field $base :param :reader; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
base: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | method kind () { return 'log' } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
kind: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 86 | ▶ | method _log ($value) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_log: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 86 | return log($value) / log($base); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 32 | ▶ | method position ($value) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
position: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 32 | ▶ | return undef unless defined $value && $value > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $value and $value > 0
MC/DC100%2/2
defined $value and $value > 0
defined $value
$value > 0
Truth table100%3/3
A and B
A= defined $valueB= $value > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 28 | my ( $low, $high ) = map { $self->_log($_) } $self->min, $self->max; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 28 | return ( $self->_log($value) - $low ) / ( $high - $low ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 1 | ▶ | method value_at ($position) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
value_at: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | my ( $low, $high ) = map { $self->_log($_) } $self->min, $self->max; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | return $base**( $low + $position * ( $high - $low ) ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 13 | ▶ | method fit :common (%options) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fit: called CC=33 SCAR=35.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 13 | ▶ | my $cells = $options{cells} // croak "$class: fit needs cells"; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options{"cells"} // croak("${class}: fit needs cells")
MC/DC0%0/1
$options{"cells"} // croak("${class}: fit needs cells")
$options{"cells"}
Truth table50%1/2
A
A= $options{"cells"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 13 | ▶ | croak "$class: cells must be a positive integer, got $cells" unless $cells =~ /\A[1-9][0-9]*\z/; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 13 | ▶ | my $base = $options{base} // 10; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options{"base"} // 10
MC/DC100%1/1
$options{"base"} // 10
$options{"base"}
Truth table100%2/2
A
A= $options{"base"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 13 | ▶ | croak "$class: base must be a number greater than 1, got $base" unless $base > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 12 | my ( $fixed_low, $fixed_high ) = @options{qw(min max)}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 12 | foreach my $end ( [ min => $fixed_low ], [ max => $fixed_high ] ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 23 | ▶ | croak "$class: $end->[0] of a logarithmic axis must be greater than 0, got $end->[1]" if defined $end->[1] && $end->[1] <= 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $end->[1] and $end->[1] <= 0
MC/DC100%2/2
defined $end->[1] and $end->[1] <= 0
defined $end->[1]
$end->[1] <= 0
Truth table100%3/3
A and B
A= defined $end->[1]B= $end->[1] <= 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 11 | ▶ | 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
Condition83%5/6
defined $fixed_low and defined $fixed_high and $fixed_low >= $fixed_high
defined $fixed_low and defined $fixed_high
MC/DC66%2/3
defined $fixed_low and defined $fixed_high and $fixed_low >= $fixed_high
defined $fixed_low
defined $fixed_high
$fixed_low >= $fixed_high
Truth table75%3/4
A and B and C
A= defined $fixed_lowB= defined $fixed_highC= $fixed_low >= $fixed_high
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 10 | ▶ | my $vertical = ( $options{orientation} // 'vertical' ) eq 'vertical'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options{"orientation"} // "vertical"
MC/DC100%1/1
$options{"orientation"} // "vertical"
$options{"orientation"}
Truth table100%2/2
A
A= $options{"orientation"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 10 | ▶ | my $logarithm = sub ($value) { log($value) / log($base) }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 10 | ▶ | my ( $low, $high ) = $options{extent} ? $options{extent}->@* : ( 1, $base ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 10 | ▶ | $low = $fixed_low if defined $fixed_low; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 10 | ▶ | $high = $fixed_high if defined $fixed_high; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 10 | ▶ | ( $low, $high ) = defined $fixed_high && !defined $fixed_low ? ( $high / $base, $high ) : ( $low, $low * $base ) if $high <= $low; # a fixed end stays | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
defined $fixed_high and not defined $fixed_low
MC/DC50%1/2
defined $fixed_high and not defined $fixed_low
defined $fixed_high
not defined $fixed_low
Truth table66%2/3
A and not(B)
A= defined $fixed_highB= not defined $fixed_low
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 10 | ▶ | my $first = defined $fixed_low ? $logarithm->($low) : floor( $logarithm->($low) + EPSILON ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 10 | ▶ | my $last = defined $fixed_high ? $logarithm->($high) : ceil( $logarithm->($high) - EPSILON ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 10 | ▶ | $last = $first + 1 if $last <= $first; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 10 | my ( $domain_low, $domain_high ) = ( $base**$first, $base**$last ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 10 | ▶ | my @powers = grep { $_ >= $first - EPSILON && $_ <= $last + EPSILON } ceil( $first - EPSILON ) .. floor( $last + EPSILON ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$_ >= $first - 1e-09 && $_ <= $last + 1e-09
MC/DC0%0/2
$_ >= $first - 1e-09 && $_ <= $last + 1e-09
$_ >= $first - 1e-09
$_ <= $last + 1e-09
Truth table33%1/3
A && B
A= $_ >= $first - 1e-09B= $_ <= $last + 1e-09
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 10 | my $decades = @powers - 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 10 | ▶ | my $spans_domain = @powers && abs( $powers[0] - $first ) < EPSILON && abs( $powers[-1] - $last ) < EPSILON && $decades > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition77%7/9
@powers && abs $powers[0] - $first < 1e-09 && abs $powers[-1] - $last < 1e-09 && $decades > 0
@powers and abs $powers[0] - $first < "1e-09" and abs $powers[-1] - $last < "1e-09"
@powers and abs $powers[0] - $first < "1e-09"
MC/DC50%2/4
@powers && abs $powers[0] - $first < 1e-09 && abs $powers[-1] - $last < 1e-09 && $decades > 0
@powers
abs $powers[0] - $first < "1e-09"
abs $powers[-1] - $last < "1e-09"
$decades > 0
Truth table60%3/5
A and B and C && D
A= @powersB= abs $powers[0] - $first < "1e-09"C= abs $powers[-1] - $last < "1e-09"D= $decades > 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 10 | ▶ | my $per_decade = $spans_domain ? floor( ( $cells - 1 ) / $decades ) : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 10 | ▶ | my $used = $spans_domain && $per_decade >= 1 ? $decades * $per_decade + 1 : $cells; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$spans_domain and $per_decade >= 1
MC/DC100%2/2
$spans_domain and $per_decade >= 1
$spans_domain
$per_decade >= 1
Truth table100%3/3
A and B
A= $spans_domainB= $per_decade >= 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # Label every power when there is room, else every second or third. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 10 | my $format = $options{format}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 10 | ▶ | my $measure = $options{measure} // sub ($label) { length $label }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$options{"measure"} // sub ($label) {
length $label;
}
MC/DC0%0/2
$options{"measure"} // sub ($label) {
length $label;
}
$options{"measure"}
sub ($label) {
length $label;
}
Truth table33%1/3
A // B
A= $options{"measure"}B= sub ($label) {
length $label;
}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 10 | my @values = map { $base**$_ } @powers; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 10 | my @labels = map { _label( $format, $_ ) } @values; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 10 | ▶ | my $room = $vertical ? 2 : 2 + List::Util::max( 1, map { $measure->($_) } @labels ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 10 | my $stride = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 10 | ▶ | $stride++ while @powers > 1 && ( $cells - 1 ) * $stride / ( $last - $first ) < $room && $stride < @powers; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
@powers > 1 and ($cells - 1) * $stride / ($last - $first) < $room and $stride < @powers
@powers > 1 and ($cells - 1) * $stride / ($last - $first) < $room
MC/DC33%1/3
@powers > 1 and ($cells - 1) * $stride / ($last - $first) < $room and $stride < @powers
@powers > 1
($cells - 1) * $stride / ($last - $first) < $room
$stride < @powers
Truth table50%2/4
A and B and C
A= @powers > 1B= ($cells - 1) * $stride / ($last - $first) < $roomC= $stride < @powers
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 10 | my @ticks = map { { value => $values[$_], label => $labels[$_] } } grep { ( $powers[$_] - $powers[0] ) % $stride == 0 } 0 .. $#powers; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 10 | return $class->new( min => $domain_low, max => $domain_high, cells => $cells, used => $used, base => $base, ticks => \@ticks ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # Powers from 1 up are written with SI prefixes (1k, 10k), smaller | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | # ones as decimals (0.01). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 40 | ▶ | sub _label ( $format, $value ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_label: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 40 | ▶ | return number_formatter( $format, $value, $value )->($value) if defined $format; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 36 | ▶ | return format_value( $value, 'si' ) if $value >= 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 3 | my $text = sprintf '%.10f', $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | $text =~ s/0+\z//; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 3 | return $text; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||