Clicker/Axis/DivisionType/LinearRounded.pm

filter: Statementstmt 100% 45 / 45 Branchbran 92.8% 13 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 98.4% 63 / 64 CC 9 SCAR 14.7
CC
9
Cov
98%
best_tick_size
5.0
_real_divvy
5.0
BEGIN
1.0
CRAP 4.3
« Clicker/Data/DataSet.pm ↑ Summary Clicker/Decoration/Plot.pm »
1 package Chart::Clicker::Axis::DivisionType::LinearRounded;
2 1 $Chart::Clicker::Axis::DivisionType::LinearRounded::VERSION = '2.90';
3 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 with qw{Chart::Clicker::Axis::DivisionType};
5  
6 # Positive only
7 1 has 'tick_slop' => (
8 is => 'rw',
9 isa => 'Num',
10 default => 0.1,
11 documentation =>
12 q{Percent of a tick unit above or below graphed which we allow inserting an additional tick. Whitespace allowance above or below.}
13 );
14  
15 # Take the rough tick size which is the smallest possible 'round' scale
16 # and use other sub-divisors to aim for the number of ticks specified.
17 5 ▶sub best_tick_size {
Subroutine best_tick_size: called CC=5 SCAR=16.1
Pod documented
18 5 my ($self) = @_;
19  
20 5 my $minimum_target_ticks = $self->ticks;
21 5 ▶ $minimum_target_ticks = 1 if ( $minimum_target_ticks < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $minimum_target_ticks < 1 0 5 5
22  
23 5 my $equal_tick_size = $self->range->span / ($minimum_target_ticks);
24  
25 # Provide a nice round divisor which is within an order of
26 # magnitude of the actual wanted tick size. We adjust
27 # this value using hand specified sub-divider values
28 #
29 # Small ranges (below 1) require an additional digit
30 5 my $digits = int( log( abs($equal_tick_size) ) / log(10) );
31 5 ▶ $digits-- if ( abs( $self->range->span ) < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if abs $self->range->span < 1 2 3 5
32 5 my $scale_size = 10**$digits;
33  
34 # Take the largest divider (smallest number of ticks) which will provide
35 # a nice looking result. The below dividers were selected arbitrarily to
36 # create visually pleasing numbers for an axis.
37 #
38 # The number of ticks will be equal to or larger than the requested number
39 # of ticks. Never smaller. The worst case is the 1 to 2 range which may
40 # provide nearly double (2N - 1) the number of requested ticks.
41 ADJUSTSCALE:
42 5 for my $scale_divider ( 25, 20, 10, 5, 4, 2.5, 2, 1 ) {
43 38 my $test_scale = $scale_divider * $scale_size;
44  
45 38 ▶ if ( $self->range->span / $test_scale >= $minimum_target_ticks ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->range->span / $test_scale >= $minimum_target_ticks) 5 33 38
46 5 $scale_size = $test_scale;
47 5 last ADJUSTSCALE;
48 }
49 }
50  
51 5 return $scale_size;
52 }
53  
54 5 ▶sub _real_divvy {
Subroutine _real_divvy: called CC=5 SCAR=16.1
55 5 my ($self) = @_;
56  
57 5 my $tickSize = $self->best_tick_size;
58 5 my $range = $self->range;
59  
60 # If the lowest value is nearby to the first tick below it (gap
61 # at front of graph would be low) then use that as the starting
62 # value; otherwise choose the first tick value above the lowest.
63 5 my $lowestTick = int( $range->lower() / $tickSize ) * $tickSize;
64 5 my $lowestValue = $range->lower();
65 5 my $lowTickDifference = $lowestValue - $lowestTick;
66  
67 5 ▶ if ( $lowTickDifference > $self->tick_slop * $tickSize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lowTickDifference > $self->tick_slop * $tickSize) 2 3 5
68 2 $lowestTick = $lowestTick + $tickSize;
69 }
70 5 ▶ if ( $lowestTick < $lowestValue ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lowestTick < $lowestValue) 2 3 5
71 2 $lowestValue = $lowestTick;
72 }
73 5 $range->lower($lowestValue);
74 5 my @vals;
75 5 push( @vals, $lowestTick );
76  
77 # Loop until upper from the starting point
78 5 my $lastTick = $lowestTick;
79 5 ▶ while ( $range->upper - $tickSize > $lastTick ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range->upper - $tickSize > $lastTick) 32 5 37
80 32 $lastTick = $lastTick + $tickSize;
81  
82 32 push( @vals, $lastTick );
83 }
84  
85 # If the upper value is nearby to the last tick above it
86 # (gap at end of graph would be low) then use that as the
87 # ending value; otherwise use the tick value immediately before
88 # the upper value.
89 5 my $potentialUpperTick = $lastTick + $tickSize;
90 5 ▶ if ( $potentialUpperTick - $range->upper < $self->tick_slop * $tickSize ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($potentialUpperTick - $range->upper < $self->tick_slop * $tickSize) 4 1 5
91 4 $range->upper($potentialUpperTick);
92 4 push( @vals, $potentialUpperTick );
93 }
94  
95 5 return \@vals;
96 }
97  
98 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
99 1 1;
100  
101 __END__
« Clicker/Data/DataSet.pm ↑ Summary Clicker/Decoration/Plot.pm »