Chart/GGPlot/Coord/Polar.pm untested

filter: Statementstmt 0.0% 0 / 86 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 113 CC 2 SCAR 11.0
CC
2
Cov
0%
_build_r
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 3.0
1 package Chart::GGPlot::Coord::Polar;
2  
3 # ABSTRACT: The polar coordinate system
4  
5 use Chart::GGPlot::Class qw(:pdl);
6 use namespace::autoclean;
7  
8 our $VERSION = '0.002003'; # VERSION
9  
10 use List::AllUtils;
11 use PDL::Primitive qw(which);
12 use Types::Standard qw(Bool Enum Int Num);
13  
14 use Chart::GGPlot::Util qw(:all);
15  
16  
17 has theta => ( is => 'ro', isa => Enum [qw(x y)] );
18 has r => ( is => 'ro', lazy => 1, builder => '_build_r', init_arg => undef );
19  
20  
21 has start => ( is => 'ro', isa => Num );
22  
23  
24 has direction =>
25 ( is => 'ro', isa => Int->where(sub { $_ == 1 or $_ == -1 }), default => 1 );
26  
27 sub _build_r {
28 my $self = shift;
29 return ( $self->theta eq 'x' ? 'y' : 'x' );
30 }
31  
32 has limits =>
33 ( is => 'ro', lazy => 1, builder => '_build_limits', init_arg => undef );
34  
35 with qw(
36 Chart::GGPlot::Coord
37 Chart::GGPlot::HasCollectibleFunctions
38 );
39  
40 my $coord_polar_pod = unindent(<<'EOT');
41  
42 coord_ploar(:$theta='x', :$start=0, :$direction=1)
43  
44 EOT
45  
46 my $coord_polar_code = fun (:$theta ='x', :$start = 0, :$direction = 1) {
47 return __PACKAGE__->new(
48 theta => $theta,
49 start => $start,
50 direction => ( $direction <=> 0 )
51 );
52 }
53  
54 classmethod ggplot_functions () {
55 return [
56 {
57 name => 'coord_polar',
58 code => $coord_polar_code,
59 pod => $coord_polar_pod,
60 }
61 ];
62 }
63  
64 sub _build_limits {
65 my $self = shift;
66 return { x => $self->xlim, y => $self->ylim };
67 }
68  
69 method aspect ($details) { 1 }
70  
71 method distance ($x, $y, $details) {
72 my $r;
73 my $theta;
74 if ( $self->theta eq 'x' ) {
75 $r = rescale( $y, $details->at('r_range') );
76 $theta = theta_rescale_no_clip( $self, $x, $details );
77 }
78 else {
79 $r = rescale( $x, $details->at('r_range') );
80 $theta = theta_rescale_no_clip( $self, $y, $details );
81 }
82  
83 return dist_polar( $r, $theta );
84 }
85  
86 method range ($panel_params) {
87 return {
88 $self->theta => $panel_params->at('theta_range'),
89 $self->r => $panel_params->at('r_range'),
90 };
91 }
92  
93 method setup_panel_params ($scale_x, $scale_y, $params = {}) {
94 my $details = {};
95 for my $n (qw(x y)) {
96 my $scale = $n eq 'x' ? $scale_x : $scale_y;
97 my $limits = $self->limits->at($n);
98  
99 my $range;
100 if ( $limits->isempty ) {
101 my $expand =
102 $self->theta eq $n
103 ? expand_default( $scale, [ 0, 0.5 ], [ 0, 0 ] )
104 : expand_default( $scale, [ 0, 0 ], [ 0, 0 ] );
105 $range = $scale->dimension($expand);
106 }
107 else {
108 $range = range( scale_transform( $scale, $limits ) );
109 }
110  
111 my $out = $scale->break_info($range);
112  
113 $details->merge(
114 {
115 "${n}_range" => $out->at('range'),
116 "${n}_major" => $out->at('major_source'),
117 "${n}_minor" => $out->at('minor_source'),
118 "${n}_labels" => $out->at('labels'),
119 "${n}_sec_range" => $out->at('sec_range'),
120 "${n}_sec_major" => $out->at('sec_major_source'),
121 "${n}_sec_minor" => $out->at('sec_minor_source'),
122 "${n}_sec_labels" => $out->at('sec_labels'),
123 }
124 );
125 }
126  
127 if ( $self->theta eq 'y' ) {
128 $details = {
129 $details->keys->map(
130 sub { $_ => ( $_ =~ s/^x_/r_/r =~ s/^y_/theta_/r ) }
131 )->flatten
132 };
133 $details->{r_arrange} = $scale_x->axis_order();
134 }
135 else {
136 $details = {
137 $details->keys->map(
138 sub { $_ => ( $_ =~ s/^x_/theta_/r =~ s/^y_/r_/r ) }
139 )->flatten
140 };
141 $details->{r_arrange} = $scale_y->axis_order();
142 }
143  
144 return $details;
145 }
146  
147 method transform ($data, $panel_params) {
148 my $data = rename_data( $self, $data );
149  
150 $data->set( 'r', r_rescale( $self, $data->at('r'), $panel_params ) );
151 $data->set( 'theta',
152 theta_rescale( $self, $data->at('theta'), $panel_params ) );
153 $data->set( 'x', $data->r * $data->theta->sin + 0.5 );
154 $data->set( 'y', $data->r * $data->theta->cos + 0.5 );
155 return $data;
156 }
157  
158 method labels ($panel_params) {
159 if ( $self->theta eq 'y' ) {
160 return { x => $panel_params->at('y'), y => $panel_params->at('x') };
161 }
162 else {
163 return $panel_params;
164 }
165 }
166  
167 __PACKAGE__->meta->make_immutable;
168  
169 1;
170  
171 __END__