lib/Chart/Pareto.pm

filter: Statementstmt 60.0% 3 / 5 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 2 / 2 Podpod n/a totaltotal 71.4% 5 / 7 CC 1 SCAR 1.4
CC
1
Cov
60%
BEGIN
1.3
BEGIN
1.0
CRAP 1.1
« lib/Chart/Mountain.pm ↑ Summary lib/Chart/Split.pm »
1 3 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Chart::Pareto;
4 our @ISA = qw(Chart::Base);
5 our $VERSION = 'v2.403.9';
6  
7 0 ▶use Chart::Base;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use GD;
9 use Carp;
10  
11 #>>>>>>>>>>>>>>>>>>>>>>>>>>#
12 # public methods go here #
13 #<<<<<<<<<<<<<<<<<<<<<<<<<<#
14  
15 #>>>>>>>>>>>>>>>>>>>>>>>>>>>#
16 # private methods go here #
17 #<<<<<<<<<<<<<<<<<<<<<<<<<<<#
18  
19 ## @fn private _find_y_scale
20 #calculate the range with the sum dataset1. all datas has to be positiv
21 sub _find_y_range
22 {
23 my $self = shift;
24 my $data = $self->{'dataref'};
25 my $sum = 0;
26  
27 for ( my $i = 0 ; $i < $self->{'num_datapoints'} ; $i++ )
28 {
29 if ( $data->[1][$i] >= 0 )
30 {
31 $sum += $data->[1][$i];
32 }
33 else
34 {
35 carp "We need positiv data, if we want to draw a pareto graph!!";
36 return 0;
37 }
38 }
39  
40 #store the sum
41 $self->{'sum'} = $sum;
42  
43 #return the range
44 ( 0, $sum );
45 }
46  
47 ## @fn private _sort_data
48 # sort the data
49 sub _sort_data
50 {
51 my $self = shift;
52 my $data = $self->{'dataref'};
53 my @labels = @{ $data->[0] };
54 my @values = @{ $data->[1] };
55  
56 # sort the values and their labels
57 @labels = @labels[ sort { $values[$b] <=> $values[$a] } 0 .. $#labels ];
58 @values = sort { $b <=> $a } @values;
59  
60 #save the sorted values and their labels
61 @{ $data->[0] } = @labels;
62 @{ $data->[1] } = @values;
63  
64 #finally return
65 return 1;
66 }
67  
68 ## @fn private _draw_legend
69 # let them know what all the pretty colors mean
70 sub _draw_legend
71 {
72 my $self = shift;
73 my ($length);
74 my $num_dataset;
75  
76 # check to see if legend type is none..
77 if ( $self->{'legend'} =~ /^none$/ )
78 {
79 return 1;
80 }
81  
82 # check to see if they have as many labels as datasets,
83 # warn them if not
84 if ( ( $#{ $self->{'legend_labels'} } >= 0 )
85 && ( ( scalar( @{ $self->{'legend_labels'} } ) ) != 2 ) )
86 {
87 carp "I need two legend labels. One for the data and one for the sum.";
88 }
89  
90 # init a field to store the length of the longest legend label
91 unless ( $self->{'max_legend_label'} )
92 {
93 $self->{'max_legend_label'} = 0;
94 }
95  
96 # fill in the legend labels, find the longest one
97 unless ( $self->{'legend_labels'}[0] )
98 {
99 $self->{'legend_labels'}[0] = "Dataset";
100 }
101 unless ( $self->{'legend_labels'}[1] )
102 {
103 $self->{'legend_labels'}[1] = "Running sum";
104 }
105  
106 if ( length( $self->{'legend_labels'}[0] ) > length( $self->{'legend_labels'}[1] ) )
107 {
108 $self->{'max_legend_label'} = length( $self->{'legend_labels'}[0] );
109 }
110 else
111 {
112 $self->{'max_legend_label'} = length( $self->{'legend_labels'}[1] );
113 }
114  
115 #set the number of datasets to 2, and store it
116 $num_dataset = $self->{'num_datasets'};
117 $self->{'num_datasets'} = 2;
118  
119 # different legend types
120 if ( $self->{'legend'} eq 'bottom' )
121 {
122 $self->_draw_bottom_legend;
123 }
124 elsif ( $self->{'legend'} eq 'right' )
125 {
126 $self->_draw_right_legend;
127 }
128 elsif ( $self->{'legend'} eq 'left' )
129 {
130 $self->_draw_left_legend;
131 }
132 elsif ( $self->{'legend'} eq 'top' )
133 {
134 $self->_draw_top_legend;
135 }
136 else
137 {
138 carp "I can't put a legend there (at " . $self->{'legend'} . ")\n";
139 }
140  
141 #reload the number of datasets
142 $self->{'num_datasets'} = $num_dataset;
143  
144 # and return
145 return 1;
146 }
147  
148 ## @fn private _draw_data
149 # finally get around to plotting the data
150 sub _draw_data
151 {
152 my $self = shift;
153 my $data = $self->{'dataref'};
154 my $misccolor = $self->_color_role_to_index('misc');
155 my ( $x1, $x2, $x3, $y1, $y2, $y3, $y1_line, $y2_line, $x1_line, $x2_line, $h, $w );
156 my ( $width, $height, $delta1, $delta2, $map, $mod, $cut );
157 my ( $i, $j, $color, $line_color, $percent, $per_label, $per_label_len );
158 my $sum = $self->{'sum'};
159 my $curr_sum = 0;
160 my $font = $self->{'legend_font'};
161 my $pink = $self->{'gd_obj'}->colorAllocate( 255, 0, 255 );
162 my $diff;
163  
164 # make sure we're using a real font
165 unless ( ( ref($font) ) eq 'GD::Font' )
166 {
167 croak "The subtitle font you specified isn\'t a GD Font object";
168 }
169  
170 # get the size of the font
171 ( $h, $w ) = ( $font->height, $font->width );
172  
173 # init the imagemap data field if they wanted it
174 if ( $self->true( $self->{'imagemap'} ) )
175 {
176 $self->{'imagemap_data'} = [];
177 }
178  
179 # find both delta values ($delta1 for stepping between different
180 # datapoint names, $delta2 for setpping between datasets for that
181 # point) and the mapping constant
182 $width = $self->{'curr_x_max'} - $self->{'curr_x_min'};
183 $height = $self->{'curr_y_max'} - $self->{'curr_y_min'};
184 $delta1 = $width / ( $self->{'num_datapoints'} > 0 ? $self->{'num_datapoints'} : 1 );
185 $diff = ( $self->{'max_val'} - $self->{'min_val'} );
186 $diff = 1 if $diff == 0;
187 $map = $height / $diff;
188 if ( $self->true( $self->{'spaced_bars'} ) )
189 {
190 $delta2 = $delta1 / 3;
191 }
192 else
193 {
194 $delta2 = $delta1;
195 }
196  
197 # get the base x-y values
198 $x1 = $self->{'curr_x_min'};
199 $y1 = $self->{'curr_y_max'};
200 $y1_line = $y1;
201 $mod = $self->{'min_val'};
202 $x1_line = $self->{'curr_x_min'};
203  
204 # draw the bars and the lines
205 $color = $self->_color_role_to_index('dataset0');
206 $line_color = $self->_color_role_to_index('dataset1');
207  
208 # draw every bar for this dataset
209 for $j ( 0 .. $self->{'num_datapoints'} )
210 {
211  
212 # don't try to draw anything if there's no data
213 if ( defined( $data->[1][$j] ) )
214 {
215  
216 #calculate the percent value for this data and the actual sum;
217 $curr_sum += $data->[1][$j];
218 $percent = int( $curr_sum / ( $sum || 1 ) * 100 );
219  
220 # find the bounds of the rectangle
221 if ( $self->true( $self->{'spaced_bars'} ) )
222 {
223 $x2 = $x1 + ( $j * $delta1 ) + $delta2;
224 }
225 else
226 {
227 $x2 = $x1 + ( $j * $delta1 );
228 }
229 $y2 = $y1;
230 $x3 = $x2 + $delta2;
231 $y3 = $y1 - ( ( $data->[1][$j] - $mod ) * $map );
232  
233 #cut the bars off, if needed
234 if ( $data->[1][$j] > $self->{'max_val'} )
235 {
236 $y3 = $y1 - ( ( $self->{'max_val'} - $mod ) * $map );
237 $cut = 1;
238 }
239 elsif ( $data->[1][$j] < $self->{'min_val'} )
240 {
241 $y3 = $y1 - ( ( $self->{'min_val'} - $mod ) * $map );
242 $cut = 1;
243 }
244 else
245 {
246 $cut = 0;
247 }
248  
249 # draw the bar
250 ## y2 and y3 are reversed in some cases because GD's fill
251 ## algorithm is lame
252 $self->{'gd_obj'}->filledRectangle( $x2, $y3, $x3, $y2, $color );
253 if ( $self->true( $self->{'imagemap'} ) )
254 {
255 $self->{'imagemap_data'}->[1][$j] = [ $x2, $y3, $x3, $y2 ];
256 }
257  
258 # now outline it. outline red if the bar had been cut off
259 unless ($cut)
260 {
261 $self->{'gd_obj'}->rectangle( $x2, $y3, $x3, $y2, $misccolor );
262 }
263 else
264 {
265  
266 $self->{'gd_obj'}->rectangle( $x2, $y3, $x3, $y2, $pink );
267 }
268 $x2_line = $x3;
269 if ( $self->{'max_val'} >= $curr_sum )
270 {
271  
272 #get the y value
273 $y2_line = $y1 - ( ( $curr_sum - $mod ) * $map );
274  
275 #draw the line
276 $self->{'gd_obj'}->line( $x1_line, $y1_line, $x2_line, $y2_line, $line_color );
277  
278 #draw a little rectangle at the end of the line
279 $self->{'gd_obj'}->filledRectangle( $x2_line - 2, $y2_line - 2, $x2_line + 2, $y2_line + 2, $line_color );
280  
281 #draw the label for the percent value
282 $per_label = $percent . '%';
283 $per_label_len = length($per_label) * $w;
284 $self->{'gd_obj'}->string( $font, $x2_line - $per_label_len - 1, $y2_line - $h - 1, $per_label, $line_color );
285  
286 #update the values for next the line
287 $y1_line = $y2_line;
288 $x1_line = $x2_line;
289 }
290 else
291 {
292  
293 #get the y value
294 $y2_line = $y1 - ( ( $self->{'max_val'} - $mod ) * $map );
295  
296 #draw the line
297 $self->{'gd_obj'}->line( $x1_line, $y1_line, $x2_line, $y2_line, $pink );
298  
299 #draw a little rectangle at the end of the line
300 $self->{'gd_obj'}->filledRectangle( $x2_line - 2, $y2_line - 2, $x2_line + 2, $y2_line + 2, $pink );
301  
302 #draw the label for the percent value
303 $per_label = $percent . '%';
304 $per_label_len = length($per_label) * $w;
305 $self->{'gd_obj'}->string( $font, $x2_line - $per_label_len - 1, $y2_line - $h - 1, $per_label, $pink );
306  
307 #update the values for the next line
308 $y1_line = $y2_line;
309 $x1_line = $x2_line;
310 }
311  
312 }
313 else
314 {
315 if ( $self->true( $self->{'imagemap'} ) )
316 {
317 $self->{'imagemap_data'}->[1][$j] = [ undef(), undef(), undef(), undef() ];
318 }
319 }
320 }
321  
322 # and finaly box it off
323 $self->{'gd_obj'}
324 ->rectangle( $self->{'curr_x_min'}, $self->{'curr_y_min'}, $self->{'curr_x_max'}, $self->{'curr_y_max'}, $misccolor );
325 return;
326  
327 }
328  
329 ## be a good module and return 1
330 1;
« lib/Chart/Mountain.pm ↑ Summary lib/Chart/Split.pm »