Graph/Pie.pm

filter: Statementstmt 96.6% 230 / 238 Branchbran 83.0% 113 / 136 Conditioncond 78.5% 11 / 14 MC/DCmcdc 66.6% 6 / 9 Subroutinesub 93.7% 15 / 16 Podpod 100% 4 / 4 totaltotal 90.8% 379 / 417 SCAR 45.1
CC
86
Cov
91%
draw
60.6
_fit_text
16.0
_valid_input
8.3
CRAP 91.0
« Graph/Horizontal.pm ↑ Summary Graph/Util.pm »
1 package Imager::Graph::Pie;
2  
3 =head1 NAME
4  
5 Imager::Graph::Pie - a tool for drawing pie charts on Imager images
6  
7 =head1 SYNOPSIS
8  
9 use Imager::Graph::Pie;
10  
11 my $chart = Imager::Graph::Pie->new;
12 # see Imager::Graph for options
13 my $img = $chart->draw(
14 data => [ $first_amount, $second_amount ],
15 size => 350);
16  
17 =head1 DESCRIPTION
18  
19 Imager::Graph::Pie is intender to make it simple to use L<Imager> to
20 create good looking pie graphs.
21  
22 Most of the basic layout and color selection is handed off to
23 L<Imager::Graph>.
24  
25 =over
26  
27 =cut
28  
29 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 5 ▶use vars qw(@ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
31 5 ▶use Imager::Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 5 @ISA = qw(Imager::Graph);
33 5 ▶use Imager::Graph::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 ▶use POSIX qw(floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 5 ▶use constant PI => 3.1415926535;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 =item $graph->draw(...)
39  
40 Draws a pie graph onto a new image and returns the image.
41  
42 You must at least supply a C<data> parameter and should probably supply a C<labels> parameter. If you supply a C<labels> parameter, you must supply a C<font> parameter.
43  
44 The C<data> parameter should be a reference to an array containing the
45 data the pie graph should present.
46  
47 The C<labels> parameter is a reference to an array of labels,
48 corresponding to the values in C<data>.
49  
50 =back
51  
52 =head1 FEATURES
53  
54 As described in L<Imager::Graph> you can enable extra features for
55 your graph. The features you can use with pie graphs are:
56  
57 =over
58  
59 =item show_callouts_onAll_segments()
60  
61 Feature: allcallouts.
62 X<allcallouts>X<features, allcallouts>
63  
64 all labels are presented as callouts
65  
66 =cut
67  
68 sub show_callouts_onAll_segments {
69 1 ▶ $_[0]->{'custom_style'}->{'features'}->{'allcallouts'} = 1;
Subroutine show_callouts_onAll_segments: called CC=1 SCAR=0.0
Pod documented
70 }
71  
72 =item show_only_label_percentages()
73  
74 Feature: labelspconly
75 X<labelspconly>X<features, labelspconly>
76  
77 only show the percentage, not the labels.
78  
79 =cut
80  
81 sub show_only_label_percentages {
82 1 ▶ $_[0]->{'custom_style'}->{'features'}->{'labelspconly'} = 1;
Subroutine show_only_label_percentages: called CC=1 SCAR=0.0
Pod documented
83 }
84  
85 =item show_label_percentages()
86  
87 Feature: labelspc
88 X<labelspc>X<features, labelspc>
89  
90 adds the percentage of the pie to each label.
91  
92 =cut
93  
94 sub show_label_percentages {
95 1 ▶ $_[0]->{'custom_style'}->{'features'}->{'labelspc'} = 1;
Subroutine show_label_percentages: called CC=1 SCAR=0.0
Pod documented
96 }
97  
98 =back
99  
100 Inherited features:
101  
102 =over
103  
104 =item legend
105  
106 adds a legend to your graph. Requires the labels parameter
107  
108 =item labels
109  
110 labels each segment of the graph. If the label doesn't fit inside the
111 segment it is presented as a callout.
112  
113 =item outline
114  
115 the pie segments are outlined.
116  
117 =item dropshadow
118  
119 the pie is given a drop shadow.
120  
121 =back
122  
123 =head1 PIE CHART STYLES
124  
125 The following style values are specific to pie charts:
126  
127 Controlling callouts, the C<callout> option:
128  
129 =over
130  
131 =item *
132  
133 color - the color of the callout line and the callout text.
134  
135 =item *
136  
137 font, size - font and size of the callout text
138  
139 =item *
140  
141 outside - the distance the radial callout line goes outside the pie
142  
143 =item *
144  
145 leadlen - the length of the horizontal callout line from the end of
146 the radial line.
147  
148 =item *
149  
150 gap - the distance between the end of the horizontal callout line and
151 the label.
152  
153 =item *
154  
155 inside - the length of the radial callout line within the pie.
156  
157 =back
158  
159 The outline, line option controls the color of the pie segment
160 outlines, if enabled with the C<outline> feature.
161  
162 Under C<pie>:
163  
164 =over
165  
166 =item *
167  
168 maxsegment - any segment below this fraction of the total of the
169 segments will be put into the "others" segment. Default: 0.01
170  
171 =back
172  
173 The top level C<otherlabel> setting controls the label for the
174 "others" segment, default "(others)".
175  
176 =head1 EXAMPLES
177  
178 Assuming:
179  
180 # from the Netcraft September 2001 web survey
181 # http://www.netcraft.com/survey/
182 my @data = qw(17874757 8146372 1321544 811406 );
183 my @labels = qw(Apache Microsoft i_planet Zeus );
184  
185 my $pie = Imager::Graph::Pie->new;
186  
187 First a simple graph, normal size, no labels:
188  
189 my $img = $pie->draw(data=>\@data)
190 or die $pie->error;
191  
192 label the segments:
193  
194 # error handling omitted for brevity from now on
195 $img = $pie->draw(data=>\@data, labels=>\@labels, features=>'labels');
196  
197 just percentages in the segments:
198  
199 $img = $pie->draw(data=>\@data, features=>'labelspconly');
200  
201 add a legend as well:
202  
203 $img = $pie->draw(data=>\@data, labels=>\@labels,
204 features=>[ 'labelspconly', 'legend' ]);
205  
206 and a title, but move the legend down, and add a dropshadow:
207  
208 $img = $pie->draw(data=>\@data, labels=>\@labels,
209 title=>'Netcraft Web Survey',
210 legend=>{ valign=>'bottom' },
211 features=>[ qw/labelspconly legend dropshadow/ ]);
212  
213 something a bit prettier:
214  
215 $img = $pie->draw(data=>\@data, labels=>\@labels,
216 style=>'fount_lin', features=>'legend');
217  
218 suitable for monochrome output:
219  
220 $img = $pie->draw(data=>\@data, labels=>\@labels,
221 style=>'mono', features=>'legend');
222  
223 =cut
224  
225 # this function is too long
226 sub draw {
227 24 ▶ my ($self, %opts) = @_;
Subroutine draw: called CC=58 SCAR=41.0
Pod documented
228  
229 24 my $data_series = $self->_get_data_series(\%opts);
230  
231 24 ▶ $self->_valid_input($data_series)
Branch: 1 branch
BranchTrueFalseTotal
$self->_valid_input($data_series) or return 3 21 24
232 or return;
233  
234 21 my @data = @{$data_series->[0]->{'data'}};
235  
236 21 ▶ my @labels = @{$self->_get_labels(\%opts) || []};
Branch: 1 branch
BranchTrueFalseTotal
$self->_get_labels(\%opts) or [] 2 19 21
237  
238 21 $self->_style_setup(\%opts);
239  
240 21 my $style = $self->{_style};
241  
242 21 ▶ my $img = $self->_make_img()
Branch: 1 branch
BranchTrueFalseTotal
my $img = $self->_make_img or return 1 20 21
243 or return;
244  
245 20 my @chart_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 );
246 20 ▶ if ($style->{title}{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style->{"title"}{"text"}) 7 13 20
247 7 ▶ $self->_draw_title($img, \@chart_box)
Branch: 1 branch
BranchTrueFalseTotal
$self->_draw_title($img, \@chart_box) or return 1 6 7
248 or return;
249 }
250  
251 19 my $total = 0;
252 19 for my $item (@data) {
253 134 $total += $item;
254 }
255  
256 # consolidate any segments that are too small to display
257 19 $self->_consolidate_segments(\@data, \@labels, $total);
258  
259 19 ▶ if ($style->{features}{legend} && (scalar @labels)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style->{"features"}{"legend"} and scalar @labels) 10 9 19
Condition100%2/2
$style->{"features"}{"legend"} and scalar @labels
!ll
910
MC/DC100%1/1
$style->{"features"}{"legend"} and scalar @labels
$style->{"features"}{"legend"}
Truth table100%2/2
A
A= $style->{"features"}{"legend"}
Aresult
00
11
260 10 ▶ $self->_draw_legend($img, \@labels, \@chart_box)
Branch: 1 branch
BranchTrueFalseTotal
$self->_draw_legend($img, \@labels, \@chart_box) or return 2 8 10
261 or return;
262 }
263  
264 # the following code is fairly ugly
265 # it attempts to work out a good layout for the components of the chart
266 17 my @info;
267 17 my $index = 0;
268 17 my $pos = 0;
269 17 my @ebox = (0, 0, 0, 0);
270 17 ▶ defined(my $callout_outside = $self->_get_number('callout.outside'))
Branch: 1 branch
BranchTrueFalseTotal
defined(my $callout_outside = $self->_get_number("callout.outside")) or return 0 17 17
271 or return;
272 17 ▶ defined(my $callout_leadlen = $self->_get_number('callout.leadlen'))
Branch: 1 branch
BranchTrueFalseTotal
defined(my $callout_leadlen = $self->_get_number("callout.leadlen")) or return 0 17 17
273 or return;
274 17 ▶ defined(my $callout_gap = $self->_get_number('callout.gap'))
Branch: 1 branch
BranchTrueFalseTotal
defined(my $callout_gap = $self->_get_number("callout.gap")) or return 0 17 17
275 or return;
276 17 ▶ defined(my $label_vpad = $self->_get_number('label.vpad'))
Branch: 1 branch
BranchTrueFalseTotal
defined(my $label_vpad = $self->_get_number("label.vpad")) or return 0 17 17
277 or return;
278 17 ▶ defined(my $label_hpad = $self->_get_number('label.hpad'))
Branch: 1 branch
BranchTrueFalseTotal
defined(my $label_hpad = $self->_get_number("label.hpad")) or return 0 17 17
279 or return;
280 my $guessradius =
281 17 int($self->_small_extent(\@chart_box) * $style->{pie}{guessfactor} * 0.5);
282 17 for my $data (@data) {
283 86 my $item = { data=>$data, index=>$index };
284 86 my $size = 2 * PI * $data / $total;
285 86 $item->{begin} = $pos;
286 86 $pos += $size;
287 86 $item->{end} = $pos;
288 86 ▶ if (scalar @labels) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @labels) 86 0 86
289 86 $item->{text} = $labels[$index];
290 }
291 86 ▶ if ($style->{features}{labelspconly}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style->{"features"}{"labelspconly"}) 15 71 86
292 $item->{text} =
293 15 $style->{label}{pconlyformat}->($data/$total * 100);
294 }
295 86 ▶ if ($item->{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"text"}) 86 0 86
296 86 ▶ if ($style->{features}{labelspc}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($style->{'features'}{'labelspc'}) { } 24 62 86
elsif ($style->{'features'}{'labelspconly'}) { } 15 47 62
elsif ($style->{'features'}{'labels'}) { } 27 20 47
297 $item->{text} =
298 24 $style->{label}{pcformat}->($item->{text}, $data/$total * 100);
299 24 $item->{label} = 1;
300 }
301 elsif ($style->{features}{labelspconly}) {
302 $item->{text} =
303 15 $style->{label}{pconlyformat}->($data/$total * 100);
304 15 $item->{label} = 1;
305 }
306 elsif ($style->{features}{labels}) {
307 27 $item->{label} = 1;
308 }
309 86 ▶ $item->{callout} = 1 if $style->{features}{allcallouts};
Branch: 1 branch
BranchTrueFalseTotal
if $style->{"features"}{"allcallouts"} 21 65 86
310 86 ▶ if (!$item->{callout}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($item->{"callout"}) 65 21 86
311 65 ▶ my @lbox = $self->_text_bbox($item->{text}, 'label')
Branch: 1 branch
BranchTrueFalseTotal
my(@lbox) = $self->_text_bbox($item->{"text"}, "label") or return 1 64 65
312 or return;
313 64 $item->{lbox} = \@lbox;
314 64 ▶ if ($item->{label}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"label"}) 51 13 64
315 51 ▶ unless ($self->_fit_text(0, 0, 'label', $item->{text}, $guessradius,
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_fit_text(0, 0, "label", $item->{"text"}, $guessradius, $item->{"begin"}, $item->{"end"})) 18 33 51
316 $item->{begin}, $item->{end})) {
317 18 $item->{callout} = 1;
318 }
319 }
320 }
321 85 ▶ if ($item->{callout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"callout"}) 39 46 85
322 39 $item->{label} = 0;
323 39 ▶ my @cbox = $self->_text_bbox($item->{text}, 'callout')
Branch: 1 branch
BranchTrueFalseTotal
my(@cbox) = $self->_text_bbox($item->{"text"}, "callout") or return 1 38 39
324 or return;
325 38 $item->{cbox} = \@cbox;
326 38 $item->{cangle} = ($item->{begin} + $item->{end}) / 2;
327 38 my $dist = cos($item->{cangle}) * ($guessradius+
328 $callout_outside);
329 38 my $co_size = $callout_leadlen + $callout_gap + $item->{cbox}[2];
330 38 ▶ if ($dist < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dist < 0) { } 4 34 38
331 4 $dist -= $co_size - $guessradius;
332 4 ▶ $dist < $ebox[0] and $ebox[0] = $dist;
Branch: 1 branch
BranchTrueFalseTotal
$dist < $ebox[0] and $ebox[0] = $dist 4 0 4
333 }
334 else {
335 34 $dist += $co_size - $guessradius;
336 34 ▶ $dist > $ebox[2] and $ebox[2] = $dist;
Branch: 1 branch
BranchTrueFalseTotal
$dist > $ebox[2] and $ebox[2] = $dist 29 5 34
337 }
338 }
339 }
340 84 push(@info, $item);
341 84 ++$index;
342 }
343  
344 my $radius =
345 15 int($self->_small_extent(\@chart_box) * $style->{pie}{size} * 0.5);
346 15 my $max_width = $chart_box[2] - $chart_box[0] + $ebox[0] - $ebox[2];
347 15 ▶ if ($radius > $max_width / 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($radius > $max_width / 2) 5 10 15
348 5 $radius = int($max_width / 2);
349 }
350 15 $chart_box[0] -= $ebox[0];
351 15 $chart_box[2] -= $ebox[2];
352 15 my $cx = int(($chart_box[0] + $chart_box[2]) / 2);
353 15 my $cy = int(($chart_box[1] + $chart_box[3]) / 2);
354 15 ▶ if ($style->{features}{dropshadow}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style->{"features"}{"dropshadow"}) 10 5 15
355 10 ▶ my @shadow_fill = $self->_get_fill('dropshadow.fill')
Branch: 1 branch
BranchTrueFalseTotal
my(@shadow_fill) = $self->_get_fill("dropshadow.fill") or return 0 10 10
356 or return;
357 10 ▶ my $offx = $self->_get_number('dropshadow.offx')
Branch: 1 branch
BranchTrueFalseTotal
my $offx = $self->_get_number("dropshadow.offx") or return 0 10 10
358 or return;
359 10 my $offy = $self->_get_number('dropshadow.offy');
360 10 for my $item (@info) {
361 $img->arc(x=>$cx+$offx, 'y'=>$cy+$offy, r=>$radius+1, aa => 1,
362 d1=>180/PI * $item->{begin}, d2=>180/PI * $item->{end},
363 59 @shadow_fill);
364 }
365 $self->_filter_region($img,
366 $cx+$offx-$radius-10, $cy+$offy-$radius-10,
367 $cx+$offx+$radius+10, $cy+$offy+$radius+10,
368 'dropshadow.filter')
369 10 ▶ if $style->{dropshadow}{filter};
Branch: 1 branch
BranchTrueFalseTotal
if $style->{"dropshadow"}{"filter"} 10 0 10
370 }
371  
372 15 my @fill_box = ( $cx-$radius, $cy-$radius, $cx+$radius, $cy+$radius );
373 15 my $fill_aa = $self->_get_number('fill.aa');
374 15 for my $item (@info) {
375 $item->{begin} < $item->{end}
376 84 ▶ or next;
Branch: 1 branch
BranchTrueFalseTotal
$item->{"begin"} < $item->{"end"} or next 3 81 84
377 81 ▶ my @fill = $self->_data_fill($item->{index}, \@fill_box)
Branch: 1 branch
BranchTrueFalseTotal
my(@fill) = $self->_data_fill($item->{"index"}, \@fill_box) or return 0 81 81
378 or return;
379 $img->arc(x=>$cx, 'y'=>$cy, r=>$radius, aa => $fill_aa,
380 d1=>180/PI * $item->{begin}, d2=>180/PI * $item->{end},
381 81 @fill);
382 }
383 15 ▶ if ($style->{features}{outline}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($style->{"features"}{"outline"}) 6 9 15
384 6 my %outstyle = $self->_line_style('outline');
385 6 my $out_radius = 0.5 + $radius;
386 6 for my $item (@info) {
387 41 my $px = int($cx + $out_radius * cos($item->{begin}));
388 41 my $py = int($cy + $out_radius * sin($item->{begin}));
389 $item->{begin} < $item->{end}
390 41 ▶ or next;
Branch: 1 branch
BranchTrueFalseTotal
$item->{"begin"} < $item->{"end"} or next 3 38 41
391 38 $img->line(x1=>$cx, y1=>$cy, x2=>$px, y2=>$py, %outstyle);
392 38 ▶ for (my $i = $item->{begin}; $i < $item->{end}; $i += PI/180) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $item->{"end"}) 2178 38 2216
393 2178 my $stroke_end = $i + PI/180;
394 2178 ▶ $stroke_end = $item->{end} if $stroke_end > $item->{end};
Branch: 1 branch
BranchTrueFalseTotal
if $stroke_end > $item->{"end"} 38 2140 2178
395 2178 my $nx = int($cx + $out_radius * cos($stroke_end));
396 2178 my $ny = int($cy + $out_radius * sin($stroke_end));
397 2178 $img->line(x1=>$px, y1=>$py, x2=>$nx, y2=>$ny, %outstyle);
398 2178 ($px, $py) = ($nx, $ny);
399 }
400 }
401 }
402  
403 15 my $callout_inside = $radius - $self->_get_number('callout.inside');
404 15 $callout_outside += $radius;
405 15 my %callout_text;
406 my %label_text;
407 15 my %callout_line;
408 15 my $leader_aa = $self->_get_number('callout.leadaa');
409 15 for my $label (@info) {
410 84 ▶ if ($label->{label} && !$label->{callout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label->{"label"} and not $label->{"callout"}) 33 51 84
Condition66%2/3
$label->{"label"} and not $label->{"callout"}
!ll&&!rl&&r
51033
MC/DC50%1/2
$label->{"label"} and not $label->{"callout"}
$label->{"label"} not $label->{"callout"}
Truth table66%2/3
A and not(B)
A= $label->{"label"}B= not $label->{"callout"}
ABresult
0X0
100
111
411 # at this point we know we need the label font, to calculate
412 # whether the label will fit if anything else
413 33 ▶ unless (%label_text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%label_text) 8 25 33
414 8 ▶ %label_text = $self->_text_style('label')
Branch: 1 branch
BranchTrueFalseTotal
%label_text = $self->_text_style("label") or return 0 8 8
415 or return;
416 }
417 my @loc = $self->_fit_text($cx, $cy, 'label', $label->{text}, $radius,
418 33 $label->{begin}, $label->{end});
419 33 ▶ if (@loc) {
Branch: 1 branch
BranchTrueFalseTotal
if (@loc) { } 33 0 33
420 33 my $tcx = ($loc[0]+$loc[2])/2;
421 33 my $tcy = ($loc[1]+$loc[3])/2;
422 #$img->box(xmin=>$loc[0], ymin=>$loc[1], xmax=>$loc[2], ymax=>$loc[3],
423 # color=>Imager::Color->new(0,0,0));
424 $img->string(%label_text, x=>$tcx-$label->{lbox}[2]/2,
425 'y'=>$tcy+$label->{lbox}[3]/2+$label->{lbox}[1],
426 33 text=>$label->{text});
427 }
428 else {
429 0 $label->{callout} = 1;
430 0 ▶ my @cbox = $self->_text_bbox($label->{text}, 'callout')
Branch: 1 branch
BranchTrueFalseTotal
my(@cbox) = $self->_text_bbox($label->{"text"}, "callout") or return 0 0 0
431 or return;
432 0 $label->{cbox} = \@cbox;
433 0 $label->{cangle} = ($label->{begin} + $label->{end}) / 2;
434 }
435 }
436 84 ▶ if ($label->{callout}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label->{"callout"}) 38 46 84
437 38 ▶ unless (%callout_text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%callout_text) 12 26 38
438 12 ▶ %callout_text = $self->_text_style('callout')
Branch: 1 branch
BranchTrueFalseTotal
%callout_text = $self->_text_style("callout") or return 0 12 12
439 or return;
440 12 %callout_line = $self->_line_style('callout');
441 }
442 38 my $ix = floor(0.5 + $cx + $callout_inside * cos($label->{cangle}));
443 38 my $iy = floor(0.5 + $cy + $callout_inside * sin($label->{cangle}));
444 38 my $ox = floor(0.5 + $cx + $callout_outside * cos($label->{cangle}));
445 38 my $oy = floor(0.5 + $cy + $callout_outside * sin($label->{cangle}));
446 38 ▶ my $lx = ($ox < $cx) ? $ox - $callout_leadlen : $ox + $callout_leadlen;
Branch: 1 branch
BranchTrueFalseTotal
$ox < $cx ? : 4 34 38
447 38 $img->polyline(points => [ [ $ix, $iy ],
448 [ $ox, $oy ],
449 [ $lx, $oy ] ],
450 %callout_line);
451 #my $tx = $lx + $callout_gap;
452 38 my $ty = $oy + $label->{cbox}[3]/2+$label->{cbox}[1];
453 38 ▶ if ($lx < $cx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lx < $cx) { } 4 34 38
454 $img->string(%callout_text, x=>$lx-$callout_gap-$label->{cbox}[2],
455 4 'y'=>$ty, text=>$label->{text});
456 }
457 else {
458 $img->string(%callout_text, x=>$lx+$callout_gap, 'y'=>$ty,
459 34 text=>$label->{text});
460 }
461 }
462 }
463  
464 15 $img;
465 }
466  
467 sub _valid_input {
468 24 ▶ my ($self, $data_series) = @_;
Subroutine _valid_input: called CC=8 SCAR=21.2
469  
470 24 ▶ if (!defined $data_series || !scalar @$data_series) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data_series and scalar @$data_series) 0 24 24
Condition33%1/3
defined $data_series and scalar @$data_series
!ll&&!rl&&r
0024
MC/DC0%0/2
defined $data_series and scalar @$data_series
defined $data_series scalar @$data_series
Truth table33%1/3
A and B
A= defined $data_seriesB= scalar @$data_series
ABresult
0X0
100
111
471 0 return $self->_error("No data supplied");
472 }
473  
474 24 ▶ @$data_series == 1
Branch: 1 branch
BranchTrueFalseTotal
@$data_series == 1 or return $self->_error("Pie charts only allow one data series") 0 24 24
475 or return $self->_error("Pie charts only allow one data series");
476  
477 24 my $data = $data_series->[0]{data};
478  
479 24 ▶ if (!scalar @$data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$data) 1 23 24
480 1 return $self->_error("No values in data series");
481 }
482  
483 23 my $total = 0;
484 {
485 23 my $index = 0;
486 23 for my $item (@$data) {
487 153 ▶ $item < 0
Branch: 1 branch
BranchTrueFalseTotal
$item < 0 and return $self->_error("Data index $index is less than zero") 1 152 153
488 and return $self->_error("Data index $index is less than zero");
489  
490 152 $total += $item;
491  
492 152 ++$index;
493 }
494 }
495 22 ▶ $total == 0
Branch: 1 branch
BranchTrueFalseTotal
$total == 0 and return $self->_error("Sum of all data values is zero") 1 21 22
496 and return $self->_error("Sum of all data values is zero");
497  
498 21 return 1;
499 }
500  
501 =head1 INTERNAL FUNCTIONS
502  
503 These are used in the implementation of Imager::Graph, and are
504 documented for debuggers and developers.
505  
506 =over
507  
508 =item _consolidate_segments($data, $labels, $total)
509  
510 Consolidate segments that are too small into an 'others' segment.
511  
512 =cut
513  
514 sub _consolidate_segments {
515 19 ▶ my ($self, $data, $labels, $total) = @_;
Subroutine _consolidate_segments: called CC=6 SCAR=17.9
516  
517 19 my @others;
518 19 my $index = 0;
519 19 for my $item (@$data) {
520 134 ▶ if ($item / $total < $self->{_style}{pie}{maxsegment}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item / $total < $self->{"_style"}{"pie"}{"maxsegment"}) 48 86 134
521 48 push(@others, $index);
522 }
523 134 ++$index;
524 }
525 19 ▶ if (@others) {
Branch: 1 branch
BranchTrueFalseTotal
if (@others) 18 1 19
526 18 my $others = 0;
527 18 for my $index (reverse @others) {
528 48 $others += $data->[$index];
529 48 splice(@$labels, $index, 1);
530 48 splice(@$data, $index, 1);
531 }
532 18 ▶ push(@$labels, $self->{_style}{otherlabel}) if @$labels;
Branch: 1 branch
BranchTrueFalseTotal
if @$labels 18 0 18
533 18 push(@$data, $others);
534 }
535 }
536  
537 # used for debugging
538 sub _test_line {
539 0 ▶ my ($x, $y, @l) = @_;
Subroutine _test_line: not called CC=2 SCAR=17.9
540  
541 0 my $res = $l[0]*$x + $l[1] * $y + $l[2];
542 0 ▶ print "test ", (abs($res) < 0.000001) ? "success\n" : "failure $res\n";
Branch: 1 branch
BranchTrueFalseTotal
abs $res < 1e-06 ? : 0 0 0
543 }
544  
545 =item _fit_text($cx, $cy, $name, $text, $radius, $begin, $end)
546  
547 Attempts to fit text into a pie segment with its center at ($cx, $cy)
548 with the given radius, covering the angles $begin through $end.
549  
550 Returns a list defining the bounding box of the text if it does fit.
551  
552 =cut
553  
554 sub _fit_text {
555 84 ▶ my ($self, $cx, $cy, $name, $text, $radius, $begin, $end) = @_;
Subroutine _fit_text: called CC=16 SCAR=27.7
556  
557 #print "fit: $cx, $cy '$text' $radius $begin $end\n";
558 84 ▶ my @tbox = $self->_text_bbox($text, $name)
Branch: 1 branch
BranchTrueFalseTotal
my(@tbox) = $self->_text_bbox($text, $name) or return 0 84 84
559 or return;
560 84 my $tcx = floor(0.5+$cx + cos(($begin+$end)/2) * $radius *3/5);
561 84 my $tcy = floor(0.5+$cy + sin(($begin+$end)/2) * $radius *3/5);
562 84 my $topy = $tcy - $tbox[3]/2;
563 84 my $boty = $topy + $tbox[3];
564 84 my @lines;
565 84 for my $y ($topy, $boty) {
566 166 my %entry = ( 'y'=>$y );
567 166 $entry{line} = [ line_from_points($tcx, $y, $tcx+1, $y) ];
568 166 $entry{left} = -$radius;
569 166 $entry{right} = $radius;
570 166 for my $angle ($begin, $end) {
571 330 my $ex = $cx + cos($angle)*$radius;
572 330 my $ey = $cy + sin($angle)*$radius;
573 330 my @line = line_from_points($cx, $cy, $ex, $ey);
574 #_test_line($cx, $cy, @line);
575 #_test_line($ex, $ey, @line);
576 330 my $goodsign = $line[0] * $tcx + $line[1] * $tcy + $line[2];
577 330 for my $pos (@entry{qw/left right/}) {
578 658 my $sign = $line[0] * ($pos+$tcx) + $line[1] * $y + $line[2];
579 658 ▶ if ($goodsign * $sign < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($goodsign * $sign < 0) 239 419 658
580 239 ▶ if (my @p = intersect_lines(@line, @{$entry{line}})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@p) = intersect_lines(@line, @{$entry{'line'};})) { } 237 2 239
581 # die "$goodsign $sign ($pos, $tcx) no intersect (@line) (@{$entry{line}})" ; # this would be wierd
582 #_test_line(@p, @line);
583 #_test_line(@p, @{$entry{line}});
584 237 $pos = $p[0]-$tcx;
585 }
586 else {
587 2 return;
588 }
589
590 }
591  
592 # circle
593 656 my $dist2 = ($pos+$tcx-$cx) * ($pos+$tcx-$cx)
594 + ($y - $cy) * ($y - $cy);
595 656 ▶ if ($dist2 > $radius * $radius) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dist2 > $radius * $radius) 204 452 656
596 my @points =
597 204 intersect_line_and_circle(@{$entry{line}}, $cx, $cy, $radius);
598 204 ▶ while (@points) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points) 408 204 612
599 408 my @p = splice(@points, 0, 2);
600 408 ▶ if ($p[0] < $cx && $tcx+$pos < $p[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if ($p[0] < $cx and $tcx + $pos < $p[0]) { } 70 338 408
elsif ($p[0] > $cx and $tcx + $pos > $p[0]) { } 106 366 472
Condition100%3/3
$p[0] < $cx and $tcx + $pos < $p[0]
!ll&&!rl&&r
20413470
MC/DC100%2/2
$p[0] < $cx and $tcx + $pos < $p[0]
$p[0] < $cx $tcx + $pos < $p[0]
Truth table100%3/3
A and B
A= $p[0] < $cxB= $tcx + $pos < $p[0]
ABresult
0X0
100
111
601 70 $pos = $p[0]-$tcx;
602 }
603 ▶ elsif ($p[0] > $cx && $tcx+$pos > $p[0]) {
Condition100%3/3
$p[0] > $cx and $tcx + $pos > $p[0]
!ll&&!rl&&r
13498106
MC/DC100%2/2
$p[0] > $cx and $tcx + $pos > $p[0]
$p[0] > $cx $tcx + $pos > $p[0]
Truth table100%3/3
A and B
A= $p[0] > $cxB= $tcx + $pos > $p[0]
ABresult
0X0
100
111
604 106 $pos = $p[0]-$tcx;
605 }
606 }
607 }
608 }
609 }
610 164 push(@lines, \%entry);
611 }
612 82 ▶ my $left = $lines[0]{left} > $lines[1]{left} ? $lines[0]{left} : $lines[1]{left};
Branch: 1 branch
BranchTrueFalseTotal
$lines[0]{'left'} > $lines[1]{'left'} ? : 41 41 82
613 82 ▶ my $right = $lines[0]{right} < $lines[1]{right} ? $lines[0]{right} : $lines[1]{right};
Branch: 1 branch
BranchTrueFalseTotal
$lines[0]{'right'} < $lines[1]{'right'} ? : 44 38 82
614 82 ▶ return if $right - $left < $tbox[2];
Branch: 1 branch
BranchTrueFalseTotal
if $right - $left < $tbox[2] 16 66 82
615  
616 66 return ($tcx+$left, $topy, $tcx+$right, $boty);
617 }
618  
619 sub _composite {
620 21 ▶ ( 'pie', $_[0]->SUPER::_composite() );
Subroutine _composite: called CC=1 SCAR=0.0
621 }
622  
623 sub _style_defs {
624 21 ▶ my ($self) = @_;
Subroutine _style_defs: called CC=1 SCAR=0.0
625  
626 21 my %work = %{$self->SUPER::_style_defs()};
627 21 $work{otherlabel} = "(others)";
628 $work{pie} =
629 {
630 21 guessfactor=>0.6,
631 size=>0.8,
632 maxsegment=> 0.01,
633 };
634  
635 21 \%work;
636 }
637  
638 5 1;
639 __END__
« Graph/Horizontal.pm ↑ Summary Graph/Util.pm »