Skip to content
This repository has been archived by the owner on Dec 22, 2023. It is now read-only.

Non-optimal values for tick marks #31

Closed
koknat opened this issue Jan 14, 2014 · 3 comments
Closed

Non-optimal values for tick marks #31

koknat opened this issue Jan 14, 2014 · 3 comments

Comments

@koknat
Copy link

koknat commented Jan 14, 2014

By default Chart::Clicker chooses non-optimal values for tick marks (such as 0.30, 1.40, 2.50, 3.60, 4.70, 5.80).

I created a subroutine getrange() which creates tick marks to produce a chart which is easier to read.
In the example above, if getrange() is given a min of 0.30 and a max of 5.80, the tick marks returned will be [0, 1, 2, 3, 4, 5, 6]

my $Yrange = getrange( $min, $max, { integer => 1 } );
$ctx->range_axis->tick_values($Yrange);   

sub getrange {
    my ( $min, $max, $options ) = @_;
    # $min and $max are the minimum and maximum values of the input data
    return if not defined $min or not defined $max;
    my $span = $max - $min;
    my $n = substr( sprintf( "%.6e", $span ), 0, 8 );     # First few number 1.000000 to 9.999999
    my $e = substr( sprintf( "%.6e", $span ), 9, 11 );    # exponent with sign such as +01
    my $step;
    if ( $n > 5 ) {
        $step = 1 * 10**($e);                            # 5.01 to 9.99
    } elsif ( $n > 2 ) {
        $step = 5 * 10**( $e - 1 );                      # 2.01 to 3.00
    } else {
        $step = 2 * 10**( $e - 1 );                      # 1.00 to 2.00
    }
    if ( $options->{integer} ) {
        # This forces steps to be integers.  If max is 2.2, then step is 1 (not 0.5)
        use POSIX;
        $step = ceil($step);
    }
    my $range;
    if ( $max/$step == int($max/$step) ) {
        $range = [ map { $_ * $step }  int( $min / $step ) .. int( $max / $step ) ];
    } else {
        $range = [ map { $_ * $step }  int( $min / $step ) .. int( $max / $step ) + 1 ];
    }
    return $range;
}  
sub test_getrange {
    my @minmax = ( 0.30, 5.80,  0, 0.85,  0, 1.1,  0, 2.2,  0, 5.8,  0, 8,  0, 15,  0, 28,  0, 44,  0, 58,  0, 88,  0, 100,  0, 102,  0, 1020,  -7, 21,  18, 45,  18, 49 );
    while (@minmax) {
        my $min = shift @minmax;
        my $max = shift @minmax;
        my $range = getrange( $min, $max, { integer => 1 } );
        say "\n$min  $max\n    " . join ' ', @{$range};
    }
    exit 0;
}
@gphat
Copy link
Owner

gphat commented Jan 25, 2014

I'd be happy to accept a patch that adds a new DivisionType (See Axis.pm and DivisionType.pm).

@gphat gphat closed this as completed Jan 25, 2014
@gphat gphat reopened this Jan 25, 2014
@gphat
Copy link
Owner

gphat commented Jan 25, 2014

Oops, closed accidentally. :)

@gphat
Copy link
Owner

gphat commented Nov 11, 2014

Closing due to inactivity.

@gphat gphat closed this as completed Nov 11, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants