| 1 | package Chart::Clicker::Axis::DivisionType::LinearExpandGraph; | ||
| 2 | $Chart::Clicker::Axis::DivisionType::LinearExpandGraph::VERSION = '2.89'; | ||
| 3 | use Moose::Role; | ||
| 4 | with qw{Chart::Clicker::Axis::DivisionType}; | ||
| 5 | |||
| 6 | use POSIX qw(ceil floor); | ||
| 7 | |||
| 8 | # Positive only | ||
| 9 | has 'tick_slop' => ( | ||
| 10 | is => 'rw', | ||
| 11 | isa => 'Num', | ||
| 12 | default => 0.1, | ||
| 13 | documentation => | ||
| 14 | q{Percent of a tick unit above or below graphed which we allow inserting an additional tick. Whitespace allowance above or below.} | ||
| 15 | ); | ||
| 16 | |||
| 17 | sub _real_divvy { | ||
| 18 | my ($self) = @_; | ||
| 19 | |||
| 20 | my $n = $self->ticks; | ||
| 21 | |||
| 22 | my $min = $self->range->lower; | ||
| 23 | my $max = $self->range->upper; | ||
| 24 | |||
| 25 | my $range = _nicenum($self->range->span, 0); | ||
| 26 | my $d = _nicenum($range / ($n - 1), 1); | ||
| 27 | my $graphmin = $min; | ||
| 28 | my $graphmax = $max; | ||
| 29 | |||
| 30 | # Expand the graph as needed | ||
| 31 | $graphmin = floor($min / $d) * $d; | ||
| 32 | $self->range->min($graphmin); | ||
| 33 | $graphmax = ceil($max / $d) * $d; | ||
| 34 | $self->range->max($graphmax); | ||
| 35 | |||
| 36 | my $x = $graphmin; | ||
| 37 | my @ticks; | ||
| 38 | do { | ||
| 39 | push(@ticks, $x); | ||
| 40 | $x += .5 * $d; | ||
| 41 | } while($x < $graphmax); | ||
| 42 | |||
| 43 | return \@ticks; | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _nicenum { | ||
| 47 | my ($num, $round) = @_; | ||
| 48 | |||
| 49 | my $exp = floor(_log10($num)); | ||
| 50 | my $f = $num / (10 ** $exp); | ||
| 51 | my $nice; | ||
| 52 | |||
| 53 | if($round) { | ||
| 54 | if($f < 1.5) { | ||
| 55 | $nice = 1.5; | ||
| 56 | } elsif($f < 3) { | ||
| 57 | $nice = 2; | ||
| 58 | } elsif($f < 7) { | ||
| 59 | $nice = 5; | ||
| 60 | } else { | ||
| 61 | $nice = 10; | ||
| 62 | } | ||
| 63 | } else { | ||
| 64 | if($f <= 1) { | ||
| 65 | $nice = 1; | ||
| 66 | } elsif($f <= 2) { | ||
| 67 | $nice = 2; | ||
| 68 | } elsif($f <= 5) { | ||
| 69 | $nice = 5; | ||
| 70 | } else { | ||
| 71 | $nice = 10; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | return $nice * (10 ** $exp); | ||
| 76 | } | ||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | sub _log10 { | ||
| 81 | my $n = shift; | ||
| 82 | return log($n) / log(10); | ||
| 83 | } | ||
| 84 | |||
| 85 | no Moose; | ||
| 86 | 1; | ||
| 87 | |||
| 88 | __END__ | ||
| 89 | |||
| 90 | =pod | ||
| 91 | |||
| 92 | =head1 NAME | ||
| 93 | |||
| 94 | Chart::Clicker::Axis::DivisionType::LinearExpandGraph | ||
| 95 | |||
| 96 | =head1 VERSION | ||
| 97 | |||
| 98 | version 2.89 | ||
| 99 | |||
| 100 | =head1 SYNOPSIS | ||
| 101 | |||
| 102 | use Chart::Clicker::Axis; | ||
| 103 | |||
| 104 | my $axis = Chart::Clicker::Axis->new({ | ||
| 105 | tick_division_type => 'LinearRounded' | ||
| 106 | }); | ||
| 107 | |||
| 108 | =head1 DESCRIPTION | ||
| 109 | |||
| 110 | Role describing how to divide data for Chart::Clicker::Axis. | ||
| 111 | |||
| 112 | =head1 NAME | ||
| 113 | |||
| 114 | Chart::Clicker::Axis::DivisionType::LinearRounded - Nicely rounded segments on a linear scale. | ||
| 115 | |||
| 116 | =head1 ATTRIBUTES | ||
| 117 | |||
| 118 | =head2 tick_slop | ||
| 119 | |||
| 120 | This setting determines whether to add a tick outside of the data. If the tick would be | ||
| 121 | within the percentage of a ticks size specified here as a decimal (10% would be 0.1), then | ||
| 122 | the tick will be added expanding the graph. | ||
| 123 | |||
| 124 | =head1 METHODS | ||
| 125 | |||
| 126 | =head2 best_tick_size | ||
| 127 | |||
| 128 | The tick division considered best for the approximate number of ticks requested | ||
| 129 | and data within the range. | ||
| 130 | |||
| 131 | =head2 divvy | ||
| 132 | |||
| 133 | Divides the range up into nicely rounded chunks for L<Chart::Clicker::Axis>. | ||
| 134 | |||
| 135 | =head1 AUTHOR | ||
| 136 | |||
| 137 | Rod Taylor <chartclicker@rbt.ca> | ||
| 138 | |||
| 139 | =head1 SEE ALSO | ||
| 140 | |||
| 141 | perl(1) | ||
| 142 | |||
| 143 | =head1 LICENSE | ||
| 144 | |||
| 145 | You can redistribute and/or modify this code under the same terms as Perl | ||
| 146 | itself. | ||
| 147 | |||
| 148 | =head1 AUTHOR | ||
| 149 | |||
| 150 | Cory G Watson <gphat@cpan.org> | ||
| 151 | |||
| 152 | =head1 COPYRIGHT AND LICENSE | ||
| 153 | |||
| 154 | This software is copyright (c) 2016 by Cory G Watson. | ||
| 155 | |||
| 156 | This is free software; you can redistribute it and/or modify it under | ||
| 157 | the same terms as the Perl 5 programming language system itself. | ||
| 158 | |||
| 159 | =cut |