| 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
| |||||||||||||||||||||||||||||||||
| 232 | or return; | ||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||
| 234 | 21 | my @data = @{$data_series->[0]->{'data'}}; | |||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | 21 | ▶ | my @labels = @{$self->_get_labels(\%opts) || []}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 247 | 7 | ▶ | $self->_draw_title($img, \@chart_box) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$style->{"features"}{"legend"} and scalar @labels
MC/DC100%1/1
$style->{"features"}{"legend"} and scalar @labels
$style->{"features"}{"legend"}
Truth table100%2/2
A
A= $style->{"features"}{"legend"}
| |||||||||||||||||||||||||||||||||
| 260 | 10 | ▶ | $self->_draw_legend($img, \@labels, \@chart_box) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 271 | or return; | ||||||||||||||||||||||||||||||||
| 272 | 17 | ▶ | defined(my $callout_leadlen = $self->_get_number('callout.leadlen')) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 273 | or return; | ||||||||||||||||||||||||||||||||
| 274 | 17 | ▶ | defined(my $callout_gap = $self->_get_number('callout.gap')) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 275 | or return; | ||||||||||||||||||||||||||||||||
| 276 | 17 | ▶ | defined(my $label_vpad = $self->_get_number('label.vpad')) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 277 | or return; | ||||||||||||||||||||||||||||||||
| 278 | 17 | ▶ | defined(my $label_hpad = $self->_get_number('label.hpad')) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 289 | 86 | $item->{text} = $labels[$index]; | |||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||
| 291 | 86 | ▶ | if ($style->{features}{labelspconly}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 292 | $item->{text} = | ||||||||||||||||||||||||||||||||
| 293 | 15 | $style->{label}{pconlyformat}->($data/$total * 100); | |||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||
| 295 | 86 | ▶ | if ($item->{text}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 296 | 86 | ▶ | if ($style->{features}{labelspc}) { | ||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 310 | 86 | ▶ | if (!$item->{callout}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 311 | 65 | ▶ | my @lbox = $self->_text_bbox($item->{text}, 'label') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 312 | or return; | ||||||||||||||||||||||||||||||||
| 313 | 64 | $item->{lbox} = \@lbox; | |||||||||||||||||||||||||||||||
| 314 | 64 | ▶ | if ($item->{label}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 315 | 51 | ▶ | unless ($self->_fit_text(0, 0, 'label', $item->{text}, $guessradius, | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 316 | $item->{begin}, $item->{end})) { | ||||||||||||||||||||||||||||||||
| 317 | 18 | $item->{callout} = 1; | |||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||
| 321 | 85 | ▶ | if ($item->{callout}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 322 | 39 | $item->{label} = 0; | |||||||||||||||||||||||||||||||
| 323 | 39 | ▶ | my @cbox = $self->_text_bbox($item->{text}, 'callout') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 331 | 4 | $dist -= $co_size - $guessradius; | |||||||||||||||||||||||||||||||
| 332 | 4 | ▶ | $dist < $ebox[0] and $ebox[0] = $dist; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 333 | } | ||||||||||||||||||||||||||||||||
| 334 | else { | ||||||||||||||||||||||||||||||||
| 335 | 34 | $dist += $co_size - $guessradius; | |||||||||||||||||||||||||||||||
| 336 | 34 | ▶ | $dist > $ebox[2] and $ebox[2] = $dist; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 355 | 10 | ▶ | my @shadow_fill = $self->_get_fill('dropshadow.fill') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 356 | or return; | ||||||||||||||||||||||||||||||||
| 357 | 10 | ▶ | my $offx = $self->_get_number('dropshadow.offx') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 377 | 81 | ▶ | my @fill = $self->_data_fill($item->{index}, \@fill_box) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 393 | 2178 | my $stroke_end = $i + PI/180; | |||||||||||||||||||||||||||||||
| 394 | 2178 | ▶ | $stroke_end = $item->{end} if $stroke_end > $item->{end}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$label->{"label"} and not $label->{"callout"}
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"}
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 414 | 8 | ▶ | %label_text = $self->_text_style('label') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 437 | 38 | ▶ | unless (%callout_text) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 438 | 12 | ▶ | %callout_text = $self->_text_style('callout') | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $data_series and scalar @$data_series
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
| |||||||||||||||||||||||||||||||||
| 471 | 0 | return $self->_error("No data supplied"); | |||||||||||||||||||||||||||||||
| 472 | } | ||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||
| 474 | 24 | ▶ | @$data_series == 1 | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 521 | 48 | push(@others, $index); | |||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||
| 523 | 134 | ++$index; | |||||||||||||||||||||||||||||||
| 524 | } | ||||||||||||||||||||||||||||||||
| 525 | 19 | ▶ | if (@others) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 580 | 239 | ▶ | if (my @p = intersect_lines(@line, @{$entry{line}})) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 596 | my @points = | ||||||||||||||||||||||||||||||||
| 597 | 204 | intersect_line_and_circle(@{$entry{line}}, $cx, $cy, $radius); | |||||||||||||||||||||||||||||||
| 598 | 204 | ▶ | while (@points) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 599 | 408 | my @p = splice(@points, 0, 2); | |||||||||||||||||||||||||||||||
| 600 | 408 | ▶ | if ($p[0] < $cx && $tcx+$pos < $p[0]) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$p[0] < $cx and $tcx + $pos < $p[0]
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]
| |||||||||||||||||||||||||||||||||
| 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]
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]
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 613 | 82 | ▶ | my $right = $lines[0]{right} < $lines[1]{right} ? $lines[0]{right} : $lines[1]{right}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 614 | 82 | ▶ | return if $right - $left < $tbox[2]; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||