line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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 to |
20
|
|
|
|
|
|
|
create good looking pie graphs. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
Most of the basic layout and color selection is handed off to |
23
|
|
|
|
|
|
|
L. |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=over |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=cut |
28
|
|
|
|
|
|
|
|
29
|
4
|
|
|
4
|
|
2385
|
use strict; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
158
|
|
30
|
4
|
|
|
4
|
|
21
|
use vars qw(@ISA); |
|
4
|
|
|
|
|
3
|
|
|
4
|
|
|
|
|
297
|
|
31
|
4
|
|
|
4
|
|
2433
|
use Imager::Graph; |
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
315
|
|
32
|
|
|
|
|
|
|
@ISA = qw(Imager::Graph); |
33
|
4
|
|
|
4
|
|
3965
|
use Imager::Graph::Util; |
|
4
|
|
|
|
|
12
|
|
|
4
|
|
|
|
|
318
|
|
34
|
4
|
|
|
4
|
|
2669
|
use POSIX qw(floor); |
|
4
|
|
|
|
|
36305
|
|
|
4
|
|
|
|
|
35
|
|
35
|
|
|
|
|
|
|
|
36
|
4
|
|
|
4
|
|
7339
|
use constant PI => 3.1415926535; |
|
4
|
|
|
|
|
10
|
|
|
4
|
|
|
|
|
17141
|
|
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 parameter and should probably supply a C parameter. If you supply a C parameter, you must supply a C parameter. |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
The C parameter should be a reference to an array containing the |
45
|
|
|
|
|
|
|
data the pie graph should present. |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
The C parameter is a reference to an array of labels, |
48
|
|
|
|
|
|
|
corresponding to the values in C. |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=back |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 FEATURES |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
As described in L 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
|
|
|
|
|
|
|
XX |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
all labels are presented as callouts |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=cut |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub show_callouts_onAll_segments { |
69
|
1
|
|
|
1
|
1
|
15
|
$_[0]->{'custom_style'}->{'features'}->{'allcallouts'} = 1; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=item show_only_label_percentages() |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
Feature: labelspconly |
75
|
|
|
|
|
|
|
XX |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
only show the percentage, not the labels. |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=cut |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
sub show_only_label_percentages { |
82
|
1
|
|
|
1
|
1
|
9
|
$_[0]->{'custom_style'}->{'features'}->{'labelspconly'} = 1; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item show_label_percentages() |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
Feature: labelspc |
88
|
|
|
|
|
|
|
XX |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
adds the percentage of the pie to each label. |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=cut |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub show_label_percentages { |
95
|
1
|
|
|
1
|
1
|
14
|
$_[0]->{'custom_style'}->{'features'}->{'labelspc'} = 1; |
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 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 feature. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
Under C: |
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 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
|
|
|
24
|
1
|
58699
|
my ($self, %opts) = @_; |
228
|
|
|
|
|
|
|
|
229
|
24
|
|
|
|
|
171
|
my $data_series = $self->_get_data_series(\%opts); |
230
|
|
|
|
|
|
|
|
231
|
24
|
100
|
|
|
|
111
|
$self->_valid_input($data_series) |
232
|
|
|
|
|
|
|
or return; |
233
|
|
|
|
|
|
|
|
234
|
21
|
|
|
|
|
32
|
my @data = @{$data_series->[0]->{'data'}}; |
|
21
|
|
|
|
|
97
|
|
235
|
|
|
|
|
|
|
|
236
|
21
|
100
|
|
|
|
41
|
my @labels = @{$self->_get_labels(\%opts) || []}; |
|
21
|
|
|
|
|
138
|
|
237
|
|
|
|
|
|
|
|
238
|
21
|
|
|
|
|
115
|
$self->_style_setup(\%opts); |
239
|
|
|
|
|
|
|
|
240
|
21
|
|
|
|
|
248
|
my $style = $self->{_style}; |
241
|
|
|
|
|
|
|
|
242
|
21
|
100
|
|
|
|
123
|
my $img = $self->_make_img() |
243
|
|
|
|
|
|
|
or return; |
244
|
|
|
|
|
|
|
|
245
|
20
|
|
|
|
|
361
|
my @chart_box = ( 0, 0, $img->getwidth-1, $img->getheight-1 ); |
246
|
20
|
100
|
|
|
|
725
|
if ($style->{title}{text}) { |
247
|
7
|
100
|
|
|
|
73
|
$self->_draw_title($img, \@chart_box) |
248
|
|
|
|
|
|
|
or return; |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
19
|
|
|
|
|
43
|
my $total = 0; |
252
|
19
|
|
|
|
|
54
|
for my $item (@data) { |
253
|
134
|
|
|
|
|
171
|
$total += $item; |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
# consolidate any segments that are too small to display |
257
|
19
|
|
|
|
|
116
|
$self->_consolidate_segments(\@data, \@labels, $total); |
258
|
|
|
|
|
|
|
|
259
|
19
|
50
|
100
|
|
|
133
|
if ($style->{features}{legend} && (scalar @labels)) { |
260
|
10
|
100
|
|
|
|
85
|
$self->_draw_legend($img, \@labels, \@chart_box) |
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
|
|
|
|
|
46
|
my @info; |
267
|
17
|
|
|
|
|
26
|
my $index = 0; |
268
|
17
|
|
|
|
|
26
|
my $pos = 0; |
269
|
17
|
|
|
|
|
73
|
my @ebox = (0, 0, 0, 0); |
270
|
17
|
50
|
|
|
|
79
|
defined(my $callout_outside = $self->_get_number('callout.outside')) |
271
|
|
|
|
|
|
|
or return; |
272
|
17
|
50
|
|
|
|
62
|
defined(my $callout_leadlen = $self->_get_number('callout.leadlen')) |
273
|
|
|
|
|
|
|
or return; |
274
|
17
|
50
|
|
|
|
59
|
defined(my $callout_gap = $self->_get_number('callout.gap')) |
275
|
|
|
|
|
|
|
or return; |
276
|
17
|
50
|
|
|
|
58
|
defined(my $label_vpad = $self->_get_number('label.vpad')) |
277
|
|
|
|
|
|
|
or return; |
278
|
17
|
50
|
|
|
|
62
|
defined(my $label_hpad = $self->_get_number('label.hpad')) |
279
|
|
|
|
|
|
|
or return; |
280
|
17
|
|
|
|
|
119
|
my $guessradius = |
281
|
|
|
|
|
|
|
int($self->_small_extent(\@chart_box) * $style->{pie}{guessfactor} * 0.5); |
282
|
17
|
|
|
|
|
51
|
for my $data (@data) { |
283
|
86
|
|
|
|
|
278
|
my $item = { data=>$data, index=>$index }; |
284
|
86
|
|
|
|
|
161
|
my $size = 2 * PI * $data / $total; |
285
|
86
|
|
|
|
|
127
|
$item->{begin} = $pos; |
286
|
86
|
|
|
|
|
88
|
$pos += $size; |
287
|
86
|
|
|
|
|
123
|
$item->{end} = $pos; |
288
|
86
|
50
|
|
|
|
168
|
if (scalar @labels) { |
289
|
86
|
|
|
|
|
164
|
$item->{text} = $labels[$index]; |
290
|
|
|
|
|
|
|
} |
291
|
86
|
100
|
|
|
|
211
|
if ($style->{features}{labelspconly}) { |
292
|
15
|
|
|
|
|
72
|
$item->{text} = |
293
|
|
|
|
|
|
|
$style->{label}{pconlyformat}->($data/$total * 100); |
294
|
|
|
|
|
|
|
} |
295
|
86
|
50
|
|
|
|
199
|
if ($item->{text}) { |
296
|
86
|
100
|
|
|
|
310
|
if ($style->{features}{labelspc}) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
297
|
24
|
|
|
|
|
106
|
$item->{text} = |
298
|
|
|
|
|
|
|
$style->{label}{pcformat}->($item->{text}, $data/$total * 100); |
299
|
24
|
|
|
|
|
50
|
$item->{label} = 1; |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
elsif ($style->{features}{labelspconly}) { |
302
|
15
|
|
|
|
|
54
|
$item->{text} = |
303
|
|
|
|
|
|
|
$style->{label}{pconlyformat}->($data/$total * 100); |
304
|
15
|
|
|
|
|
34
|
$item->{label} = 1; |
305
|
|
|
|
|
|
|
} |
306
|
|
|
|
|
|
|
elsif ($style->{features}{labels}) { |
307
|
27
|
|
|
|
|
43
|
$item->{label} = 1; |
308
|
|
|
|
|
|
|
} |
309
|
86
|
100
|
|
|
|
247
|
$item->{callout} = 1 if $style->{features}{allcallouts}; |
310
|
86
|
100
|
|
|
|
207
|
if (!$item->{callout}) { |
311
|
65
|
100
|
|
|
|
229
|
my @lbox = $self->_text_bbox($item->{text}, 'label') |
312
|
|
|
|
|
|
|
or return; |
313
|
64
|
|
|
|
|
152
|
$item->{lbox} = \@lbox; |
314
|
64
|
100
|
|
|
|
159
|
if ($item->{label}) { |
315
|
51
|
100
|
|
|
|
174
|
unless ($self->_fit_text(0, 0, 'label', $item->{text}, $guessradius, |
316
|
|
|
|
|
|
|
$item->{begin}, $item->{end})) { |
317
|
18
|
|
|
|
|
78
|
$item->{callout} = 1; |
318
|
|
|
|
|
|
|
} |
319
|
|
|
|
|
|
|
} |
320
|
|
|
|
|
|
|
} |
321
|
85
|
100
|
|
|
|
188
|
if ($item->{callout}) { |
322
|
39
|
|
|
|
|
68
|
$item->{label} = 0; |
323
|
39
|
100
|
|
|
|
144
|
my @cbox = $self->_text_bbox($item->{text}, 'callout') |
324
|
|
|
|
|
|
|
or return; |
325
|
38
|
|
|
|
|
128
|
$item->{cbox} = \@cbox; |
326
|
38
|
|
|
|
|
136
|
$item->{cangle} = ($item->{begin} + $item->{end}) / 2; |
327
|
38
|
|
|
|
|
114
|
my $dist = cos($item->{cangle}) * ($guessradius+ |
328
|
|
|
|
|
|
|
$callout_outside); |
329
|
38
|
|
|
|
|
94
|
my $co_size = $callout_leadlen + $callout_gap + $item->{cbox}[2]; |
330
|
38
|
100
|
|
|
|
94
|
if ($dist < 0) { |
331
|
4
|
|
|
|
|
10
|
$dist -= $co_size - $guessradius; |
332
|
4
|
50
|
|
|
|
17
|
$dist < $ebox[0] and $ebox[0] = $dist; |
333
|
|
|
|
|
|
|
} |
334
|
|
|
|
|
|
|
else { |
335
|
34
|
|
|
|
|
61
|
$dist += $co_size - $guessradius; |
336
|
34
|
100
|
|
|
|
118
|
$dist > $ebox[2] and $ebox[2] = $dist; |
337
|
|
|
|
|
|
|
} |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
} |
340
|
84
|
|
|
|
|
105
|
push(@info, $item); |
341
|
84
|
|
|
|
|
158
|
++$index; |
342
|
|
|
|
|
|
|
} |
343
|
|
|
|
|
|
|
|
344
|
15
|
|
|
|
|
417
|
my $radius = |
345
|
|
|
|
|
|
|
int($self->_small_extent(\@chart_box) * $style->{pie}{size} * 0.5); |
346
|
15
|
|
|
|
|
45
|
my $max_width = $chart_box[2] - $chart_box[0] + $ebox[0] - $ebox[2]; |
347
|
15
|
100
|
|
|
|
57
|
if ($radius > $max_width / 2) { |
348
|
5
|
|
|
|
|
10
|
$radius = int($max_width / 2); |
349
|
|
|
|
|
|
|
} |
350
|
15
|
|
|
|
|
34
|
$chart_box[0] -= $ebox[0]; |
351
|
15
|
|
|
|
|
26
|
$chart_box[2] -= $ebox[2]; |
352
|
15
|
|
|
|
|
36
|
my $cx = int(($chart_box[0] + $chart_box[2]) / 2); |
353
|
15
|
|
|
|
|
36
|
my $cy = int(($chart_box[1] + $chart_box[3]) / 2); |
354
|
15
|
100
|
|
|
|
70
|
if ($style->{features}{dropshadow}) { |
355
|
10
|
50
|
|
|
|
47
|
my @shadow_fill = $self->_get_fill('dropshadow.fill') |
356
|
|
|
|
|
|
|
or return; |
357
|
10
|
50
|
|
|
|
73
|
my $offx = $self->_get_number('dropshadow.offx') |
358
|
|
|
|
|
|
|
or return; |
359
|
10
|
|
|
|
|
35
|
my $offy = $self->_get_number('dropshadow.offy'); |
360
|
10
|
|
|
|
|
27
|
for my $item (@info) { |
361
|
59
|
|
|
|
|
109883
|
$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
|
|
|
|
|
|
|
@shadow_fill); |
364
|
|
|
|
|
|
|
} |
365
|
10
|
50
|
|
|
|
26100
|
$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
|
|
|
|
|
|
|
if $style->{dropshadow}{filter}; |
370
|
|
|
|
|
|
|
} |
371
|
|
|
|
|
|
|
|
372
|
15
|
|
|
|
|
270687
|
my @fill_box = ( $cx-$radius, $cy-$radius, $cx+$radius, $cy+$radius ); |
373
|
15
|
|
|
|
|
105
|
my $fill_aa = $self->_get_number('fill.aa'); |
374
|
15
|
|
|
|
|
45
|
for my $item (@info) { |
375
|
84
|
100
|
|
|
|
140280
|
$item->{begin} < $item->{end} |
376
|
|
|
|
|
|
|
or next; |
377
|
81
|
50
|
|
|
|
534
|
my @fill = $self->_data_fill($item->{index}, \@fill_box) |
378
|
|
|
|
|
|
|
or return; |
379
|
81
|
|
|
|
|
1249
|
$img->arc(x=>$cx, 'y'=>$cy, r=>$radius, aa => $fill_aa, |
380
|
|
|
|
|
|
|
d1=>180/PI * $item->{begin}, d2=>180/PI * $item->{end}, |
381
|
|
|
|
|
|
|
@fill); |
382
|
|
|
|
|
|
|
} |
383
|
15
|
100
|
|
|
|
3988
|
if ($style->{features}{outline}) { |
384
|
6
|
|
|
|
|
54
|
my %outstyle = $self->_line_style('outline'); |
385
|
6
|
|
|
|
|
15
|
my $out_radius = 0.5 + $radius; |
386
|
6
|
|
|
|
|
17
|
for my $item (@info) { |
387
|
41
|
|
|
|
|
148
|
my $px = int($cx + $out_radius * cos($item->{begin})); |
388
|
41
|
|
|
|
|
100
|
my $py = int($cy + $out_radius * sin($item->{begin})); |
389
|
41
|
100
|
|
|
|
131
|
$item->{begin} < $item->{end} |
390
|
|
|
|
|
|
|
or next; |
391
|
38
|
|
|
|
|
156
|
$img->line(x1=>$cx, y1=>$cy, x2=>$px, y2=>$py, %outstyle); |
392
|
38
|
|
|
|
|
2513
|
for (my $i = $item->{begin}; $i < $item->{end}; $i += PI/180) { |
393
|
2178
|
|
|
|
|
2040
|
my $stroke_end = $i + PI/180; |
394
|
2178
|
100
|
|
|
|
3425
|
$stroke_end = $item->{end} if $stroke_end > $item->{end}; |
395
|
2178
|
|
|
|
|
3736
|
my $nx = int($cx + $out_radius * cos($stroke_end)); |
396
|
2178
|
|
|
|
|
2805
|
my $ny = int($cy + $out_radius * sin($stroke_end)); |
397
|
2178
|
|
|
|
|
6138
|
$img->line(x1=>$px, y1=>$py, x2=>$nx, y2=>$ny, %outstyle); |
398
|
2178
|
|
|
|
|
98105
|
($px, $py) = ($nx, $ny); |
399
|
|
|
|
|
|
|
} |
400
|
|
|
|
|
|
|
} |
401
|
|
|
|
|
|
|
} |
402
|
|
|
|
|
|
|
|
403
|
15
|
|
|
|
|
116
|
my $callout_inside = $radius - $self->_get_number('callout.inside'); |
404
|
15
|
|
|
|
|
36
|
$callout_outside += $radius; |
405
|
15
|
|
|
|
|
32
|
my %callout_text; |
406
|
|
|
|
|
|
|
my %label_text; |
407
|
0
|
|
|
|
|
0
|
my %callout_line; |
408
|
15
|
|
|
|
|
48
|
my $leader_aa = $self->_get_number('callout.leadaa'); |
409
|
15
|
|
|
|
|
43
|
for my $label (@info) { |
410
|
84
|
100
|
66
|
|
|
4564
|
if ($label->{label} && !$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
|
100
|
|
|
|
142
|
unless (%label_text) { |
414
|
8
|
50
|
|
|
|
40
|
%label_text = $self->_text_style('label') |
415
|
|
|
|
|
|
|
or return; |
416
|
|
|
|
|
|
|
} |
417
|
33
|
|
|
|
|
147
|
my @loc = $self->_fit_text($cx, $cy, 'label', $label->{text}, $radius, |
418
|
|
|
|
|
|
|
$label->{begin}, $label->{end}); |
419
|
33
|
50
|
|
|
|
68
|
if (@loc) { |
420
|
33
|
|
|
|
|
66
|
my $tcx = ($loc[0]+$loc[2])/2; |
421
|
33
|
|
|
|
|
51
|
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
|
33
|
|
|
|
|
294
|
$img->string(%label_text, x=>$tcx-$label->{lbox}[2]/2, |
425
|
|
|
|
|
|
|
'y'=>$tcy+$label->{lbox}[3]/2+$label->{lbox}[1], |
426
|
|
|
|
|
|
|
text=>$label->{text}); |
427
|
|
|
|
|
|
|
} |
428
|
|
|
|
|
|
|
else { |
429
|
0
|
|
|
|
|
0
|
$label->{callout} = 1; |
430
|
0
|
0
|
|
|
|
0
|
my @cbox = $self->_text_bbox($label->{text}, 'callout') |
431
|
|
|
|
|
|
|
or return; |
432
|
0
|
|
|
|
|
0
|
$label->{cbox} = \@cbox; |
433
|
0
|
|
|
|
|
0
|
$label->{cangle} = ($label->{begin} + $label->{end}) / 2; |
434
|
|
|
|
|
|
|
} |
435
|
|
|
|
|
|
|
} |
436
|
84
|
100
|
|
|
|
4757
|
if ($label->{callout}) { |
437
|
38
|
100
|
|
|
|
215
|
unless (%callout_text) { |
438
|
12
|
50
|
|
|
|
58
|
%callout_text = $self->_text_style('callout') |
439
|
|
|
|
|
|
|
or return; |
440
|
12
|
|
|
|
|
74
|
%callout_line = $self->_line_style('callout'); |
441
|
|
|
|
|
|
|
} |
442
|
38
|
|
|
|
|
222
|
my $ix = floor(0.5 + $cx + $callout_inside * cos($label->{cangle})); |
443
|
38
|
|
|
|
|
186
|
my $iy = floor(0.5 + $cy + $callout_inside * sin($label->{cangle})); |
444
|
38
|
|
|
|
|
107
|
my $ox = floor(0.5 + $cx + $callout_outside * cos($label->{cangle})); |
445
|
38
|
|
|
|
|
102
|
my $oy = floor(0.5 + $cy + $callout_outside * sin($label->{cangle})); |
446
|
38
|
100
|
|
|
|
104
|
my $lx = ($ox < $cx) ? $ox - $callout_leadlen : $ox + $callout_leadlen; |
447
|
38
|
|
|
|
|
259
|
$img->polyline(points => [ [ $ix, $iy ], |
448
|
|
|
|
|
|
|
[ $ox, $oy ], |
449
|
|
|
|
|
|
|
[ $lx, $oy ] ], |
450
|
|
|
|
|
|
|
%callout_line); |
451
|
|
|
|
|
|
|
#my $tx = $lx + $callout_gap; |
452
|
38
|
|
|
|
|
3039
|
my $ty = $oy + $label->{cbox}[3]/2+$label->{cbox}[1]; |
453
|
38
|
100
|
|
|
|
93
|
if ($lx < $cx) { |
454
|
4
|
|
|
|
|
32
|
$img->string(%callout_text, x=>$lx-$callout_gap-$label->{cbox}[2], |
455
|
|
|
|
|
|
|
'y'=>$ty, text=>$label->{text}); |
456
|
|
|
|
|
|
|
} |
457
|
|
|
|
|
|
|
else { |
458
|
34
|
|
|
|
|
210
|
$img->string(%callout_text, x=>$lx+$callout_gap, 'y'=>$ty, |
459
|
|
|
|
|
|
|
text=>$label->{text}); |
460
|
|
|
|
|
|
|
} |
461
|
|
|
|
|
|
|
} |
462
|
|
|
|
|
|
|
} |
463
|
|
|
|
|
|
|
|
464
|
15
|
|
|
|
|
2529
|
$img; |
465
|
|
|
|
|
|
|
} |
466
|
|
|
|
|
|
|
|
467
|
|
|
|
|
|
|
sub _valid_input { |
468
|
24
|
|
|
24
|
|
49
|
my ($self, $data_series) = @_; |
469
|
|
|
|
|
|
|
|
470
|
24
|
50
|
33
|
|
|
144
|
if (!defined $data_series || !scalar @$data_series) { |
471
|
0
|
|
|
|
|
0
|
return $self->_error("No data supplied"); |
472
|
|
|
|
|
|
|
} |
473
|
|
|
|
|
|
|
|
474
|
24
|
50
|
|
|
|
84
|
@$data_series == 1 |
475
|
|
|
|
|
|
|
or return $self->_error("Pie charts only allow one data series"); |
476
|
|
|
|
|
|
|
|
477
|
24
|
|
|
|
|
64
|
my $data = $data_series->[0]{data}; |
478
|
|
|
|
|
|
|
|
479
|
24
|
100
|
|
|
|
75
|
if (!scalar @$data) { |
480
|
1
|
|
|
|
|
5
|
return $self->_error("No values in data series"); |
481
|
|
|
|
|
|
|
} |
482
|
|
|
|
|
|
|
|
483
|
23
|
|
|
|
|
39
|
my $total = 0; |
484
|
|
|
|
|
|
|
{ |
485
|
23
|
|
|
|
|
33
|
my $index = 0; |
|
23
|
|
|
|
|
37
|
|
486
|
23
|
|
|
|
|
62
|
for my $item (@$data) { |
487
|
153
|
100
|
|
|
|
250
|
$item < 0 |
488
|
|
|
|
|
|
|
and return $self->_error("Data index $index is less than zero"); |
489
|
|
|
|
|
|
|
|
490
|
152
|
|
|
|
|
123
|
$total += $item; |
491
|
|
|
|
|
|
|
|
492
|
152
|
|
|
|
|
188
|
++$index; |
493
|
|
|
|
|
|
|
} |
494
|
|
|
|
|
|
|
} |
495
|
22
|
100
|
|
|
|
92
|
$total == 0 |
496
|
|
|
|
|
|
|
and return $self->_error("Sum of all data values is zero"); |
497
|
|
|
|
|
|
|
|
498
|
21
|
|
|
|
|
75
|
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
|
|
|
19
|
|
57
|
my ($self, $data, $labels, $total) = @_; |
516
|
|
|
|
|
|
|
|
517
|
19
|
|
|
|
|
33
|
my @others; |
518
|
19
|
|
|
|
|
28
|
my $index = 0; |
519
|
19
|
|
|
|
|
56
|
for my $item (@$data) { |
520
|
134
|
100
|
|
|
|
336
|
if ($item / $total < $self->{_style}{pie}{maxsegment}) { |
521
|
48
|
|
|
|
|
57
|
push(@others, $index); |
522
|
|
|
|
|
|
|
} |
523
|
134
|
|
|
|
|
146
|
++$index; |
524
|
|
|
|
|
|
|
} |
525
|
19
|
100
|
|
|
|
64
|
if (@others) { |
526
|
18
|
|
|
|
|
29
|
my $others = 0; |
527
|
18
|
|
|
|
|
45
|
for my $index (reverse @others) { |
528
|
48
|
|
|
|
|
60
|
$others += $data->[$index]; |
529
|
48
|
|
|
|
|
81
|
splice(@$labels, $index, 1); |
530
|
48
|
|
|
|
|
110
|
splice(@$data, $index, 1); |
531
|
|
|
|
|
|
|
} |
532
|
18
|
50
|
|
|
|
88
|
push(@$labels, $self->{_style}{otherlabel}) if @$labels; |
533
|
18
|
|
|
|
|
55
|
push(@$data, $others); |
534
|
|
|
|
|
|
|
} |
535
|
|
|
|
|
|
|
} |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
# used for debugging |
538
|
|
|
|
|
|
|
sub _test_line { |
539
|
0
|
|
|
0
|
|
0
|
my ($x, $y, @l) = @_; |
540
|
|
|
|
|
|
|
|
541
|
0
|
|
|
|
|
0
|
my $res = $l[0]*$x + $l[1] * $y + $l[2]; |
542
|
0
|
0
|
|
|
|
0
|
print "test ", (abs($res) < 0.000001) ? "success\n" : "failure $res\n"; |
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
|
|
|
84
|
|
166
|
my ($self, $cx, $cy, $name, $text, $radius, $begin, $end) = @_; |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
#print "fit: $cx, $cy '$text' $radius $begin $end\n"; |
558
|
84
|
50
|
|
|
|
228
|
my @tbox = $self->_text_bbox($text, $name) |
559
|
|
|
|
|
|
|
or return; |
560
|
84
|
|
|
|
|
538
|
my $tcx = floor(0.5+$cx + cos(($begin+$end)/2) * $radius *3/5); |
561
|
84
|
|
|
|
|
386
|
my $tcy = floor(0.5+$cy + sin(($begin+$end)/2) * $radius *3/5); |
562
|
84
|
|
|
|
|
160
|
my $topy = $tcy - $tbox[3]/2; |
563
|
84
|
|
|
|
|
98
|
my $boty = $topy + $tbox[3]; |
564
|
84
|
|
|
|
|
75
|
my @lines; |
565
|
84
|
|
|
|
|
127
|
for my $y ($topy, $boty) { |
566
|
166
|
|
|
|
|
425
|
my %entry = ( 'y'=>$y ); |
567
|
166
|
|
|
|
|
492
|
$entry{line} = [ line_from_points($tcx, $y, $tcx+1, $y) ]; |
568
|
166
|
|
|
|
|
267
|
$entry{left} = -$radius; |
569
|
166
|
|
|
|
|
187
|
$entry{right} = $radius; |
570
|
166
|
|
|
|
|
195
|
for my $angle ($begin, $end) { |
571
|
330
|
|
|
|
|
530
|
my $ex = $cx + cos($angle)*$radius; |
572
|
330
|
|
|
|
|
439
|
my $ey = $cy + sin($angle)*$radius; |
573
|
330
|
|
|
|
|
667
|
my @line = line_from_points($cx, $cy, $ex, $ey); |
574
|
|
|
|
|
|
|
#_test_line($cx, $cy, @line); |
575
|
|
|
|
|
|
|
#_test_line($ex, $ey, @line); |
576
|
330
|
|
|
|
|
513
|
my $goodsign = $line[0] * $tcx + $line[1] * $tcy + $line[2]; |
577
|
330
|
|
|
|
|
518
|
for my $pos (@entry{qw/left right/}) { |
578
|
658
|
|
|
|
|
828
|
my $sign = $line[0] * ($pos+$tcx) + $line[1] * $y + $line[2]; |
579
|
658
|
100
|
|
|
|
986
|
if ($goodsign * $sign < 0) { |
580
|
239
|
100
|
|
|
|
225
|
if (my @p = intersect_lines(@line, @{$entry{line}})) { |
|
239
|
|
|
|
|
580
|
|
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
|
|
|
|
|
363
|
$pos = $p[0]-$tcx; |
585
|
|
|
|
|
|
|
} |
586
|
|
|
|
|
|
|
else { |
587
|
2
|
|
|
|
|
13
|
return; |
588
|
|
|
|
|
|
|
} |
589
|
|
|
|
|
|
|
|
590
|
|
|
|
|
|
|
} |
591
|
|
|
|
|
|
|
|
592
|
|
|
|
|
|
|
# circle |
593
|
656
|
|
|
|
|
1046
|
my $dist2 = ($pos+$tcx-$cx) * ($pos+$tcx-$cx) |
594
|
|
|
|
|
|
|
+ ($y - $cy) * ($y - $cy); |
595
|
656
|
100
|
|
|
|
1471
|
if ($dist2 > $radius * $radius) { |
596
|
204
|
|
|
|
|
575
|
my @points = |
597
|
204
|
|
|
|
|
190
|
intersect_line_and_circle(@{$entry{line}}, $cx, $cy, $radius); |
598
|
204
|
|
|
|
|
433
|
while (@points) { |
599
|
408
|
|
|
|
|
621
|
my @p = splice(@points, 0, 2); |
600
|
408
|
100
|
100
|
|
|
2432
|
if ($p[0] < $cx && $tcx+$pos < $p[0]) { |
|
|
100
|
100
|
|
|
|
|
601
|
70
|
|
|
|
|
157
|
$pos = $p[0]-$tcx; |
602
|
|
|
|
|
|
|
} |
603
|
|
|
|
|
|
|
elsif ($p[0] > $cx && $tcx+$pos > $p[0]) { |
604
|
106
|
|
|
|
|
437
|
$pos = $p[0]-$tcx; |
605
|
|
|
|
|
|
|
} |
606
|
|
|
|
|
|
|
} |
607
|
|
|
|
|
|
|
} |
608
|
|
|
|
|
|
|
} |
609
|
|
|
|
|
|
|
} |
610
|
164
|
|
|
|
|
355
|
push(@lines, \%entry); |
611
|
|
|
|
|
|
|
} |
612
|
82
|
100
|
|
|
|
270
|
my $left = $lines[0]{left} > $lines[1]{left} ? $lines[0]{left} : $lines[1]{left}; |
613
|
82
|
100
|
|
|
|
213
|
my $right = $lines[0]{right} < $lines[1]{right} ? $lines[0]{right} : $lines[1]{right}; |
614
|
82
|
100
|
|
|
|
248
|
return if $right - $left < $tbox[2]; |
615
|
|
|
|
|
|
|
|
616
|
66
|
|
|
|
|
365
|
return ($tcx+$left, $topy, $tcx+$right, $boty); |
617
|
|
|
|
|
|
|
} |
618
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
sub _composite { |
620
|
21
|
|
|
21
|
|
136
|
( 'pie', $_[0]->SUPER::_composite() ); |
621
|
|
|
|
|
|
|
} |
622
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
sub _style_defs { |
624
|
21
|
|
|
21
|
|
37
|
my ($self) = @_; |
625
|
|
|
|
|
|
|
|
626
|
21
|
|
|
|
|
36
|
my %work = %{$self->SUPER::_style_defs()}; |
|
21
|
|
|
|
|
109
|
|
627
|
21
|
|
|
|
|
79
|
$work{otherlabel} = "(others)"; |
628
|
21
|
|
|
|
|
128
|
$work{pie} = |
629
|
|
|
|
|
|
|
{ |
630
|
|
|
|
|
|
|
guessfactor=>0.6, |
631
|
|
|
|
|
|
|
size=>0.8, |
632
|
|
|
|
|
|
|
maxsegment=> 0.01, |
633
|
|
|
|
|
|
|
}; |
634
|
|
|
|
|
|
|
|
635
|
21
|
|
|
|
|
236
|
\%work; |
636
|
|
|
|
|
|
|
} |
637
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
1; |
639
|
|
|
|
|
|
|
__END__ |