Clicker/Renderer/Pie.pm

filter: Statementstmt 39.5% 38 / 96 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 83.3% 10 / 12 Podpod n/a totaltotal 42.1% 48 / 114 CC 9 SCAR 28.4
CC
9
Cov
37%
__ANON__
42.0
__ANON__
20.0
BEGIN
1.0
CRAP 17.1
« Clicker/Decoration/Grid.pm ↑ Summary Clicker/Decoration/Legend.pm »
1 package Chart::Clicker::Renderer::Pie;
2 1 $Chart::Clicker::Renderer::Pie::VERSION = '2.89';
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 extends 'Chart::Clicker::Renderer';
6  
7 # ABSTRACT: Pie renderer
8  
9 1 ▶use Graphics::Color::RGB;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Geometry::Primitive::Arc;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Geometry::Primitive::Circle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Graphics::Primitive::Brush;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Graphics::Primitive::Paint::Gradient::Radial;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 has 'border_color' => (
19 is => 'rw',
20 isa => 'Graphics::Color::RGB',
21 1 ▶ default => sub { Graphics::Color::RGB->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 1 );
23  
24  
25 has 'brush' => (
26 is => 'rw',
27 isa => 'Graphics::Primitive::Brush',
28 1 ▶ default => sub { Graphics::Primitive::Brush->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
29 1 );
30  
31  
32 1 has 'gradient_color' => (
33 is => 'rw',
34 isa => 'Graphics::Color::RGB',
35 predicate => 'has_gradient_color'
36 );
37  
38  
39 1 has 'gradient_reverse' => (
40 is => 'rw',
41 isa => 'Bool',
42 default => 0,
43 );
44  
45  
46 1 has 'starting_angle' => (
47 is => 'rw',
48 isa => 'Int',
49 default => -90,
50 );
51  
52 1 my $TO_RAD = (4 * atan2(1, 1)) / 180;
53  
54 override('prepare', sub {
55 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=4 SCAR=30.0
56  
57 0 super;
58  
59 0 my $clicker = $self->clicker;
60  
61 0 my $dses = $clicker->get_datasets_for_context($self->context);
62 0 foreach my $ds (@{ $dses }) {
63 0 foreach my $series (@{ $ds->series }) {
64 0 foreach my $val (@{ $series->values }) {
65 0 $self->{ACCUM}->{refaddr($series)} += $val;
66 0 $self->{TOTAL} += $val;
67 }
68 }
69 }
70  
71 1 });
72  
73 override('finalize', sub {
74 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=6 SCAR=37.4
75  
76 0 my $clicker = $self->clicker;
77  
78 0 my $radius = $self->height;
79 0 ▶ if($self->width < $self->height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->width < $self->height) 0 0 0
80 0 $radius = $self->width;
81 }
82  
83 0 $radius = $radius / 2;
84  
85 # Take into acount the line around the edge when working out the radius
86 0 $radius -= $self->brush->width;
87  
88 0 my $height = $self->height;
89 0 my $linewidth = 1;
90 0 my $midx = $self->width / 2;
91 0 my $midy = $height / 2;
92 0 my $pos = $self->starting_angle;
93  
94 0 my $dses = $clicker->get_datasets_for_context($self->context);
95 0 foreach my $ds (@{ $dses }) {
96 0 foreach my $series (@{ $ds->series }) {
97  
98 # TODO if undef...
99 0 my $ctx = $clicker->get_context($ds->context);
100 0 my $domain = $ctx->domain_axis;
101 0 my $range = $ctx->range_axis;
102  
103 0 my $avg = $self->{ACCUM}->{refaddr($series)} / $self->{TOTAL};
104 0 my $degs = ($avg * 360) + $pos;
105  
106 0 $self->move_to($midx, $midy);
107 0 $self->arc($radius, $degs * $TO_RAD, $pos * $TO_RAD);
108  
109 0 $self->close_path;
110  
111 0 my $color = $clicker->color_allocator->next;
112  
113 0 my $paint;
114 0 ▶ if($self->has_gradient_color) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_gradient_color) { } 0 0 0
115 0 my $gc = $self->gradient_color;
116 0 my $start_radius = 0;
117 0 my $end_radius = $radius;
118  
119 0 ▶ if($self->gradient_reverse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->gradient_reverse) 0 0 0
120 0 $start_radius = $radius;
121 0 $end_radius = 0;
122 }
123  
124 0 $paint = Graphics::Primitive::Paint::Gradient::Radial->new(
125 start => Geometry::Primitive::Circle->new(
126 origin => [ $midx, $midy ],
127 radius => $start_radius
128 ),
129 end => Geometry::Primitive::Circle->new(
130 origin => [ $midx, $midy ],
131 radius => $end_radius
132 )
133 );
134 0 $paint->add_stop(0, $color);
135 0 $paint->add_stop(1, $color->clone(
136 red => $color->red + ($gc->red - $color->red) * $gc->alpha,
137 green => $color->green + ($gc->green - $color->green) * $gc->alpha,
138 blue => $color->blue + ($gc->blue - $color->blue) * $gc->alpha,
139 ));
140 } else {
141 0 $paint = Graphics::Primitive::Paint::Solid->new(
142 color => $color,
143 );
144 }
145  
146 0 my $fop = Graphics::Primitive::Operation::Fill->new(
147 preserve => 1,
148 paint => $paint
149 );
150 0 $self->do($fop);
151  
152 0 my $op = Graphics::Primitive::Operation::Stroke->new;
153 0 $op->brush($self->brush->clone);
154 0 $op->brush->color($self->border_color);
155 0 $self->do($op);
156  
157 0 $pos = $degs;
158 }
159 }
160  
161 0 return 1;
162 1 });
163  
164 1 __PACKAGE__->meta->make_immutable;
165  
166 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
167  
168 1 1;
169  
170 __END__
171  
172 =pod
173  
174 =head1 NAME
175  
176 Chart::Clicker::Renderer::Pie - Pie renderer
177  
178 =head1 VERSION
179  
180 version 2.89
181  
182 =head1 SYNOPSIS
183  
184 my $pier = Chart::Clicker::Renderer::Pie->new;
185 # Optionally set the stroke width
186 $pier->brush->width(2);
187  
188 =head1 DESCRIPTION
189  
190 Chart::Clicker::Renderer::Pie renders a dataset as slices of a pie. The keys
191 of like-named Series are totaled and keys are ignored. So for a dataset like:
192  
193 my $series = Chart::Clicker::Data::Series->new(
194 keys => [ 1, 2, 3 ],
195 values => [ 1, 2, 3],
196 );
197  
198 my $series2 = Chart::Clicker::Data::Series->new(
199 keys => [ 1, 2, 3],
200 values => [ 1, 1, 1 ],
201 );
202  
203 The keys are discarded and a pie chart will be drawn with $series' slice at
204 66% (1 + 2 + 3 = 6) and $series2's at 33% (1 + 1 + 1 = 3).
205  
206 =for HTML <p><img src="http://gphat.github.com/chart-clicker/static/images/examples/pie.png" width="300" height="250" alt="Pie Chart" /></p>
207  
208 =for HTML <p><img src="http://gphat.github.com/chart-clicker/static/images/examples/pie-gradient.png" width="300" height="250" alt="Pie Chart" /></p>
209  
210 =head1 ATTRIBUTES
211  
212 =head2 border_color
213  
214 Set/Get the L<color|Graphics::Color::RGB> to use for the border.
215  
216 =head2 brush
217  
218 Set/Get a L<brush|Graphics::Primitive::Brush> to be used for the pie's border.
219  
220 =head2 gradient_color
221  
222 If supplied, specifies a L<color|Graphics::Color::RGB> to mix with each slice's color for use as a
223 radial gradient. The best results are usually gotten from mixing with a
224 white or black and manipulating the alpha, like so:
225  
226 $ren->gradient_color(
227 Graphics::Color::RGB->new(red => 1, green => 1, blue => 1, alpha => .3)
228 );
229  
230 The above will cause each generated color to fade toward a lighter version of
231 itself. Adjust the alpha to increase or decrease the effect.
232  
233 =head2 brush
234  
235 Set/Get whether or not the gradient is to be reversed.
236  
237 =head2 starting_angle
238  
239 Set/Get a starting angle for the gradient.
240  
241 =head1 AUTHOR
242  
243 Cory G Watson <gphat@cpan.org>
244  
245 =head1 COPYRIGHT AND LICENSE
246  
247 This software is copyright (c) 2016 by Cory G Watson.
248  
249 This is free software; you can redistribute it and/or modify it under
250 the same terms as the Perl 5 programming language system itself.
251  
252 =cut
« Clicker/Decoration/Grid.pm ↑ Summary Clicker/Decoration/Legend.pm »