File Coverage

blib/lib/Chart/Pie.pm
Criterion Covered Total %
statement 285 394 72.3
branch 84 168 50.0
condition 9 15 60.0
subroutine 11 12 91.7
pod n/a
total 389 589 66.0


line stmt bran cond sub pod time code
1             #>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>#
2             # Chart::Pie #
3             # #
4             # written by Chart Group #
5             # #
6             # maintained by the Chart Group #
7             # Chart@wettzell.ifag.de #
8             # #
9             # #
10             #<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<#
11              
12             package Chart::Pie;
13              
14 5     5   189 use Chart::Base 2.3;
  5         276  
  5         99  
15 5     5   128 use GD;
  5         48  
  5         96  
16 5     5   235 use Carp;
  5         75  
  5         104  
17 5     5   85 use strict;
  5         59  
  5         4428  
18              
19             @Chart::Pie::ISA = qw(Chart::Base);
20             $Chart::Pie::VERSION = '2.3';
21              
22             #>>>>>>>>>>>>>>>>>>>>>>>>>>#
23             # public methods go here #
24             #<<<<<<<<<<<<<<<<<<<<<<<<<<#
25              
26              
27              
28             #>>>>>>>>>>>>>>>>>>>>>>>>>>>#
29             # private methods go here #
30             #<<<<<<<<<<<<<<<<<<<<<<<<<<<#
31              
32             #Overwrite the legend methods to get the right legend
33             sub _draw_right_legend {
34 1     1   10   my $self = shift;
35 1         11   my $data = $self->{'dataref'};
36 1         10   my @labels = @{$data->[0]};
  1         15  
37 1         10   my ($x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h, $brush);
38 1         9   my $font = $self->{'legend_font'};
39 1         10   my $l1 = 0;
40 1         9   my $l2 =0;
41 1         8   my ($i, $j, $label, $dataset_sum);
42 1         10   my $max_label_len = 1;
43               
44             # make sure we're using a real font
45 1 50       12   unless ((ref ($font)) eq 'GD::Font') {
46 0         0     croak "The subtitle font you specified isn\'t a GD Font object";
47               }
48              
49             # get the size of the font
50 1         21   ($h, $w) = ($font->height, $font->width);
51              
52             # get the miscellaneous color
53 1         13   $misccolor = $self->_color_role_to_index('misc');
54              
55             #find out what the sum of all datapoits is, needed for the Labels with percent
56 1         14   for my $j (0..$self->{'num_datapoints'}) {
57 11 100       108      if(defined $data->[1][$j])
58                     {
59 10         129           $dataset_sum += $data->[1][$j];
60                     }
61               }
62               
63             # find out how who wide the largest label text is
64 1         11   foreach (@labels) {
65 10 100       101    if ( length($_) > $l1) {
66 2         77      $l1 = length($_);
67                }
68               }
69               for (my $i =0 ; $i < ($self->{'num_datapoints'}) ; $i++) {
70 10 100       119    if ( length($data->[1][$i]) > $l2 ) {
71 1         12       $l2 = length($data->[1][$i]);
72              
73                }
74 1         10   }
75               
76 1 50       20   if ($self->{'legend_label_values'} =~ /^value$/i ) {
    50          
    0          
77 0         0     $max_label_len = $l1 + $l2 +1;
78               }
79               elsif ($self->{'legend_label_values'} =~ /^percent$/i ) {
80 1         9     $max_label_len = $l1 +7;
81               }
82               elsif ($self->{'legend_label_values'} =~ /^both$/i ) {
83 0         0     $max_label_len = $l1 + $l2 +9;
84               }
85               else {
86 0         0     $max_label_len = $l1;
87               }
88              
89             # find out how wide the largest label is
90 1         13   $width = (2 * $self->{'text_space'})
91             #+ ($self->{'max_legend_label'} * $w)
92                 + $max_label_len *$w
93                 + $self->{'legend_example_size'}
94                 + (2 * $self->{'legend_space'});
95              
96             # get some starting x-y values
97 1         11   $x1 = $self->{'curr_x_max'} - $width;
98 1         9   $x2 = $self->{'curr_x_max'};
99 1         11   $y1 = $self->{'curr_y_min'} + $self->{'graph_border'} ;
100 1         12   $y2 = $self->{'curr_y_min'} + $self->{'graph_border'} + $self->{'text_space'}
101                       + ($self->{'num_datapoints'} * ($h + $self->{'text_space'}))
102             + (2 * $self->{'legend_space'});
103              
104             # box the legend off
105 1         89   $self->{'gd_obj'}->rectangle ($x1, $y1, $x2, $y2, $misccolor);
106              
107             # leave that nice space inside the legend box
108 1         9   $x1 += $self->{'legend_space'};
109 1         10   $y1 += $self->{'legend_space'} + $self->{'text_space'};
110              
111             # now draw the actual legend
112 1         14   for (0..$#labels) {
113             # get the color
114 10         167     $color = $self->_color_role_to_index('dataset'.$_);
115              
116             # find the x-y coords
117 10         86     $x2 = $x1;
118 10         119     $x3 = $x2 + $self->{'legend_example_size'};
119 10         99     $y2 = $y1 + ($_ * ($self->{'text_space'} + $h)) + $h/2;
120              
121             # do the line first
122 10         152     $self->{'gd_obj'}->line ($x2, $y2, $x3, $y2, $color);
123              
124             # reset the brush for points
125 10         151     $brush = $self->_prepare_brush($color, 'point',
126             $self->{'pointStyle' . $_});
127 10         249     $self->{'gd_obj'}->setBrush($brush);
128             # draw the point
129 10         135     $self->{'gd_obj'}->line(int(($x3+$x2)/2), $y2,
130             int(($x3+$x2)/2), $y2, gdBrushed);
131              
132             # now the label
133 10         99     $x2 = $x3 + (2 * $self->{'text_space'});
134 10         88     $y2 -= $h/2;
135 10 50       108     if (defined $data->[1][$_]) {
136 10 50       171        if ( $self->{'legend_label_values'} =~ /^value$/i ) {
    50          
    0          
137 0         0         $self->{'gd_obj'}->string($font, $x2, $y2, $labels[$_].' '.$data->[1][$_], $color);
138                    }
139                    elsif ( $self->{'legend_label_values'} =~ /^percent$/i ) {
140 10         223         $label = sprintf("%s %4.2f%%",$labels[$_], $data->[1][$_] / $dataset_sum * 100);
141 10         363         $self->{'gd_obj'}->string($font, $x2, $y2, $label, $color);
142                    }
143                    elsif ( $self->{'legend_label_values'} =~ /^both$/i ) {
144 0 0       0           if ( $data->[1][$_] =~ /\./ ) {
145 0         0             $label = sprintf("%s %4.2f%% %.2f",$labels[$_], $data->[1][$_] / $dataset_sum * 100, $data->[1][$_]);
146                       }
147                       else {
148 0         0             $label = sprintf("%s %4.2f%% %d",$labels[$_], $data->[1][$_] / $dataset_sum * 100, $data->[1][$_]);
149                       }
150 0         0         $self->{'gd_obj'}->string($font, $x2, $y2, $label, $color);
151                    }
152                    else {
153 0         0         $self->{'gd_obj'}->string($font, $x2, $y2, $labels[$_], $color);
154                   }
155              
156                 }
157               }
158              
159             # mark off the used space
160 1         13   $self->{'curr_x_max'} -= $width;
161              
162             # and return
163 1         19   return 1;
164             }
165              
166              
167             ## put the legend on the left of the chart
168             sub _draw_left_legend {
169 0     0   0   my $self = shift;
170 0         0   my $data = $self->{'dataref'};
171 0         0   my @labels = @{$data->[0]};
  0         0  
172 0         0   my ($x1, $x2, $x3, $y1, $y2, $width, $color, $misccolor, $w, $h, $brush);
173 0         0   my $font = $self->{'legend_font'};
174 0         0   my $max_label_len= 1;;
175 0         0   my $l1 = 0;
176 0         0   my $l2 = 0;
177 0         0   my ($dataset_sum, $label);
178             # make sure we're using a real font
179 0 0       0   unless ((ref ($font)) eq 'GD::Font') {
180 0         0     croak "The subtitle font you specified isn\'t a GD Font object";
181               }
182              
183             # get the size of the font
184 0         0   ($h, $w) = ($font->height, $font->width);
185              
186             # get the miscellaneous color
187 0         0   $misccolor = $self->_color_role_to_index('misc');
188              
189             #find out what the sum of all datapoits is, needed for the Labels with percent
190 0         0   for my $j (0..$self->{'num_datapoints'}) {
191 0 0       0      if(defined $data->[1][$j]) {
192 0         0        $dataset_sum += $data->[1][$j];
193                  }
194               }
195              
196             # find out how who wide the largest label text is
197 0         0   foreach (@labels) {
198 0 0       0    if ( length($_) > $l1) {
199 0         0      $l1 = length($_);
200                }
201               }
202               for (my $i =0 ; $i < ($self->{'num_datapoints'}) ; $i++) {
203 0 0       0    if ( length($data->[1][$i]) > $l2 ) {
204 0         0       $l2 = length($data->[1][$i]);
205                }
206 0         0   }
207              
208 0 0       0   if ($self->{'legend_label_values'} =~ /^value$/i ) {
    0          
    0          
209 0         0     $max_label_len = $l1 + $l2 +1;
210               }
211               elsif ($self->{'legend_label_values'} =~ /^percent$/i ) {
212 0         0     $max_label_len = $l1 +7;
213               }
214               elsif ($self->{'legend_label_values'} =~ /^both$/i ) {
215 0         0     $max_label_len = $l1 + $l2 +9;
216               }
217               else {
218 0         0     $max_label_len = $l1;
219               }
220              
221             # find out how wide the largest label is
222 0         0   $width = (2 * $self->{'text_space'})
223                 + ($max_label_len * $w)
224                 + $self->{'legend_example_size'}
225                 + (2 * $self->{'legend_space'});
226              
227             # get some base x-y coordinates
228 0         0   $x1 = $self->{'curr_x_min'};
229 0         0   $x2 = $self->{'curr_x_min'} + $width;
230 0         0   $y1 = $self->{'curr_y_min'} + $self->{'graph_border'} ;
231 0         0   $y2 = $self->{'curr_y_min'} + $self->{'graph_border'} + $self->{'text_space'}
232                       + ($self->{'num_datapoints'} * ($h + $self->{'text_space'}))
233             + (2 * $self->{'legend_space'});
234              
235             # box the legend off
236 0         0   $self->{'gd_obj'}->rectangle ($x1, $y1, $x2, $y2, $misccolor);
237              
238             # leave that nice space inside the legend box
239 0         0   $x1 += $self->{'legend_space'};
240 0         0   $y1 += $self->{'legend_space'} + $self->{'text_space'};
241              
242             # now draw the actual legend
243 0         0   for (0..$#labels) {
244             # get the color
245 0         0     $color = $self->_color_role_to_index('dataset'.$_);
246              
247             # find the x-y coords
248 0         0     $x2 = $x1;
249 0         0     $x3 = $x2 + $self->{'legend_example_size'};
250 0         0     $y2 = $y1 + ($_ * ($self->{'text_space'} + $h)) + $h/2;
251              
252             # do the line first
253 0         0     $self->{'gd_obj'}->line ($x2, $y2, $x3, $y2, $color);
254              
255             # reset the brush for points
256 0         0     $brush = $self->_prepare_brush($color, 'point',
257             $self->{'pointStyle' . $_});
258 0         0     $self->{'gd_obj'}->setBrush($brush);
259             # draw the point
260 0         0     $self->{'gd_obj'}->line(int(($x3+$x2)/2), $y2,
261             int(($x3+$x2)/2), $y2, gdBrushed);
262              
263             # now the label
264 0         0     $x2 = $x3 + (2 * $self->{'text_space'});
265 0         0     $y2 -= $h/2;
266 0 0       0     if ( $self->{'legend_label_values'} =~ /^value$/i ) {
    0          
    0          
267 0         0         $self->{'gd_obj'}->string($font, $x2, $y2, $labels[$_].' '.$data->[1][$_], $color);
268                 }
269                 elsif ( $self->{'legend_label_values'} =~ /^percent$/i ) {
270 0         0         $label = sprintf("%s %4.2f%%",$labels[$_], $data->[1][$_] / $dataset_sum * 100);
271 0         0         $self->{'gd_obj'}->string($font, $x2, $y2, $label, $color);
272                 }
273                 elsif ( $self->{'legend_label_values'} =~ /^both$/i ) {
274 0 0       0         if ($data->[1][$_] =~ /\./) {
275 0         0            $label = sprintf("%s %4.2f%% %.2f",$labels[$_], $data->[1][$_] / $dataset_sum * 100, $data->[1][$_]);
276                     }
277                     else {
278 0         0            $label = sprintf("%s %4.2f%% %d",$labels[$_], $data->[1][$_] / $dataset_sum * 100, $data->[1][$_]);
279                     }
280 0         0         $self->{'gd_obj'}->string($font, $x2, $y2, $label, $color);
281                 }
282                 else {
283 0         0         $self