Chart-GGPlot

 view release on metacpan or  search on metacpan

lib/Chart/GGPlot/Util.pm  view on Meta::CPAN

    $idx = which( !$slope->isbad & !$slope->isfinite );
    $dist->slice($idx) .=
      $lf->at('r1')->slice($idx) - $lf->at('r2')->slice($idx);

    # Find the maximum possible length, a spiral line from
    # (r=0, theta=0) to (r=1, theta=2*pi)
    my $max_dist = spiral_arc_length( 1 / ( 2 * PI ), 0, 2 * PI );

    # Final distance values, normalized
    return ( $dist / $max_dist );
}

# Given n points, find the slope, xintercept, and yintercept of
#  the lines connecting them.
# Returns a data frame with $x->length-1 rows.

fun find_line_formula ($x, $y) {
    state $check =
      Type::Params::compile( ( Piddle1D->plus_coercions(PiddleFromAny) ) x 2 );
    ( $x, $y ) = $check->( $x, $y );

    my $slope      = $y->diff / $x->diff;
    my $yintercept = $y->slice("1:") - $slope * $x->slice("1:");
    my $xintercept = $x->slice("1:") - $y->slice("1:") / $slope;
    return Data::Frame->new(
        columns => [
            x1 => $x->slice( "0:" . ( $x->length - 2 ) ),
            y1 => $y->slice( "0:" . ( $y->length - 2 ) ),
            x2 => $x->slice("1:"),
            y2 => $y->slice("1:"),
            slope      => $slope,
            yintercept => $yintercept,
            xintercept => $xintercept
        ]
    );
}

fun spiral_arc_length ($a, $theta1, $theta2) {
    state $check =
      Type::Params::compile( ( Piddle1D->plus_coercions(PiddleFromAny) ) x 3 );
    ( $a, $theta1, $theta2 ) = $check->( $a, $theta1, $theta2 );
    return $a * 0.5 *
      ( ( $theta1 * ( $theta1**2 + 1 )->sqrt + $theta1->asinh ) -
          ( $theta2 * ( $theta2**2 + 1 )->sqrt + $theta2->asinh ) );
}


fun resolution(Piddle1D $x, $zero=true) {
    if ($x->type < PDL::float or zero_range(range_($x, true))) {
        return 1;
    }
    if ($zero) {
        $x = $x->glue(0, pdl(0))->uniq;
    } else {
        $x = $x->uniq;
    }
    return $x->qsort->diff->min;
}


fun stat($x) { $x }

use constant NO_GROUP => -1;

fun has_groups ($df) {

    # If no group aesthetic is specified, all values of the group column
    # equal to NO_GROUP. On the other hand, if a group aesthetic is
    # specified, all values are different from NO_GROUP.
    # undef is returned for 0-row data frames.
    return undef if ( $df->nrow == 0 );
    return ( $df->at('group')->at(0) >= 0 );
}

sub collect_functions_from_package {
    my ($package) = @_; 

    load $package;

    my ($caller_package) = caller();
    my @func_names;
    my $funcs = $package->ggplot_functions();
    for (@$funcs) {
        my $name = $_->{name};

        no strict 'refs';
        if (defined &{$name}) {
            warn qq{Duplicate function definitions for "$name". } . 
                "Definition from package $package would mask earlier " .
                "definition loaded into " . __PACKAGE__ . ".";
        }

        *{"${caller_package}::${name}"} = $_->{code};
        push @func_names, $name;
    }   
    return @func_names;
}


sub arraylike {
    my ($x) = @_;
    return (Piddle1D->check($x) or ArrayRef->check($x));
}


sub mm_to_pt {
    my $result = 72 / 25.4 * $_[0];
    $result->$_call_if_can('rint') // floor($result + 0.5);
}

sub mm_to_px {
    my ( $x, $dpi ) = @_;
    $dpi //= 96;
    my $result = $x / 25.4 * $dpi;
    $result->$_call_if_can('rint') // floor($result + 0.5);
}

1;

__END__



( run in 0.977 second using v1.01-cache-2.11-cpan-39bf76dae61 )