Chart-GGPlot

 view release on metacpan or  search on metacpan

lib/Chart/GGPlot/Scale/Functions.pm  view on Meta::CPAN

fun _scale_position_continuous ($aes) {
    return fun(:$name = undef, :$breaks = undef, :$minor_breaks = undef,
               :$labels = undef, :$limits = [],
               :$expand = undef, :$oob = \&censor, :$na_value = 'nan',
               :$trans = 'identity', :$position = _default_position($aes),
               :$sec_axis = undef,
               %rest,
      )
    {
        if ( defined $sec_axis ) {
            if ( is_formula($sec_axis) ) {
                $sec_axis = sec_axis($sec_axis);
            }
            if ( $sec_axis->$_isa('Chart::GGPlot::AxisSecondary') ) {
                die(
"Secondary axes must be specified using a Chart::GGPlot::AxisSecondary object"
                );
            }
        }

        return continuous_scale(

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

my @export_all = (
    @export_ggplot,
    qw(
      PI
      pt stroke
      isnt_null_or
      clist
      call_if_coderef
      alias_color_functions
      dist_euclidean dist_polar
      find_line_formula spiral_arc_length
      has_groups
      collect_functions_from_package
      arraylike
      mm_to_pt mm_to_px
      ),
);

our @EXPORT_OK = (
    @Chart::GGPlot::Util::_Base::EXPORT_OK,
    @Chart::GGPlot::Util::Scales::EXPORT_OK, @export_all,

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

# Euclidean distance between points.
fun dist_euclidean ($x, $y) {
    my $n   = $x->length;
    my $idx = sequence( $n - 1 );
    return ( ( $x->slice($idx) - $x->slice( $idx + 1 ) )**2 +
          ( $y->slice($idx) - $y->slice( $idx + 1 ) )**2 )->sqrt;
}

# Polar distance between points.
fun dist_polar ($r, $theta) {
    my $lf = find_line_formula( $theta, $r );

    # Rename x and y columns to r and t, since we're working in polar
    $lf = $lf->rename(
        {
            x1          => 't1',
            x2          => 't2',
            y1          => 'r1',
            y2          => 'r2',
            x_intercept => 't_int',
            yintercept  => 'r_int'

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

    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 ) ),

t/09-util.t  view on Meta::CPAN

    pdl_is(
        expand_range4( pdl( [ 0, 1 ] ), pdl( [ 1, 2, 3, 4 ] ) ),
        pdl([ 0 - 3, 1 + 7 ]),
        'expand_range4() with 4 expand params'
    );
    ok( expand_range4( null, pdl( [ 0, 0, 0, 0 ] ) )->isempty,
        'expand_range4() with empty limits' );
};

my $df =
  Chart::GGPlot::Util::find_line_formula( [ 4, 7, 9 ], [ 1, 5, 3 ] );

#diag($df->stringify);

{
    my $spiral_arc_length = Chart::GGPlot::Util::spiral_arc_length(
        [ 0.2,      0.5 ],
        [ 0.5 * PI, PI ],
        [ PI,       1.25 * PI ]
    );
    pdl_is( $spiral_arc_length,
        pdl([ -0.806146217, -1.442604138 ]),
        'spiral_arc_length()'
    );

    my $lf = find_line_formula( pdl( [ 4, 7, 9 ] ), pdl( [ 1, 5, 3 ] ) );
    dataframe_is(
        $lf,
        Data::Frame->new(
            columns => [
                x1         => pdl( 4,              7 ),
                y1         => pdl( 1,              5 ),
                x2         => pdl( 7,              9 ),
                y2         => pdl( 5,              3 ),
                slope      => pdl( 1 + 1 / 3,      -1.000000 ),
                yintercept => pdl( -( 4 + 1 / 3 ), 12.000000 ),
                xintercept => pdl( 3.25,           12.00 )
            ]
        ),
        'find_line_formula()'
    );
}

subtest resolution => sub {
    is( resolution( pdl( [ 1 .. 10 ] ) ),       1 );
    is( resolution( pdl( [ 1 .. 10 ] ) - 0.5 ), 0.5 );
    is( resolution( pdl( [ 1 .. 10 ] ) - 0.5, false ), 1, '$zero=false' );

    # Note the difference between float and integer piddles.
    is( resolution( pdl( [ 2, 10, 20, 50 ] ) ), 2 );



( run in 0.460 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )